authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-14 18:04:11-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-21 16:25:25-07:00
log3cf48d0fd77dac3c2efa000eb97364e916987b74
treee586247aa31b0c04dce2759cda901d03ec0c1a4d
parent4624c635139d7da85a6ee1310adafc3b6ed5840a

link: PTRACE_ATTACH/PTRACE_DETACH


1 files changed, 13 insertions(+), 1 deletions(-)

src/link.zig+13-1
......@@ -332,7 +332,7 @@ pub const File = struct {
332332 .coff, .elf, .macho, .plan9 => {
333333 if (base.file != null) return;
334334 const emit = base.options.emit orelse return;
335 if (base.child_pid != null) {
335 if (base.child_pid) |pid| {
336336 // If we try to open the output file in write mode while it is running,
337337 // it will return ETXTBSY. So instead, we copy the file, atomically rename it
338338 // over top of the exe path, and then proceed normally. This changes the inode,
......@@ -342,6 +342,11 @@ pub const File = struct {
342342 });
343343 try emit.directory.handle.copyFile(emit.sub_path, emit.directory.handle, tmp_sub_path, .{});
344344 try emit.directory.handle.rename(tmp_sub_path, emit.sub_path);
345
346 switch (std.os.errno(std.os.linux.ptrace(std.os.linux.PTRACE.ATTACH, pid, 0, 0, 0))) {
347 .SUCCESS => {},
348 else => |errno| log.warn("ptrace failure: {s}", .{@tagName(errno)}),
349 }
345350 }
346351 base.file = try emit.directory.handle.createFile(emit.sub_path, .{
347352 .truncate = false,
......@@ -393,6 +398,13 @@ pub const File = struct {
393398 }
394399 f.close();
395400 base.file = null;
401
402 if (base.child_pid) |pid| {
403 switch (std.os.errno(std.os.linux.ptrace(std.os.linux.PTRACE.DETACH, pid, 0, 0, 0))) {
404 .SUCCESS => {},
405 else => |errno| log.warn("ptrace failure: {s}", .{@tagName(errno)}),
406 }
407 }
396408 },
397409 .c, .wasm, .spirv => {},
398410 }