diff options
Diffstat (limited to 'anim/src/disasm.zig')
| -rw-r--r-- | anim/src/disasm.zig | 310 |
1 files changed, 231 insertions, 79 deletions
diff --git a/anim/src/disasm.zig b/anim/src/disasm.zig index e429643..efdb80e 100644 --- a/anim/src/disasm.zig +++ b/anim/src/disasm.zig @@ -109,10 +109,12 @@ pub const Disasm = struct { // 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 (shown.rows) |row| if (!row.outside and n < addrs.len) { + for ((if (going >= 0.5) to else from).rows) |row| if (!row.outside and n < addrs.len) { addrs[n] = row.addr; n += 1; }; @@ -129,45 +131,66 @@ pub const Disasm = struct { 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; - var run: usize = 0; - var run_start: ?f32 = 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. - const on_line = line != null and line.? == current_line and mark_shows >= 1; - if (on_line and run_start == null) run_start = @max(y, top); - if (!on_line and run_start != null) { - ties.anchor(u, .{ .run = run }, .{ .x = 0, .y = run_start.?, .w = box.w, .h = @min(y, box.h - m.pad) - run_start.? }); - run += 1; - run_start = null; - } + 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 = line; + last_line = if (above_body) null else line; + covered -|= 1; continue; } - // `ran`: it ran in one of the last three steps. - var ran: ?usize = null; - for (0..3) |k| { - const at_k = now.stops[@min(base + 1 + k, now.stops.len - 1)]; - if (at_k != now.stops[base] and rec.register(at_k, "rip") == row.addr) { - ran = k; - break; - } - } const is_pc = row.addr == pc[if (going >= 0.5) 1 else 0]; - if (ran != null and !is_pc) lit.trail(c.mark, y, ran.?); - d.insn(lit, now, row, y, box.w, line, line != last_line, current_line, lead, if (is_pc) .current else if (ran) |k| .{ .ran = k } else .ahead, base); - last_line = line; - // The jump that brought control here: from its row, up or down the lane. - if (ran == 0 and !is_pc) if (rec.insn_branches[now.stops[base + 1]]) |b| if (b.kind == .jump and b.taken) { - lanes[0] = y; - }; + 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; } - if (run_start) |y0| ties.anchor(u, .{ .run = run }, .{ .x = 0, .y = y0, .w = box.w, .h = @min(top + (@as(f32, @floatFromInt(merged.len)) - scroll) * m.row, box.h - m.pad) - y0 }); + 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; @@ -176,23 +199,32 @@ pub const Disasm = struct { context.veiled(u, box, now.veil); } - const State = union(enum) { current, ran: usize, ahead }; + 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. - 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) void { + /// `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); - u.text(.mono, size, c.operands, base_y, clipText(u, row.operands(), c.operand_chars), t.faint); - if (rec.strings.get(row.addr)) |name| u.text(.mono, size, c.annotation, base_y, name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len], t.faint); - return; + 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; } - const on = state == .current; _ = 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}); @@ -200,31 +232,58 @@ pub const Disasm = struct { 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); - operands(u, row, base_y); + // 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. 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. + /// 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) { - .current => .{ now.stops[base], if (base == 1) now.stops[0] else null }, - .ran => |k| .{ now.stops[base + 1 + k], now.stops[base + k] }, + // 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 room = w - m.side; - const x: f32 = c.annotation; 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]; - _ = on; if (branch) |b| switch (b.kind) { .call => { const callee = rec.strings.get(b.to) orelse ""; - u.text(.mono, size, x, base_y, callee[0 .. std.mem.lastIndexOfScalar(u8, callee, '+') orelse callee.len], t.bright); + 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) { - const callee_name = callee[0 .. std.mem.lastIndexOfScalar(u8, callee, '+') orelse callee.len]; // 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; @@ -236,34 +295,41 @@ pub const Disasm = struct { 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; + return note; }, .ret => { u.text(.mono, size, x, base_y, "\u{2192}", t.faint); - _ = u.address(x + 2 * adv, base_y, b.to, .{ .ink = ui.rgb(code_ink), .name = rec.strings.get(b.to) orelse "" }); - return; + 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; - if (!b.taken) return u.text(.mono, size, x, base_y, "not taken", t.dim); - u.text(.mono, size, x, base_y, "taken", t.primary); - return; + 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"); - u.text(.mono, size, x, base_y, zt.syscalls.name(nr) orelse "syscall", t.bright); + 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" }; - if (state == .current) for (names[0..@min(names.len, 3)], carriers[0..@min(names.len, 3)], 1..) |arg, reg, k| { + 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); - _ = rows.chain(u, x + @as(f32, @floatFromInt(arg.len + 1)) * adv, ry, rec.moment(moment), rec.register(moment, reg), room, .{}); + 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; + 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 @@ -287,22 +353,79 @@ pub const Disasm = struct { marks.changed_heat = now.written(touch.addr, touch.len, .memory, 0); } }; - _ = rows.chain(u, x, y, shown, touch.addr, room, marks); - return; + 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 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, + .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); - _ = rows.chain(u, x + @as(f32, @floatFromInt(reg.len + 1)) * adv, y, rec.moment(value_at), value, room, .{}); + 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 { @@ -313,25 +436,54 @@ fn alone(rec: *const Recording, i: u64, after: u64, row: Insn) bool { return false; } -/// `text` in `chars` characters: cut at its far end, and saying so. -fn clipText(u: ui.Ui, text: []const u8, chars: usize) []const u8 { - if (text.len <= chars) return text; - return u.print("{s}\u{2026}", .{text[0 .. chars - 1]}); +/// 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; } -/// The operands, as the disassembler writes them, 24 characters; a jump's -/// or a call's target is an `address` in code's ink. -fn operands(u: ui.Ui, row: Insn, base_y: f32) void { - const ops = row.operands(); +/// 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(); - const branches = mn[0] == 'j' or std.mem.eql(u8, mn, "call"); - if (branches and std.mem.startsWith(u8, ops, "0x")) { - if (std.fmt.parseInt(u64, ops[2..], 16)) |target| { - _ = u.address(c.operands, base_y, target, .{ .ink = ui.rgb(theme.color.region.code_) }); - return; - } else |_| {} + // 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; } - u.text(.mono, size, c.operands, base_y, clipText(u, ops, c.operand_chars), theme.color.code.plain); + 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 |
