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.zig90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index 5e679dd..41a908f 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -65,6 +65,22 @@ next_id: u32 = 1,
/// What the sink was sent of the target's memory, by page.
shadow: std.AutoHashMapUnmanaged(u64, *Page) = .empty,
scratch: std.ArrayList(u8) = .empty,
+/// Decodes one instruction into `text`; null where there is none. Whoever
+/// has a disassembler sets it (the ztrace command links capstone): the
+/// library has none of its own.
+disasm: ?*const fn (code: []const u8, addr: u64, text: []u8) ?Insn = null,
+/// The instructions from the pc on whose text a stop records.
+disasm_ahead: usize = 8,
+/// 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
+/// default: not at all (the commands ask for 3).
+telescope: u8 = 0,
+telescope_words: usize = 16,
+/// The addresses already named, and the instructions already recorded
+/// with the length of each.
+described: std.AutoHashMapUnmanaged(u64, void) = .empty,
+code_len: std.AutoHashMapUnmanaged(u64, u8) = .empty,
/// The static probes armed (`usdt`): what each one's hook reads.
sites: std.ArrayList(*Site) = .empty,
/// The threads that have stopped before: a first stop is a thread's birth.
@@ -99,6 +115,8 @@ pub const Hook = struct {
pub const Action = enum { go, stop };
pub const Breakpoint = struct { id: u32, addr: u64, saved: u8, hook: ?Hook };
+/// An instruction decoded: its length and its text ("mov rbp, rsp").
+pub const Insn = struct { len: u8, text: []const u8 };
pub const Watch = struct { addr: u64, len: usize };
const Page = struct {
@@ -140,6 +158,8 @@ pub fn deinit(d: *Debugger) void {
d.breakpoints.deinit(d.gpa);
for (d.sites.items) |site| d.gpa.destroy(site);
d.sites.deinit(d.gpa);
+ d.described.deinit(d.gpa);
+ d.code_len.deinit(d.gpa);
var pages = d.shadow.valueIterator();
while (pages.next()) |page| d.gpa.destroy(page.*);
d.shadow.deinit(d.gpa);
@@ -368,11 +388,74 @@ fn report(d: *Debugger, stop: Stop) !Stop {
try d.emitLine();
if (d.stack_watch > 0) try d.snapshot(d.regs.rsp, d.stack_watch);
for (d.watches.items) |w| try d.snapshot(w.addr, w.len);
+ try d.emitCode();
+ try d.emitTelescope();
}
try d.emitStop(stop);
return stop;
}
+/// The instructions at the pc and after it, each a string (`event.codeId`)
+/// recorded once: what a viewer shows as the disassembly.
+fn emitCode(d: *Debugger) !void {
+ const insnAt = d.disasm orelse return;
+ var code: [256]u8 = undefined;
+ // 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;
+ }
+ 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.
+ var text: [192]u8 = undefined;
+ const insn = insnAt(code[at..n.?], addr, text[1..]) 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;
+ }
+}
+
+/// What a viewer follows from the registers and the top of the stack, as
+/// pwndbg's telescope does: recorded here, while the memory is there to read.
+fn emitTelescope(d: *Debugger) !void {
+ if (d.telescope == 0) return;
+ const r = &d.regs;
+ for ([_]u64{ r.rip, r.rax, r.rbx, r.rcx, r.rdx, r.rsi, r.rdi, r.rbp, r.rsp, r.r8, r.r9, r.r10, r.r11, r.r12, r.r13, r.r14, r.r15 }) |value| try d.chase(value);
+ var words: [64]u64 = undefined;
+ const n = d.read(r.rsp, std.mem.sliceAsBytes(words[0..@min(words.len, d.telescope_words)])) / 8;
+ for (words[0..n]) |value| try d.chase(value);
+}
+
+/// Follows `value` as a pointer: a pointer into code gets its name and
+/// ends the chain, any other readable target is recorded (32 bytes: a word
+/// to follow, and enough to tell a string) and followed.
+fn chase(d: *Debugger, value: u64) !void {
+ var v = value;
+ for (0..d.telescope) |_| {
+ const m = d.syms.mappingAt(v) orelse return;
+ if (m.prot & 1 == 0) return;
+ if (m.prot & 4 != 0) return d.nameAddress(v);
+ const bytes = try d.snap(v, 32);
+ const off: usize = @intCast(v & 7);
+ if (bytes.len < off + 8) return;
+ v = std.mem.readInt(u64, bytes[off..][0..8], .little);
+ }
+}
+
+/// The string whose id is `addr`: its name ("main+4"), recorded once.
+fn nameAddress(d: *Debugger, addr: u64) !void {
+ if ((try d.described.getOrPut(d.gpa, addr)).found_existing) return;
+ var buf: [256]u8 = undefined;
+ var w: std.Io.Writer = .fixed(&buf);
+ if (d.syms.label(&w, addr) catch false) try d.emit(.{ .kind = .string, .a = .{ addr, 0, 0, 0, 0, 0 } }, w.buffered());
+}
+
fn emitStop(d: *Debugger, stop: Stop) !void {
const r = &d.regs;
const regs = std.mem.asBytes(r);
@@ -488,6 +571,12 @@ pub fn watch(d: *Debugger, addr: u64, len: usize) !void {
/// target, then only the words (8 bytes, aligned) the sink was not sent
/// before or that changed since.
pub fn snapshot(d: *Debugger, addr: u64, len: usize) !void {
+ _ = try d.snap(addr, len);
+}
+
+/// `snapshot`, returning what was read: from `addr` rounded down to a word,
+/// valid until the next read.
+fn snap(d: *Debugger, addr: u64, len: usize) ![]const u8 {
const start = addr & ~@as(u64, 7);
try d.scratch.resize(d.gpa, std.mem.alignForward(usize, len + (addr - start), 8));
const bytes = d.scratch.items[0 .. d.read(start, d.scratch.items) & ~@as(usize, 7)];
@@ -521,6 +610,7 @@ pub fn snapshot(d: *Debugger, addr: u64, len: usize) !void {
}
}
if (run_from) |from| try d.emitWrite(start + from, bytes[from..]);
+ return bytes;
}
fn emitWrite(d: *Debugger, addr: u64, bytes: []const u8) !void {