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> 2023-03-15 10:48:12-07:00
log50a2bb58d23a686f52b389c062f0299cc2a5f8ed
tree22eb3d26a53fc2fe511bc748fce6bc12508abd6a
parentae8e7c8f5a6065d12087aa547971001a399667b9

link: PTRACE_ATTACH/PTRACE_DETACH


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

src/link.zig+13-1
......@@ -378,7 +378,7 @@ pub const File = struct {
378378 if (build_options.only_c) unreachable;
379379 if (base.file != null) return;
380380 const emit = base.options.emit orelse return;
381 if (base.child_pid != null) {
381 if (base.child_pid) |pid| {
382382 // If we try to open the output file in write mode while it is running,
383383 // it will return ETXTBSY. So instead, we copy the file, atomically rename it
384384 // over top of the exe path, and then proceed normally. This changes the inode,
......@@ -388,6 +388,11 @@ pub const File = struct {
388388 });
389389 try emit.directory.handle.copyFile(emit.sub_path, emit.directory.handle, tmp_sub_path, .{});
390390 try emit.directory.handle.rename(tmp_sub_path, emit.sub_path);
391
392 switch (std.os.errno(std.os.linux.ptrace(std.os.linux.PTRACE.ATTACH, pid, 0, 0, 0))) {
393 .SUCCESS => {},
394 else => |errno| log.warn("ptrace failure: {s}", .{@tagName(errno)}),
395 }
391396 }
392397 base.file = try emit.directory.handle.createFile(emit.sub_path, .{
393398 .truncate = false,
......@@ -437,6 +442,13 @@ pub const File = struct {
437442 }
438443 f.close();
439444 base.file = null;
445
446 if (base.child_pid) |pid| {
447 switch (std.os.errno(std.os.linux.ptrace(std.os.linux.PTRACE.DETACH, pid, 0, 0, 0))) {
448 .SUCCESS => {},
449 else => |errno| log.warn("ptrace failure: {s}", .{@tagName(errno)}),
450 }
451 }
440452 },
441453 .c, .spirv, .nvptx => {},
442454 }