From 3cf48d0fd77dac3c2efa000eb97364e916987b74 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 14 Jan 2022 18:04:11 -0700 Subject: [PATCH] link: PTRACE_ATTACH/PTRACE_DETACH --- src/link.zig | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/link.zig b/src/link.zig index 6c6c6927ab9212b371ee7cb7e98321f874509136..6e000943b4eaabbe1828903e279cff4bf09e0cdf 100644 --- a/src/link.zig +++ b/src/link.zig @@ -332,7 +332,7 @@ pub const File = struct { .coff, .elf, .macho, .plan9 => { if (base.file != null) return; const emit = base.options.emit orelse return; - if (base.child_pid != null) { + if (base.child_pid) |pid| { // If we try to open the output file in write mode while it is running, // it will return ETXTBSY. So instead, we copy the file, atomically rename it // over top of the exe path, and then proceed normally. This changes the inode, @@ -342,6 +342,11 @@ pub const File = struct { }); try emit.directory.handle.copyFile(emit.sub_path, emit.directory.handle, tmp_sub_path, .{}); try emit.directory.handle.rename(tmp_sub_path, emit.sub_path); + + switch (std.os.errno(std.os.linux.ptrace(std.os.linux.PTRACE.ATTACH, pid, 0, 0, 0))) { + .SUCCESS => {}, + else => |errno| log.warn("ptrace failure: {s}", .{@tagName(errno)}), + } } base.file = try emit.directory.handle.createFile(emit.sub_path, .{ .truncate = false, @@ -393,6 +398,13 @@ pub const File = struct { } f.close(); base.file = null; + + if (base.child_pid) |pid| { + switch (std.os.errno(std.os.linux.ptrace(std.os.linux.PTRACE.DETACH, pid, 0, 0, 0))) { + .SUCCESS => {}, + else => |errno| log.warn("ptrace failure: {s}", .{@tagName(errno)}), + } + } }, .c, .wasm, .spirv => {}, } -- 2.54.0