summaryrefslogtreecommitdiff
path: root/src/agent/hook.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent/hook.zig')
-rw-r--r--src/agent/hook.zig20
1 files changed, 8 insertions, 12 deletions
diff --git a/src/agent/hook.zig b/src/agent/hook.zig
index d11ddbd..57a374c 100644
--- a/src/agent/hook.zig
+++ b/src/agent/hook.zig
@@ -33,6 +33,11 @@ const Frame = struct { slot: usize, ret: usize, callee: usize };
threadlocal var frames: [1024]Frame = @splat(.{ .slot = 0, .ret = 0, .callee = 0 });
threadlocal var depth: u32 = 0;
+/// SIGTRAP is the agent's.
+pub fn active() bool {
+ return count != 0;
+}
+
/// Hooked calls the calling thread has open.
pub fn open() u32 {
return depth;
@@ -45,7 +50,7 @@ pub fn add(addr: usize) void {
for (hooks[0..count]) |h| {
if (h.addr == addr) return;
}
- listen();
+ if (count == 0) signal.install(.TRAP, onTrap);
// Left writable: a single step puts the first byte back for a moment.
const page = addr & ~@as(usize, 4095);
if (linux.errno(linux.mprotect(@ptrFromInt(page), 4096, .{ .READ = true, .WRITE = true, .EXEC = true })) != .SUCCESS) return;
@@ -55,22 +60,13 @@ pub fn add(addr: usize) void {
code[0] = 0xcc;
}
-var listening = false;
-
-/// SIGTRAP is the agent's from here on: breakpoints, and single steps (its own and sud.zig's).
-pub fn listen() void {
- if (!listening) signal.install(.TRAP, onTrap);
- listening = true;
-}
-
fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.c) void {
const TRAP_TRACE = 2;
if (info.code == TRAP_TRACE) {
- // A single step ended: over a hooked function's first instruction,
- // or over a syscall the dispatcher let through.
+ // The step over a hooked function's first instruction ended. Whose,
+ // nobody wrote down: all the breakpoints go back in.
for (hooks[0..count]) |h| @as(*u8, @ptrFromInt(h.addr)).* = 0xcc;
uc.eflags &= ~@as(u64, signal.trap_flag);
- sud.stepped(uc);
return;
}
const hook = for (hooks[0..count]) |*h| {