diff options
Diffstat (limited to 'src/trace/telescope.zig')
| -rw-r--r-- | src/trace/telescope.zig | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig index 6ab2929..006bb22 100644 --- a/src/trace/telescope.zig +++ b/src/trace/telescope.zig @@ -293,6 +293,72 @@ pub fn function(m: Moment, pc: u64) ?Function { return .{ .start = start, .len = len, .base = t[4], .rows = t[7..][0..rows], .locals = t[7 + rows ..] }; } +/// A basic block of a function: where it is, how it ends +/// (`Debugger.End`), and its edges. +pub const Block = struct { + start: u64, + len: u64, + end: Debugger.End, + /// Where it goes: a jump's target, a conditional jump's when taken, a + /// tail call's or a call's that does not return; null for none, or + /// where it is not known (`indirect`). + to: ?u64, + /// The block it falls into, after it: `fall`'s, and a conditional + /// jump's when not taken; null otherwise. + falls: ?u64, +}; + +/// A function as its blocks, from the recording (`Debugger.emitBlocks`): +/// its bounds, its blocks in address order (`blocks`, as many as `out` +/// holds of `count`), the one that holds the pc, and whether every +/// instruction's text is recorded (`instruction`; false past +/// `Debugger.function_text`, the blocks recorded without it). +pub const Graph = struct { start: u64, len: u64, blocks: []Block, count: usize, here: ?usize, text: bool }; + +/// The blocks of the function that holds `pc`; null where the recording +/// has none (no stop was ever in it). +pub fn blocks(m: Moment, pc: u64, out: []Block) ?Graph { + const found = m.trace.stringBelow(event.blocksId(pc)) orelse return null; + if (found.id < event.blocksId(0)) return null; + const start = found.id - event.blocksId(0); + const t = found.text; + if (t.len < 5) return null; + const len = std.mem.readInt(u32, t[0..4], .little); + if (pc - start >= len) return null; + const size = @sizeOf(Debugger.Block); + const count = (t.len - 5) / size; + var g: Graph = .{ .start = start, .len = len, .blocks = out[0..@min(out.len, count)], .count = count, .here = null, .text = t[4] != 0 }; + for (0..count) |i| { + const b = std.mem.bytesToValue(Debugger.Block, t[5 + i * size ..][0..size]); + const at = start + b.offset; + if (pc -% at < b.len) g.here = i; + if (i >= out.len) continue; + const to: ?u64 = switch (b.end) { + .jump, .branch, .tail, .noreturn => if (b.to == Debugger.no_target) null else start +% @as(u64, @bitCast(@as(i64, b.to))), + else => null, + }; + out[i] = .{ .start = at, .len = b.len, .end = b.end, .to = to, .falls = if (b.end == .fall or b.end == .branch) at + b.len else null }; + } + return g; +} + +/// ASan's shadow byte of `addr` at a moment (`ztrace record --asan`): where +/// it is, and what it holds where the recording has it: 0 all of its 8 +/// bytes addressable, 1 to 7 that many first ones, else why none is (f1 a +/// stack frame's left redzone, f2 between its variables, f3 its right, f5 +/// a frame returned from, f8 a scope left, fa a heap block's redzone, fd +/// freed). Null for a recording without the shadow. +pub fn shadow(m: Moment, addr: u64) !?struct { addr: u64, value: ?u8 } { + const text = m.trace.strings.get(Debugger.asan_id) orelse return null; + if (text.len != @sizeOf(Debugger.Asan)) return null; + const asan = std.mem.bytesToValue(Debugger.Asan, text[0..@sizeOf(Debugger.Asan)]); + const at = (addr >> @intCast(asan.scale)) + asan.offset; + var byte: [1]u8 = undefined; + var known: [1]bool = undefined; + try m.read(at, &byte, &known); + return .{ .addr = at, .value = if (known[0]) byte[0] else null }; +} + /// A GOT slot at a moment: what it holds, and whether that is still the way /// back into the PLT (`lazy`: the next call through it runs the loader's /// resolver, which binds it) rather than the function. Null where the @@ -533,3 +599,51 @@ test "frames walked by their call frame information; each stack word's role" { var two: [2]u64 = undefined; try std.testing.expectEqual(@as(u64, 0x400050), (try backtrace(m, &entry, &two))[1]); } + +test "a function's blocks: their edges, the one that holds the pc, and whether their text is recorded" { + 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 function at 400000, 0x40 long, past the text budget (its byte 0): a + // test that branches over a loop, the loop, a call through a register + // that never returns, and a tail call. + const B = Debugger.Block; + const blocks_ = [_]B{ + .{ .offset = 0, .len = 8, .to = 0x20, .end = .branch }, + .{ .offset = 8, .len = 0x10, .to = 8, .end = .branch }, + .{ .offset = 0x18, .len = 8, .to = Debugger.no_target, .end = .noreturn }, + .{ .offset = 0x20, .len = 0x20, .to = 0x1000, .end = .tail }, + }; + try rec.emit(.{ .kind = .string, .a = .{ event.blocksId(0x400000), 0, 0, 0, 0, 0 } }, "\x40\x00\x00\x00\x00" ++ std.mem.asBytes(&blocks_)); + 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 out: [3]Block = undefined; + const g = blocks(m, 0x400024, &out).?; + try std.testing.expectEqual(@as(u64, 0x400000), g.start); + try std.testing.expectEqual(@as(u64, 0x40), g.len); + try std.testing.expect(!g.text); + // Four blocks, three of them given; the pc in the last. + try std.testing.expectEqual(@as(usize, 4), g.count); + try std.testing.expectEqual(@as(usize, 3), g.blocks.len); + try std.testing.expectEqual(@as(?usize, 3), g.here); + try std.testing.expectEqualSlices(Block, &.{ + .{ .start = 0x400000, .len = 8, .end = .branch, .to = 0x400020, .falls = 0x400008 }, + .{ .start = 0x400008, .len = 0x10, .end = .branch, .to = 0x400008, .falls = 0x400018 }, + .{ .start = 0x400018, .len = 8, .end = .noreturn, .to = null, .falls = null }, + }, g.blocks); + var all: [4]Block = undefined; + try std.testing.expectEqual(Block{ .start = 0x400020, .len = 0x20, .end = .tail, .to = 0x401000, .falls = null }, blocks(m, 0x400020, &all).?.blocks[3]); + // Outside it, nothing. + try std.testing.expect(blocks(m, 0x400040, &all) == null); + try std.testing.expect(blocks(m, 0x3fffff, &all) == null); +} |
