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.zig45
1 files changed, 33 insertions, 12 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index 729b93a..9368a42 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -51,6 +51,9 @@ syscalls: enum { ignore, record, stop } = .ignore,
/// Called at each syscall's entry and exit unless they are ignored; it
/// decides whether the target stops there.
on_syscall: ?Hook = null,
+/// A controller's mailbox, serviced while waiting without moving ptrace to another thread.
+on_wait: ?struct { ctx: *anyopaque, func: *const fn (*anyopaque, *Debugger) anyerror!void } = null,
+interrupt_sent: bool = false,
/// Bytes above the stack pointer watched at every stop.
stack_watch: usize = 0,
/// The most captured of what a syscall points at: a path, the buffer of a
@@ -122,7 +125,7 @@ threads: std.AutoHashMapUnmanaged(linux.pid_t, void) = .empty,
/// String ids of source files: by path, and by line-table entry.
files: std.StringHashMapUnmanaged(u64) = .empty,
file_ids: std.AutoHashMapUnmanaged(usize, u64) = .empty,
-last_line: struct { file: u64 = 0, line: u32 = 0 } = .{},
+last_line: struct { file: u64 = 0, line: u32 = 0, tid: linux.pid_t = 0 } = .{},
pub const Stop = union(enum) {
/// `stepi` or `step` finished.
@@ -259,14 +262,14 @@ fn inVdso(d: *const Debugger, addr: u64) bool {
/// To the next source line: instructions until the line is another, a call
/// into code without lines (a library) run through as one.
pub fn step(d: *Debugger) !Stop {
- const from = d.lineAt(d.regs.rip);
+ const from = d.lineAt(d.regs.rip) orelse return d.stepi();
const tid = d.proc.tid;
while (true) {
const before = d.regs;
var stop = try d.stepOne();
if (stop == .step and d.proc.tid == tid) {
if (d.lineAt(d.regs.rip)) |here| {
- if (from == null or from.?.key != here.key or from.?.line != here.line or d.breakpointAt(d.regs.rip) != null) return d.report(.step);
+ if (from.key != here.key or from.line != here.line or d.breakpointAt(d.regs.rip) != null) return d.report(.step);
continue;
}
// In code without lines. A call there from code with lines
@@ -324,20 +327,29 @@ fn waitAny(d: *Debugger) !ptrace.Stop {
/// 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);
+ if (d.ring == null and d.on_wait == null) return d.proc.wait(tid);
const tapped: event.Sink = .{ .ctx = d, .emitFn = tap };
var idle: u32 = 0;
while (true) {
+ if (d.on_wait) |hook| try hook.func(hook.ctx, d);
if (try d.proc.poll(tid)) |stop| {
- _ = try ring.drain(tapped);
+ if (d.ring) |ring| _ = try ring.drain(tapped);
return stop;
}
// A step is back in microseconds: look again before sleeping.
- idle = if (try ring.drain(tapped) > 0) 0 else idle + 1;
- if (idle > 64) ring.wait(200 * std.time.ns_per_us);
+ const drained = if (d.ring) |ring| try ring.drain(tapped) else 0;
+ idle = if (drained > 0) 0 else idle +| 1;
+ if (idle > 64) try std.Io.sleep(d.syms.io, .fromMicroseconds(200), .awake);
}
}
+/// Request a stop from the owning controller's wait hook. Our SIGSTOP is not redelivered.
+pub fn interrupt(d: *Debugger) !void {
+ if (d.done or d.interrupt_sent) return;
+ if (linux.errno(linux.tgkill(d.proc.pid, d.proc.tid, .STOP)) != .SUCCESS) return error.InterruptFailed;
+ d.interrupt_sent = true;
+}
+
/// What the target says of itself goes to the sink as it is; and a
/// variable it declares (`ztrace cc --instr vars`, the agent's
/// `ztrace_watch`) is watched from then on, so its bytes are recorded at
@@ -424,6 +436,11 @@ fn run(d: *Debugger, request: u32) !Stop {
.exited, .killed => unreachable,
.clone => |child| try d.emit(.{ .kind = .thread_start, .a = .{ @intCast(child), 0, 0, 0, 0, 0 } }, std.mem.asBytes(&d.regs)),
.signal => |sig| {
+ if (sig == .STOP and d.interrupt_sent) {
+ d.interrupt_sent = false;
+ d.proc.resume_signal = 0;
+ return .{ .signal = sig };
+ }
// A thread's first stop is the SIGSTOP it is born with under tracing.
if (!born or sig != .STOP) return .{ .signal = sig };
d.proc.resume_signal = 0;
@@ -518,6 +535,8 @@ fn emit(d: *Debugger, ev: Event, payload: []const u8) !void {
fn report(d: *Debugger, stop: Stop) !Stop {
if (d.sink == null) return stop;
if (!d.done) {
+ // Code loaded or JIT-compiled while running without syscall stops.
+ if (d.syms.mappingAt(d.regs.rip) == null) try d.refreshMaps();
try d.emitLine();
if (d.asan and d.asan_shadow == null) try d.findShadow();
// With the 128 bytes under the stack pointer: the red zone, a leaf's locals.
@@ -955,11 +974,13 @@ fn fileId(d: *Debugger, l: Symbols.Line) !u64 {
}
fn emitLine(d: *Debugger) !void {
- const l = d.lineAt(d.regs.rip) orelse return;
- const file = try d.fileId(l);
- if (file == d.last_line.file and l.line == d.last_line.line) return;
- d.last_line = .{ .file = file, .line = l.line };
- try d.emit(.{ .kind = .line, .a = .{ file, l.line, l.column, d.regs.rip, 0, 0 } }, "");
+ const l = d.lineAt(d.regs.rip);
+ const file = if (l) |loc| try d.fileId(loc) else 0;
+ const line = if (l) |loc| loc.line else 0;
+ if (file == d.last_line.file and line == d.last_line.line and (d.proc.tid == d.last_line.tid or d.last_line.tid == 0)) return;
+ d.last_line = .{ .file = file, .line = line, .tid = d.proc.tid };
+ // File zero clears a native source location when entering code without lines (including JIT code).
+ try d.emit(.{ .kind = .line, .a = .{ file, line, if (l) |loc| loc.column else 0, d.regs.rip, 0, 0 } }, "");
}
/// Reads the target's mappings again: what changed goes out as `map` and