summaryrefslogtreecommitdiff
path: root/src/trace
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace')
-rw-r--r--src/trace/Debugger.zig33
-rw-r--r--src/trace/event.zig9
-rw-r--r--src/trace/host.zig27
-rw-r--r--src/trace/ptrace.zig13
4 files changed, 67 insertions, 15 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index b2cdd5e..d69adcc 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -29,6 +29,7 @@ const event = @import("event.zig");
const Event = event.Event;
const Regs = event.Regs;
const ptrace = @import("ptrace.zig");
+const Ring = @import("Ring.zig");
const Symbols = @import("Symbols.zig");
const syscalls_table = @import("syscalls.zig");
const P = linux.PTRACE;
@@ -37,6 +38,10 @@ gpa: std.mem.Allocator,
proc: ptrace.Process,
syms: Symbols,
sink: ?event.Sink,
+/// The target's ring, when it carries a producer too (instrumentation, the
+/// agent): drained into the sink while the target runs and before a stop
+/// is reported, so its events and the stops are one recording, in order.
+ring: ?*Ring = null,
/// What a syscall does to the running target: nothing (the fastest), an
/// event, or a stop as well.
syscalls: enum { ignore, record, stop } = .ignore,
@@ -207,12 +212,32 @@ fn resumeThread(d: *Debugger, request: u32) !ptrace.Stop {
if (d.breakpointAt(d.regs.rip)) |bp| {
try d.proc.write(bp.addr, &.{bp.saved});
try d.proc.run(P.SINGLESTEP);
- const stop = try d.proc.wait(d.proc.tid);
+ const stop = try d.wait(d.proc.tid);
if (!d.proc.gone) try d.proc.write(bp.addr, &.{0xcc});
if (request == P.SINGLESTEP or stop != .trap) return stop;
}
try d.proc.run(request);
- return d.proc.waitAny();
+ return d.waitAny();
+}
+
+fn waitAny(d: *Debugger) !ptrace.Stop {
+ return d.wait(if (d.proc.threads > 1) -1 else d.proc.pid);
+}
+
+/// The next stop. With a ring it is polled for and the ring drained
+/// meanwhile: a target waiting for room in a full ring would never stop.
+fn wait(d: *Debugger, tid: linux.pid_t) !ptrace.Stop {
+ const ring = d.ring orelse return d.proc.wait(tid);
+ var idle: u32 = 0;
+ while (true) {
+ if (try d.proc.poll(tid)) |stop| {
+ _ = try ring.drain(d.sink.?);
+ return stop;
+ }
+ // A step is back in microseconds: look again before sleeping.
+ idle = if (try ring.drain(d.sink.?) > 0) 0 else idle + 1;
+ if (idle > 64) ring.wait(200 * std.time.ns_per_us);
+ }
}
/// Runs the target with `request` until something stops it for the caller.
@@ -224,7 +249,7 @@ fn run(d: *Debugger, request: u32) !Stop {
while (true) {
const before = d.regs;
const tid = d.proc.tid;
- const raw = if (resumes) try d.resumeThread(request) else try d.proc.waitAny();
+ const raw = if (resumes) try d.resumeThread(request) else try d.waitAny();
resumes = true;
switch (raw) {
.exited, .killed => {
@@ -386,7 +411,7 @@ fn emitLine(d: *Debugger) !void {
return err;
};
if (named.found_existing) d.gpa.free(path) else {
- named.value_ptr.* = d.files.count();
+ named.value_ptr.* = event.stringId(path);
try d.emit(.{ .kind = .string, .a = .{ named.value_ptr.*, 0, 0, 0, 0, 0 } }, path);
}
try d.file_ids.put(d.gpa, l.key, named.value_ptr.*);
diff --git a/src/trace/event.zig b/src/trace/event.zig
index 4a9f6f7..9ce8e94 100644
--- a/src/trace/event.zig
+++ b/src/trace/event.zig
@@ -159,6 +159,15 @@ pub const Regs = extern struct {
}
};
+/// The id of a string, for a producer that keeps no table: its text's
+/// hash, with the top bit set. The instrumentation counts its strings from
+/// 1, and a symbol's name is the string whose id is its address; a hash up
+/// here meets neither, so the producers of one recording (a debugger, an
+/// agent and instrumentation at once) never define an id twice.
+pub fn stringId(text: []const u8) u64 {
+ return std.hash.Wyhash.hash(0, text) | 1 << 63;
+}
+
pub fn now() u64 {
var ts: std.os.linux.timespec = undefined;
_ = std.os.linux.clock_gettime(.MONOTONIC, &ts);
diff --git a/src/trace/host.zig b/src/trace/host.zig
index df2f845..7c43663 100644
--- a/src/trace/host.zig
+++ b/src/trace/host.zig
@@ -16,6 +16,22 @@ pub const Options = struct {
stop_mask: u64 = 0,
};
+/// `env` with the ring in it, the agent preloaded when there is one, and
+/// `extra` (settings for the agent) after them.
+pub fn environment(arena: std.mem.Allocator, env: [:null]const ?[*:0]const u8, ring: *const Ring, preload: ?[]const u8, extra: []const [*:0]const u8) ![:null]const ?[*:0]const u8 {
+ var envp: std.ArrayList(?[*:0]const u8) = .empty;
+ for (env) |v| {
+ const s = std.mem.span(v.?);
+ if (std.mem.startsWith(u8, s, client.env_fd ++ "=")) continue;
+ if (preload != null and std.mem.startsWith(u8, s, "LD_PRELOAD=")) continue;
+ try envp.append(arena, v);
+ }
+ try envp.append(arena, try std.fmt.allocPrintSentinel(arena, client.env_fd ++ "={d}", .{ring.fd}, 0));
+ if (preload) |lib| try envp.append(arena, try std.fmt.allocPrintSentinel(arena, "LD_PRELOAD={s}", .{lib}, 0));
+ for (extra) |v| try envp.append(arena, v);
+ return envp.toOwnedSliceSentinel(arena, null);
+}
+
pub const Session = struct {
ring: Ring,
pid: linux.pid_t,
@@ -27,16 +43,7 @@ pub const Session = struct {
var ring = try Ring.create(options.ring_bytes);
errdefer ring.close();
ring.hdr.stop_mask.store(options.stop_mask, .monotonic);
- var envp: std.ArrayList(?[*:0]const u8) = .empty;
- for (env) |v| {
- const s = std.mem.span(v.?);
- if (std.mem.startsWith(u8, s, client.env_fd ++ "=")) continue;
- if (options.preload != null and std.mem.startsWith(u8, s, "LD_PRELOAD=")) continue;
- try envp.append(arena, v);
- }
- try envp.append(arena, try std.fmt.allocPrintSentinel(arena, client.env_fd ++ "={d}", .{ring.fd}, 0));
- if (options.preload) |lib| try envp.append(arena, try std.fmt.allocPrintSentinel(arena, "LD_PRELOAD={s}", .{lib}, 0));
- const block = try envp.toOwnedSliceSentinel(arena, null);
+ const block = try environment(arena, env, &ring, options.preload, &.{});
const rc = linux.fork();
if (linux.errno(rc) != .SUCCESS) return error.Fork;
diff --git a/src/trace/ptrace.zig b/src/trace/ptrace.zig
index fd7ebd1..ca3dc39 100644
--- a/src/trace/ptrace.zig
+++ b/src/trace/ptrace.zig
@@ -119,8 +119,19 @@ pub const Process = struct {
/// The next stop of one thread (-1: of any child), which becomes the current one.
pub fn wait(p: *Process, tid: linux.pid_t) !Stop {
+ return (try p.next(tid, WALL)).?;
+ }
+
+ /// The same without waiting: null while nothing has stopped.
+ pub fn poll(p: *Process, tid: linux.pid_t) !?Stop {
+ return p.next(tid, WALL | linux.W.NOHANG);
+ }
+
+ fn next(p: *Process, tid: linux.pid_t, flags: u32) !?Stop {
var status: u32 = 0;
- p.tid = @intCast(try check(linux.waitpid(tid, @ptrCast(&status), WALL)));
+ const stopped = try check(linux.waitpid(tid, @ptrCast(&status), flags));
+ if (stopped == 0) return null;
+ p.tid = @intCast(stopped);
if (linux.W.IFEXITED(status) or linux.W.IFSIGNALED(status)) {
if (p.tid == p.pid) p.gone = true else p.threads -= 1;
return if (linux.W.IFEXITED(status)) .{ .exited = linux.W.EXITSTATUS(status) } else .{ .killed = linux.W.TERMSIG(status) };