macho: add wrappers for attaching/detaching from HCS process

This commit is contained in:
Jakub Konka
2023-03-17 14:51:08 +01:00
parent 37192bcdcb
commit 0aab3bda12
2 changed files with 27 additions and 18 deletions

View File

@@ -392,19 +392,8 @@ pub const File = struct {
.linux => std.os.ptrace(std.os.linux.PTRACE.ATTACH, pid, 0, 0) catch |err| {
log.warn("ptrace failure: {s}", .{@errorName(err)});
},
.macos => {
const macho = base.cast(MachO).?;
if (macho.mach_task == null) {
if (std.os.darwin.machTaskForPid(pid)) |task| {
macho.mach_task = task;
// TODO enable ones we register for exceptions
// std.os.ptrace(std.os.darwin.PT.ATTACHEXC, pid, 0, 0) catch |err| {
// log.warn("ptrace failure: {s}", .{@errorName(err)});
// };
} else |err| {
log.warn("failed to acquire Mach task for child process: {s}", .{@errorName(err)});
}
}
.macos => base.cast(MachO).?.ptraceAttach(pid) catch |err| {
log.warn("attaching failed with error: {s}", .{@errorName(err)});
},
else => return error.HotSwapUnavailableOnHostOperatingSystem,
}
@@ -444,11 +433,9 @@ pub const File = struct {
.linux => std.os.ptrace(std.os.linux.PTRACE.DETACH, pid, 0, 0) catch |err| {
log.warn("ptrace failure: {s}", .{@errorName(err)});
},
.macos => {},
// TODO see comment above in makeWritable
// .macos => std.os.ptrace(std.os.darwin.PT.DETACH, pid, 0, 0) catch |err| {
// log.warn("ptrace failure: {s}", .{@errorName(err)});
// },
.macos => base.cast(MachO).?.ptraceDetach(pid) catch |err| {
log.warn("detaching failed with error: {s}", .{@errorName(err)});
},
else => return error.HotSwapUnavailableOnHostOperatingSystem,
}
}

View File

@@ -3811,6 +3811,28 @@ pub fn allocatedVirtualSize(self: *MachO, start: u64) u64 {
return min_pos - start;
}
pub fn ptraceAttach(self: *MachO, pid: std.os.pid_t) !void {
const mach_task = try std.os.darwin.machTaskForPid(pid);
log.debug("Mach task for pid {d}: {any}", .{ pid, mach_task });
self.mach_task = mach_task;
// TODO start exception handler in another thread
// TODO enable ones we register for exceptions
// try std.os.ptrace(std.os.darwin.PT.ATTACHEXC, pid, 0, 0);
}
pub fn ptraceDetach(self: *MachO, pid: std.os.pid_t) !void {
_ = pid;
// TODO stop exception handler
// TODO see comment in ptraceAttach
// try std.os.ptrace(std.os.darwin.PT.DETACH, pid, 0, 0);
self.mach_task = null;
}
pub fn makeStaticString(bytes: []const u8) [16]u8 {
var buf = [_]u8{0} ** 16;
assert(bytes.len <= buf.len);