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 {...@@ -332,7 +332,7 @@ pub const File = struct {
332 .coff, .elf, .macho, .plan9 => {332 .coff, .elf, .macho, .plan9 => {
333 if (base.file != null) return;333 if (base.file != null) return;
334 const emit = base.options.emit orelse return;334 const emit = base.options.emit orelse return;
335 if (base.child_pid != null) {335 if (base.child_pid) |pid| {
336 // If we try to open the output file in write mode while it is running,336 // If we try to open the output file in write mode while it is running,
337 // it will return ETXTBSY. So instead, we copy the file, atomically rename it337 // it will return ETXTBSY. So instead, we copy the file, atomically rename it
338 // over top of the exe path, and then proceed normally. This changes the inode,338 // over top of the exe path, and then proceed normally. This changes the inode,
...@@ -342,6 +342,11 @@ pub const File = struct {...@@ -342,6 +342,11 @@ pub const File = struct {
342 });342 });
343 try emit.directory.handle.copyFile(emit.sub_path, emit.directory.handle, tmp_sub_path, .{});343 try emit.directory.handle.copyFile(emit.sub_path, emit.directory.handle, tmp_sub_path, .{});
344 try emit.directory.handle.rename(tmp_sub_path, emit.sub_path);344 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 }
345 }350 }
346 base.file = try emit.directory.handle.createFile(emit.sub_path, .{351 base.file = try emit.directory.handle.createFile(emit.sub_path, .{
347 .truncate = false,352 .truncate = false,
...@@ -393,6 +398,13 @@ pub const File = struct {...@@ -393,6 +398,13 @@ pub const File = struct {
393 }398 }
394 f.close();399 f.close();
395 base.file = null;400 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 }
396 },408 },
397 .c, .wasm, .spirv => {},409 .c, .wasm, .spirv => {},
398 }410 }