diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 11:58:00 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 11:58:53 -0300 |
| commit | 8beab959b633e6641d07aba053a55e6e8e61eb00 (patch) | |
| tree | 0e5a91c0a00e121fe4094a333438ee90ccc5fdda /targets/graph.zig | |
| parent | 3fdb67c479dcc1ad0f1e51832bc086622692249c (diff) | |
| download | computeranim-8beab959b633e6641d07aba053a55e6e8e61eb00.tar.gz computeranim-8beab959b633e6641d07aba053a55e6e8e61eb00.zip | |
targets, test-debugger, bench-debugger
A linked list, a graph search in Zig, the same C under glibc and musl, two
threads. The tests drive the debugger over them: breakpoint counts, the
lines a step walks (as gdb), a list snapshotted by a hook, a watch's
words, the syscalls that tell glibc from musl, threads, attach, and
scripted dbg and record sessions. The bench measures steps, stops and
memory access from bare ptrace up to a recording.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'targets/graph.zig')
| -rw-r--r-- | targets/graph.zig | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/targets/graph.zig b/targets/graph.zig new file mode 100644 index 0000000..987eaa5 --- /dev/null +++ b/targets/graph.zig @@ -0,0 +1,41 @@ +//! A small graph and a breadth-first search over it. `dist` and `queue` are +//! exported globals, so a tracer finds them by name and watches them fill. +//! Exits with the sum of the distances from node 0 (18). +const n = 8; +const unreached = 0xff; + +const edges = [_][2]u8{ .{ 0, 1 }, .{ 0, 2 }, .{ 1, 3 }, .{ 2, 3 }, .{ 3, 4 }, .{ 4, 5 }, .{ 5, 6 }, .{ 2, 7 } }; + +/// Aligned so that it is one machine word to a watcher. +export var dist: [n]u8 align(8) = @splat(unreached); +export var queue: [n]u8 = @splat(0); + +fn linked(a: u8, b: u8) bool { + for (edges) |e| { + if ((e[0] == a and e[1] == b) or (e[0] == b and e[1] == a)) return true; + } + return false; +} + +fn bfs(start: u8) void { + var head: usize = 0; + var tail: usize = 1; + queue[0] = start; + dist[start] = 0; + while (head < tail) : (head += 1) { + const at = queue[head]; + for (0..n) |next| { + if (dist[next] != unreached or !linked(at, @intCast(next))) continue; + dist[next] = dist[at] + 1; + queue[tail] = @intCast(next); + tail += 1; + } + } +} + +pub fn main() u8 { + bfs(0); + var sum: u8 = 0; + for (dist) |d| sum += d; + return sum; +} |
