From ce7c0abf14e7ab056d5479adc3f05df9976e94e4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:44:48 -0300 Subject: 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 --- targets/agent_sys.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 targets/agent_sys.c (limited to 'targets/agent_sys.c') 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 +#include +#include +#include +#include +#include + +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(); +} -- cgit v1.3