summaryrefslogtreecommitdiff
path: root/src/trace/Debugger.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/trace/Debugger.zig')
-rw-r--r--src/trace/Debugger.zig33
1 files changed, 29 insertions, 4 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.*);