summaryrefslogtreecommitdiff
path: root/src/trace/telescope.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace/telescope.zig')
-rw-r--r--src/trace/telescope.zig100
1 files changed, 84 insertions, 16 deletions
diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig
index ba7caa3..7e3e3e0 100644
--- a/src/trace/telescope.zig
+++ b/src/trace/telescope.zig
@@ -6,6 +6,7 @@ const std = @import("std");
const event = @import("event.zig");
const store = @import("store.zig");
const Moment = store.Moment;
+const Debugger = @import("Debugger.zig");
/// The kind of memory an address is in, from its mapping.
pub const Region = enum {
@@ -91,31 +92,50 @@ pub fn chase(m: Moment, value: u64, links: []Link, text: []u8) ![]Link {
/// 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`), and its text. Null
-/// where none was recorded.
-pub fn instruction(m: Moment, addr: u64, buf: []u8) !?struct { len: u8, back: u8, text: []const u8 } {
+/// 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;
- return .{ .len = s[0], .back = s[1], .text = s[2..] };
+ 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, by frame pointer, from what the
/// recording holds of its stack: the pc, then each return address outwards.
/// It ends where the frames were not recorded.
pub fn backtrace(m: Moment, regs: *const event.Regs, out: []u64) ![]u64 {
- 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;
- }
- };
- // At a function's first instruction its frame is not made yet: the
- // return address is on top of the stack.
+ 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);
+}
+
+/// A moment's memory as the frame walk reads it: all of it recorded, or none.
+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;
+ }
+};
+
+/// 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;
- const at_entry = if (try m.string(regs.rip, &name)) |n| std.mem.endsWith(u8, n, "+0") else false;
- return @import("Debugger.zig").backtrace(Memory{ .m = m }, regs, at_entry, out);
+ 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" {
@@ -187,5 +207,53 @@ test "a chain is followed to a string, to code, to a number, and no further than
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, &regs, &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, &regs, &out))[0..2]);
+}