1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
//! `hexdump` (design/README.md, "3. Panels"): memory as `bytes` rows,
//! sixteen a row, the right half of a lens: the pane its drawing is a view
//! of. A window of the region kept round the rows the step touches (five
//! beside, nine as the subject), in address order; a gap in the addresses
//! is one row of `more`'s dots and the count left out. Each row's runs
//! come from the types of what the program's pointers reach: a field's
//! bytes on raised paper in its ink, the allocator's dim, never read two
//! dots; and its reading at 640: what the runs read as, little-endian.
//! Bytes a step wrote are struck here before the drawing reacts.
//! Reads `Moment.read`, `Moment.allocation`, and the recording's types.
const std = @import("std");
const ui = @import("ui.zig");
const rows = @import("rows.zig");
const cursors = @import("cursor.zig");
const Recording = @import("Recording.zig");
const objects = @import("objects.zig");
const ties = @import("ties.zig");
const theme = ui.theme;
const Rect = ui.Rect;
const t = theme.color.text;
const m = theme.metric.context;
const b = theme.metric.bytes;
const adv = theme.size.mono.context * 3 / 5;
/// What a word of the region is.
pub const Kind = enum { number, pointer, allocator, unread };
/// The kind of the word at `addr`: a field of a struct the program's
/// pointers reach (by its type), other allocated bytes, the allocator's
/// own between allocations, or never observed.
pub fn kindOf(things: []const objects.Object, moment: Recording.Moment, addr: u64) Kind {
for (things) |o| {
if (!o.holds(addr)) continue;
for (o.type.record.fields) |field| if (o.addr + field.offset == addr) return if (field.type == .pointer) .pointer else .number;
return .number;
}
var bytes: [8]u8 = undefined;
var known: [8]bool = undefined;
moment.read(addr, &bytes, &known) catch return .unread;
if (std.mem.indexOfScalar(bool, &known, true) == null) return .unread;
return if ((moment.allocation(addr) catch null) != null) .number else .allocator;
}
/// The rows of a region (`lo .. hi`, sixteen-aligned) a dump of `count`
/// rows shows: all of them, or a window round the first the step touched.
pub fn window(now: cursors.Now, lo: u64, hi: u64, count: usize) u64 {
const all = (hi - lo) / 16;
if (all <= count) return lo;
var touched: ?u64 = null;
for ([_][]const Recording.Span{ now.rec.written(now.stops[1], now.stops[0]), now.rec.read(now.stops[1], now.stops[0]) }) |spans| for (spans) |span| {
if (touched == null and span.addr >= lo and span.addr < hi) touched = span.addr & ~@as(u64, 15);
};
const at = touched orelse lo;
const row = (at - lo) / 16;
const first = @min(row -| (count / 2), all - count);
return lo + first * 16;
}
/// What a dump is of: the structs a drawing beside it draws, or an array
/// of numbers (an element a run), and the ink of the memory it is in.
pub const Of = struct {
things: []const objects.Object = &.{},
array: ?struct { addr: u64, len: u64, elem: u8 } = null,
region: u24 = theme.color.region.heap,
};
/// The band's view of bytes: its own panel over the state column, the
/// tag `its bytes` with the range shown (`about`) and the legend of runs.
/// Returns its `Ui` (the panel's origin) and how many rows it holds.
pub fn panel(f: *@import("sketch").Frame, placed: @import("anim").Placed, stage: *const @import("stage.zig").Stage, about: []const u8, legend: []const ui.Legend) View {
const box = stage.box(placed, .view);
const u = @import("context.zig").at(f, placed, box).faded(placed.value(stage.view));
if (box.h < m.tag + m.row) return .{ .u = u, .rows = 0, .box = box };
// Its own box, as it moves; the drawing's panel clips itself again after.
u.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h });
_ = u.panel(box.w, box.h, "its bytes", .{ .legend = legend });
const tag = theme.size.sans.tag;
u.text(.sans, theme.size.sans.micro, 28 + u.width(.sans_medium, tag, "its bytes") + 12, m.tag - 6, about, t.faint);
return .{ .u = u, .rows = theme.layout.rowsIn(box.h), .box = box };
}
/// A view of a region the scene names (`of`), by its fields: the band's
/// right half, over the state column. The scene marks fields as read
/// (`marks`, a value each: how much the frame shows). In a step that hands
/// the region to the kernel (a write's buffer), its fields are framed as
/// read while the kernel takes them, and the bytes the recording had not
/// seen come in from the call's capture, dots to digits, with no mark.
pub const Dump = struct {
cursor: cursors.Cursor,
stage: *const @import("stage.zig").Stage,
/// The tag's words, after `its bytes`; and the legend of its fields.
about: []const u8,
legend: []const ui.Legend = &.{},
of: Fields,
marks: []const @import("anim").Value = &.{},
pub fn draw(d: *const Dump, f: *@import("sketch").Frame, placed: @import("anim").Placed) void {
const v = panel(f, placed, d.stage, d.about, d.legend);
defer v.u.unclip();
if (v.rows == 0) return;
const now = d.cursor.now(placed);
var of = d.of;
var shows: [8]f32 = @splat(0);
const marked = @min(d.marks.len, shows.len);
for (d.marks[0..marked], shows[0..marked]) |mark, *value| value.* = placed.value(mark);
of.marks = shows[0..marked];
// A call the step made that was given these bytes: as the kernel
// took them, from when its work begins (and at the stop after it;
// a step later the program may have written them again).
const syscalls = @import("syscalls.zig");
for (d.cursor.rec.syscalls) |made| {
if (made.enter <= now.stops[1] or made.enter > now.stops[0]) continue;
const span = syscalls.bufferRead(made) orelse continue;
if (span.addr >= of.base + of.len or span.addr + span.len <= of.base) continue;
var shown: f32 = 1;
if (!now.resting()) {
const p = syscalls.Plan.of(made);
shown = std.math.clamp((now.time - p.work) / theme.time.quick, 0, 1);
of.read = now.held(p.work);
}
of.given = .{ .addr = span.addr, .bytes = made.text[0..@min(made.text.len, span.len)], .shows = shown };
}
fielded(v, now, m.tag + m.pad, of);
v.veil(now);
}
};
/// The view's panel as drawn: its `Ui`, how many rows it holds, its box.
pub const View = struct {
u: ui.Ui,
rows: usize,
box: Rect,
/// What a sprint cannot show: veiled, over its rows.
pub fn veil(v: View, now: cursors.Now) void {
@import("context.zig").veiled(v.u, v.box, now.sprint_veil);
}
};
/// The dump: `count` rows from `first`, at `x` in `u` (the view's
/// panel), the rows from `y`. The rows of what the drawing draws have
/// quiet brackets.
pub fn draw(u: ui.Ui, now: cursors.Now, x: f32, y: f32, first: u64, count: usize, of: Of) void {
const v = u.moved(x, 0);
const moment = now.shown(.memory);
const lead = ui.shared(&.{ first, first + 16 * @as(u64, count -| 1) });
for (0..count) |i| {
const addr = first + 16 * i;
const ry = y + @as(f32, @floatFromInt(i)) * m.row;
var values: [16]u8 = undefined;
var known: [16]bool = undefined;
moment.read(addr, &values, &known) catch return;
var runs: [16]rows.Run = undefined;
var n: usize = 0;
var holds_drawn = false;
var at: u64 = 0;
while (at < 16) {
const here = addr + at;
var run: rows.Run = .{ .first = @intCast(at), .len = 8, .kind = .unread };
if (of.array) |arr| if (here >= arr.addr and here < arr.addr + arr.len) {
run.len = arr.elem;
run.kind = .number;
holds_drawn = true;
};
if (run.kind == .unread) {
// A word, or what is left of one before an array begins.
run.len = @intCast(@min(8 - (here % 8), 16 - at));
if (of.array) |arr| if (here < arr.addr and arr.addr < here + run.len) {
run.len = @intCast(arr.addr - here);
};
run.kind = switch (kindOf(of.things, moment, here)) {
.number => .number,
.pointer => .pointer,
.allocator => .allocator,
.unread => .unread,
};
for (of.things) |o| if (o.holds(here)) {
holds_drawn = true;
};
}
run.heat = now.written(here, run.len, .memory, 0);
if (now.wasRead(here, run.len) and now.writeAge(here, run.len) != 0) run.read = now.reading(.memory, 0);
if (run.kind == .number or run.kind == .pointer) ties.anchor(v, .{ .dump_run = here }, .{ .x = rows.byteX(run.first) - 4, .y = ry, .w = rows.byteX(run.first + run.len - 1) + 28 - rows.byteX(run.first) + 4, .h = m.row });
runs[n] = run;
n += 1;
at += run.len;
}
rows.bytes(v, ry, addr, &values, &known, runs[0..n], of.region, lead, holds_drawn, moment);
reading(v, ry, addr, moment, &values, &known, runs[0..n]);
}
}
/// The reading: what the runs read as, little-endian. A number ranged
/// right in three characters; a pointer as `points` and its address;
/// `NULL` dim; an allocator's run as a note, faint.
fn reading(u: ui.Ui, y: f32, addr: u64, moment: Recording.Moment, values: *const [16]u8, known: *const [16]bool, runs: []const rows.Run) void {
const size = theme.size.mono.context;
const base = rows.baseline(y);
var x: f32 = b.reading;
for (runs) |run| {
var bytes: [8]u8 = @splat(0);
@memcpy(bytes[0..run.len], values[run.first..][0..run.len]);
const word = std.mem.readInt(u64, &bytes, .little);
const whole = std.mem.indexOfScalar(bool, known[run.first..][0..run.len], false) == null;
if (!whole) continue;
switch (run.kind) {
.number => {
// A field typed a number reads as its decimal.
const shift: u6 = @intCast(64 - 8 * @as(u32, @min(run.len, 8)));
const digits = u.print("{d}", .{@as(i64, @bitCast(word << shift)) >> shift});
const w = @as(f32, @floatFromInt(digits.len)) * adv;
u.text(.mono, size, x + @max(3 * adv - w, 0), base, digits, t.primary);
x += @max(3 * adv, w) + 8;
},
.pointer => if (word == 0) {
u.text(.mono, size, x + theme.metric.context.arrow_w, base, "NULL", t.dim);
x += theme.metric.context.arrow_w + 4 * adv + 8;
} else {
x = u.arrow(x, y + m.row / 2, .points);
x = u.address(x, base, word, .{ .ink = ui.rgb(rows.ink(rows.regionOf(moment, word))) }) + 8;
},
.allocator => if (run.first == 8 and run.len == 8 and chunkAfter(moment, addr + 8)) {
// The size word of the chunk after: malloc's.
u.text(.sans, theme.size.sans.micro, x, base - 1, u.print("malloc's: a chunk of {d}", .{word & ~@as(u64, 7)}), t.faint);
return;
},
.text, .unread => {},
}
}
}
/// Whether an allocation the recording has begins just after the word at
/// `word` (the chunk's size word, glibc's and musl's alike): only there are
/// bytes between allocations the allocator's own, to be read as its.
fn chunkAfter(moment: Recording.Moment, word: u64) bool {
const a = (moment.allocation(word + 8) catch return false) orelse return false;
return a.ptr == word + 8;
}
/// What a field of a region reads as: a number (little-endian, or `big`),
/// a pointer, text (in quotes, C's escapes), or bytes the reading leaves
/// to the eye.
pub const Reads = enum { number, pointer, text, bytes };
/// A field of the region a view is about, from the region's first byte.
pub const Field = struct {
at: u64,
len: u64,
reads: Reads = .number,
/// Stored high byte first (SQLite's pages are).
big: bool = false,
/// Which of the view's marks frames it as read (`Fields.marks`).
mark: ?u8 = null,
};
/// What a dump of fields is of, and how it is marked now.
pub const Fields = struct {
/// The region: where it begins, how long it is, the ink of its memory.
base: u64,
len: u64,
fields: []const Field,
region: u24 = theme.color.region.heap,
/// How much each of the view's marks shows (a frame, read's colour).
marks: []const f32 = &.{},
/// Bytes known from elsewhere than the moment's memory (what a syscall
/// was given, as the kernel took them), and how much they show (0 to 1).
given: ?struct { addr: u64, bytes: []const u8, shows: f32 } = null,
/// How much a frame round every field shows: the kernel reading them.
read: f32 = 0,
/// The part of the region the recording read at the stop shown (null:
/// all of it as last seen). Memory nothing watches is read only where a
/// pointer leads, 32 bytes at a stop; elsewhere its last seen bytes may
/// be long out of date, and are drawn as not seen.
seen: ?Recording.Span = null,
};
/// The region's rows in view `v`, from `y`, sixteen bytes a row from its
/// first byte: those that hold a field (all of them, where they fit), and
/// for a stretch left out one row of `more`'s dots and how many rows it
/// stands for. A field is a run, cut where a row ends; a byte that is no
/// field is drawn as the allocator's (dim, no paper) and reads as nothing.
pub fn fielded(v: View, now: cursors.Now, y: f32, of: Fields) void {
const u = v.u;
const moment = now.shown(.memory);
const total = (of.len + 15) / 16;
var shown: [16]u64 = undefined;
var n: usize = 0;
for (0..total) |r| {
const lo = of.base + 16 * r;
const holds = for (of.fields) |field| {
if (of.base + field.at < lo + 16 and of.base + field.at + field.len > lo) break true;
} else false;
if ((holds or total <= v.rows) and n < shown.len) {
shown[n] = r;
n += 1;
}
}
const lead = ui.shared(&.{ of.base, of.base + 16 * (total -| 1) });
var ry = y;
var last: ?u64 = null;
for (shown[0..n]) |r| {
if (last) |l| if (r > l + 1) {
// A stretch left out: one row, three dots and a count.
const after = u.arrow(b.address, ry + m.row / 2, .more);
u.text(.mono, theme.size.mono.context, after + adv, rows.baseline(ry), u.print("{d} rows", .{r - l - 1}), t.faint);
ry += m.row;
};
last = r;
fieldRow(u, now, ry, of.base + 16 * r, moment, of, lead);
ry += m.row;
}
}
fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Moment, of: Fields, lead: usize) void {
var values: [16]u8 = undefined;
var known: [16]bool = undefined;
moment.read(addr, &values, &known) catch return;
if (of.seen) |seen| for (&known, 0..) |*k, i| {
if (addr + i < seen.addr or addr + i >= seen.addr + seen.len) k.* = false;
};
// What a call was given is as the kernel took it, and not what the
// recording saw there before (a debugger reads a buffer it is not
// watching only where a pointer leads, 32 bytes at a stop: the rest
// is as it was when last seen): it comes in over what was drawn.
var given_values = values;
var given_known = known;
var gives = false;
var given_from: u8 = 16;
var given_to: u8 = 0;
if (of.given) |g| for (0..16) |i| {
const a = addr + i;
if (a < g.addr or a >= g.addr + g.bytes.len) continue;
given_values[i] = g.bytes[a - g.addr];
given_known[i] = true;
gives = true;
given_from = @min(given_from, @as(u8, @intCast(i)));
given_to = @max(given_to, @as(u8, @intCast(i)));
};
const shift = rows.addressRoom(addr);
var runs: [16]rows.Run = undefined;
var readings: [16]Field = undefined;
var n: usize = 0;
var at: u8 = 0;
while (at < 16 and addr + at < of.base + of.len) {
const here = addr + at;
const field = for (of.fields) |field| {
if (of.base + field.at <= here and here < of.base + field.at + field.len) break field;
} else null;
var run: rows.Run = .{ .first = at, .len = 1, .kind = .allocator };
if (field) |fl| {
run.len = @intCast(@min(of.base + fl.at + fl.len - here, 16 - @as(u64, at)));
run.kind = switch (fl.reads) {
.number, .bytes => .number,
.pointer => .pointer,
.text => .text,
};
run.heat = now.written(here, run.len, .memory, 0);
var framed: f32 = 0;
if (fl.mark) |k| if (k < of.marks.len) {
framed = of.marks[k];
};
if (now.wasRead(here, run.len) and now.writeAge(here, run.len) != 0) framed = @max(framed, now.reading(.memory, 0));
run.read = framed;
readings[n] = .{ .at = at, .len = run.len, .reads = fl.reads, .big = fl.big };
ties.anchor(u, .{ .dump_run = here }, .{ .x = shift + rows.byteX(run.first) - 4, .y = y, .w = rows.byteX(run.first + run.len - 1) + 28 - rows.byteX(run.first) + 4, .h = m.row });
} else {
// Up to the next field, or the row's end.
var end: u64 = @min(16, of.base + of.len - addr);
for (of.fields) |other| if (of.base + other.at > here and of.base + other.at < addr + end) {
end = of.base + other.at - addr;
};
run.len = @intCast(end - at);
readings[n] = .{ .at = at, .len = run.len, .reads = .bytes };
}
runs[n] = run;
n += 1;
at += run.len;
}
// The kernel reading them: one frame round what it took of the row.
if (gives and of.read > 0) u.faded(of.read).frame(.{ .x = shift + rows.byteX(given_from) - 7, .y = y - 1, .w = rows.byteX(given_to) - rows.byteX(given_from) + 38, .h = m.row + 2 }, theme.mark(.read).solid, 2);
const shows = if (of.given) |g| g.shows else 0;
if (gives and shows > 0) {
if (shows < 1) rows.bytesAt(u.faded(1 - shows), y, addr, &values, &known, runs[0..n], of.region, lead, true, moment, shift);
rows.bytesAt(u.faded(shows), y, addr, &given_values, &given_known, runs[0..n], of.region, lead, true, moment, shift);
fieldReading(u.faded(shows), y, shift, moment, &given_values, &given_known, readings[0..n]);
} else {
rows.bytesAt(u, y, addr, &values, &known, runs[0..n], of.region, lead, true, moment, shift);
fieldReading(u, y, shift, moment, &values, &known, readings[0..n]);
}
}
/// The reading of a row of fields, left to right, a character apart: a
/// number in decimal, a pointer as `points` and its address, text in
/// quotes; bytes left to the eye read as nothing. Cut where the panel
/// ends, with `…`.
fn fieldReading(u: ui.Ui, y: f32, shift: f32, moment: Recording.Moment, values: *const [16]u8, known: *const [16]bool, fields: []const Field) void {
const size = theme.size.mono.context;
const base = rows.baseline(y);
const room = theme.layout.state.w - theme.metric.context.side;
var x: f32 = b.reading + shift;
for (fields) |fl| {
const first: usize = @intCast(fl.at);
const len: usize = @intCast(fl.len);
if (fl.reads == .bytes) continue;
if (std.mem.indexOfScalar(bool, known[first..][0..len], false) != null) continue;
const bytes = values[first..][0..len];
const words: []const u8 = switch (fl.reads) {
.number => blk: {
var word: u64 = 0;
for (0..@min(len, 8)) |k| word |= @as(u64, bytes[if (fl.big) len - 1 - k else k]) << @intCast(8 * k);
break :blk u.print("{d}", .{word});
},
.pointer => blk: {
var word: u64 = 0;
for (0..@min(len, 8)) |k| word |= @as(u64, bytes[k]) << @intCast(8 * k);
if (word == 0) break :blk "NULL";
if (x + theme.metric.context.arrow_w + @as(f32, @floatFromInt(ui.digits(word))) * adv > room) break :blk "\u{2026}";
x = u.arrow(x, y + m.row / 2, .points);
x = u.address(x, base, word, .{ .ink = ui.rgb(rows.ink(rows.regionOf(moment, word))) }) + adv;
continue;
},
.text => quoted(u, bytes),
.bytes => unreachable,
};
const w = u.width(.mono, size, words);
const fits = x + w <= room;
const shown = if (fits) words else rows.fitted(u, words, room - x);
const ink = if (fl.reads == .text) theme.color.string else if (std.mem.eql(u8, words, "NULL")) t.dim else t.primary;
u.text(.mono, size, x, base, shown, ink);
if (!fits) return;
x += w + adv;
}
}
/// Bytes as C text, in quotes: the printable as they are, the rest by C's
/// escapes; a NUL ends it.
fn quoted(u: ui.Ui, bytes: []const u8) []const u8 {
const a = u.f.scratch;
var out: std.ArrayList(u8) = .empty;
out.append(a, '"') catch return "";
for (bytes) |c| {
if (c == 0) break;
switch (c) {
'\n' => out.appendSlice(a, "\\n") catch {},
'\t' => out.appendSlice(a, "\\t") catch {},
'"', '\\' => out.appendSlice(a, &.{ '\\', c }) catch {},
else => if (std.ascii.isPrint(c)) out.append(a, c) catch {} else out.print(a, "\\x{x:0>2}", .{c}) catch {},
}
}
out.append(a, '"') catch {};
return out.items;
}
|