diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 14:35:56 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 14:39:55 -0300 |
| commit | 97ce7f485fc0419df693fdf16d634e9e9a6085fc (patch) | |
| tree | 9d36463512024242665c7836b94a4de4e541b3bf /src/cli | |
| parent | f8740f6996eefc235af066a6686f52847c158a6b (diff) | |
| download | computeranim-97ce7f485fc0419df693fdf16d634e9e9a6085fc.tar.gz computeranim-97ce7f485fc0419df693fdf16d634e9e9a6085fc.zip | |
context: what an instruction is about to touch, and the call chain
At a stop the instruction at the pc is decoded against the registers it
will run with (capstone's operands): a touch event for each memory operand
(address, size, read or write), what is there recorded with it, and what a
write left there read back at the next stop. An instruction-level
recording so has every memory write of the stepped thread, nothing watched.
The frames of the call chain are recorded and their return addresses
named; telescope.backtrace walks them from the recording and ztrace show
prints them. Process.read no longer panics on address 0.
Diffstat (limited to 'src/cli')
| -rw-r--r-- | src/cli/disasm.c | 65 | ||||
| -rw-r--r-- | src/cli/disasm.zig | 10 | ||||
| -rw-r--r-- | src/cli/show.zig | 11 |
3 files changed, 80 insertions, 6 deletions
diff --git a/src/cli/disasm.c b/src/cli/disasm.c index db19059..4144986 100644 --- a/src/cli/disasm.c +++ b/src/cli/disasm.c @@ -4,17 +4,78 @@ static csh handle; +// A memory operand of an instruction, worked out against the registers. +struct zt_touch { + uint64_t addr; + uint8_t size; + // 1 the instruction reads it, 2 it writes it, 3 both. + uint8_t how; +}; + +// Where `reg` is in the kernel's user_regs_struct, as 64-bit words; -1 for +// a register an address is not made of here. +static int slot(x86_reg reg) { + switch (reg) { + case X86_REG_R15: return 0; + case X86_REG_R14: return 1; + case X86_REG_R13: return 2; + case X86_REG_R12: return 3; + case X86_REG_RBP: return 4; + case X86_REG_RBX: return 5; + case X86_REG_R11: return 6; + case X86_REG_R10: return 7; + case X86_REG_R9: return 8; + case X86_REG_R8: return 9; + case X86_REG_RAX: return 10; + case X86_REG_RCX: return 11; + case X86_REG_RDX: return 12; + case X86_REG_RSI: return 13; + case X86_REG_RDI: return 14; + case X86_REG_RSP: return 19; + default: return -1; + } +} + // The instruction at `code`, which is at `addr`: its text into `text`, its -// length returned. 0 where there is no instruction. -size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap) { +// 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`. +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) { if (!handle) { if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return 0; cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL); + cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON); } + size_t room = count ? *count : 0; + if (count) *count = 0; cs_insn *insn; 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); + // lea and the long nops name memory and touch none. + if (regs && insn->detail && insn->id != X86_INS_LEA && insn->id != X86_INS_NOP) { + const cs_x86 *x = &insn->detail->x86; + for (int i = 0; i < x->op_count && *count < room; i++) { + const cs_x86_op *op = &x->operands[i]; + if (op->type != X86_OP_MEM || !(op->access & (CS_AC_READ | CS_AC_WRITE))) continue; + uint64_t at = (uint64_t)op->mem.disp; + if (op->mem.base == X86_REG_RIP) at += addr + size; + else if (op->mem.base != X86_REG_INVALID) { + if (slot(op->mem.base) < 0) continue; + at += regs[slot(op->mem.base)]; + } + if (op->mem.index != X86_REG_INVALID) { + if (slot(op->mem.index) < 0) continue; + at += regs[slot(op->mem.index)] * (uint64_t)op->mem.scale; + } + if (op->mem.segment == X86_REG_FS) at += regs[21]; + if (op->mem.segment == X86_REG_GS) at += regs[22]; + touches[*count].addr = at; + touches[*count].size = op->size; + touches[*count].how = (op->access & CS_AC_READ ? 1 : 0) | (op->access & CS_AC_WRITE ? 2 : 0); + ++*count; + } + } cs_free(insn, 1); return size; } diff --git a/src/cli/disasm.zig b/src/cli/disasm.zig index 5c0d27b..40cab9d 100644 --- a/src/cli/disasm.zig +++ b/src/cli/disasm.zig @@ -1,12 +1,14 @@ //! Disassembly for the debugger: capstone, through src/cli/disasm.c. const std = @import("std"); const zt = @import("ztrace"); +const Touch = zt.Debugger.Touch; -extern fn zt_disasm(code: [*]const u8, len: usize, addr: u64, text: [*]u8, cap: usize) usize; +extern fn zt_disasm(code: [*]const u8, len: usize, addr: u64, text: [*]u8, cap: usize, regs: ?*const zt.Regs, touches: [*]Touch, count: ?*usize) usize; /// What `Debugger.disasm` is set to. -pub fn one(code: []const u8, addr: u64, text: []u8) ?zt.Debugger.Insn { - const len = zt_disasm(code.ptr, code.len, addr, text.ptr, text.len); +pub fn one(code: []const u8, addr: u64, text: []u8, regs: ?*const zt.Regs, touches: []Touch) ?zt.Debugger.Insn { + var count = touches.len; + const len = zt_disasm(code.ptr, code.len, addr, text.ptr, text.len, regs, touches.ptr, &count); if (len == 0) return null; - return .{ .len = @intCast(len), .text = std.mem.sliceTo(text, 0) }; + return .{ .len = @intCast(len), .text = std.mem.sliceTo(text, 0), .touches = touches[0..count] }; } diff --git a/src/cli/show.zig b/src/cli/show.zig index 057b210..35cc928 100644 --- a/src/cli/show.zig +++ b/src/cli/show.zig @@ -114,6 +114,17 @@ pub fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io. try w.writeAll("\n"); } } + if (try m.regs(tid)) |regs| { + // The call chain, by frame pointer; the calls the program itself + // reported (instrumentation, hooks) follow it when there are any. + var frames: [32]u64 = undefined; + const chain = try zt.telescope.backtrace(m, ®s, &frames); + if (chain.len > 1) try w.writeAll("\n"); + if (chain.len > 1) for (chain, 0..) |pc, depth| { + var text: [256]u8 = undefined; + try w.print(" #{d} {x} {s}\n", .{ depth, pc, (try m.string(pc, &text)) orelse "" }); + }; + } for (try m.stack(arena, tid), 0..) |frame, depth| { try w.print(" #{d} {s} from {x} sp {x}\n", .{ depth, try name(m, arena, frame.function), frame.site, frame.sp }); } |
