diff options
Diffstat (limited to 'src/cli')
| -rw-r--r-- | src/cli/dbg.zig | 1 | ||||
| -rw-r--r-- | src/cli/disasm.c | 9 | ||||
| -rw-r--r-- | src/cli/perf.zig | 2 | ||||
| -rw-r--r-- | src/cli/probe.zig | 2 | ||||
| -rw-r--r-- | src/cli/record.zig | 1 | ||||
| -rw-r--r-- | src/cli/run.zig | 3 | ||||
| -rw-r--r-- | src/cli/util.zig | 16 |
7 files changed, 27 insertions, 7 deletions
diff --git a/src/cli/dbg.zig b/src/cli/dbg.zig index 22923be..5fcf88e 100644 --- a/src/cli/dbg.zig +++ b/src/cli/dbg.zig @@ -87,6 +87,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { defer s.d.deinit(); if (produced) |p| s.d.ring = p.ring; s.d.disasm = disasm.one; + s.d.syms.demangle = util.demangle; s.d.telescope = 3; defer s.stop_payload.deinit(init.gpa); s.d.stack_watch = stack_bytes; diff --git a/src/cli/disasm.c b/src/cli/disasm.c index 82fd0bb..9b15e0c 100644 --- a/src/cli/disasm.c +++ b/src/cli/disasm.c @@ -76,7 +76,8 @@ static int slot(x86_reg reg) { // The instruction at `code`, which is at `addr`: its text into `text`, its // length returned; 0 where there is no instruction. With `regs` (the // thread's, about to run it), the memory it touches into `touches`, at -// most `*count` of them, and how many into `*count`. +// most `*count` of them, and how many into `*count`. A branch is told +// without them too, its target where the instruction alone says it. size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap, const uint64_t *regs, struct zt_touch *touches, size_t *count, struct zt_branch *branch) { if (!handle) { if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return 0; @@ -89,17 +90,17 @@ size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, siz if (cs_disasm(handle, code, len, addr, 1, &insn) != 1) return 0; size_t size = insn->size; snprintf(text, cap, insn->op_str[0] ? "%s %s" : "%s", insn->mnemonic, insn->op_str); - if (regs && branch && insn->detail) { + if (branch && insn->detail) { const cs_x86 *x = &insn->detail->x86; branch->kind = cs_insn_group(handle, insn, CS_GRP_RET) ? 3 : cs_insn_group(handle, insn, CS_GRP_CALL) ? 1 : cs_insn_group(handle, insn, CS_GRP_JUMP) ? 2 : 0; - branch->taken = holds(insn->id, regs[18], regs[11]); + branch->taken = regs ? holds(insn->id, regs[18], regs[11]) : 1; branch->indirect = 0; branch->target = 0; if (branch->kind == 1 || branch->kind == 2) { const cs_x86_op *op = &x->operands[0]; if (x->op_count < 1) branch->kind = 0; else if (op->type == X86_OP_IMM) branch->target = (uint64_t)op->imm; - else if (op->type == X86_OP_REG && slot(op->reg) >= 0) branch->target = regs[slot(op->reg)]; + else if (op->type == X86_OP_REG && slot(op->reg) >= 0) branch->target = regs ? regs[slot(op->reg)] : 0; else if (op->type == X86_OP_MEM) { // Its address is the first touch below: the target is what is there. branch->indirect = 1; diff --git a/src/cli/perf.zig b/src/cli/perf.zig index 48523ce..f43c51f 100644 --- a/src/cli/perf.zig +++ b/src/cli/perf.zig @@ -142,7 +142,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { // Symbols: the program's own from the start, a library's once the // loader has mapped it, which is at the program's entry point. - var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io }; + var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io, .demangle = util.demangle }; defer symbols.deinit(); symbols.free(try symbols.refresh(proc.pid)); var at_entry = false; diff --git a/src/cli/probe.zig b/src/cli/probe.zig index 311f2c0..abe67c1 100644 --- a/src/cli/probe.zig +++ b/src/cli/probe.zig @@ -126,7 +126,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { defer tracer.close(); if (syscalls) tracer.syscalls() catch |err| refused(err, &tracer); if (sched) tracer.sched(btf) catch |err| refused(err, &tracer); - var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io }; + var symbols: zt.Symbols = .{ .gpa = init.gpa, .io = init.io, .demangle = util.demangle }; defer symbols.deinit(); symbols.free(try symbols.refresh(proc.pid)); for (places.items) |p| { diff --git a/src/cli/record.zig b/src/cli/record.zig index f5281ca..a82fa9e 100644 --- a/src/cli/record.zig +++ b/src/cli/record.zig @@ -108,6 +108,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { defer d.deinit(); if (produced) |p| d.ring = p.ring; d.disasm = disasm.one; + d.syms.demangle = util.demangle; d.telescope = telescope; d.syscalls = if (by == .syscall) .stop else .record; d.stack_watch = stack_bytes; diff --git a/src/cli/run.zig b/src/cli/run.zig index 34455dd..00c18d1 100644 --- a/src/cli/run.zig +++ b/src/cli/run.zig @@ -119,7 +119,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { const start = zt.event.now(); var full = false; - while (session.pump(stops.sink(), 10 * std.time.ns_per_ms) catch |err| switch (err) { + // An idle wait is how late the target's exit is noticed: a millisecond. + while (session.pump(stops.sink(), std.time.ns_per_ms) catch |err| switch (err) { error.RecordingFull => full: { full = true; break :full false; diff --git a/src/cli/util.zig b/src/cli/util.zig index 1d7ee4c..652956d 100644 --- a/src/cli/util.zig +++ b/src/cli/util.zig @@ -62,6 +62,22 @@ pub const Producer = struct { } }; +extern fn __cxa_demangle(name: [*:0]const u8, out: ?[*]u8, len: ?*usize, status: *c_int) ?[*:0]u8; + +/// What `Symbols.demangle` is set to: the C++ runtime's demangler. +pub fn demangle(name: []const u8, buf: []u8) ?[]const u8 { + if (!std.mem.startsWith(u8, name, "_Z") or name.len >= buf.len) return null; + @memcpy(buf[0..name.len], name); + buf[name.len] = 0; + var status: c_int = 0; + const out = __cxa_demangle(buf[0..name.len :0], null, null, &status) orelse return null; + defer std.c.free(out); + const text = std.mem.span(out); + if (text.len > buf.len) return null; + @memcpy(buf[0..text.len], text); + return buf[0..text.len]; +} + /// The arguments after a command's own, as the target's argv. pub fn targetArgv(arena: std.mem.Allocator, args: []const [:0]const u8) ![:null]?[*:0]const u8 { const argv = try arena.allocSentinel(?[*:0]const u8, args.len, null); |
