From 8beab959b633e6641d07aba053a55e6e8e61eb00 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:58:00 -0300 Subject: 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 --- targets/graph.zig | 41 +++++++++++++++++++++++++++++++++++++++++ targets/libc.c | 19 +++++++++++++++++++ targets/list.c | 42 ++++++++++++++++++++++++++++++++++++++++++ targets/threads.c | 26 ++++++++++++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 targets/graph.zig create mode 100644 targets/libc.c create mode 100644 targets/list.c create mode 100644 targets/threads.c (limited to 'targets') 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; +} diff --git a/targets/libc.c b/targets/libc.c new file mode 100644 index 0000000..2dad81b --- /dev/null +++ b/targets/libc.c @@ -0,0 +1,19 @@ +// The same C under two C libraries (glibc linked dynamically, musl +// statically): what startup, malloc and stdio turn into, seen as syscalls. +#include +#include +#include + +// Volatile, or the compiler proves the allocations away. +static char *volatile small, *volatile big; + +int main(void) { + small = malloc(64); + big = malloc(1 << 20); + strcpy(small, "small"); + memset(big, 'b', 1 << 20); + printf("%s %c\n", small, big[4096]); + free(big); + free(small); + return 0; +} diff --git a/targets/list.c b/targets/list.c new file mode 100644 index 0000000..e09d301 --- /dev/null +++ b/targets/list.c @@ -0,0 +1,42 @@ +// A linked list on the heap: built, reversed, summed. `list_done` gets the +// finished list, the place for a tracer to look at it. +#include +#include + +struct node { + long value; + struct node *next; +}; + +__attribute__((noinline)) static struct node *push(struct node *head, long value) { + struct node *n = malloc(sizeof *n); + n->value = value; + n->next = head; + return n; +} + +__attribute__((noinline)) static struct node *reverse(struct node *head) { + struct node *prev = NULL; + while (head) { + struct node *next = head->next; + head->next = prev; + prev = head; + head = next; + } + return prev; +} + +__attribute__((noinline)) long list_done(struct node *head) { + long sum = 0; + for (; head; head = head->next) sum += head->value; + return sum; +} + +int main(int argc, char **argv) { + int n = argc > 1 ? atoi(argv[1]) : 6; + struct node *head = NULL; + for (int i = n; i >= 1; i--) head = push(head, i * i); + head = reverse(head); + printf("sum = %ld\n", list_done(head)); + return 0; +} diff --git a/targets/threads.c b/targets/threads.c new file mode 100644 index 0000000..ab4d80d --- /dev/null +++ b/targets/threads.c @@ -0,0 +1,26 @@ +// Two threads and a mutex: each adds its share to `total`, main joins them +// and prints it. What clone, futex and a thread's exit look like to a tracer. +#include +#include + +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +long total; + +__attribute__((noinline)) static void *work(void *arg) { + for (long i = 0; i < 1000; i++) { + pthread_mutex_lock(&lock); + total += (long)arg; + pthread_mutex_unlock(&lock); + } + return NULL; +} + +int main(void) { + pthread_t a, b; + pthread_create(&a, NULL, work, (void *)1); + pthread_create(&b, NULL, work, (void *)2); + pthread_join(a, NULL); + pthread_join(b, NULL); + printf("total = %ld\n", total); + return 0; +} -- cgit v1.3