summaryrefslogtreecommitdiff
path: root/anim/src/hexdump.zig
blob: bd7615053b585a9604e7fdcb24df0bdb29aeeb5a (plain) (blame)
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
//! `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 };
}

/// 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, 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, 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) {
                // 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;
            },
            .unread => {},
        }
    }
}