summaryrefslogtreecommitdiff
path: root/src/cli
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli')
-rw-r--r--src/cli/disasm.c56
-rw-r--r--src/cli/disasm.zig7
-rw-r--r--src/cli/show.zig18
3 files changed, 73 insertions, 8 deletions
diff --git a/src/cli/disasm.c b/src/cli/disasm.c
index 4144986..82fd0bb 100644
--- a/src/cli/disasm.c
+++ b/src/cli/disasm.c
@@ -12,6 +12,43 @@ struct zt_touch {
uint8_t how;
};
+// Where an instruction that is a call, a jump or a return goes.
+struct zt_branch {
+ // The target; with `indirect`, the address the target is read from.
+ uint64_t target;
+ // 0 not a branch, 1 call, 2 jump, 3 return (the target is on the stack).
+ uint8_t kind;
+ // A conditional jump whose condition does not hold is not taken.
+ uint8_t taken;
+ uint8_t indirect;
+};
+
+// Whether the flags (and rcx) make a conditional jump go.
+static int holds(unsigned id, uint64_t flags, uint64_t rcx) {
+ int cf = flags & 1, pf = flags >> 2 & 1, zf = flags >> 6 & 1, sf = flags >> 7 & 1, of = flags >> 11 & 1;
+ switch (id) {
+ case X86_INS_JO: return of;
+ case X86_INS_JNO: return !of;
+ case X86_INS_JB: return cf;
+ case X86_INS_JAE: return !cf;
+ case X86_INS_JE: return zf;
+ case X86_INS_JNE: return !zf;
+ case X86_INS_JBE: return cf || zf;
+ case X86_INS_JA: return !cf && !zf;
+ case X86_INS_JS: return sf;
+ case X86_INS_JNS: return !sf;
+ case X86_INS_JP: return pf;
+ case X86_INS_JNP: return !pf;
+ case X86_INS_JL: return sf != of;
+ case X86_INS_JGE: return sf == of;
+ case X86_INS_JLE: return zf || sf != of;
+ case X86_INS_JG: return !zf && sf == of;
+ case X86_INS_JRCXZ: return rcx == 0;
+ case X86_INS_JECXZ: return (uint32_t)rcx == 0;
+ default: return 1;
+ }
+}
+
// 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) {
@@ -40,7 +77,7 @@ static int slot(x86_reg reg) {
// 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) {
+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;
cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL);
@@ -52,6 +89,23 @@ 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) {
+ 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->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_MEM) {
+ // Its address is the first touch below: the target is what is there.
+ branch->indirect = 1;
+ } else branch->kind = 0;
+ }
+ }
// 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;
diff --git a/src/cli/disasm.zig b/src/cli/disasm.zig
index 40cab9d..e096084 100644
--- a/src/cli/disasm.zig
+++ b/src/cli/disasm.zig
@@ -3,12 +3,13 @@ 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, regs: ?*const zt.Regs, touches: [*]Touch, count: ?*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, branch: ?*zt.Debugger.Branch) usize;
/// What `Debugger.disasm` is set to.
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);
+ var branch: zt.Debugger.Branch = .{};
+ const len = zt_disasm(code.ptr, code.len, addr, text.ptr, text.len, regs, touches.ptr, &count, &branch);
if (len == 0) return null;
- return .{ .len = @intCast(len), .text = std.mem.sliceTo(text, 0), .touches = touches[0..count] };
+ return .{ .len = @intCast(len), .text = std.mem.sliceTo(text, 0), .touches = touches[0..count], .branch = branch };
}
diff --git a/src/cli/show.zig b/src/cli/show.zig
index 35cc928..1657644 100644
--- a/src/cli/show.zig
+++ b/src/cli/show.zig
@@ -93,12 +93,22 @@ pub fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io.
if (comptime std.mem.eql(u8, reg, "eflags")) try w.print("{x}", .{regs.eflags}) else try telescope(m, w, @field(regs, reg), colour);
try w.writeAll("\n");
}
- // The instructions from the pc on, as far as the recording has them.
- var pc = regs.rip;
+ // The instructions round the pc, as far as the recording has them:
+ // eight above (inside a function they are known), the pc's, eight below.
var text: [192]u8 = undefined;
- for (0..8) |i| {
+ var from = regs.rip;
+ for (0..8) |_| {
+ const insn = (try zt.telescope.instruction(m, from, &text)) orelse break;
+ if (insn.back == 0) break;
+ from -= insn.back;
+ }
+ var pc = from;
+ var below: usize = 0;
+ try w.writeAll("\n");
+ while (below <= 8) {
const insn = (try zt.telescope.instruction(m, pc, &text)) orelse break;
- try w.print("{s}{x:0>12} {s}\n", .{ if (i == 0) "\n ► " else " ", pc, insn.text });
+ try w.print("{s}{x:0>12} {s}\n", .{ if (pc == regs.rip) " ► " else " ", pc, insn.text });
+ if (pc >= regs.rip) below += 1;
pc += insn.len;
}
// The top of the stack, a word a row.