diff options
Diffstat (limited to 'anim/src/hexdump.zig')
| -rw-r--r-- | anim/src/hexdump.zig | 251 |
1 files changed, 155 insertions, 96 deletions
diff --git a/anim/src/hexdump.zig b/anim/src/hexdump.zig index b98d9bd..fba212d 100644 --- a/anim/src/hexdump.zig +++ b/anim/src/hexdump.zig @@ -1,11 +1,12 @@ //! `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 +//! beside, seven 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 on raised paper in its ink, `loose` ones (the allocator's, a +//! struct's padding) dim, never read two dots; and its reading at 640: +//! what the runs read as, a list a character apart. //! 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"); @@ -23,22 +24,23 @@ 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 }; +pub const Kind = enum { number, pointer, loose, 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 { +/// The kind of the word at `addr`, and its type where a field's is known: +/// a field of a struct the program's pointers reach (by its type), other +/// allocated bytes, `loose` (the allocator's own between allocations), or +/// never observed. +pub fn kindOf(things: []const objects.Object, moment: Recording.Moment, addr: u64) struct { Kind, ?Recording.types.Type } { 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; + for (o.type.record.fields) |field| if (o.addr + field.offset == addr) return .{ if (field.type == .pointer) .pointer else .number, field.type }; + return .{ .number, null }; } 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; + moment.read(addr, &bytes, &known) catch return .{ .unread, null }; + if (std.mem.indexOfScalar(bool, &known, true) == null) return .{ .unread, null }; + return .{ if ((moment.allocation(addr) catch null) != null) .number else .loose, null }; } /// The rows of a region (`lo .. hi`, sixteen-aligned) a dump of `count` @@ -57,10 +59,11 @@ pub fn window(now: cursors.Now, lo: u64, hi: u64, count: usize) u64 { } /// 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. +/// of numbers (an element a run, read as its type), 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, + array: ?struct { addr: u64, len: u64, elem: u8, type: ?Recording.types.Type = null } = null, region: u24 = theme.color.region.heap, }; @@ -93,6 +96,9 @@ pub const Dump = struct { legend: []const ui.Legend = &.{}, of: Fields, marks: []const @import("anim").Value = &.{}, + /// The stop it comes in at: what changed before then, the viewer did + /// not see change, and it is plain. + since: u64 = 0, 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); @@ -100,6 +106,8 @@ pub const Dump = struct { if (v.rows == 0) return; const now = d.cursor.now(placed); var of = d.of; + of.watched = 0; + while (of.watched + 1 < now.stops.len and now.stops[of.watched + 1] >= d.since) of.watched += 1; 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); @@ -144,6 +152,8 @@ pub fn draw(u: ui.Ui, now: cursors.Now, x: f32, y: f32, first: u64, count: usize const v = u.moved(x, 0); const moment = now.shown(.memory); const lead = ui.shared(&.{ first, first + 16 * @as(u64, count -| 1) }); + // Room for the addresses, once for the dump: by its widest. + const shift = rows.addressRoom(first + 16 * @as(u64, count -| 1)); for (0..count) |i| { const addr = first + 16 * i; const ry = y + @as(f32, @floatFromInt(i)) * m.row; @@ -151,15 +161,18 @@ pub fn draw(u: ui.Ui, now: cursors.Now, x: f32, y: f32, first: u64, count: usize var known: [16]bool = undefined; moment.read(addr, &values, &known) catch return; var runs: [16]rows.Run = undefined; + var readings: [16]Field = 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 }; + var kind: ?Recording.types.Type = null; if (of.array) |arr| if (here >= arr.addr and here < arr.addr + arr.len) { run.len = arr.elem; run.kind = .number; + kind = arr.type; holds_drawn = true; }; if (run.kind == .unread) { @@ -168,10 +181,11 @@ pub fn draw(u: ui.Ui, now: cursors.Now, x: f32, y: f32, first: u64, count: usize 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)) { + const word, kind = kindOf(of.things, moment, here); + run.kind = switch (word) { .number => .number, .pointer => .pointer, - .allocator => .allocator, + .loose => .loose, .unread => .unread, }; for (of.things) |o| if (o.holds(here)) { @@ -180,51 +194,23 @@ pub fn draw(u: ui.Ui, now: cursors.Now, x: f32, y: f32, first: u64, count: usize } 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 }); + if (run.kind == .number or run.kind == .pointer) ties.anchor(v, .{ .dump_run = here }, .{ .x = shift + rows.byteX(run.first) - 4, .y = ry, .w = rows.byteX(run.first + run.len - 1) + 28 - rows.byteX(run.first) + 4, .h = m.row }); + readings[n] = .{ .at = run.first, .len = run.len, .reads = switch (run.kind) { + .number => .number, + .pointer => .pointer, + else => .bytes, + }, .type = kind }; 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 => {}, + rows.bytesAt(v, ry, addr, &values, &known, runs[0..n], of.region, lead, holds_drawn, moment, shift); + const end = reading(v, ry, shift, moment, &values, &known, readings[0..n]); + // The size word of the chunk after: malloc's, a note after the reading. + const last = runs[n - 1]; + if (last.kind == .loose and last.first == 8 and last.len == 8 and std.mem.indexOfScalar(bool, known[8..], false) == null and chunkAfter(moment, addr + 8)) { + const size = std.mem.readInt(u64, values[8..16], .little) & ~@as(u64, 7); + v.text(.sans, theme.size.sans.micro, end, rows.baseline(ry) - 1, v.print("malloc's: a chunk of {d}", .{size}), t.faint); } } } @@ -251,6 +237,9 @@ pub const Field = struct { big: bool = false, /// Which of the view's marks frames it as read (`Fields.marks`). mark: ?u8 = null, + /// A number's type, where the debug info gives it: read as the lens's + /// values are (`objects.reading`: a `char` quoted, a `bool` by name). + type: ?Recording.types.Type = null, }; /// What a dump of fields is of, and how it is marked now. @@ -272,46 +261,108 @@ pub const Fields = struct { /// 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, + /// How many of the newest steps it has been on screen through: only + /// their changes are marked. + watched: usize = std.math.maxInt(usize), }; /// 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. +/// stands for. More rows of fields than the window: it is kept round the +/// first row a mark or the step is on, and the others are `more` rows too. +/// A field is a run, cut where a row ends; a byte that is no field is +/// `loose` (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; - } - } + var held: std.ArrayList(u64) = .empty; + for (0..total) |r| if (total <= v.rows or holdsField(of, r)) held.append(u.f.scratch, r) catch return; + const all = held.items; + if (all.len == 0) return; + const range = keep(all, focus(now, of, all), v.rows, total - 1); + const shown = all[range[0]..range[1]]; const lead = ui.shared(&.{ of.base, of.base + 16 * (total -| 1) }); + // Room for the addresses, once for the dump: by the widest it shows. + const shift = rows.addressRoom(of.base + 16 * shown[shown.len - 1]); + const more = struct { + fn row(at: ui.Ui, ry: f32, count: u64) void { + const after = at.arrow(b.address, ry + m.row / 2, .more); + at.text(.mono, theme.size.mono.context, after + adv, rows.baseline(ry), at.print("{d} rows", .{count}), t.faint); + } + }.row; 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); + for (shown) |r| { + // A stretch left out: one row, three dots and a count. + const gap = if (last) |l| r - l - 1 else r; + if (gap > 0) { + more(u, ry, gap); ry += m.row; - }; + } last = r; - fieldRow(u, now, ry, of.base + 16 * r, moment, of, lead); + fieldRow(u, now, ry, of.base + 16 * r, moment, of, lead, shift); ry += m.row; } + if (shown[shown.len - 1] < total - 1) more(u, ry, total - 1 - shown[shown.len - 1]); +} + +/// Which of `rs` (rows of fields, the region's last row `last`) a window +/// of `room` rows shows: as many as fit round `rs[focus_at]`, each row a +/// line and each stretch left out (before, between, after) a `more` line. +fn keep(rs: []const u64, focus_at: usize, room: usize, last: u64) [2]usize { + const lines = struct { + fn count(all: []const u64, lo: usize, hi: usize, end: u64) usize { + var n = hi - lo + @intFromBool(all[lo] > 0) + @intFromBool(all[hi - 1] < end); + for (all[lo .. hi - 1], all[lo + 1 .. hi]) |a, b2| n += @intFromBool(b2 > a + 1); + return n; + } + }.count; + var lo = focus_at; + var hi = lo + 1; + while (true) { + const before = hi - lo; + if (hi < rs.len and lines(rs, lo, hi + 1, last) <= room) hi += 1; + if (lo > 0 and lines(rs, lo - 1, hi, last) <= room) lo -= 1; + if (hi - lo == before) break; + } + return .{ lo, hi }; +} + +test "a dump of more rows of fields than its window keeps those round its focus" { + // A page's header row, then two cells at its end: all fit in seven. + try std.testing.expectEqual([2]usize{ 0, 3 }, keep(&.{ 0, 254, 255 }, 0, 7, 255)); + // Ten rows with gaps, a window of five round the sixth: two rows, a `more` before, between and after. + const spread = [_]u64{ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 }; + try std.testing.expectEqual([2]usize{ 5, 7 }, keep(&spread, 5, 5, 18)); + // Rows next to each other, the window of five round the last: four rows and a `more` before. + const run = [_]u64{ 0, 1, 2, 3, 4, 5, 6, 7 }; + try std.testing.expectEqual([2]usize{ 4, 8 }, keep(&run, 7, 5, 7)); } -fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Moment, of: Fields, lead: usize) void { +/// Whether row `r` of the region holds a field. +fn holdsField(of: Fields, r: u64) bool { + const lo = of.base + 16 * r; + for (of.fields) |field| if (of.base + field.at < lo + 16 and of.base + field.at + field.len > lo) return true; + return false; +} + +/// Which of `rs` (rows of fields) the window is kept round: the first a +/// mark shows on, else the first the step wrote or read, else the first. +fn focus(now: cursors.Now, of: Fields, rs: []const u64) usize { + for (rs, 0..) |r, k| for (of.fields) |field| { + if (field.at >= 16 * r + 16 or field.at + field.len <= 16 * r) continue; + if (field.mark) |i| if (i < of.marks.len and of.marks[i] > 0) return k; + }; + for (rs, 0..) |r, k| { + const span: Recording.Span = .{ .addr = of.base + 16 * r, .len = 16 }; + if (Recording.touches(now.rec.written(now.stops[1], now.stops[0]), span.addr, span.len) or Recording.touches(now.rec.read(now.stops[1], now.stops[0]), span.addr, span.len)) return k; + } + return 0; +} + +fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Moment, of: Fields, lead: usize, shift: f32) void { var values: [16]u8 = undefined; var known: [16]bool = undefined; moment.read(addr, &values, &known) catch return; @@ -336,7 +387,6 @@ fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Mom 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; @@ -346,7 +396,7 @@ fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Mom 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 }; + var run: rows.Run = .{ .first = at, .len = 1, .kind = .loose }; if (field) |fl| { run.len = @intCast(@min(of.base + fl.at + fl.len - here, 16 - @as(u64, at))); run.kind = switch (fl.reads) { @@ -354,14 +404,14 @@ fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Mom .pointer => .pointer, .text => .text, }; - run.heat = now.written(here, run.len, .memory, 0); + run.heat = now.writtenWatched(here, run.len, .memory, 0, 0, of.watched); 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)); + if (of.watched > 0 and 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 }; + readings[n] = .{ .at = at, .len = run.len, .reads = fl.reads, .big = fl.big, .type = fl.type }; 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. @@ -382,33 +432,41 @@ fn fieldRow(u: ui.Ui, now: cursors.Now, y: f32, addr: u64, moment: Recording.Mom 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]); + _ = reading(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]); + _ = reading(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 { +/// `bytes`'s reading, for every dump: what a row's runs (`fields`, from +/// the row's first byte) read as, left to right, a character apart, from +/// 508 after the first byte (`shift` right of 640): a number in decimal, +/// in its byte order, or as its type says (`objects.reading`: a `char` +/// quoted, a `bool` by name); a pointer as `points` and its address, +/// `NULL` dim; text in quotes, C's escapes; bytes left to the eye, or +/// `loose`, as nothing. Cut where the panel ends, with `…`. Returns where +/// it ends. +pub fn reading(u: ui.Ui, y: f32, shift: f32, moment: Recording.Moment, values: *const [16]u8, known: *const [16]bool, fields: []const Field) f32 { 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; + // A typed number is as wide as its type (an `int` in a word: 4 bytes). + const len: usize = @intCast(if (fl.type) |ty| @min(fl.len, ty.size() orelse fl.len) else fl.len); + if (fl.reads == .bytes or len == 0) 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}); + const ty = fl.type orelse break :blk u.print("{d}", .{word}); + const shift_bits: u6 = @intCast(64 - 8 * @as(u64, @min(len, 8))); + const signed = ty == .scalar and ty.scalar.kind == .int; + break :blk objects.reading(u.f.scratch, if (signed) @as(i64, @bitCast(word << shift_bits)) >> shift_bits else @bitCast(word), ty); }, .pointer => blk: { var word: u64 = 0; @@ -427,9 +485,10 @@ fn fieldReading(u: ui.Ui, y: f32, shift: f32, moment: Recording.Moment, values: 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; + if (!fits) return room; x += w + adv; } + return x; } /// Bytes as C text, in quotes: the printable as they are, the rest by C's |
