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 /src/agent/signal.zig | |
| 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 'src/agent/signal.zig')
| -rw-r--r-- | src/agent/signal.zig | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/agent/signal.zig b/src/agent/signal.zig new file mode 100644 index 0000000..99aead0 --- /dev/null +++ b/src/agent/signal.zig @@ -0,0 +1,116 @@ +//! What the agent's signal handlers share: the frame the kernel pushes on +//! x86-64, and handlers installed with the raw syscall, behind libc's back. +const std = @import("std"); +const linux = std.os.linux; +const zt = @import("ztrace"); + +/// The kernel's `struct ucontext`: the interrupted thread's registers, which +/// a handler may change before they are restored. +pub const Ucontext = extern struct { + flags: u64, + link: ?*Ucontext, + stack: extern struct { sp: usize, flags: i32, size: usize }, + r8: u64, + r9: u64, + r10: u64, + r11: u64, + r12: u64, + r13: u64, + r14: u64, + r15: u64, + rdi: u64, + rsi: u64, + rbp: u64, + rbx: u64, + rdx: u64, + rax: u64, + rcx: u64, + rsp: u64, + rip: u64, + eflags: u64, + cs: u16, + gs: u16, + fs: u16, + ss: u16, + err: u64, + trapno: u64, + oldmask: u64, + cr2: u64, + fpstate: usize, + reserved: [8]u64, + sigmask: u64, + + comptime { + std.debug.assert(@offsetOf(Ucontext, "sigmask") == 296); + } + + pub fn regs(uc: *const Ucontext) zt.Regs { + var out: zt.Regs = .{ .orig_rax = uc.rax, .cs = uc.cs, .ss = uc.ss, .fs = uc.fs, .gs = uc.gs }; + inline for (zt.Regs.names) |name| @field(out, name) = @field(uc, name); + return out; + } +}; + +/// The trap flag: one instruction, then SIGTRAP. +pub const trap_flag = 0x100; + +/// The bit of a signal in a kernel signal set. +pub fn bit(sig: linux.SIG) u64 { + return @as(u64, 1) << @intCast(@intFromEnum(sig) - 1); +} + +// The gate: the only syscall instructions the dispatcher (sud.zig) lets +// through whatever the selector says. One symbol after the other, so that +// the range between the first and the last is these and nothing else. +comptime { + asm ( + \\ .pushsection .text.ztrace_gate, "ax", @progbits + \\ .globl ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_end + \\ .hidden ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_end + \\ ztrace_gate_sigreturn: + \\ movq %rdi, %rsp + \\ ztrace_gate_restorer: + \\ movl $15, %eax + \\ syscall + \\ ztrace_gate_syscall: + \\ movq %rdi, %rax + \\ movq %rsi, %rdi + \\ movq %rdx, %rsi + \\ movq %rcx, %rdx + \\ movq %r8, %r10 + \\ movq %r9, %r8 + \\ movq 8(%rsp), %r9 + \\ syscall + \\ retq + \\ ztrace_gate_end: + \\ .popsection + ); +} + +/// rt_sigreturn from the signal frame whose `Ucontext` is at `uc`. +pub extern fn ztrace_gate_sigreturn(uc: u64) callconv(.c) noreturn; +/// The same for a handler that returns: its stack pointer is there already. +extern fn ztrace_gate_restorer() callconv(.c) void; +/// Any syscall, by number. +pub extern fn ztrace_gate_syscall(number: u64, a0: u64, a1: u64, a2: u64, a3: u64, a4: u64, a5: u64) callconv(.c) u64; +extern const ztrace_gate_end: u8; + +/// Where the gate is: start and length. +pub fn gate() [2]usize { + return .{ @intFromPtr(&ztrace_gate_sigreturn), @intFromPtr(&ztrace_gate_end) - @intFromPtr(&ztrace_gate_sigreturn) }; +} + +pub const Handler = *const fn (i32, *const linux.siginfo_t, *Ucontext) callconv(.c) void; + +/// Null puts the default action back. The handler runs with no signal +/// blocked, its own included: inside it the thread is still the target, with +/// the target's mask. +pub fn install(sig: linux.SIG, handler: ?Handler) void { + const act: linux.k_sigaction = .{ + .handler = @ptrCast(handler), + .flags = linux.SA.SIGINFO | linux.SA.RESTORER | linux.SA.NODEFER, + .restorer = @ptrCast(&ztrace_gate_restorer), + .mask = linux.sigemptyset(), + }; + _ = linux.syscall4(.rt_sigaction, @intFromEnum(sig), @intFromPtr(&act), 0, @sizeOf(linux.sigset_t)); +} |
