summaryrefslogtreecommitdiff
path: root/src/trace/event.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace/event.zig')
-rw-r--r--src/trace/event.zig163
1 files changed, 163 insertions, 0 deletions
diff --git a/src/trace/event.zig b/src/trace/event.zig
new file mode 100644
index 0000000..91e8c62
--- /dev/null
+++ b/src/trace/event.zig
@@ -0,0 +1,163 @@
+//! The one record every backend produces (ptrace, the in-process agent,
+//! LLVM instrumentation, kernel tracing) and every consumer reads (the
+//! recorder, the animations). 64 bytes, plus an optional payload carried
+//! beside it. `extern` because it crosses processes (the ring) and is
+//! written to disk (the recorder).
+const std = @import("std");
+
+pub const Kind = enum(u16) {
+ // Execution stops. Payload: `Regs`, when the backend has them.
+ /// a0=pc
+ step = 1,
+ /// a0=pc a1=breakpoint id
+ breakpoint = 2,
+ /// aux=nr a0..a5=args. Payload: optional decoded bytes (a path).
+ syscall_enter = 3,
+ /// aux=nr a0=return value
+ syscall_exit = 4,
+ /// aux=signo a0=fault address a1=pc
+ signal = 5,
+ /// a0=exit code, or 128+signo when killed
+ exit = 6,
+ /// a0=new tid
+ thread_start = 7,
+ thread_exit = 8,
+
+ // Memory.
+ /// a0=addr a1=len. Payload: the bytes now at addr (len of them).
+ mem_write = 16,
+ /// a0=addr a1=len a2=pc
+ mem_read = 17,
+ /// a0=ptr a1=size a2=caller pc. aux: `Alloc`.
+ alloc = 18,
+ /// a0=ptr a1=caller pc
+ free = 19,
+ /// a0=addr a1=len a2=prot a3=file offset. Payload: path, if file-backed.
+ map = 20,
+ /// a0=addr a1=len
+ unmap = 21,
+
+ // Instrumentation (compiler passes, hooks).
+ /// a0=callee a1=call site a2=sp
+ call = 32,
+ /// a0=callee a1=return value
+ ret = 33,
+ /// a0=file string id a1=line a2=column a3=pc
+ line = 34,
+ /// a0=pc (a coverage edge)
+ edge = 35,
+ /// aux=operand bytes a0=pc a1=lhs a2=rhs
+ cmp = 36,
+ /// A named variable comes alive: a0=addr a1=size a2=name string id a3=type string id
+ variable = 37,
+
+ // Kernel.
+ /// a0=prev tid a1=next tid a2=cpu
+ sched = 48,
+ /// a0=fault address a1=pc. aux: fault flags.
+ fault = 49,
+ /// a0=pc a1=counter id a2=value
+ sample = 50,
+ /// A kprobe, uprobe, tracepoint or USDT probe: a0=probe string id a1..a5=arguments
+ probe = 51,
+
+ // Annotations.
+ /// a0=user value. Payload: text.
+ mark = 60,
+ /// Defines a string id for later events: a0=id. Payload: the text.
+ string = 61,
+ _,
+
+ /// The bit of a kind in a 64-bit set (kinds are all below 64).
+ pub fn bit(k: Kind) u64 {
+ return @as(u64, 1) << @intCast(@intFromEnum(k));
+ }
+};
+
+pub const Alloc = enum(u16) { malloc, calloc, realloc, memalign, mmap };
+
+pub const Event = extern struct {
+ /// CLOCK_MONOTONIC nanoseconds.
+ ts: u64 = 0,
+ tid: u32 = 0,
+ kind: Kind,
+ /// Small kind-specific value (syscall number, signal, operand size).
+ aux: u16 = 0,
+ a: [6]u64 = @splat(0),
+
+ comptime {
+ std.debug.assert(@sizeOf(Event) == 64);
+ }
+};
+
+/// Where events go. A recorder, a ring, a printer.
+pub const Sink = struct {
+ ctx: *anyopaque,
+ emitFn: *const fn (ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!void,
+
+ pub fn emit(s: Sink, ev: Event, payload: []const u8) !void {
+ return s.emitFn(s.ctx, &ev, payload);
+ }
+};
+
+/// x86-64 general registers, in the kernel's `user_regs_struct` order, so
+/// PTRACE_GETREGS fills it directly.
+pub const Regs = extern struct {
+ r15: u64 = 0,
+ r14: u64 = 0,
+ r13: u64 = 0,
+ r12: u64 = 0,
+ rbp: u64 = 0,
+ rbx: u64 = 0,
+ r11: u64 = 0,
+ r10: u64 = 0,
+ r9: u64 = 0,
+ r8: u64 = 0,
+ rax: u64 = 0,
+ rcx: u64 = 0,
+ rdx: u64 = 0,
+ rsi: u64 = 0,
+ rdi: u64 = 0,
+ orig_rax: u64 = 0,
+ rip: u64 = 0,
+ cs: u64 = 0,
+ eflags: u64 = 0,
+ rsp: u64 = 0,
+ ss: u64 = 0,
+ fs_base: u64 = 0,
+ gs_base: u64 = 0,
+ ds: u64 = 0,
+ es: u64 = 0,
+ fs: u64 = 0,
+ gs: u64 = 0,
+
+ comptime {
+ std.debug.assert(@sizeOf(Regs) == 27 * 8);
+ }
+
+ /// The registers a viewer shows, in display order.
+ pub const names = [_][]const u8{ "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rip", "eflags" };
+
+ pub fn get(r: *const Regs, comptime name: []const u8) u64 {
+ return @field(r, name);
+ }
+
+ /// By name at runtime: null for a name that is no register.
+ pub fn named(r: *const Regs, name: []const u8) ?u64 {
+ inline for (@typeInfo(Regs).@"struct".field_names) |f| {
+ if (std.mem.eql(u8, f, name)) return @field(r, f);
+ }
+ return null;
+ }
+};
+
+pub fn now() u64 {
+ var ts: std.os.linux.timespec = undefined;
+ _ = std.os.linux.clock_gettime(.MONOTONIC, &ts);
+ return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec));
+}
+
+test "kind bits fit a u64 set" {
+ inline for (@typeInfo(Kind).@"enum".field_values) |v| try std.testing.expect(v < 64);
+ try std.testing.expectEqual(@as(?u64, 7), (Regs{ .rip = 7 }).named("rip"));
+}