//! canim on its real recordings (made by the build, under ztrace's //! debugger): finding moments, what a step changed, calls, pointers //! followed, and the cursor's timetable for a step. const std = @import("std"); const canim = @import("canim"); const anim = @import("anim"); const recordings = @import("recordings"); const Recording = canim.Recording; const expect = std.testing.expect; const expectEqual = std.testing.expectEqual; test "steps by line stop on lines of the program's own source, and no step stands still" { inline for (.{ recordings.steps, recordings.hello, recordings.sort, recordings.list }) |program| { const rec = canim.open(program); // From the first call it shows: main, or the one a recording begins in. var at = rec.calls[0].enter; var stops: usize = 0; while (at < rec.end() and stops < 400) : (stops += 1) { const next = rec.next(.line, at); try expect(next > at); if (next < rec.end()) try expect(rec.reached[next]); at = next; } try expect(stops > 4); } } test "calls are a tree: fib(5) makes fifteen, each with what it was given and gave back" { const rec = canim.open(recordings.steps); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const nodes = try canim.calls.layout(arena.allocator(), rec, "fib"); try expectEqual(@as(usize, 15), nodes.len); try expectEqual(@as(?u64, 5), rec.call("fib", 0).arg); try expectEqual(@as(u64, 5), rec.call("fib", 0).result); try expectEqual(@as(?u64, 1), rec.call("fib", 4).arg); // A node is where it will always be: a parent over the middle of its children. for (nodes) |n| if (n.parent) |p| try expect(nodes[p].level + 1 == n.level); } test "a program recorded by instruction, its variables told: calls, locals, and what they reach" { const rec = canim.open(recordings.list); var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const reverse = rec.call("reverse", 0); try expectEqual(@as(usize, 3), rec.allocations.len); // The recording begins in it (`--from reverse`): the call is open from the first stop. try expect(rec.own[reverse.enter] == 17 or rec.own[reverse.enter] == 18); // A local is not its value until its call has stored to it. const first = rec.lineMoment(18, reverse.enter); const cur = rec.variable(arena.allocator(), rec.moment(first), "cur").?; try expect(!rec.assigned(cur, first)); try expect(rec.assigned(cur, rec.lineMoment(21, reverse.enter))); // Three nodes are reached from the program's pointers, each in the place its allocation gives it. const things = canim.objects.reach(arena.allocator(), rec, rec.moment(rec.lineMoment(21, reverse.enter)), null); try expectEqual(@as(usize, 3), things.len); for (things) |o| try expect(o.slot.? < 3); } test "the program's four system calls are found, with what each was given" { const rec = canim.open(recordings.hello); var names: [4][]const u8 = undefined; var n: usize = 0; for (rec.syscalls) |call| { if (call.enter < rec.call("main", 0).enter or n == names.len) continue; names[n] = @import("ztrace").syscalls.name(call.nr) orelse "?"; n += 1; } try expectEqual(@as(usize, 4), n); // glibc's `open` makes `openat`. try std.testing.expectEqualStrings("openat", names[0]); try std.testing.expectEqualStrings("read", names[1]); try std.testing.expectEqualStrings("close", names[2]); try std.testing.expectEqualStrings("write", names[3]); } test "a walked step is as long as what it changed: fire, a slot a level, the hold, the advance, the rest" { const rec = canim.open(recordings.steps); const scene = struct { var at: canim.Cursor = undefined; fn frame(s: *anim.Frame) void { const r = canim.open(recordings.steps); at = canim.cursor(s, r, r.lineMoment(10, r.call("main", 0).enter)); at.levels = .{ .idea = .calls }; } }; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); var frame: anim.Frame = .{ .t = 0, .clock = 0, .in = undefined, .pointer = .{ 0, 0 }, .arena = arena.allocator() }; scene.frame(&frame); const t = canim.theme.time; // main's line calls fib(5): its two registers (rdi, rsp) 6 f apart, // the stack 0.30 s after the second, the call's node when the // projection from its way back arrives. const from = rec.lineMoment(10, rec.call("main", 0).enter); const call = scene.at.reading(from, rec.next(.line, from)); const second: f32 = 6.0 / 60.0; try std.testing.expectApproxEqAbs(@as(f32, 2 * t.cascade + second), call.at[1], 1e-4); try std.testing.expectApproxEqAbs(@as(f32, 2 * t.cascade + second + t.project.bracket + t.project.far), call.at[2], 1e-4); try expect(call.total > 2.0 and call.total < 3.5); // A line that changes nothing shown: fire, advance, rest. var quiet = scene.at; quiet.levels = .{ .cpu = false, .memory = false }; const line = quiet.reading(rec.lineMoment(4, from), rec.lineMoment(6, from)); try std.testing.expectApproxEqAbs(t.fire + t.step + t.rest + t.step_per_row, line.total, 1e-3); } /// The first moment whose pc is in the function named `name` (as the recording names it: `libc.so.6!open`). fn firstIn(rec: *const Recording, name: []const u8) u64 { for (rec.registers, 0..) |regs, i| { const at = rec.strings.get(regs.rip) orelse continue; if (std.mem.startsWith(u8, at, name) and at.len > name.len and at[name.len] == '+') return i; } unreachable; } test "sort's graph: two loops in their lanes, the hot path straight down, the jle never taken" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const graph = canim.graph; const rec = canim.open(recordings.sort); const at = firstIn(rec, "sort"); var g = graph.read(arena.allocator(), rec, rec.end(), rec.registers[at].rip).?; try expect(g.exact and g.text); try expectEqual(@as(usize, 11), g.nodes.len); for (g.nodes) |n| try expect(n.reached); // The inner loop's `j > 0` is never false: its jle never taken. const jle = g.edgeOf(g.nodeOf(g.start + 0x8a).?, g.nodeOf(g.start + 0xbc).?).?; try expect(g.edges[jle].kind == .taken and g.edges[jle].total == 0); // The compare's jne to the swap, ten times; the swap entered as often. const swap = g.nodeOf(g.start + 0xc5).?; try expectEqual(@as(u32, 10), g.edges[g.edgeOf(g.nodeOf(g.start + 0xbc).?, swap).?].count); try expectEqual(@as(u32, 10), g.nodes[swap].visits); graph.sizes(rec, rec.end(), &g, .map); graph.place(&g, graph.metricOf(.map)); var backs: usize = 0; for (g.edges) |e| backs += @intFromBool(e.back); try expectEqual(@as(usize, 2), backs); // The outer loop (i++ back to the test) outermost, the inner (the swap back to `j > 0`) inside it. const outer = g.edges[g.edgeOf(g.nodeOf(g.start + 0xff).?, g.nodeOf(g.start + 0x78).?).?]; const inner = g.edges[g.edgeOf(swap, g.nodeOf(g.start + 0x8a).?).?]; try expect(outer.back and inner.back and outer.lane == 0 and inner.lane == 1); // The swap straight under the compare that goes to it ten times in ten; the way on beside it. try expectEqual(g.nodes[g.nodeOf(g.start + 0xbc).?].col, g.nodes[swap].col); try expect(g.nodes[g.nodeOf(g.start + 0xc3).?].col > g.nodes[swap].col); // The jle and the loop's exit would cross blocks: the right channel. try expectEqual(@as(u8, 6), g.edges[jle].n); try expectEqual(g.channel, g.edges[jle].points[2][0]); // On the map (1688 high): too tall; the long prologue folds, and the outer loop is panned into view whole. var buf: [1 << 20]u8 = undefined; var fba = std.heap.FixedBufferAllocator.init(&buf); const room = 1688 - 24; const in_loop = rec.lineMoment(12, at); const laid = graph.layout(fba.allocator(), rec, in_loop, rec.registers[in_loop].rip, .map, room).?; try expectEqual(@as(u32, 9), laid.nodes[0].shown); const off = graph.pan(laid, room, graph.metricOf(.map)); for (laid.nodes[1 .. laid.nodes.len - 1]) |n| try expect(n.y >= off and n.y + n.h <= off + room); } test "reverse's graph: one loop, its exit in the right channel" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const graph = canim.graph; const rec = canim.open(recordings.list); const at = firstIn(rec, "reverse"); var g = graph.read(arena.allocator(), rec, rec.end(), rec.registers[at].rip).?; try expectEqual(@as(usize, 4), g.nodes.len); graph.sizes(rec, rec.end(), &g, .map); graph.place(&g, graph.metricOf(.map)); const layers = [_]u32{ 0, 1, 2, 3 }; for (g.nodes, layers) |n, l| try expect(n.layer == l and n.col == 0); // The body's jmp back to the test: the one edge that climbs, three turns. const turn = g.edges[g.edgeOf(2, 1).?]; try expect(turn.back and turn.count == 3); try expectEqual(@as(u8, 6), g.edges[g.edgeOf(1, 3).?].n); // The recording begins inside it: its first block was entered once. try expectEqual(@as(u32, 1), g.nodes[0].visits); try expect(g.h <= 1688 - 24); } test "libc's open at its syscall: the blocks never reached folded, no loop made by folding" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); const graph = canim.graph; const rec = canim.open(recordings.hello); var at = firstIn(rec, "libc.so.6!open"); var buf: [256]u8 = undefined; while (!std.mem.eql(u8, rec.instruction(at, rec.registers[at].rip, &buf).?.text, "syscall")) at += 1; const g = graph.layout(arena.allocator(), rec, at, rec.registers[at].rip, .map, 1688 - 24).?; // Twenty blocks (two of `nop` padding left out), eight of them reached. try expectEqual(@as(usize, 20), g.blocks); var reached: usize = 0; var folds: usize = 0; for (g.nodes) |n| { reached += @intFromBool(n.reached); folds += @intFromBool(n.folded > 0); } try expectEqual(@as(usize, 8), reached); try expectEqual(@as(usize, 12), g.foldedBlocks()); try expect(folds >= 4 and folds < 12); // open has no loop, and the folds make none. for (g.edges) |e| try expect(!e.back); // At its syscall: the blocks before entered once, those after not yet. try expectEqual(g.nodeOf(rec.registers[at].rip).?, g.here.?); for (g.nodes) |n| if (n.reached) try expectEqual(@as(u32, @intFromBool(n.start <= rec.registers[at].rip)), n.visits); // Still taller than the map: it pans, the pc's block in view; as wide as two columns at most. const room = 1688 - 24; const off = graph.pan(g, room, graph.metricOf(.map)); const here = g.nodes[g.here.?]; try expect(here.y >= off and here.y + here.h <= off + room and g.w <= 1792); }