//! `disasm` (design/README.md, "3. Panels"): seventeen `insn`s in address //! order, the pc's, eight above and eight below (eleven as the lens's //! subject): pwndbg's way, each instruction and what its operands are now. //! Rows above the pc are the function's own; near its start there are //! fewer and the pc's row sits higher. Past the function's end the rows //! are `outside`. The rows that ran in the last three steps are `ran`, //! with the jump lane showing how control came here. The current line's //! runs of instructions are published for the `thread`. //! Reads the recorded instructions (`zt.telescope.instruction`), each //! moment's `touch` and `branch`, and the registers. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const zt = @import("ztrace"); const ui = @import("ui.zig"); const rows = @import("rows.zig"); const cursors = @import("cursor.zig"); const Recording = @import("Recording.zig"); const ties = @import("ties.zig"); const context = @import("context.zig"); const Stage = @import("stage.zig").Stage; const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; const t = theme.color.text; const m = theme.metric.context; const c = theme.metric.insn; const size = theme.size.mono.context; const adv = size * 3 / 5; /// An instruction in the window. pub const Insn = struct { addr: u64, len: u8, text: []const u8, /// Past the function's end. outside: bool = false, /// The line of the program's source it was compiled from. line: ?u32 = null, /// A direct call's callee's parameter count. params: ?u8 = null, pub fn mnemonic(i: Insn) []const u8 { return i.text[0 .. std.mem.indexOfScalar(u8, i.text, ' ') orelse i.text.len]; } pub fn operands(i: Insn) []const u8 { return std.mem.trim(u8, i.text[i.mnemonic().len..], " "); } }; /// The window at a moment: the instructions, and which is the pc's. const Listing = struct { rows: []Insn, pc: usize }; pub const Disasm = struct { cursor: cursors.Cursor, stage: *const Stage, focus: bool = false, pub fn draw(d: *const Disasm, f: *sk.Frame, placed: anim.Placed) void { const box = d.stage.box(placed, .disasm); const u = context.at(f, placed, box); const now = d.cursor.now(placed); const rec = d.cursor.rec; const room = theme.layout.rowsIn(box.h); const above = (room -| 1) / 2; // The mark: it moves last, with the source's and rip. const going = now.advancing(); const from = listing(u, rec, now.marks[0], room, above); const to = listing(u, rec, now.marks[1], room, above); const pc = [2]u64{ rec.register(now.marks[0], "rip"), rec.register(now.marks[1], "rip") }; const name = rec.strings.get(pc[if (going >= 0.5) 1 else 0]) orelse ""; const body = u.panel(box.w, box.h, "disassembly", .{ .focus = d.focus, .note = name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len] }); const top = body.y + m.pad; u.clip(.{ .x = theme.stroke.rule, .y = m.tag, .w = box.w - 2 * theme.stroke.rule, .h = box.h - m.tag - theme.stroke.rule }); defer u.unclip(); // The two windows as one list, if they are of one function: the // rows scroll, whole rows, on `glide`; else what leaves has left // before what replaces it arrives. var merged: []const Insn = to.rows; var offsets = [2]f32{ 0, 0 }; var fade: f32 = 1; var shown = to; if (pc[0] != pc[1] and going < 1) { if (merge(u, from.rows, to.rows)) |both| { merged = both.rows; offsets = .{ @floatFromInt(both.from), @floatFromInt(both.to) }; } else { merged = if (going < 0.5) from.rows else to.rows; shown = if (going < 0.5) from else to; fade = @abs(2 * going - 1); } } const scroll = std.math.lerp(offsets[0], offsets[1], cursors.glide(going)); const lit = if (fade < 1) u.faded(fade) else u; // Where the pc's plaque is: it slides from the row it was on to the // one it goes to (in one function), or goes out and comes in. var mark_y = top + @as(f32, @floatFromInt(shown.pc)) * m.row; var mark_shows: f32 = fade; if (pc[0] != pc[1] and going < 1 and fade >= 1) { const j = std.math.lerp(@as(f32, @floatFromInt(from.pc)) + offsets[0], @as(f32, @floatFromInt(to.pc)) + offsets[1], cursors.standard(going)); mark_y = top + (j - scroll) * m.row; } if (to.rows.len == 0) mark_shows = 0; // Which moment the current mark stands for, and the three steps before it. const base: usize = if (going >= 0.5) 0 else 1; const current_line = rec.own[now.marks[if (going >= 0.5) 1 else 0]]; // The addresses' shared digits, of the window the mark is in: they // change with the advance, not before it. const lead = blk: { var addrs: [24]u64 = undefined; var n: usize = 0; for ((if (going >= 0.5) to else from).rows) |row| if (!row.outside and n < addrs.len) { addrs[n] = row.addr; n += 1; }; break :blk ui.shared(addrs[0..n]); }; // The pc's row: gold ember across, a plaque with an ink triangle. const gold = theme.mark(.current); const out = now.fires(.disasm); if (mark_shows > 0) { const v = u.faded(mark_shows); v.current(.{ .x = theme.stroke.rule, .y = mark_y, .w = box.w - 2 * theme.stroke.rule, .h = m.row }, theme.metric.plaque.pc, out); sk.draw.polygon(u.f, &.{ .{ 20, mark_y + m.row / 2 }, .{ 10, mark_y + 6 }, .{ 10, mark_y + m.row - 6 } }, v.at.pen(.{ .fill = ui.rgb(gold.on_solid) })); ties.anchor(u, .pc, .{ .x = 0, .y = mark_y, .w = theme.metric.plaque.pc, .h = m.row }); } // `ran`: what the last three steps ran (every instruction, where the // recording is by instruction), the newest step's mark on each row, // and the moment it last ran at for its annotation. const ran = u.f.scratch.alloc(?Ran, merged.len) catch return; @memset(ran, null); for (0..3) |k| { const first = now.stops[@min(base + 1 + k, now.stops.len - 1)]; const last = now.stops[base + k]; var at = last; // shortcut: a step through more than 4096 moments marks its last 4096. while (at > first and last - at < 4096) { at -= 1; const j = rowOf(merged, rec.register(at, "rip")) orelse continue; if (ran[j] == null) ran[j] = .{ .k = k, .at = at }; } } // The jump that brought control here: the instruction just before // the pc's moment, a jump taken to it. const jumped: ?u64 = blk: { const at = now.stops[base]; if (at == 0 or at <= now.stops[base + 1]) break :blk null; const b = rec.insn_branches[at - 1] orelse break :blk null; if (b.kind != .jump or !b.taken or b.to != rec.register(at, "rip")) break :blk null; break :blk rec.register(at - 1, "rip"); }; var last_line: ?u32 = null; // The thread's runs: of the line the mark leaves and of the one it // goes to, each a span of rows; published sliding from the one to // the other with the advance (`threadRuns`). var runs: [2]Runs = .{ .{}, .{} }; const lines_of = [2]u32{ rec.own[now.marks[0]], rec.own[now.marks[1]] }; var lanes: [2]?f32 = .{ null, null }; // A call's arguments hang in the annotation column of the rows under it. var covered: usize = 0; for (merged, 0..) |row, j| { const y = top + (@as(f32, @floatFromInt(j)) - scroll) * m.row; const line = if (row.outside) null else row.line; // The thread: the current line's runs of instructions. for (&runs, lines_of) |*r, of| r.add(line != null and line.? == of and mark_shows >= 1, @max(y, top), @min(y, box.h - m.pad)); // A row wholly above the body (the window it scrolls from or to) // does not take the line number from the first row shown. const above_body = y + m.row <= top + 0.5; if (y + m.row < m.tag or y > box.h) { last_line = if (above_body) null else line; covered -|= 1; continue; } 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, line, line != last_line, current_line, lead, if (is_pc) .current else if (was) |r| .{ .ran = r } else .ahead, base, covered > 0); covered = @max(covered -| 1, hangs); last_line = if (above_body) null else line; if (jumped == row.addr and !is_pc) lanes[0] = y; if (is_pc) lanes[1] = y; } const end_y = @min(top + (@as(f32, @floatFromInt(merged.len)) - scroll) * m.row, box.h - m.pad); for (&runs) |*r| r.add(false, end_y, end_y); threadRuns(u, box.w, runs, if (lines_of[0] == lines_of[1]) 1 else cursors.standard(going)); if (lanes[0] != null and lanes[1] != null and lanes[0].? != lanes[1].?) { const ya = lanes[0].? + m.row / 2; const yb = lanes[1].? + m.row / 2; lit.route(&.{ .{ 38, ya }, .{ c.jump_lane, ya }, .{ c.jump_lane, yb }, .{ 39, yb } }, theme.color.trail[0], .{ .width = theme.stroke.rule, .head = .{ 7, 8 }, .radius = 4 }); } context.veiled(u, box, now.veil); } const State = union(enum) { current, ran: Ran, ahead }; /// A row that ran `k` steps ago (0 newest), last at moment `at`. 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 /// begin to fit (never left of its column), and are cut only there, /// 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, u: ui.Ui, now: cursors.Now, row: Insn, y: f32, w: f32, line: ?u32, first_of_line: bool, current_line: u32, lead: usize, state: State, base: usize, covered: bool) usize { const rec = d.cursor.rec; const base_y = rows.baseline(y); const code_ink = theme.color.region.code_; const room = w - m.side; if (row.outside) { // The function ends: a rule, and the next one's name. u.fill(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule); _ = u.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); u.text(.mono, size, x, base_y, name, t.faint); return 0; } _ = u.address(c.address, base_y, row.addr, .{ .ink = ui.rgb(code_ink), .lead = lead }); if (first_of_line) if (line) |l| { const num = u.print("{d}", .{l}); 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); // 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); // 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(); 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; } const Note = struct { drawn: bool = false, end: f32 = 0, hangs: usize = 0 }; /// 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 { const rec = d.cursor.rec; const base_y = rows.baseline(y); const code_ink = theme.color.region.code_; const moment, var after = switch (state) { // The moment the mark stands for (at a sprint it waits at a // phrase's end while the steps go on): the pc's row is its. .current => blk: { const marked = now.marks[if (base == 0) 1 else 0]; break :blk .{ marked, if (base == 1 and marked == now.stops[1]) now.stops[0] else null }; }, .ran => |r| .{ r.at, r.at + 1 }, .ahead => .{ now.stops[base], 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]; 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 }; // 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); } return note; }, .ret => { u.text(.mono, size, x, base_y, "\u{2192}", t.faint); 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 }; }, .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, .{}); }; return .{ .drawn = true, .end = x + @as(f32, @floatFromInt(name.len)) * adv, .hangs = if (state == .current) n else 0 }; } // 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 // dump's run: an echo), or the value it leaves is struck. if (touches[0]) |touch| { // In the step that runs it: the read is this step's (an echo of // the dump's or the stack's), and a write's value after it, struck. const in_step = state == .current and base == 1; const reads = touch.how & 1 != 0; const writes = touch.how & 2 != 0; var marks: rows.Marks = .{}; var shown = rec.moment(moment); if (in_step and reads and !writes) { 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 (in_step) { marks.changed = 1; marks.changed_heat = now.written(touch.addr, touch.len, .memory, 0); } }; return .{ .drawn = true, .end = rows.chain(u, x, y, shown, touch.addr, room, marks).end }; } // 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); 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 }; } }; /// 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 { spans: [8][2]f32 = undefined, n: usize = 0, open: ?f32 = null, 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) { r.spans[r.n] = .{ y0, end }; r.n += 1; } r.open = null; }; } }; /// 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 { const from = runs[0]; const to = runs[1]; if (to.n == 0 or p >= 1 or from.n == 0) { const shown = if (p >= 0.5 or from.n == 0) to else from; for (shown.spans[0..shown.n], 0..) |span, k| ties.anchor(u, .{ .run = k }, .{ .x = 0, .y = span[0], .w = w, .h = span[1] - span[0] }); return; } for (0..@max(from.n, to.n)) |k| { const a = from.spans[@min(k, from.n - 1)]; const b = to.spans[@min(k, to.n - 1)]; const y0 = std.math.lerp(a[0], b[0], p); ties.anchor(u, .{ .run = k }, .{ .x = 0, .y = y0, .w = w, .h = std.math.lerp(a[1], b[1], p) - y0 }); } } /// Where an annotation `need` wide goes in a row that ends at `room`, /// after operands `width` wide: at its column, or a character after the /// operands, or (when both do not fit) where it just fits. fn annotationX(width: f32, room: f32, need: f32) f32 { return @max(c.annotation, @min(c.operands + width + gap, room - need)); } /// The row in `rows` (in address order) of the instruction at `addr`. fn rowOf(list: []const Insn, addr: u64) ?usize { var lo: usize = 0; var hi: usize = list.len; while (lo < hi) { const mid = (lo + hi) / 2; if (list[mid].addr < addr) lo = mid + 1 else hi = mid; } return if (lo < list.len and list[lo].addr == addr) lo else null; } /// Whether the step from moment `i` to `after` ran the instruction `row` /// alone: the moment after it is at the next instruction, or where it jumped. fn alone(rec: *const Recording, i: u64, after: u64, row: Insn) bool { if (after != i + 1) return false; const next = rec.register(after, "rip"); if (next == row.addr + row.len) return true; if (rec.insn_branches[i]) |b| return b.to == next; return false; } /// A jump's or a call's target, when the disassembler wrote it as one. 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; } /// 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 { 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 ..] }); } /// 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 { if (target(row)) |to| { _ = u.address(c.operands, base_y, to, .{ .ink = ui.rgb(theme.color.region.code_) }); 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; } 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); } /// The register an instruction leaves a value in, if that is all it does /// (Intel syntax: the first operand is where it goes). fn written(row: Insn) ?[]const u8 { const mn = row.mnemonic(); for ([_][]const u8{ "cmp", "test", "push", "nop", "endbr64", "call", "ret", "leave", "hlt", "syscall", "ud2", "int3" }) |none| if (std.mem.eql(u8, mn, none)) return null; if (mn[0] == 'j' or std.mem.startsWith(u8, mn, "cmov") and false) return null; const ops = row.operands(); const dest = ops[0 .. std.mem.indexOfScalar(u8, ops, ',') orelse ops.len]; return full(std.mem.trim(u8, dest, " ")); } /// A register's 64-bit name, from any of its widths. fn full(name: []const u8) ?[]const u8 { const table = [_][2][]const u8{ .{ "rax", "eax" }, .{ "rbx", "ebx" }, .{ "rcx", "ecx" }, .{ "rdx", "edx" }, .{ "rsi", "esi" }, .{ "rdi", "edi" }, .{ "rbp", "ebp" }, .{ "rsp", "esp" }, }; for (table) |pair| { if (std.mem.eql(u8, name, pair[0]) or std.mem.eql(u8, name, pair[1])) return pair[0]; if (name.len == 2 and pair[0][1] == name[0] and (name[1] == 'x' or name[1] == 'l' or name[1] == 'h' or name[1] == 'i' or name[1] == 'p')) return pair[0]; } if (name.len >= 2 and name[0] == 'r' and std.ascii.isDigit(name[1])) { const digits = if (name.len >= 3 and std.ascii.isDigit(name[2])) name[0..3] else name[0..2]; for ([_][]const u8{ "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" }) |r| if (std.mem.eql(u8, r, digits)) return r; } return null; } /// The window at moment `i`: up to `above` of the function's own /// 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 { 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; var addr = pc; while (n_up < @min(above, up.len)) { const here = rec.instruction(i, addr, &buf) orelse break; if (here.back == 0) break; addr -= here.back; const one = rec.instruction(i, addr, &buf) orelse break; up[n_up] = .{ .addr = addr, .len = one.len, .text = u.print("{s}", .{one.text}), .line = one.line, .params = one.params }; n_up += 1; } var n: usize = 0; while (n < n_up) : (n += 1) out[n] = up[n_up - 1 - n]; const pc_row = n; addr = pc; var outside = false; while (n < room) : (n += 1) { const one = rec.instruction(i, addr, &buf) orelse break; if (n > pc_row and one.back == 0) outside = true; out[n] = .{ .addr = addr, .len = one.len, .text = u.print("{s}", .{one.text}), .outside = outside, .line = one.line, .params = one.params }; addr += one.len; } return .{ .rows = out[0..n], .pc = pc_row }; } /// Two windows of one function as one list in address order, and where each begins in it. fn merge(u: ui.Ui, a: []const Insn, b: []const Insn) ?struct { rows: []const Insn, from: usize, to: usize } { if (a.len == 0 or b.len == 0) return null; const lo = if (a[0].addr <= b[0].addr) a else b; const hi = if (lo.ptr == a.ptr) b else a; // Where `hi` begins in `lo`: they must overlap. const at = for (lo, 0..) |row, k| { if (row.addr == hi[0].addr) break k; } else return null; const total = @max(lo.len, at + hi.len); const out = u.f.scratch.alloc(Insn, total) catch return null; @memcpy(out[0..lo.len], lo); if (at + hi.len > lo.len) @memcpy(out[lo.len..], hi[lo.len - at ..]); return .{ .rows = out, .from = if (lo.ptr == a.ptr) 0 else at, .to = if (lo.ptr == b.ptr) 0 else at }; } test "a register is known by any of its widths" { try std.testing.expectEqualStrings("rax", full("eax").?); try std.testing.expectEqualStrings("rdi", full("rdi").?); try std.testing.expectEqualStrings("r10", full("r10d").?); try std.testing.expectEqualStrings("rax", full("al").?); try std.testing.expect(full("qword ptr [rbp - 8]") == null); }