//! Following a value through a moment of a recording, as pwndbg's telescope //! does: what it points at, what that points at, until something that is no //! pointer; and for each step the kind of memory it lives in, which is what //! a viewer colours a pointer by. const std = @import("std"); const event = @import("event.zig"); const store = @import("store.zig"); const Moment = store.Moment; const Debugger = @import("Debugger.zig"); const Symbols = @import("Symbols.zig"); /// The kind of memory an address is in, from its mapping. pub const Region = enum { /// Not mapped: the value is a number. none, /// Executable. code, /// Writable and executable: worth a look of its own. rwx, stack, heap, /// Writable: a program's variables, an anonymous mapping. data, /// Readable only: constants. rodata, pub fn of(mapping: store.Mapping, name: []const u8) Region { if (mapping.prot & 4 != 0) return if (mapping.prot & 2 != 0) .rwx else .code; if (std.mem.eql(u8, name, "[stack]")) return .stack; if (std.mem.eql(u8, name, "[heap]")) return .heap; return if (mapping.prot & 2 != 0) .data else .rodata; } }; pub const Link = struct { value: u64, /// Where `value` points; `.none` when it is no pointer. region: Region, /// What the chain ends on, when it ends on something that can be said: /// a code address's name ("main+4"), or the string the pointer leads to. text: []const u8 = "", /// `text` is a string found in memory, not a name. string: bool = false, }; /// The kind of memory `addr` is in at this moment. pub fn region(m: Moment, addr: u64) !Region { var name: []const u8 = ""; var buf: [64]u8 = undefined; const mapping = (try m.mapping(addr, &name, &buf)) orelse return .none; const kind = Region.of(mapping, name); // An allocation outside the heap the kernel names: an arena of mmap's. return if (kind == .data and try m.allocation(addr) != null) .heap else kind; } /// The chain from `value`: itself, what it points at, and on, as far as /// `links` goes. A chain ends at a number, at code (named, when the /// recording named it), at a string (four printable bytes or more, ended /// by a zero), or at memory that was never recorded. `text` holds the /// names and strings the links refer to. pub fn chase(m: Moment, value: u64, links: []Link, text: []u8) ![]Link { var v = value; var n: usize = 0; while (n < links.len) { const link = &links[n]; link.* = .{ .value = v, .region = try region(m, v) }; n += 1; switch (link.region) { .none => break, .code, .rwx => { if (try m.string(v, text)) |name| link.text = name; break; }, else => {}, } var bytes: [32]u8 = undefined; var known: [32]bool = undefined; try m.read(v, &bytes, &known); var printable: usize = 0; while (printable < bytes.len and known[printable] and bytes[printable] >= 0x20 and bytes[printable] < 0x7f) printable += 1; const ended = printable == bytes.len or (known[printable] and bytes[printable] == 0); if (printable >= 4 and ended and printable <= text.len) { @memcpy(text[0..printable], bytes[0..printable]); link.text = text[0..printable]; link.string = true; break; } if (std.mem.indexOfScalar(bool, known[0..8], false) != null) break; v = std.mem.readInt(u64, bytes[0..8], .little); } return links[0..n]; } /// The instruction at `addr` as the recording has it (`event.codeId`): its /// length, the length of the one before it (0 when the recording does not /// know: the one before is then not at `addr - back`), its text, the /// source line it is of, and for a direct call how many parameters the /// callee takes (its DWARF's). Null where none was recorded; `line` and /// `params` null where they are not known, or `buf` cut them off. pub fn instruction(m: Moment, addr: u64, buf: []u8) !?struct { len: u8, back: u8, text: []const u8, line: ?store.Line = null, params: ?u8 = null } { const s = (try m.string(event.codeId(addr), buf)) orelse return null; if (s.len < 2) return null; const text = std.mem.sliceTo(s[2..], 0); // After the text, when there is one: a zero, the line, the parameters + 1. const more = s[2 + text.len ..]; if (more.len < 2 + @sizeOf(store.Line)) return .{ .len = s[0], .back = s[1], .text = text }; const line = std.mem.bytesToValue(store.Line, more[1..][0..@sizeOf(store.Line)]); const params = more[1 + @sizeOf(store.Line)]; return .{ .len = s[0], .back = s[1], .text = text, .line = if (line.line != 0) line else null, .params = if (params != 0) params - 1 else null }; } /// The call chain of a thread at a moment, from what the recording holds of /// its stack: the pc, then each return address outwards. Each frame is /// walked through by its function's call frame information where the /// debugger recorded it (`Debugger.emitFunction`), else by frame pointer /// (`Debugger.backtrace`). It ends where the frames were not recorded. pub fn backtrace(m: Moment, regs: *const event.Regs, out: []u64) ![]u64 { return Debugger.backtrace(Memory{ .m = m }, regs, try atEntry(m, regs), out); } /// `backtrace`, each frame with the stack it holds (`Debugger.Frame`): a /// call's span over the stack words. pub fn frames(m: Moment, regs: *const event.Regs, out: []Debugger.Frame) ![]Debugger.Frame { return Debugger.frames(Memory{ .m = m }, regs, try atEntry(m, regs), out); } /// What a word of the stack is, where the recording says. pub const Role = union(enum) { none, /// A register its frame's function saved for its caller: "rbx", /// "rbp", "r12" to "r15". saved: []const u8, /// Where its frame's function returns to. return_address, /// A local variable or parameter of its frame's function that the debug /// info places on the stack: its name (the recording's, good while it /// is open), its size (0: not known), and where the word is in it: 0 at /// its first byte, 8 a word on, negative where it begins inside the word. local: struct { name: []const u8, size: u32, offset: i32 }, /// The process's first stack (`Debugger.Start`): argc; argv[i], the /// zero that ends them at i = argc; envp[i] likewise, at i = envc; the /// auxiliary vector's i-th word, two an entry (its type, its value), /// the zero entry last. argc, argv: u32, envp: u32, auxv: u32, }; /// A word of the stack: what it is, and the frame of `frames` it lies in /// (0 the newest; null in none: under the stack pointer, or above the /// outermost frame, where the process's first stack is). A leaf's locals /// under the stack pointer (the red zone, at -O0) are its frame's. pub const Slot = struct { role: Role = .none, frame: ?u8 = null }; /// What each word of a thread's stack from `low` (rounded down to a word) /// is at this moment: `out[i]` is the word at `low + 8 i`. The frames are /// `frames`'s; what tells their words is the call frame information (the /// registers each function saved, where; its return address), or the /// frame pointer (rbp saved under the return address); the debug info (a /// local's place at -O0 and -Og, while the frame's pc is in its block: /// none at all where it is in a register or a list of places, nor in a /// block whose code is in pieces, nor in a prologue or an epilogue where /// the frame base is a register the CFA is not yet or no longer kept in); /// and the process's first stack pointer. pub fn slots(m: Moment, regs: *const event.Regs, low: u64, out: []Slot) ![]Slot { @memset(out, .{}); const base = low & ~@as(u64, 7); const end = base + 8 * out.len; const in = struct { fn at(slots_: []Slot, from: u64, addr: u64) ?*Slot { if (addr < from or (addr - from) / 8 >= slots_.len) return null; return &slots_[(addr - from) / 8]; } }.at; var found: [64]Debugger.Unwound = undefined; for (Debugger.unwind(Memory{ .m = m }, regs, try atEntry(m, regs), &found), 0..) |u, k| { const f = u.frame; var a = @max(f.low & ~@as(u64, 7), base); while (a < @min(f.high, end)) : (a += 8) in(out, base, a).?.frame = @intCast(k); const row = u.row orelse { // By frame pointer: rbp saved under the return address; at a // function's entry, the return address alone. if (f.high == f.low + 8) if (in(out, base, f.low)) |s| { s.role = .return_address; }; if (f.high >= f.low + 16) { if (in(out, base, f.high - 16)) |s| s.role = .{ .saved = "rbp" }; if (in(out, base, f.high - 8)) |s| s.role = .return_address; } continue; }; const cfa = f.high; if (function(m, u.function)) |func| { // The frame base is the CFA, or the register the CFA is from. const frame_base: ?u64 = if (func.base == Symbols.base_cfa) cfa else if (func.base == row.cfa_reg) cfa -% @as(u64, @bitCast(@as(i64, row.cfa))) else null; var locals = func.locals; // Where the frame is in its function: a caller is in its call. const here = (if (k == 0) f.pc else f.pc -% 1) -% u.function; if (frame_base) |fb| while (locals.len >= 17) { const offset = std.mem.readInt(i32, locals[0..4], .little); const size = std.mem.readInt(u32, locals[4..8], .little); const from = std.mem.readInt(u32, locals[8..12], .little); const to = std.mem.readInt(u32, locals[12..16], .little); const name = std.mem.sliceTo(locals[16..], 0); locals = locals[@min(locals.len, 16 + name.len + 1)..]; // Blocks share slots: a local is its slot's in its block only. if (here < from or here >= to) continue; const at = fb +% @as(u64, @bitCast(@as(i64, offset))); var w = @max(at & ~@as(u64, 7), base); while (w < @min(at +% @max(size, 1), end)) : (w += 8) { const s = in(out, base, w) orelse continue; // Two small locals in one word: the first named keeps it. if (s.role == .none) s.role = .{ .local = .{ .name = name, .size = size, .offset = @intCast(@as(i64, @bitCast(w -% at))) } }; // A leaf's locals under the stack pointer (the red zone) are its frame's too. if (s.frame == null) s.frame = @intCast(k); } }; } for (row.saved, 0..) |at, i| { if (at == 0 or at == Symbols.lost) continue; const s = in(out, base, cfa +% @as(u64, @bitCast(@as(i64, at) * 8))) orelse continue; s.role = if (i == 6) .return_address else .{ .saved = Symbols.saved_names[i] }; } } const text = m.trace.strings.get(event.frameId(0)) orelse return out; if (text.len != @sizeOf(Debugger.Start)) return out; const start = std.mem.bytesToValue(Debugger.Start, text[0..@sizeOf(Debugger.Start)]); for (out, 0..) |*s, i| { const a = base + 8 * i; if (a < start.sp) continue; // In words from argc: argv, its zero, envp, its zero, the auxiliary vector. const w = (a - start.sp) / 8; const envp = start.argc + 2; const auxv = envp + start.envc + 1; s.role = if (w == 0) .argc else if (w < envp) .{ .argv = @intCast(w - 1) } else if (w < auxv) .{ .envp = @intCast(w - envp) } else if (w < auxv + 2 * (start.auxc + 1)) .{ .auxv = @intCast(w - auxv) } else continue; } return out; } /// A moment's memory as the frame walk reads it: all of it recorded, or /// none; and the call frame information the debugger recorded. const Memory = struct { m: Moment, pub fn read(mem: @This(), addr: u64, buf: []u8) usize { var known: [16]bool = undefined; mem.m.read(addr, buf, known[0..buf.len]) catch return 0; return if (std.mem.indexOfScalar(bool, known[0..buf.len], false) == null) buf.len else 0; } pub fn cfi(mem: @This(), pc: u64) ?Symbols.Cfi { const f = function(mem.m, pc) orelse return null; return .{ .function = f.start, .row = f.rowAt(pc) }; } }; /// A function's frame as the recording has it (`Debugger.emitFunction`): /// its rows and its locals, each end to end. const Function = struct { start: u64, base: u8, rows: []const u8, locals: []const u8, fn rowAt(f: Function, pc: u64) Symbols.Row { const size = @sizeOf(Symbols.Row); // The last row that begins at or before the pc. var lo: usize = 0; var hi = f.rows.len / size; while (lo < hi) { const mid = (lo + hi) / 2; if (std.mem.readInt(u32, f.rows[mid * size ..][0..4], .little) <= pc - f.start) lo = mid + 1 else hi = mid; } return std.mem.bytesToValue(Symbols.Row, f.rows[(lo -| 1) * size ..][0..size]); } }; /// The recorded frame of the function that holds `pc`. fn function(m: Moment, pc: u64) ?Function { const found = m.trace.stringBelow(event.frameId(pc)) orelse return null; // Below every function recorded is another kind of string, or the process's start. if (found.id < event.frameId(1)) return null; const t = found.text; if (t.len < 7) return null; const start = found.id - event.frameId(0); const rows = @as(usize, std.mem.readInt(u16, t[5..7], .little)) * @sizeOf(Symbols.Row); if (pc - start >= std.mem.readInt(u32, t[0..4], .little) or rows == 0 or t.len < 7 + rows) return null; return .{ .start = start, .base = t[4], .rows = t[7..][0..rows], .locals = t[7 + rows ..] }; } /// At a function's first instruction its frame is not made yet: the /// return address is on top of the stack. fn atEntry(m: Moment, regs: *const event.Regs) !bool { var name: [256]u8 = undefined; return if (try m.string(regs.rip, &name)) |n| std.mem.endsWith(u8, n, "+0") else false; } test "a chain is followed to a string, to code, to a number, and no further than is known" { const gpa = std.testing.allocator; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; const path = try std.fs.path.join(gpa, &.{ dir, "t.ztrace" }); defer gpa.free(path); var rec = try store.Recorder.create(gpa, std.testing.io, path); const map = struct { fn at(r: *store.Recorder, addr: u64, len: u64, prot: u64, name: []const u8) !void { try r.emit(.{ .kind = .map, .tid = 1, .a = .{ addr, len, prot, 0, 0, 0 } }, name); } }.at; try map(&rec, 0x400000, 0x1000, 5, "/bin/prog"); try map(&rec, 0x600000, 0x1000, 3, "/bin/prog"); try map(&rec, 0x7ff000, 0x1000, 3, "[stack]"); try map(&rec, 0x900000, 0x1000, 3, ""); try rec.emit(.{ .kind = .alloc, .tid = 1, .a = .{ 0x900010, 32, 0, 0, 0, 0 } }, ""); try rec.emit(.{ .kind = .string, .a = .{ 0x400123, 0, 0, 0, 0, 0 } }, "main+4"); try rec.emit(.{ .kind = .string, .a = .{ event.codeId(0x400123), 0, 0, 0, 0, 0 } }, "\x03\x01mov rbp, rsp"); const word = struct { fn at(r: *store.Recorder, addr: u64, value: u64) !void { try r.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ addr, 8, 0, 0, 0, 0 } }, std.mem.asBytes(&value)); } }.at; // Stack word -> heap -> a string in the data; another -> code; another -> a number. try word(&rec, 0x7ff100, 0x900010); try word(&rec, 0x900010, 0x600020); try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ 0x600020, 6, 0, 0, 0, 0 } }, "hello\x00"); try word(&rec, 0x7ff108, 0x400123); try word(&rec, 0x7ff110, 42); try rec.emit(.{ .kind = .mark, .tid = 1 }, ""); try rec.close(); var trace = try store.Trace.open(gpa, std.testing.io, path); defer trace.close(); const m = try trace.moment(0); var links: [6]Link = undefined; var text: [64]u8 = undefined; const to_string = try chase(m, 0x7ff100, &links, &text); try std.testing.expectEqual(@as(usize, 3), to_string.len); try std.testing.expectEqual(Region.stack, to_string[0].region); try std.testing.expectEqual(Region.heap, to_string[1].region); try std.testing.expectEqual(Region.data, to_string[2].region); try std.testing.expectEqualStrings("hello", to_string[2].text); try std.testing.expect(to_string[2].string); const to_code = try chase(m, 0x7ff108, &links, &text); try std.testing.expectEqual(@as(usize, 2), to_code.len); try std.testing.expectEqual(Region.code, to_code[1].region); try std.testing.expectEqualStrings("main+4", to_code[1].text); const to_number = try chase(m, 0x7ff110, &links, &text); try std.testing.expectEqual(@as(usize, 2), to_number.len); try std.testing.expectEqual(@as(u64, 42), to_number[1].value); try std.testing.expectEqual(Region.none, to_number[1].region); // A pointer to memory never recorded is a pointer still, and ends there. const unknown = try chase(m, 0x7ff800, &links, &text); try std.testing.expectEqual(@as(usize, 1), unknown.len); try std.testing.expectEqual(Region.stack, unknown[0].region); const insn = (try instruction(m, 0x400123, &text)).?; try std.testing.expectEqual(@as(u8, 3), insn.len); try std.testing.expectEqual(@as(u8, 1), insn.back); try std.testing.expectEqualStrings("mov rbp, rsp", insn.text); try std.testing.expect(insn.line == null and insn.params == null); try std.testing.expect(try instruction(m, 0x400126, &text) == null); } test "an instruction's line and its callee's parameters; each frame's extent on the stack" { const gpa = std.testing.allocator; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; const path = try std.fs.path.join(gpa, &.{ dir, "t.ztrace" }); defer gpa.free(path); var rec = try store.Recorder.create(gpa, std.testing.io, path); // A call on line 12 of file 9 to a function of two parameters, as the debugger records it. const line: store.Line = .{ .file = 9, .line = 12, .column = 3 }; try rec.emit(.{ .kind = .string, .a = .{ event.codeId(0x400200), 0, 0, 0, 0, 0 } }, "\x05\x00call 0x400300\x00" ++ std.mem.asBytes(&line) ++ "\x03"); try rec.emit(.{ .kind = .string, .a = .{ 0x400123, 0, 0, 0, 0, 0 } }, "main+4"); try rec.emit(.{ .kind = .string, .a = .{ 0x400300, 0, 0, 0, 0, 0 } }, "f+0"); // rsp 7ff100, rbp 7ff120 -> 7ff140 -> 0: three frames, the last of no known end. const stack = [_]u64{ 0x7ff140, 0x400205, 0, 0, 0x7ff160, 0x400400 }; try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ 0x7ff120, 48, 0, 0, 0, 0 } }, std.mem.sliceAsBytes(&stack)); try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ 0x7ff100, 8, 0, 0, 0, 0 } }, std.mem.asBytes(&@as(u64, 0x400205))); try rec.emit(.{ .kind = .mark, .tid = 1 }, ""); try rec.close(); var trace = try store.Trace.open(gpa, std.testing.io, path); defer trace.close(); const m = try trace.moment(0); var text: [64]u8 = undefined; const call = (try instruction(m, 0x400200, &text)).?; try std.testing.expectEqualStrings("call 0x400300", call.text); try std.testing.expectEqual(line, call.line.?); try std.testing.expectEqual(@as(?u8, 2), call.params); const Frame = Debugger.Frame; var out: [4]Frame = undefined; var regs: event.Regs = .{ .rip = 0x400123, .rsp = 0x7ff100, .rbp = 0x7ff120 }; try std.testing.expectEqualSlices(Frame, &.{ .{ .pc = 0x400123, .low = 0x7ff100, .high = 0x7ff130 }, .{ .pc = 0x400205, .low = 0x7ff130, .high = 0x7ff150 }, .{ .pc = 0x400400, .low = 0x7ff150, .high = 0x7ff150 }, }, try frames(m, ®s, &out)); // At a function's first instruction its frame is its return address. regs.rip = 0x400300; try std.testing.expectEqualSlices(Frame, &.{ .{ .pc = 0x400300, .low = 0x7ff100, .high = 0x7ff108 }, .{ .pc = 0x400205, .low = 0x7ff108, .high = 0x7ff130 }, }, (try frames(m, ®s, &out))[0..2]); } test "frames walked by their call frame information; each stack word's role" { const gpa = std.testing.allocator; var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)]; const path = try std.fs.path.join(gpa, &.{ dir, "t.ztrace" }); defer gpa.free(path); var rec = try store.Recorder.create(gpa, std.testing.io, path); // Two functions as `Debugger.emitFunction` records them. g, at 400100, // keeps no frame and pushes rbx; f, at 400000, keeps one in rbp and has // three locals by it: x, and buf and y in two blocks, sharing a slot. const Row = Symbols.Row; const record = struct { fn at(r: *store.Recorder, start: u64, len: u32, base: u8, rows: []const Row, locals: []const u8) !void { var text: std.Io.Writer.Allocating = .init(std.testing.allocator); defer text.deinit(); try text.writer.writeInt(u32, len, .little); try text.writer.writeByte(base); try text.writer.writeInt(u16, @intCast(rows.len), .little); try text.writer.writeAll(std.mem.sliceAsBytes(rows)); try text.writer.writeAll(locals); try r.emit(.{ .kind = .string, .a = .{ event.frameId(start), 0, 0, 0, 0, 0 } }, text.written()); } }.at; try record(&rec, 0x400100, 0x20, Symbols.no_base, &.{ .{ .offset = 0, .cfa = 8, .cfa_reg = 7, .saved = .{ 0, 0, 0, 0, 0, 0, -1 } }, .{ .offset = 1, .cfa = 16, .cfa_reg = 7, .saved = .{ -2, 0, 0, 0, 0, 0, -1 } }, }, ""); try record(&rec, 0x400000, 0x100, 6, &.{ .{ .offset = 0, .cfa = 8, .cfa_reg = 7, .saved = .{ 0, 0, 0, 0, 0, 0, -1 } }, .{ .offset = 1, .cfa = 16, .cfa_reg = 7, .saved = .{ 0, -2, 0, 0, 0, 0, -1 } }, .{ .offset = 4, .cfa = 16, .cfa_reg = 6, .saved = .{ 0, -2, 0, 0, 0, 0, -1 } }, }, "\xf8\xff\xff\xff\x08\x00\x00\x00" ++ "\x00\x00\x00\x00\xff\xff\xff\xff" ++ "x\x00" ++ // buf is in a block that holds the pc; y shares its slot, in another block. "\xe0\xff\xff\xff\x10\x00\x00\x00" ++ "\x40\x00\x00\x00\x60\x00\x00\x00" ++ "buf\x00" ++ "\xe0\xff\xff\xff\x08\x00\x00\x00" ++ "\x60\x00\x00\x00\x80\x00\x00\x00" ++ "y\x00"); // The process began with its stack at 7ff158: one argument, one variable, one auxiliary entry. const start: Debugger.Start = .{ .sp = 0x7ff158, .argc = 1, .envc = 1, .auxc = 1 }; try rec.emit(.{ .kind = .string, .a = .{ event.frameId(0), 0, 0, 0, 0, 0 } }, std.mem.asBytes(&start)); // g stopped after its push (rsp 7ff100): rbx, then its return into f; // f's frame up to 7ff150 (rbp 7ff140), which returns to code with no frame recorded. var stack: [10]u64 = @splat(0); stack[0] = 0x1234; stack[1] = 0x400050; stack[9] = 0x400999; try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ 0x7ff100, 80, 0, 0, 0, 0 } }, std.mem.sliceAsBytes(&stack)); try rec.emit(.{ .kind = .mark, .tid = 1 }, ""); try rec.close(); var trace = try store.Trace.open(gpa, std.testing.io, path); defer trace.close(); const m = try trace.moment(0); const regs: event.Regs = .{ .rip = 0x400101, .rsp = 0x7ff100, .rbp = 0x7ff140 }; var out: [4]Debugger.Frame = undefined; // By the frame pointer alone g's caller f would be passed over. try std.testing.expectEqualSlices(Debugger.Frame, &.{ .{ .pc = 0x400101, .low = 0x7ff100, .high = 0x7ff110 }, .{ .pc = 0x400050, .low = 0x7ff110, .high = 0x7ff150 }, .{ .pc = 0x400999, .low = 0x7ff150, .high = 0x7ff150 }, }, try frames(m, ®s, &out)); var words: [16]Slot = undefined; const got = try slots(m, ®s, 0x7ff100, &words); const local = struct { fn of(name: []const u8, size: u32, offset: i32) Role { return .{ .local = .{ .name = name, .size = size, .offset = offset } }; } }.of; const want = [16]struct { Role, ?u8 }{ .{ .{ .saved = "rbx" }, 0 }, .{ .return_address, 0 }, .{ .none, 1 }, .{ .none, 1 }, .{ local("buf", 16, 0), 1 }, .{ local("buf", 16, 8), 1 }, .{ .none, 1 }, .{ local("x", 8, 0), 1 }, .{ .{ .saved = "rbp" }, 1 }, .{ .return_address, 1 }, .{ .none, null }, .{ .argc, null }, .{ .{ .argv = 0 }, null }, .{ .{ .argv = 1 }, null }, .{ .{ .envp = 0 }, null }, .{ .{ .envp = 1 }, null }, }; for (want, got) |w, have| { try std.testing.expectEqual(w[1], have.frame); try std.testing.expectEqual(std.meta.activeTag(w[0]), std.meta.activeTag(have.role)); switch (w[0]) { .saved => |name| try std.testing.expectEqualStrings(name, have.role.saved), .local => |l| { try std.testing.expectEqualStrings(l.name, have.role.local.name); try std.testing.expectEqual(l.size, have.role.local.size); try std.testing.expectEqual(l.offset, have.role.local.offset); }, .argv, .envp => try std.testing.expectEqual(w[0], have.role), else => {}, } } // In g's first instruction it has pushed nothing: its return address is on top. const entry: event.Regs = .{ .rip = 0x400100, .rsp = 0x7ff108, .rbp = 0x7ff140 }; var two: [2]u64 = undefined; try std.testing.expectEqual(@as(u64, 0x400050), (try backtrace(m, &entry, &two))[1]); }