summaryrefslogtreecommitdiff
path: root/anim/src/disasm.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-11 00:06:30 -0300
committerGabriel Schneider <[email protected]>2026-11-11 01:16:48 -0300
commitc3d9f7ba5effd615374b1359ad98ecf801559f83 (patch)
tree07f85932abc1df23c722bcd1bd1b3b65bca28f2e /anim/src/disasm.zig
parentc94ade9e77a811d3ff75ffc2889a65375262ab1f (diff)
downloadcomputeranim-c3d9f7ba5effd615374b1359ad98ecf801559f83.tar.gz
computeranim-c3d9f7ba5effd615374b1359ad98ecf801559f83.zip
canim: a call followed down to the kernel; rows that say what they do; the heap from the inside
From the user. The disassembly can descend: the syscalls scene is glibc's hello by instruction, and open is followed from main through its PLT stub, whose jmp reads the GOT, into libc's wrapper to the syscall row, which crosses to the kernel's window under it, and back up. The band keeps the calls so far as strace writes them. Operands lose their size words and show a width as byte cells only where nothing else says it. A compare shows its values and the flags it leaves, a jump the flags it reads, lea its result, an indirect jump its target and the slot it read, a call that has run its C form. The backtrace has its newest frame at the bottom. A new scene shows glibc 2.44's heap from blocks that exist: chunks read by fields (a size with its bits, a protected pointer), the free list as arcs, the tcache in a loupe, the program's list over malloc's own. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim/src/disasm.zig')
-rw-r--r--anim/src/disasm.zig717
1 files changed, 608 insertions, 109 deletions
diff --git a/anim/src/disasm.zig b/anim/src/disasm.zig
index 7f17ed6..0b6f931 100644
--- a/anim/src/disasm.zig
+++ b/anim/src/disasm.zig
@@ -41,8 +41,18 @@ pub const Insn = struct {
/// A direct call's callee's parameter count.
params: ?u8 = null,
+ /// Its mnemonic, with a prefix that is part of it (`rep stosq`).
pub fn mnemonic(i: Insn) []const u8 {
- return i.text[0 .. std.mem.indexOfScalar(u8, i.text, ' ') orelse i.text.len];
+ const first = std.mem.indexOfScalar(u8, i.text, ' ') orelse return i.text;
+ for ([_][]const u8{ "rep", "repe", "repz", "repne", "repnz", "lock", "notrack", "bnd" }) |prefix| if (std.mem.eql(u8, i.text[0..first], prefix)) {
+ return i.text[0 .. std.mem.indexOfScalarPos(u8, i.text, first + 1, ' ') orelse i.text.len];
+ };
+ return i.text[0..first];
+ }
+
+ /// Padding, which touches nothing: drawn faint.
+ pub fn padding(i: Insn) bool {
+ return std.mem.startsWith(u8, i.mnemonic(), "nop") or std.mem.eql(u8, i.mnemonic(), "endbr64");
}
pub fn operands(i: Insn) []const u8 {
@@ -51,9 +61,11 @@ pub const Insn = struct {
};
/// The window at a moment: the instructions, and which is the pc's.
-const Listing = struct { rows: []Insn, pc: usize };
+pub const Listing = struct { rows: []Insn, pc: usize };
pub const Disasm = struct {
+ /// An instruction, as a row draws it.
+ pub const Row = Insn;
cursor: cursors.Cursor,
stage: *const Stage,
focus: bool = false,
@@ -171,6 +183,9 @@ pub const Disasm = struct {
var lanes: [2]?f32 = .{ null, null };
// A call's arguments hang in the annotation column of the rows under it.
var covered: usize = 0;
+ // What the window holds: a jump out of it says where it goes.
+ const window = [2]u64{ if (merged.len > 0) merged[0].addr else 0, if (merged.len > 0) merged[merged.len - 1].addr else 0 };
+ const echo = jumpEcho(now, base);
for (merged, 0..) |row, j| {
const y = top + (@as(f32, @floatFromInt(j)) - scroll) * m.row;
const line = if (row.outside) null else row.line;
@@ -188,7 +203,7 @@ pub const Disasm = struct {
const is_pc = row.addr == pc[if (going >= 0.5) 1 else 0];
const was = if (is_pc) null else ran[j];
if (was) |r| lit.trail(c.mark, y, r.k);
- const hangs = d.insn(lit, now, row, y, box.w, widen, line, line != last_line, current_line, lead, if (is_pc) .current else if (was) |r| .{ .ran = r } else .ahead, base, covered > 0);
+ const hangs = d.insn(lit, now, row, y, box.w, widen, line, line != last_line, current_line, lead, if (is_pc) .current else if (was) |r| .{ .ran = r } else .ahead, base, covered > 0, window, echo);
covered = @max(covered -| 1, hangs);
last_line = if (above_body) null else line;
if (jumped == row.addr and !is_pc) lanes[0] = y;
@@ -205,9 +220,16 @@ pub const Disasm = struct {
context.veiled(u, box, now.veil);
}
- const State = union(enum) { current, ran: Ran, ahead };
+ pub const State = union(enum) {
+ current,
+ ran: Ran,
+ ahead,
+ /// A call whose callee's window is open under it (a `descent`): the
+ /// moment it was made.
+ waiting: u64,
+ };
/// A row that ran `k` steps ago (0 newest), last at moment `at`.
- const Ran = struct { k: usize, at: u64 };
+ pub const Ran = struct { k: usize, at: u64 };
/// `insn`: an instruction and its annotation, pwndbg's way. Nothing is
/// cut that has room: the operands run on to where the annotation must
@@ -215,7 +237,7 @@ pub const Disasm = struct {
/// where the annotation carries the address they name. `covered`: a
/// call's arguments above hang in this row's annotation column. Returns
/// how many rows under it its own arguments hang in.
- fn insn(d: *const Disasm, at: ui.Ui, now: cursors.Now, row: Insn, y: f32, w: f32, widen: f32, line: ?u32, first_of_line: bool, current_line: u32, lead: usize, state: State, base: usize, covered: bool) usize {
+ pub fn insn(d: *const Disasm, at: ui.Ui, now: cursors.Now, row: Insn, y: f32, w: f32, widen: f32, line: ?u32, first_of_line: bool, current_line: u32, lead: usize, state: State, base: usize, covered: bool, window: [2]u64, echo: ?Echo) usize {
const rec = d.cursor.rec;
const base_y = rows.baseline(y);
const code_ink = theme.color.region.code_;
@@ -228,8 +250,9 @@ pub const Disasm = struct {
_ = at.address(c.address, base_y, row.addr, .{ .ink = ui.mix(code_ink, theme.color.page, theme.metric.context.lead_alpha) });
u.text(.mono, size, c.mnemonic, base_y, row.mnemonic(), t.faint);
const name = if (rec.strings.get(row.addr)) |n| n[0 .. std.mem.lastIndexOfScalar(u8, n, '+') orelse n.len] else "";
- const x = if (name.len > 0) annotationX(operandsWidth(row, row.operands()), room, @as(f32, @floatFromInt(name.len)) * adv) else room + gap;
- u.text(.mono, size, c.operands, base_y, rows.fitted(u, row.operands(), (if (covered) c.annotation else x) - gap - c.operands), t.faint);
+ const ops = shorn(u, row);
+ const x = if (name.len > 0) annotationX(operandsWidth(u, row, ops, ""), room, @as(f32, @floatFromInt(name.len)) * adv) else room + gap;
+ operands(u, row, ops, "", base_y, (if (covered) c.annotation else x) - gap - c.operands, t.faint);
u.text(.mono, size, x, base_y, name, t.faint);
return 0;
}
@@ -239,33 +262,31 @@ pub const Disasm = struct {
const now_line = l == current_line;
u.text(if (now_line) .mono_bold else .mono, size, c.line + 3 * adv - @as(f32, @floatFromInt(num.len)) * adv, base_y, num, if (now_line) theme.mark(.current).solid else t.faint);
};
- u.text(.mono_bold, size, c.mnemonic, base_y, row.mnemonic(), theme.color.code.keyword);
+ // Padding touches nothing: faint, with its operands.
+ const pad = row.padding();
+ u.text(.mono_bold, size, c.mnemonic, base_y, row.mnemonic(), if (pad) t.faint else theme.color.code.keyword);
// How wide the annotation is, drawn where nothing shows; then where it goes.
- const need = d.annotation(u.faded(0), now, row, y, c.annotation, std.math.inf(f32), state, base);
+ const need = d.annotation(u.faded(0), now, row, y, c.annotation, std.math.inf(f32), state, base, window, echo);
// When the operands and the annotation do not both fit: the
- // annotation moves left (never past its column), then a size word
- // a register already gives goes, then the operands give way.
- var ops = row.operands();
+ // annotation moves left (never past its column), then the operands give way.
+ const ops = shorn(u, row);
+ const stub = if (target(row)) |to| pltName(u, rec, to) else "";
var x = room + gap;
- if (need.drawn) {
- const wide = need.end - c.annotation;
- x = annotationX(operandsWidth(row, ops), room, wide);
- if (c.operands + operandsWidth(row, ops) + gap > x) if (lean(u, row)) |shorter| {
- ops = shorter;
- x = annotationX(operandsWidth(row, ops), room, wide);
- };
- }
- operands(u, row, ops, base_y, (if (covered) @min(x, c.annotation) else x) - gap - c.operands);
- return if (need.drawn) d.annotation(u, now, row, y, x, room, state, base).hangs else 0;
+ if (need.drawn) x = annotationX(operandsWidth(u, row, ops, stub), room, need.end - c.annotation);
+ operands(u, row, ops, stub, base_y, (if (covered) @min(x, c.annotation) else x) - gap - c.operands, if (pad) t.faint else theme.color.code.plain);
+ return if (need.drawn) d.annotation(u, now, row, y, x, room, state, base, window, echo).hangs else 0;
}
const Note = struct { drawn: bool = false, end: f32 = 0, hangs: usize = 0 };
+ /// The row that set the flags a conditional jump reads, while the jump
+ /// runs: its chips are framed as read with it (an `echo`).
+ const Echo = struct { addr: u64, shows: f32 };
/// The annotation: what it does, now, from `x`, cut where `room` ends.
/// What it left behind is shown only where the step ran this
/// instruction alone: a step by line runs others after it, and the
/// state after them is not its doing.
- fn annotation(d: *const Disasm, u: ui.Ui, now: cursors.Now, row: Insn, y: f32, x: f32, room: f32, state: State, base: usize) Note {
+ fn annotation(d: *const Disasm, u: ui.Ui, now: cursors.Now, row: Insn, y: f32, x: f32, room: f32, state: State, base: usize, window: [2]u64, echo: ?Echo) Note {
const rec = d.cursor.rec;
const base_y = rows.baseline(y);
const code_ink = theme.color.region.code_;
@@ -278,33 +299,30 @@ pub const Disasm = struct {
},
.ran => |r| .{ r.at, r.at + 1 },
.ahead => .{ now.stops[base], null },
+ .waiting => |w| .{ w, null },
};
if (after) |a| if (!alone(rec, moment, a, row)) {
after = null;
};
const branch = if (state == .ahead) null else rec.insn_branches[moment];
const touches = if (state == .ahead) [2]?Recording.Touch{ null, null } else rec.insn_touches[moment];
+ const mn = row.mnemonic();
+ const a: Say = .{ .u = u, .rec = rec, .y = y, .x = x, .room = room };
+ // Waiting on its callee: the call in C's form.
+ if (state == .waiting) if (branch) |b| return a.said(a.cForm(row, b.to, moment, null, a.room - a.x), t.primary);
+ // A system call: its name and number, its arguments hanging under it.
+ if (std.mem.eql(u8, mn, "syscall") and state != .ahead) return a.syscall(moment, state == .current);
if (branch) |b| switch (b.kind) {
.call => {
- const callee = rec.strings.get(b.to) orelse "";
- const callee_name = callee[0 .. std.mem.lastIndexOfScalar(u8, callee, '+') orelse callee.len];
- u.text(.mono, size, x, base_y, callee_name, t.bright);
- var note: Note = .{ .drawn = true, .end = x + @as(f32, @floatFromInt(callee_name.len)) * adv };
+ // Through memory or a register: where it went, and where that was read.
+ if (target(row) == null) return a.went(b.to, touches[0], moment);
+ // Once it has run: the call in C's form and its result.
+ if (state == .ran) if (returned(rec, row, moment, now.stops[base])) |back| {
+ return a.said(a.cForm(row, b.to, moment, rec.register(back, "rax"), a.room - a.x), t.primary);
+ };
+ var note = a.callee(b.to, moment);
// Its arguments hang under it, a small `reg` a row, as many as the prototype has.
- if (state == .current) {
- // As many as the callee's prototype has (the debug info's, else the source's), three when neither says.
- const count: usize = row.params orelse for (rec.functions) |fun| {
- if (std.mem.eql(u8, fun.name, callee_name)) break fun.params;
- } else 3;
- const shown_moment = rec.moment(moment);
- const args = [_][]const u8{ "rdi", "rsi", "rdx", "rcx", "r8", "r9" };
- for (args[0..@min(count, 6)], 1..) |reg, k| {
- const ry = y + @as(f32, @floatFromInt(k)) * m.row;
- u.text(.mono, size, x, rows.baseline(ry), reg, t.dim);
- _ = rows.chain(u, x + 4 * adv, ry, shown_moment, rec.register(moment, reg), room, .{});
- }
- note.hangs = @min(count, 6);
- }
+ if (state == .current) note.hangs = a.arguments(row, b.to, moment);
return note;
},
.ret => {
@@ -312,32 +330,80 @@ pub const Disasm = struct {
return .{ .drawn = true, .end = u.address(x + 2 * adv, base_y, b.to, .{ .ink = ui.rgb(code_ink), .name = rec.strings.get(b.to) orelse "" }) };
},
.jump => {
- // A jump always taken says nothing; a conditional one, which way.
- if (std.mem.eql(u8, row.mnemonic(), "jmp")) return .{};
- const said = if (b.taken) "taken" else "not taken";
- u.text(.mono, size, x, base_y, said, if (b.taken) t.primary else t.dim);
- return .{ .drawn = true, .end = x + @as(f32, @floatFromInt(said.len)) * adv };
+ // Through memory or a register: where it goes. Straight: nothing to say.
+ if (std.mem.eql(u8, mn, "jmp")) return if (target(row) == null) a.went(b.to, touches[0], moment) else .{};
+ // A conditional: which way, and the flags it reads; taken out of the window, where to.
+ var end = a.word(x, if (b.taken) "taken" else "not taken", if (b.taken) t.primary else t.dim);
+ end = a.word(end + adv, "\u{00b7} reads", t.faint);
+ end = flags(u, end + adv, y, rec.register(moment, "eflags"), flagsRead(mn), 0);
+ if (b.taken and (b.to < window[0] or b.to > window[1])) {
+ end = a.word(end + adv, "\u{2192}", t.faint);
+ end = u.address(end + adv, base_y, b.to, .{ .ink = ui.rgb(code_ink), .name = ui.symbol(u, rec.strings.get(b.to) orelse "") });
+ }
+ return .{ .drawn = true, .end = end };
},
.none => {},
};
- if (std.mem.eql(u8, row.mnemonic(), "syscall") and state != .ahead) {
- const nr = rec.register(moment, "rax");
- const name = zt.syscalls.name(nr) orelse "syscall";
- u.text(.mono, size, x, base_y, name, t.bright);
- const names = zt.syscalls.names(nr);
- const carriers = [_][]const u8{ "rdi", "rsi", "rdx" };
- const n = @min(names.len, 3);
- if (state == .current) for (names[0..n], carriers[0..n], 1..) |arg, reg, k| {
- const ry = y + @as(f32, @floatFromInt(k)) * m.row;
- u.text(.mono, size, x, rows.baseline(ry), arg, t.dim);
- const ax = x + @as(f32, @floatFromInt(arg.len + 1)) * adv;
- const value = rec.register(moment, reg);
- // A descriptor is the `value` class `fd`: what it is open on.
- if (std.mem.endsWith(u8, arg, "fd")) {
- _ = u.typed(ax, rows.baseline(ry), value, .{ .fd = rec.openOn(u.f.scratch, value, moment) }, .{});
- } else _ = rows.chain(u, ax, ry, rec.moment(moment), value, room, .{});
+ if (state == .ahead) {
+ // Only what is static: a direct call's callee; a jump through the PLT's slot.
+ if (std.mem.eql(u8, mn, "call")) if (target(row)) |to| if (rec.strings.get(to) != null) return a.callee(to, moment);
+ if (std.mem.eql(u8, mn, "jmp")) if (ripSlot(row)) |slot| if ((zt.telescope.got(rec.moment(moment), slot) catch null)) |held| return a.went(held.value, .{ .addr = slot, .len = 8, .how = 1 }, moment);
+ return .{};
+ }
+ // A compare: the two values, then (once it has run) the flags it leaves.
+ if (std.mem.eql(u8, mn, "cmp") or std.mem.eql(u8, mn, "test")) {
+ const left = a.compared(row, moment, touches[0]);
+ var end = a.word(x, left[0], t.primary);
+ end = a.word(end, " : ", t.faint);
+ end = a.word(end, left[1], t.primary);
+ const left_at: ?u64 = switch (state) {
+ .ran => after,
+ .current => if (after != null and now.landed(.cpu, 0)) after else null,
+ .ahead, .waiting => null,
};
- return .{ .drawn = true, .end = x + @as(f32, @floatFromInt(name.len)) * adv, .hangs = if (state == .current) n else 0 };
+ if (left_at) |l| {
+ var buf: [256]u8 = undefined;
+ const next = rec.instruction(moment, row.addr + row.len, &buf);
+ var mask = if (next) |n| flagsRead((Insn{ .addr = 0, .len = 0, .text = n.text }).mnemonic()) else 0;
+ if (mask == 0) mask = flag_bits.cf | flag_bits.zf | flag_bits.sf | flag_bits.of;
+ end = flags(u, end + 2 * adv, y, rec.register(l, "eflags"), mask, if (echo) |e| if (e.addr == row.addr) e.shows else 0 else 0);
+ }
+ return .{ .drawn = true, .end = end };
+ }
+ // The rest is what it left behind, or where it reads: by what it is.
+ const value_at: ?u64 = switch (state) {
+ .ran => after,
+ .current => if (after != null and now.landed(.cpu, 0)) after.? else null,
+ .ahead, .waiting => null,
+ };
+ // A place the C library's layout names: the canary.
+ if (std.mem.indexOf(u8, row.text, "fs:[0x28]") != null) {
+ const end = a.word(x, if (x + 35 * adv <= room) "the stack's canary" else "canary", t.dim);
+ const word_ = if (touches[0]) |touch| Recording.word(rec.moment(value_at orelse moment), touch.addr, 8) catch null else null;
+ if (word_) |v| return .{ .drawn = true, .end = a.word(end + adv, u.print("{x}", .{v}), t.primary) };
+ u.unknown(.{ .x = end + adv, .y = y, .w = 24, .h = m.row });
+ return .{ .drawn = true, .end = end + adv + 24 };
+ }
+ // `lea`: `=` and what it made.
+ if (std.mem.eql(u8, mn, "lea")) {
+ const at = value_at orelse return .{};
+ const reg = written(row) orelse return .{};
+ const end = a.word(x, "=", t.faint);
+ return .{ .drawn = true, .end = rows.chain(u, end + adv, y, rec.moment(at), rec.register(at, reg), room, .{}).end };
+ }
+ // A `rep`: how many times, how wide each, and where it goes.
+ if (std.mem.startsWith(u8, mn, "rep")) {
+ const each: u8 = switch (mn[mn.len - 1]) {
+ 'b' => 1,
+ 'w' => 2,
+ 'd' => 4,
+ else => 8,
+ };
+ var end = a.word(x, "rcx", t.dim);
+ end = a.word(end + adv, u.print("{d}", .{rec.register(moment, "rcx")}), t.primary);
+ end = a.word(end + adv, u.print("\u{00b7} {d} bytes each", .{each}), t.faint);
+ end = a.word(end + adv, "rdi", t.dim);
+ return .{ .drawn = true, .end = rows.chain(u, end + adv, y, rec.moment(moment), rec.register(moment, "rdi"), room, .{}).end };
}
// Memory: a chain from the address it computes. Before the step,
// where it will look; in the step its first hop is read (with the
@@ -354,8 +420,8 @@ pub const Disasm = struct {
marks.read = 0;
marks.read_shows = now.reading(.memory, 0);
}
- if (writes) if (after) |a| {
- if (state == .ran or now.landed(.memory, 0)) shown = rec.moment(a);
+ if (writes) if (after) |aa| {
+ if (state == .ran or now.landed(.memory, 0)) shown = rec.moment(aa);
if (in_step) {
marks.changed = 1;
marks.changed_heat = now.written(touch.addr, touch.len, .memory, 0);
@@ -365,27 +431,350 @@ pub const Disasm = struct {
}
// Only a register: a small `reg` of what it left there.
const reg = written(row) orelse return .{};
- const value_at = switch (state) {
- .ran => after orelse return .{},
- .current => if (after != null and now.landed(.cpu, 0)) after.? else return .{},
- .ahead => return .{},
- };
- const value = rec.register(value_at, reg);
+ const at = value_at orelse return .{};
+ const value = rec.register(at, reg);
u.text(.mono, size, x, base_y, reg, t.dim);
- return .{ .drawn = true, .end = rows.chain(u, x + @as(f32, @floatFromInt(reg.len + 1)) * adv, y, rec.moment(value_at), value, room, .{}).end };
+ return .{ .drawn = true, .end = rows.chain(u, x + @as(f32, @floatFromInt(reg.len + 1)) * adv, y, rec.moment(at), value, room, .{}).end };
+ }
+};
+
+/// What an annotation says, from `x` on the row at `y`, cut where `room` ends.
+const Say = struct {
+ u: ui.Ui,
+ rec: *const Recording,
+ y: f32,
+ x: f32,
+ room: f32,
+
+ /// `text` at `x` in `ink`; where it ends.
+ fn word(a: Say, x: f32, text: []const u8, ink: u24) f32 {
+ a.u.text(.mono, size, x, rows.baseline(a.y), text, ink);
+ return x + @as(f32, @floatFromInt(std.unicode.utf8CountCodepoints(text) catch text.len)) * adv;
+ }
+
+ /// `text` from the annotation's place, cut at the room's end.
+ fn said(a: Say, text: []const u8, ink: u24) Disasm.Note {
+ return .{ .drawn = true, .end = a.word(a.x, if (std.math.isInf(a.room)) text else rows.fitted(a.u, text, a.room - a.x), ink) };
+ }
+
+ /// A function's name as a caller knows it: a PLT stub's is the
+ /// function it reaches (`open`), or the resolver while its slot is lazy.
+ fn functionName(a: Say, to: u64, moment: u64) []const u8 {
+ const name = ui.symbol(a.u, a.rec.strings.get(to) orelse return a.u.print("{x}", .{to}));
+ const bare = name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len];
+ if (!std.mem.endsWith(u8, bare, "@plt")) return bare;
+ var buf: [256]u8 = undefined;
+ const jmp = a.rec.instruction(moment, to, &buf) orelse return bare[0 .. bare.len - 4];
+ if (ripSlot(.{ .addr = to, .len = jmp.len, .text = jmp.text })) |slot| if ((zt.telescope.got(a.rec.moment(moment), slot) catch null)) |held| {
+ if (held.lazy orelse false) return "the resolver";
+ };
+ return bare[0 .. bare.len - 4];
+ }
+
+ /// A direct call's callee, bright.
+ fn callee(a: Say, to: u64, moment: u64) Disasm.Note {
+ return .{ .drawn = true, .end = a.word(a.x, a.functionName(to, moment), t.bright) };
+ }
+
+ /// A call's arguments under it, a small `reg` a row: as many as its
+ /// prototype has (the debug info's, the source's, a system call's of
+ /// its name), three when none says. How many rows they take.
+ fn arguments(a: Say, row: Insn, to: u64, moment: u64) usize {
+ const name = a.functionName(to, moment);
+ const count = @min(row.params orelse for (a.rec.functions) |fun| {
+ if (std.mem.eql(u8, fun.name, name)) break fun.params;
+ } else argumentCount(a.rec, row, name) orelse 3, 6);
+ const shown = a.rec.moment(moment);
+ // Flags by name, where a system call of its name says which argument they are.
+ const nr: ?u64 = if (std.meta.stringToEnum(std.os.linux.SYS, name)) |sys| @intFromEnum(sys) else null;
+ const names: []const []const u8 = if (nr) |n| zt.syscalls.names(n) else &.{};
+ for (carriers[0..count], 1..) |reg, k| {
+ const ry = a.y + @as(f32, @floatFromInt(k)) * m.row;
+ a.u.text(.mono, size, a.x, rows.baseline(ry), reg, t.dim);
+ const v = a.rec.register(moment, reg);
+ if (k - 1 < names.len and std.mem.eql(u8, names[k - 1], "flags")) {
+ a.u.text(.mono, size, a.x + 4 * adv, rows.baseline(ry), openFlags(a.u, v), t.primary);
+ } else _ = rows.chain(a.u, a.x + 4 * adv, ry, shown, v, a.room, .{});
+ }
+ return count;
+ }
+
+ /// The call at `row` (to `to`, made at `moment`) in C's form, its
+ /// arguments as the source wrote that many and as the system call of its
+ /// name types them; with ` = result` when it has returned.
+ fn cForm(a: Say, row: Insn, to: u64, moment: u64, result: ?u64, width: f32) []const u8 {
+ const u = a.u;
+ const name = a.functionName(to, moment);
+ const nr: ?u64 = if (std.meta.stringToEnum(std.os.linux.SYS, name)) |sys| @intFromEnum(sys) else null;
+ const names: []const []const u8 = if (nr) |n| zt.syscalls.names(n) else &.{};
+ const count = @min(row.params orelse argumentCount(a.rec, row, name) orelse names.len, 6);
+ // Out of room the arguments go from the right, `…` for them; the result stays.
+ var shown = count;
+ while (true) : (shown -= 1) {
+ var out: []const u8 = u.print("{s}(", .{name});
+ for (carriers[0..shown], 0..) |reg, k| {
+ const v = a.rec.register(moment, reg);
+ out = u.print("{s}{s}{s}", .{ out, if (k > 0) ", " else "", argument(u, a.rec, moment, if (k < names.len) names[k] else "", v) });
+ }
+ if (shown < count) out = u.print("{s}{s}\u{2026}", .{ out, if (shown > 0) ", " else "" });
+ out = u.print("{s})", .{out});
+ if (result) |r| out = u.print("{s} = {d}", .{ out, @as(i64, @bitCast(r)) });
+ if (shown == 0 or @as(f32, @floatFromInt(std.unicode.utf8CountCodepoints(out) catch out.len)) * adv <= width) return out;
+ }
+ }
+
+ /// Where a call or a jump through memory or a register went: `→ addr
+ /// name`, then faint where it was read, `· read from open@got, 403fd8`.
+ /// Out of room the slot goes first, then the digits; the name stays.
+ fn went(a: Say, to: u64, slot: ?Recording.Touch, moment: u64) Disasm.Note {
+ const u = a.u;
+ const name = a.functionName(to, moment);
+ const from: []const u8 = if (slot) |s| blk: {
+ const named = ui.symbol(u, a.rec.strings.get(s.addr) orelse "");
+ break :blk u.print("\u{00b7} read from {s}{s}{x}", .{ named[0 .. std.mem.lastIndexOfScalar(u8, named, '+') orelse named.len], if (named.len > 0) ", " else "", s.addr });
+ } else "";
+ const digits_w = @as(f32, @floatFromInt(ui.digits(to) + 1)) * adv;
+ const name_w = @as(f32, @floatFromInt(name.len)) * adv;
+ const from_w = @as(f32, @floatFromInt(std.unicode.utf8CountCodepoints(from) catch from.len)) * adv;
+ const fits_all = a.x + 2 * adv + digits_w + name_w + adv + from_w <= a.room;
+ const fits_digits = a.x + 2 * adv + digits_w + name_w <= a.room;
+ var end = a.word(a.x, "\u{2192}", t.faint) + adv;
+ if (fits_digits) {
+ end = u.address(end, rows.baseline(a.y), to, .{ .ink = ui.rgb(theme.color.region.code_), .name = name });
+ } else end = a.word(end, name, t.primary);
+ if (fits_all and from.len > 0) end = a.word(end + adv, from, t.faint);
+ return .{ .drawn = true, .end = end };
+ }
+
+ /// The two values a compare compares, as its operands' size reads them.
+ fn compared(a: Say, row: Insn, moment: u64, touch: ?Recording.Touch) [2][]const u8 {
+ const ops = shorn(a.u, row);
+ var parts = std.mem.splitScalar(u8, ops.text, ',');
+ var operands_: [2][]const u8 = .{ "", "" };
+ for (&operands_) |*o| o.* = std.mem.trim(u8, parts.next() orelse "", " ");
+ // The size: a register's, else the width's, else a word's.
+ var bytes: u8 = if (ops.bytes > 0) ops.bytes else 8;
+ for (operands_) |o| if (registerSize(o)) |n| {
+ bytes = n;
+ };
+ var out: [2][]const u8 = undefined;
+ for (operands_, &out) |o, *v| {
+ const raw: ?u64 = if (std.mem.indexOfScalar(u8, o, '[') != null)
+ (if (touch) |tt| Recording.word(a.rec.moment(moment), tt.addr, @min(tt.len, 8)) catch null else null)
+ else if (full(o)) |reg|
+ a.rec.register(moment, reg)
+ else
+ parseImmediate(o);
+ v.* = if (raw) |r| number(a.u, r, bytes) else "..";
+ }
+ return out;
+ }
+
+ /// `syscall`: its name and number (`openat nr 257`); its arguments hang
+ /// under it, a register a row, each read as its name says.
+ fn syscall(a: Say, moment: u64, current: bool) Disasm.Note {
+ const u = a.u;
+ const nr = a.rec.register(moment, "rax");
+ var end = a.word(a.x, zt.syscalls.name(nr) orelse "syscall", t.bright);
+ end = a.word(end + adv, "nr", t.faint);
+ end = a.word(end + adv, u.print("{d}", .{nr}), t.primary);
+ const names = zt.syscalls.names(nr);
+ const n = @min(zt.syscalls.signature(nr).len, 6);
+ if (!current) return .{ .drawn = true, .end = end };
+ for (carriers_sys[0..n], 0..) |reg, k| {
+ const ry = a.y + @as(f32, @floatFromInt(k + 1)) * m.row;
+ u.text(.mono, size, a.x, rows.baseline(ry), reg, t.dim);
+ const v = a.rec.register(moment, reg);
+ const what = if (k < names.len) names[k] else "";
+ const ax = a.x + 4 * adv;
+ if (std.mem.eql(u8, what, "path") or std.mem.eql(u8, what, "buf")) {
+ _ = rows.chain(u, ax, ry, a.rec.moment(moment), v, a.room, .{});
+ } else if (std.mem.endsWith(u8, what, "fd") and v == fdcwd) {
+ // The value class `fd`: the number, and faint what it names.
+ u.text(.mono, size, ax, rows.baseline(ry), "-100", t.primary);
+ u.text(.mono, size, ax + 5 * adv, rows.baseline(ry), "AT_FDCWD", t.faint);
+ } else if (std.mem.endsWith(u8, what, "fd")) {
+ _ = u.typed(ax, rows.baseline(ry), v, .{ .fd = a.rec.openOn(u.f.scratch, v, moment) }, .{});
+ } else u.text(.mono, size, ax, rows.baseline(ry), argument(u, a.rec, moment, what, v), t.primary);
+ }
+ return .{ .drawn = true, .end = end, .hangs = n };
+ }
+};
+
+/// A call's argument registers, in order; a system call's.
+const carriers = [_][]const u8{ "rdi", "rsi", "rdx", "rcx", "r8", "r9" };
+const carriers_sys = [_][]const u8{ "rdi", "rsi", "rdx", "r10", "r8", "r9" };
+/// `AT_FDCWD`, as a register holds it.
+const fdcwd: u64 = @bitCast(@as(i64, -100));
+
+/// An argument's value as C would write it, by its name: a path as its
+/// string, flags by name, a descriptor and a count in decimal, a pointer as
+/// its address.
+pub fn argument(u: ui.Ui, rec: *const Recording, moment: u64, what: []const u8, v: u64) []const u8 {
+ if (std.mem.eql(u8, what, "path")) {
+ var links: [2]zt.telescope.Link = undefined;
+ var text: [48]u8 = undefined;
+ const chain = zt.telescope.chase(rec.moment(moment), v, &links, &text) catch &.{};
+ if (chain.len > 0 and chain[chain.len - 1].string) return u.print("\"{s}\"", .{chain[chain.len - 1].text});
+ }
+ if (std.mem.eql(u8, what, "flags")) return openFlags(u, v);
+ if (std.mem.endsWith(u8, what, "fd") and v == fdcwd) return "AT_FDCWD";
+ const signed: i64 = @bitCast(v);
+ if (signed > -4096 and signed < 0x10000) return u.print("{d}", .{signed});
+ return u.print("{x}", .{v});
+}
+
+/// `open`'s flags by name, `O_RDONLY`; bits no name covers as `0x` and their hex.
+fn openFlags(u: ui.Ui, v: u64) []const u8 {
+ var out: []const u8 = switch (v & 3) {
+ 0 => "O_RDONLY",
+ 1 => "O_WRONLY",
+ else => "O_RDWR",
+ };
+ var rest = v & ~@as(u64, 3);
+ for ([_]struct { []const u8, u64 }{ .{ "O_CREAT", 0o100 }, .{ "O_EXCL", 0o200 }, .{ "O_NOCTTY", 0o400 }, .{ "O_TRUNC", 0o1000 }, .{ "O_APPEND", 0o2000 }, .{ "O_NONBLOCK", 0o4000 }, .{ "O_DIRECTORY", 0o200000 }, .{ "O_NOFOLLOW", 0o400000 }, .{ "O_CLOEXEC", 0o2000000 } }) |f| if (rest & f[1] != 0) {
+ out = u.print("{s}|{s}", .{ out, f[0] });
+ rest &= ~f[1];
+ };
+ return if (rest != 0) u.print("{s} 0x{x}", .{ out, rest }) else out;
+}
+
+/// How many arguments the program's source gives `name` on the call's line.
+fn argumentCount(rec: *const Recording, row: Insn, name: []const u8) ?usize {
+ const line = row.line orelse return null;
+ var lines = std.mem.splitScalar(u8, rec.source, '\n');
+ var n: u32 = 1;
+ const text = while (lines.next()) |l| : (n += 1) {
+ if (n == line) break l;
+ } else return null;
+ const open = (std.mem.indexOf(u8, text, name) orelse return null) + name.len;
+ if (open >= text.len or text[open] != '(') return null;
+ var depth: usize = 0;
+ var count: usize = 0;
+ var any = false;
+ for (text[open + 1 ..]) |ch| switch (ch) {
+ '(' => depth += 1,
+ ')' => {
+ if (depth == 0) return if (any) count + 1 else 0;
+ depth -= 1;
+ },
+ ',' => if (depth == 0) {
+ count += 1;
+ },
+ ' ' => {},
+ else => any = true,
+ };
+ return null;
+}
+
+/// The moment a call made at `moment` from `row` came back to the row after
+/// it, at or before `until`.
+fn returned(rec: *const Recording, row: Insn, moment: u64, until: u64) ?u64 {
+ const sp = rec.register(moment, "rsp");
+ var i = moment + 1;
+ while (i <= until) : (i += 1) {
+ if (rec.register(i, "rip") == row.addr + row.len and rec.register(i, "rsp") == sp) return i;
}
+ return null;
+}
+
+/// The slot a `jmp` or `call` through `[rip + X]` reads.
+fn ripSlot(row: Insn) ?u64 {
+ const ops = row.operands();
+ const at = std.mem.indexOf(u8, ops, "[rip + 0x") orelse return null;
+ const close = std.mem.indexOfScalarPos(u8, ops, at, ']') orelse return null;
+ const disp = std.fmt.parseInt(u64, ops[at + 9 .. close], 16) catch return null;
+ return row.addr + row.len + disp;
+}
+
+/// An immediate operand's value.
+fn parseImmediate(o: []const u8) ?u64 {
+ const negative = std.mem.startsWith(u8, o, "-");
+ const digits_ = if (negative) o[1..] else o;
+ const v = (if (std.mem.startsWith(u8, digits_, "0x")) std.fmt.parseInt(u64, digits_[2..], 16) else std.fmt.parseInt(u64, digits_, 10)) catch return null;
+ return if (negative) 0 -% v else v;
+}
+
+/// `v` as a number `bytes` wide: signed and in decimal when small, else hex.
+fn number(u: ui.Ui, v: u64, bytes: u8) []const u8 {
+ const bits: u7 = @as(u7, @min(bytes, 8)) * 8;
+ const masked = if (bits == 64) v else v & ((@as(u64, 1) << @intCast(bits)) - 1);
+ const signed: i64 = if (bits == 64) @bitCast(masked) else @as(i64, @bitCast(masked << @intCast(64 - @as(u7, bits)))) >> @intCast(64 - @as(u7, bits));
+ if (signed > -0x10000 and signed < 0x10000) return u.print("{d}", .{signed});
+ return u.print("{x}", .{masked});
+}
+
+/// eflags' bits a reader meets.
+const flag_bits = struct {
+ const cf: u16 = 1 << 0;
+ const pf: u16 = 1 << 2;
+ const zf: u16 = 1 << 6;
+ const sf: u16 = 1 << 7;
+ const of: u16 = 1 << 11;
};
+/// The flags a conditional (`jcc`, `setcc`, `cmovcc`) reads; 0 for anything else.
+pub fn flagsRead(mn: []const u8) u16 {
+ const cond = if (std.mem.startsWith(u8, mn, "cmov")) mn[4..] else if (std.mem.startsWith(u8, mn, "set")) mn[3..] else if (mn.len > 1 and mn[0] == 'j' and !std.mem.eql(u8, mn, "jmp")) mn[1..] else return 0;
+ const f = flag_bits;
+ const table = [_]struct { []const u8, u16 }{
+ .{ "a", f.cf | f.zf }, .{ "nbe", f.cf | f.zf }, .{ "be", f.cf | f.zf }, .{ "na", f.cf | f.zf },
+ .{ "ae", f.cf }, .{ "nb", f.cf }, .{ "nc", f.cf }, .{ "b", f.cf },
+ .{ "c", f.cf }, .{ "nae", f.cf }, .{ "e", f.zf }, .{ "z", f.zf },
+ .{ "ne", f.zf }, .{ "nz", f.zf }, .{ "g", f.zf | f.sf | f.of }, .{ "nle", f.zf | f.sf | f.of },
+ .{ "le", f.zf | f.sf | f.of }, .{ "ng", f.zf | f.sf | f.of }, .{ "ge", f.sf | f.of }, .{ "nl", f.sf | f.of },
+ .{ "l", f.sf | f.of }, .{ "nge", f.sf | f.of }, .{ "o", f.of }, .{ "no", f.of },
+ .{ "s", f.sf }, .{ "ns", f.sf }, .{ "p", f.pf }, .{ "pe", f.pf },
+ .{ "np", f.pf }, .{ "po", f.pf },
+ };
+ for (table) |e| if (std.mem.eql(u8, cond, e[0])) return e[1];
+ return 0;
+}
+
+/// The flags of `mask`, as rip's row draws them: set, upper case on a
+/// chip; clear, lower case and dim; each framed as read by `read`. Where they end.
+fn flags(u: ui.Ui, x: f32, y: f32, eflags: u64, mask: u16, read: f32) f32 {
+ var at = x;
+ for (rows.flags) |flag| {
+ if (@as(u64, mask) >> flag.bit & 1 == 0) continue;
+ const chip: Rect = .{ .x = at, .y = y + 2, .w = theme.metric.flags.chip[0], .h = theme.metric.flags.chip[1] };
+ if (eflags >> flag.bit & 1 != 0) {
+ u.chip(chip);
+ u.text(.mono_bold, size, at + 4, rows.baseline(y), flag.name, t.bright);
+ } else u.text(.mono, size, at + 4, rows.baseline(y), u.print("{c}{c}", .{ std.ascii.toLower(flag.name[0]), std.ascii.toLower(flag.name[1]) }), t.dim);
+ if (read > 0) u.read(chip, read);
+ at += chip.w + 4;
+ }
+ return at - 4;
+}
+
+/// While a conditional jump runs (the mark on it, its step under way), the
+/// row that set the flags it reads: the instruction just before it ran.
+fn jumpEcho(now: cursors.Now, base: usize) ?Disasm.Echo {
+ if (base != 1 or now.resting()) return null;
+ const flags_ = readsFlags(now.rec, now.stops[1]);
+ if (flags_ == 0 or now.stops[1] == 0) return null;
+ return .{ .addr = now.rec.register(now.stops[1] - 1, "rip"), .shows = now.reading(.cpu, 0) };
+}
+
+/// The flags the instruction at moment `i`'s pc reads, if it is a conditional.
+pub fn readsFlags(rec: *const Recording, i: u64) u16 {
+ var buf: [256]u8 = undefined;
+ const pc = rec.register(i, "rip");
+ const here = rec.instruction(i, pc, &buf) orelse return 0;
+ return flagsRead((Insn{ .addr = pc, .len = here.len, .text = here.text }).mnemonic());
+}
+
/// The gap between the operands and the annotation: a character.
const gap = adv;
/// The runs of rows of one line, top and bottom of each, as the rows go by.
-const Runs = struct {
+pub const Runs = struct {
spans: [8][2]f32 = undefined,
n: usize = 0,
open: ?f32 = null,
- fn add(r: *Runs, on: bool, y: f32, end: f32) void {
+ pub fn add(r: *Runs, on: bool, y: f32, end: f32) void {
if (on and r.open == null) r.open = y;
if (!on) if (r.open) |y0| {
if (r.n < r.spans.len and end > y0) {
@@ -400,7 +789,7 @@ const Runs = struct {
/// The thread's runs, `p` of the way from the line the mark leaves to the
/// one it goes to: each slides to its partner (by order, the last of the
/// fewer standing in for the rest), as the source's bracket slides.
-fn threadRuns(u: ui.Ui, w: f32, runs: [2]Runs, p: f32) void {
+pub fn threadRuns(u: ui.Ui, w: f32, runs: [2]Runs, p: f32) void {
const from = runs[0];
const to = runs[1];
if (to.n == 0 or p >= 1 or from.n == 0) {
@@ -445,53 +834,147 @@ fn alone(rec: *const Recording, i: u64, after: u64, row: Insn) bool {
}
/// A jump's or a call's target, when the disassembler wrote it as one.
-fn target(row: Insn) ?u64 {
+pub fn target(row: Insn) ?u64 {
const ops = row.operands();
const mn = row.mnemonic();
if (!(mn[0] == 'j' or std.mem.eql(u8, mn, "call")) or !std.mem.startsWith(u8, ops, "0x")) return null;
return std.fmt.parseInt(u64, ops[2..], 16) catch null;
}
-/// How wide operands `ops` of `row` are, whole.
-fn operandsWidth(row: Insn, ops: []const u8) f32 {
- if (target(row)) |to| return @as(f32, @floatFromInt(ui.digits(to))) * adv;
- return @as(f32, @floatFromInt(ops.len)) * adv;
+/// An `insn` with no state and no annotation: its address, its mnemonic
+/// (faint for padding) and its operands as canim draws them, at `y` in `u`
+/// (the panel's origin). For the gallery's page of rows.
+pub fn plainRow(u: ui.Ui, row: Insn, y: f32) void {
+ const base_y = rows.baseline(y);
+ _ = u.address(c.address, base_y, row.addr, .{ .ink = ui.rgb(theme.color.region.code_) });
+ // A library's twelve digits move what is right of them, 12 a digit past seven.
+ const v = u.moved(@as(f32, @floatFromInt(@max(ui.digits(row.addr), 7) - 7)) * adv, 0);
+ const pad = row.padding();
+ v.text(.mono_bold, size, c.mnemonic, base_y, row.mnemonic(), if (pad) t.faint else theme.color.code.keyword);
+ operands(v, row, shorn(v, row), "", base_y, c.annotation - gap - c.operands, if (pad) t.faint else theme.color.code.plain);
}
-/// The operands without the size word of a memory operand whose size a
-/// register operand gives (`mov rax, [rbp - 0x28]`): nothing is lost.
-/// Null where there is none to drop.
-fn lean(u: ui.Ui, row: Insn) ?[]const u8 {
+/// Operands as canim draws them (design/README.md, `insn` and `width`):
+/// the disassembler's less every size word (`qword ptr `); and where no
+/// register outside the brackets has the access's size and the mnemonic
+/// does not fix it, how many bytes it touches (`bytes`), drawn as byte
+/// cells after the `]` that ends at `after`.
+const Operands = struct { text: []const u8, after: usize = 0, bytes: u8 = 0 };
+
+fn shorn(u: ui.Ui, row: Insn) Operands {
+ var ops = row.operands();
+ var bytes: u8 = 0;
+ while (std.mem.indexOf(u8, ops, " ptr ")) |ptr| {
+ var word = ptr;
+ while (word > 0 and ops[word - 1] != ' ' and ops[word - 1] != ',') word -= 1;
+ for ([_]struct { []const u8, u8 }{ .{ "byte", 1 }, .{ "word", 2 }, .{ "dword", 4 }, .{ "qword", 8 }, .{ "tbyte", 10 }, .{ "xmmword", 16 }, .{ "ymmword", 32 }, .{ "zmmword", 64 } }) |w| {
+ if (std.mem.eql(u8, ops[word..ptr], w[0])) bytes = w[1];
+ }
+ ops = u.print("{s}{s}", .{ ops[0..word], ops[ptr + 5 ..] });
+ }
+ const close = std.mem.indexOfScalar(u8, ops, ']') orelse return .{ .text = ops };
+ if (bytes == 0) return .{ .text = ops };
+ // Said already: by the mnemonic (a stack op, a branch through memory,
+ // what touches nothing, a string op's suffix, SSE's scalar ops), or by
+ // a register outside the brackets of the access's size.
const mn = row.mnemonic();
- // These widen or convert: the register does not give the memory's size.
- for ([_][]const u8{ "movzx", "movsx", "movsxd", "cvt" }) |w| if (std.mem.startsWith(u8, mn, w)) return null;
- const ops = row.operands();
- const ptr = std.mem.indexOf(u8, ops, " ptr ") orelse return null;
- var word = ptr;
- while (word > 0 and ops[word - 1] != ' ' and ops[word - 1] != ',') word -= 1;
- const comma = std.mem.indexOfScalar(u8, ops, ',') orelse return null;
- const other = std.mem.trim(u8, if (comma < word) ops[0..comma] else ops[comma + 1 ..], " ");
- if (full(other) == null) return null;
- return u.print("{s}{s}", .{ ops[0..word], ops[ptr + 5 ..] });
+ for ([_][]const u8{ "push", "pop", "call", "jmp", "lea", "nop", "rep" }) |says| if (std.mem.startsWith(u8, mn, says)) return .{ .text = ops };
+ if (mn.len == 0 or mn[0] == 'j') return .{ .text = ops };
+ var parts = std.mem.splitScalar(u8, ops, ',');
+ while (parts.next()) |part| {
+ const operand = std.mem.trim(u8, part, " ");
+ if (std.mem.indexOfScalar(u8, operand, '[') != null) continue;
+ const of = registerSize(operand) orelse continue;
+ if (of == bytes or of >= 16) return .{ .text = ops };
+ }
+ return .{ .text = ops, .after = close + 1, .bytes = bytes };
+}
+
+/// How many bytes register `name` is; null for anything else.
+fn registerSize(name: []const u8) ?u8 {
+ if (name.len < 2) return null;
+ for ([_]struct { []const u8, u8 }{ .{ "xmm", 16 }, .{ "ymm", 32 }, .{ "zmm", 64 } }) |v| if (std.mem.startsWith(u8, name, v[0])) return v[1];
+ if (full(name) == null and !(name.len == 3 and name[2] == 'l')) return null;
+ if (name[0] == 'r' and std.ascii.isDigit(name[1])) return switch (name[name.len - 1]) {
+ 'd' => 4,
+ 'w' => 2,
+ 'b' => 1,
+ else => 8,
+ };
+ if (name.len == 3) return switch (name[0]) {
+ 'r' => 8,
+ 'e' => 4,
+ else => 1, // sil, dil, bpl, spl
+ };
+ return if (name[1] == 'l' or name[1] == 'h') 1 else 2;
+}
+
+/// How wide `width`'s cells are for an access of `bytes`, with the gaps
+/// round them (3 after the `]`, 4 before what follows) and, past eight
+/// bytes, the count.
+fn cellsWidth(u: ui.Ui, bytes: u8) f32 {
+ if (bytes == 0) return 0;
+ const w = theme.metric.width;
+ const n: f32 = @floatFromInt(@min(bytes, 8));
+ const count: f32 = if (bytes > 8) u.width(.mono, size, u.print("{d}", .{bytes})) + w.after else 0;
+ return w.before + (n - 1) * w.pitch + w.cell[0] + w.after + count;
+}
+
+/// `width`: `bytes` byte cells at `x`, standing on the baseline; a count past eight.
+fn cells(u: ui.Ui, x: f32, base_y: f32, bytes: u8) void {
+ const w = theme.metric.width;
+ for (0..@min(bytes, 8)) |k| u.fill(.{ .x = x + w.before + @as(f32, @floatFromInt(k)) * w.pitch, .y = base_y - w.cell[1], .w = w.cell[0], .h = w.cell[1] }, t.dim);
+ if (bytes > 8) u.text(.mono, size, x + w.before + 7 * w.pitch + w.cell[0] + w.after, base_y, u.print("{d}", .{bytes}), t.faint);
+}
+
+/// A PLT stub's name, for a call or a jump to one: `open@plt`; "" otherwise.
+fn pltName(u: ui.Ui, rec: *const Recording, to: u64) []const u8 {
+ const name = ui.symbol(u, rec.strings.get(to) orelse return "");
+ const stub = name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len];
+ return if (std.mem.endsWith(u8, stub, "@plt")) stub else "";
}
-/// The operands `ops`, in `width` units; a jump's or a call's target is
-/// an `address` in code's ink. Cut only at their far end, saying so, and
-/// never inside `[]`: a cut that would fall there falls before the `[`.
-fn operands(u: ui.Ui, row: Insn, ops: []const u8, base_y: f32, width: f32) void {
+/// How wide operands `ops` of `row` are, whole (a target with its stub's name `stub`).
+fn operandsWidth(u: ui.Ui, row: Insn, ops: Operands, stub: []const u8) f32 {
+ if (target(row)) |to| return @as(f32, @floatFromInt(ui.digits(to) + if (stub.len > 0) stub.len + 1 else 0)) * adv;
+ return @as(f32, @floatFromInt(ops.text.len)) * adv + cellsWidth(u, ops.bytes);
+}
+
+/// The operands `ops`, in `width` units, in `ink`; a jump's or a call's
+/// target is an `address` in code's ink, a PLT stub's with its name. Cut
+/// only at their far end, saying so, and never inside `[]`: a cut that
+/// would fall there falls before the `[`.
+fn operands(u: ui.Ui, row: Insn, ops: Operands, stub: []const u8, base_y: f32, width: f32, ink: u24) void {
if (target(row)) |to| {
- _ = u.address(c.operands, base_y, to, .{ .ink = ui.rgb(theme.color.region.code_) });
+ _ = u.address(c.operands, base_y, to, .{ .ink = ui.rgb(theme.color.region.code_), .name = stub });
+ return;
+ }
+ const text = ops.text;
+ const extra = cellsWidth(u, ops.bytes);
+ const at_after = c.operands + @as(f32, @floatFromInt(ops.after)) * adv;
+ if (@as(f32, @floatFromInt(text.len)) * adv + extra <= width) {
+ if (ops.bytes == 0) return u.text(.mono, size, c.operands, base_y, text, ink);
+ u.text(.mono, size, c.operands, base_y, text[0..ops.after], ink);
+ cells(u, at_after, base_y, ops.bytes);
+ u.text(.mono, size, at_after + extra, base_y, text[ops.after..], ink);
return;
}
- const chars: usize = @intFromFloat(@max(width / adv, 1));
- if (ops.len <= chars) return u.text(.mono, size, c.operands, base_y, ops, theme.color.code.plain);
- var cut = chars - 1;
- if (std.mem.lastIndexOfScalar(u8, ops[0..cut], '[')) |open| {
- if (std.mem.indexOfScalarPos(u8, ops, open, ']') orelse ops.len >= cut) cut = open;
+ var chars: usize = @intFromFloat(@max(width / adv, 1));
+ if (ops.bytes > 0 and chars > ops.after) chars = @max(@as(usize, @intFromFloat(@max((width - extra) / adv, 1))), ops.after);
+ var cut = @min(chars - 1, text.len);
+ if (std.mem.lastIndexOfScalar(u8, text[0..cut], '[')) |open| {
+ if (std.mem.indexOfScalarPos(u8, text, open, ']') orelse text.len >= cut) cut = open;
}
- while (cut > 0 and ops[cut - 1] == ' ') cut -= 1;
- u.text(.mono, size, c.operands, base_y, ops[0..cut], theme.color.code.plain);
- u.text(.mono, size, c.operands + @as(f32, @floatFromInt(cut)) * adv, base_y, "\u{2026}", t.faint);
+ while (cut > 0 and text[cut - 1] == ' ') cut -= 1;
+ const shown_cells = ops.bytes > 0 and cut >= ops.after;
+ u.text(.mono, size, c.operands, base_y, text[0..if (shown_cells) ops.after else cut], ink);
+ var end = c.operands + @as(f32, @floatFromInt(cut)) * adv;
+ if (shown_cells) {
+ cells(u, at_after, base_y, ops.bytes);
+ u.text(.mono, size, at_after + extra, base_y, text[ops.after..cut], ink);
+ end += extra;
+ }
+ u.text(.mono, size, end, base_y, "\u{2026}", t.faint);
}
/// The register an instruction leaves a value in, if that is all it does
@@ -526,8 +1009,12 @@ fn full(name: []const u8) ?[]const u8 {
/// instructions over the pc (fewer near its start), the pc's, and as many
/// after it as fill `room`; past the function's end, `outside`.
fn listing(u: ui.Ui, rec: *const Recording, i: u64, room: usize, above: usize) Listing {
+ return listingAt(u, rec, i, rec.register(i, "rip"), room, above);
+}
+
+/// The same round the instruction at `pc` (in the window, `Listing.pc`).
+pub fn listingAt(u: ui.Ui, rec: *const Recording, i: u64, pc: u64, room: usize, above: usize) Listing {
const out = u.f.scratch.alloc(Insn, room) catch return .{ .rows = &.{}, .pc = 0 };
- const pc = rec.register(i, "rip");
var buf: [256]u8 = undefined;
var up: [16]Insn = undefined;
var n_up: usize = 0;
@@ -570,6 +1057,18 @@ fn merge(u: ui.Ui, a: []const Insn, b: []const Insn) ?struct { rows: []const Ins
return .{ .rows = out, .from = if (lo.ptr == a.ptr) 0 else at, .to = if (lo.ptr == b.ptr) 0 else at };
}
+test "a register's size, by its name" {
+ try std.testing.expectEqual(@as(?u8, 8), registerSize("rcx"));
+ try std.testing.expectEqual(@as(?u8, 4), registerSize("eax"));
+ try std.testing.expectEqual(@as(?u8, 4), registerSize("r10d"));
+ try std.testing.expectEqual(@as(?u8, 2), registerSize("ax"));
+ try std.testing.expectEqual(@as(?u8, 1), registerSize("dil"));
+ try std.testing.expectEqual(@as(?u8, 1), registerSize("al"));
+ try std.testing.expectEqual(@as(?u8, 16), registerSize("xmm0"));
+ try std.testing.expectEqual(@as(?u8, null), registerSize("0x10"));
+ try std.testing.expectEqual(@as(?u8, null), registerSize("fs:[0x28]"));
+}
+
test "a register is known by any of its widths" {
try std.testing.expectEqualStrings("rax", full("eax").?);
try std.testing.expectEqualStrings("rdi", full("rdi").?);