diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 11:44:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 12:09:34 -0300 |
| commit | ce7c0abf14e7ab056d5479adc3f05df9976e94e4 (patch) | |
| tree | 8931de9a55b3d7c2ec2d52d3b3817c6f7eb5db5a /targets | |
| parent | e7aa7801c10ed34194b7fe3f5ca8722a72404e65 (diff) | |
| download | computeranim-ce7c0abf14e7ab056d5479adc3f05df9976e94e4.tar.gz computeranim-ce7c0abf14e7ab056d5479adc3f05df9976e94e4.zip | |
agent: the in-process backend, and ztrace run's options for it
libztrace-agent.so (preloaded) and libztrace-agent.a (linked in), one
source: map and thread_start at startup; alloc/free from the interposed
allocator; syscall_enter/exit through Syscall User Dispatch; call/ret
from int3 hooks; mem_write from watched regions diffed in-process; the
target-side API (include/ztrace.h, and the ztrace-agent module for Zig).
ztrace run turns each on (--alloc --syscalls --hook --watch), chooses the
moments (--cut), and stops the target at event kinds without ptrace
(--stop-at, --read, -i). test-agent records real targets and checks the
recordings.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'targets')
| -rw-r--r-- | targets/agent_api.c | 22 | ||||
| -rw-r--r-- | targets/agent_fib.c | 15 | ||||
| -rw-r--r-- | targets/agent_list.c | 46 | ||||
| -rw-r--r-- | targets/agent_sort.c | 22 | ||||
| -rw-r--r-- | targets/agent_sys.c | 57 | ||||
| -rw-r--r-- | targets/agent_zig.zig | 20 |
6 files changed, 182 insertions, 0 deletions
diff --git a/targets/agent_api.c b/targets/agent_api.c new file mode 100644 index 0000000..1c954cf --- /dev/null +++ b/targets/agent_api.c @@ -0,0 +1,22 @@ +// A program that annotates itself: the agent linked in, an array on its +// stack watched, a mark after every step of an insertion sort. +#include <stdio.h> +#include <ztrace.h> + +int main(void) { + int numbers[16] = {9, 3, 14, 0, 7, 12, 5, 1, 15, 8, 2, 11, 6, 13, 4, 10}; + int n = sizeof numbers / sizeof *numbers; + ztrace_watch(numbers, sizeof numbers, "numbers"); + for (int i = 1; i < n; i++) { + int value = numbers[i], j = i; + for (; j > 0 && numbers[j - 1] > value; j--) numbers[j] = numbers[j - 1]; + numbers[j] = value; + ztrace_mark("inserted", i); + } + unsigned long checksum = 0; + for (int i = 0; i < n; i++) checksum = checksum * 31 + numbers[i]; + ztrace_snapshot(&checksum, sizeof checksum); + ztrace_mark("done", checksum); + printf("%lu\n", checksum); + return 0; +} diff --git a/targets/agent_fib.c b/targets/agent_fib.c new file mode 100644 index 0000000..33ae856 --- /dev/null +++ b/targets/agent_fib.c @@ -0,0 +1,15 @@ +// A function to hook: fib(n) the slow way, kept from being turned into a +// loop so that it makes the 2 fib(n+1) - 1 calls the definition says. +#include <stdio.h> +#include <stdlib.h> + +__attribute__((noinline, optnone)) static unsigned long fib(unsigned long n) { + if (n < 2) return n; + return fib(n - 1) + fib(n - 2); +} + +int main(int argc, char **argv) { + unsigned long n = argc > 1 ? strtoul(argv[1], NULL, 10) : 10; + printf("fib(%lu) = %lu\n", n, fib(n)); + return 0; +} diff --git a/targets/agent_list.c b/targets/agent_list.c new file mode 100644 index 0000000..8787638 --- /dev/null +++ b/targets/agent_list.c @@ -0,0 +1,46 @@ +// What the allocation tracker is for: a linked list of N nodes built and +// torn down, beside one of each other way to allocate. +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct node { + struct node *next; + long value; +}; + +int main(int argc, char **argv) { + long n = argc > 1 ? atol(argv[1]) : 100; + struct node *head = NULL; + for (long i = 0; i < n; i++) { + struct node *node = malloc(sizeof *node); + node->value = i; + node->next = head; + head = node; + } + + // A vector that grows: realloc in place and realloc that moves. + size_t cap = 4, len = 0; + long *values = calloc(cap, sizeof *values); + for (struct node *node = head; node; node = node->next) { + if (len == cap) values = realloc(values, (cap *= 2) * sizeof *values); + values[len++] = node->value; + } + void *aligned = NULL; + if (posix_memalign(&aligned, 64, 100)) return 1; + void *page = aligned_alloc(4096, 4096); + memset(page, 1, 4096); + + long sum = ((char *)page)[100] - 1; + while (head) { + struct node *node = head; + head = node->next; + sum += node->value; + free(node); + } + free(values); + free(aligned); + free(page); + printf("sum %ld of %zu\n", sum, len); + return 0; +} diff --git a/targets/agent_sort.c b/targets/agent_sort.c new file mode 100644 index 0000000..7e29a9d --- /dev/null +++ b/targets/agent_sort.c @@ -0,0 +1,22 @@ +// A program that knows nothing of the tracer: a global array sorted by +// swaps. Hooked at `swap` and watched at `numbers` from outside, each moment +// of its recording is the array one swap later. +#include <stdio.h> + +int numbers[16] = {9, 3, 14, 0, 7, 12, 5, 1, 15, 8, 2, 11, 6, 13, 4, 10}; + +__attribute__((noinline)) void swap(int *a, int *b) { + int t = *a; + *a = *b; + *b = t; +} + +int main(void) { + int n = sizeof numbers / sizeof *numbers; + for (int i = 0; i < n; i++) + for (int j = 0; j + 1 < n - i; j++) + if (numbers[j] > numbers[j + 1]) swap(&numbers[j], &numbers[j + 1]); + for (int i = 0; i < n; i++) printf("%d ", numbers[i]); + printf("\n"); + return 0; +} diff --git a/targets/agent_sys.c b/targets/agent_sys.c new file mode 100644 index 0000000..12a6ada --- /dev/null +++ b/targets/agent_sys.c @@ -0,0 +1,57 @@ +// Syscalls to find again in a recording: a file opened, written and closed, +// from the main thread, from a second thread, from a forked child, and from +// a signal handler (which has to return through the dispatcher too). +#include <fcntl.h> +#include <pthread.h> +#include <signal.h> +#include <string.h> +#include <sys/wait.h> +#include <unistd.h> + +static int fd; + +static void say(const char *text) { + if (write(fd, text, strlen(text)) < 0) _exit(1); +} + +static void on_signal(int signo) { + (void)signo; + say("signal\n"); +} + +static void *thread(void *arg) { + say(arg); + return NULL; +} + +int main(void) { + fd = open("/dev/null", O_WRONLY); + say("hello from main\n"); + + // Every signal blocked around a syscall: the agent must keep its own deliverable. + sigset_t all, old; + sigfillset(&all); + sigprocmask(SIG_BLOCK, &all, &old); + say("masked\n"); + sigprocmask(SIG_SETMASK, &old, NULL); + + struct sigaction action = {.sa_handler = on_signal}; + sigfillset(&action.sa_mask); + sigaction(SIGUSR1, &action, NULL); + raise(SIGUSR1); + + pthread_t t; + pthread_create(&t, NULL, thread, "thread\n"); + pthread_join(t, NULL); + + pid_t child = fork(); + if (child == 0) { + say("child\n"); + _exit(3); + } + int status = 0; + waitpid(child, &status, 0); + say(WEXITSTATUS(status) == 3 ? "child exited 3\n" : "child lost\n"); + close(fd); + return getpid() == getppid(); +} diff --git a/targets/agent_zig.zig b/targets/agent_zig.zig new file mode 100644 index 0000000..bfb2f5d --- /dev/null +++ b/targets/agent_zig.zig @@ -0,0 +1,20 @@ +//! A Zig program that annotates itself: agent_api.c again, with the agent +//! compiled in as a module instead of linked as a library. +const std = @import("std"); +const agent = @import("ztrace-agent"); + +pub fn main() void { + var numbers = [16]i32{ 9, 3, 14, 0, 7, 12, 5, 1, 15, 8, 2, 11, 6, 13, 4, 10 }; + agent.watch(std.mem.asBytes(&numbers), "numbers"); + for (1..numbers.len) |i| { + const value = numbers[i]; + var j = i; + while (j > 0 and numbers[j - 1] > value) : (j -= 1) numbers[j] = numbers[j - 1]; + numbers[j] = value; + agent.mark("inserted", i); + } + var checksum: u64 = 0; + for (numbers) |v| checksum = checksum *% 31 +% @as(u64, @intCast(v)); + agent.snapshot(std.mem.asBytes(&checksum)); + agent.mark("done", checksum); +} |
