summaryrefslogtreecommitdiff
path: root/src/trace/Debugger.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace/Debugger.zig')
-rw-r--r--src/trace/Debugger.zig118
1 files changed, 92 insertions, 26 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index fd5431f..13f5ce8 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -73,8 +73,10 @@ disasm: ?*const fn (code: []const u8, addr: u64, text: []u8, regs: ?*const Regs,
/// back at the next: an instruction's writes, with no region watched.
written: [2]Touch = undefined,
written_count: usize = 0,
-/// The instructions from the pc on whose text a stop records.
-disasm_ahead: usize = 8,
+/// The instructions above and below the pc whose text a stop records.
+disasm_around: usize = 8,
+/// Each function met, as where its instructions begin (`instructions`).
+functions: std.AutoHashMapUnmanaged(u64, []const u32) = .empty,
/// How far a stop follows pointers, from each register and from each of
/// the first `telescope_words` words of the stack: the memory they lead to
/// is recorded, and each that leads into code is named. 0, the library's
@@ -121,7 +123,11 @@ pub const Action = enum { go, stop };
pub const Breakpoint = struct { id: u32, addr: u64, saved: u8, hook: ?Hook };
/// An instruction decoded: its length, its text ("mov rbp, rsp"), and with
/// the registers given, the memory it is about to touch.
-pub const Insn = struct { len: u8, text: []const u8, touches: []const Touch = &.{} };
+pub const Insn = struct { len: u8, text: []const u8, touches: []const Touch = &.{}, branch: Branch = .{} };
+/// Where a call, a jump or a return goes. `kind`: 0 not one, 1 call, 2
+/// jump, 3 return. With `indirect` the target is read from the
+/// instruction's memory operand; a return's is on the stack.
+pub const Branch = extern struct { target: u64 = 0, kind: u8 = 0, taken: u8 = 1, indirect: u8 = 0 };
/// Memory an instruction touches: `how` is 1 for a read, 2 a write, 3 both.
pub const Touch = extern struct { addr: u64, size: u8, how: u8 };
pub const Watch = struct { addr: u64, len: usize };
@@ -167,6 +173,9 @@ pub fn deinit(d: *Debugger) void {
d.sites.deinit(d.gpa);
d.described.deinit(d.gpa);
d.code_len.deinit(d.gpa);
+ var decoded = d.functions.valueIterator();
+ while (decoded.next()) |starts| d.gpa.free(starts.*);
+ d.functions.deinit(d.gpa);
var pages = d.shadow.valueIterator();
while (pages.next()) |page| d.gpa.destroy(page.*);
d.shadow.deinit(d.gpa);
@@ -416,33 +425,90 @@ fn emitCode(d: *Debugger) !void {
// stop after.
if (d.read(d.regs.rip, code[0..15]) > 0) {
var touches: [2]Touch = undefined;
- if (insnAt(code[0..15], d.regs.rip, text[1..], &d.regs, &touches)) |insn| for (insn.touches) |t| {
- try d.snapshot(t.addr, t.size);
- try d.emit(.{ .kind = .touch, .aux = t.how, .a = .{ t.addr, t.size, d.regs.rip, 0, 0, 0 } }, "");
- if (t.how & 2 != 0) {
- d.written[d.written_count] = t;
- d.written_count += 1;
+ if (insnAt(code[0..15], d.regs.rip, text[1..], &d.regs, &touches)) |insn| {
+ for (insn.touches) |t| {
+ try d.snapshot(t.addr, t.size);
+ try d.emit(.{ .kind = .touch, .aux = t.how, .a = .{ t.addr, t.size, d.regs.rip, 0, 0, 0 } }, "");
+ if (t.how & 2 != 0) {
+ d.written[d.written_count] = t;
+ d.written_count += 1;
+ }
}
- };
+ // Where a call, a jump or a return goes, and whether it will.
+ const b = insn.branch;
+ const from: ?u64 = if (b.kind == 3) d.regs.rsp else if (b.indirect != 0 and insn.touches.len > 0) insn.touches[0].addr else null;
+ var word: [8]u8 = undefined;
+ const target: ?u64 = if (from) |addr| (if (d.read(addr, &word) == 8) std.mem.readInt(u64, &word, .little) else null) else b.target;
+ if (b.kind != 0) if (target) |to| {
+ try d.nameAddress(to);
+ try d.emit(.{ .kind = .branch, .aux = b.kind, .a = .{ to, b.taken, d.regs.rip, 0, 0, 0 } }, "");
+ };
+ }
}
- // Read only when an instruction not seen before is met: a loop costs nothing.
- var n: ?usize = null;
- var at: usize = 0;
- for (0..d.disasm_ahead) |_| {
- const addr = d.regs.rip + at;
- if (d.code_len.get(addr)) |len| {
- at += len;
- continue;
+ // The instructions round the pc, in address order. Inside a function
+ // they are known both ways, the function decoded once from its start;
+ // elsewhere (no symbol: stripped code, a JIT's) only from the pc on.
+ const around = d.disasm_around;
+ if (d.syms.symbolAt(d.regs.rip)) |sym| if (sym.size > 0 and sym.size <= 1 << 16) {
+ const starts = try d.instructions(sym, insnAt);
+ const here: u32 = @intCast(d.regs.rip - sym.addr);
+ const at = std.sort.binarySearch(u32, starts, here, struct {
+ fn order(want: u32, have: u32) std.math.Order {
+ return std.math.order(want, have);
+ }
+ }.order);
+ if (at) |k| {
+ for (k -| around..@min(starts.len, k + around + 1)) |n| {
+ _ = try d.emitInstruction(insnAt, sym.addr + starts[n], if (n > 0) @intCast(starts[n] - starts[n - 1]) else 0);
+ }
+ return;
}
- if (n == null) n = d.read(d.regs.rip, code[0..@min(code.len, d.disasm_ahead * 15)]);
- if (at >= n.?) break;
- // A byte for the length, then the text.
- const insn = insnAt(code[at..n.?], addr, text[1..], null, &.{}) orelse break;
- text[0] = insn.len;
- try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0 .. 1 + insn.text.len]);
- try d.code_len.put(d.gpa, addr, insn.len);
- at += insn.len;
+ };
+ var addr = d.regs.rip;
+ var back: u8 = 0;
+ for (0..around + 1) |_| {
+ back = (try d.emitInstruction(insnAt, addr, back)) orelse break;
+ addr += back;
+ }
+}
+
+/// Records the instruction at `addr` as a string (`event.codeId`), once: a
+/// byte for its length, a byte for the length of the one before it (0:
+/// not known), then its text. Returns its length; null for no instruction.
+fn emitInstruction(d: *Debugger, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child, addr: u64, back: u8) !?u8 {
+ if (d.code_len.get(addr)) |len| return len;
+ var code: [15]u8 = undefined;
+ var text: [192]u8 = undefined;
+ const n = d.read(addr, &code);
+ const insn = insnAt(code[0..n], addr, text[2..], null, &.{}) orelse return null;
+ text[0] = insn.len;
+ text[1] = back;
+ try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0 .. 2 + insn.text.len]);
+ try d.code_len.put(d.gpa, addr, insn.len);
+ return insn.len;
+}
+
+/// Where each instruction of a function begins, as offsets from its start:
+/// decoded once, from the first byte on, which is the only way to know
+/// where the instruction before another begins.
+fn instructions(d: *Debugger, sym: Symbols.Symbol, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child) ![]const u32 {
+ const entry = try d.functions.getOrPut(d.gpa, sym.addr);
+ if (entry.found_existing) return entry.value_ptr.*;
+ errdefer d.functions.removeByPtr(entry.key_ptr);
+ const code = try d.gpa.alloc(u8, sym.size);
+ defer d.gpa.free(code);
+ const n = d.read(sym.addr, code);
+ var starts: std.ArrayList(u32) = .empty;
+ errdefer starts.deinit(d.gpa);
+ var text: [192]u8 = undefined;
+ var off: usize = 0;
+ while (off < n) {
+ try starts.append(d.gpa, @intCast(off));
+ // Bytes that are no instruction (padding, a table) are stepped over one at a time.
+ off += if (insnAt(code[off..n], sym.addr + off, &text, null, &.{})) |insn| insn.len else 1;
}
+ entry.value_ptr.* = try starts.toOwnedSlice(d.gpa);
+ return entry.value_ptr.*;
}
/// What a viewer follows from the registers and the top of the stack, as