authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-16 12:13:19+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-18 21:52:40+01:00
loga23ef3783bb5357376acdce12f73b0285636d6cf
tree98ee1401d93a964c4c0293d9b23762002166690f
parent2ac8d90df05fdbb59a0ee9ff6609a058185f66ff

os.zig: expose ptrace wrapper for darwin and linux


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

lib/std/os.zig+40-13
...@@ -7129,22 +7129,49 @@ pub fn timerfd_gettime(fd: i32) TimerFdGetError!linux.itimerspec {...@@ -7129,22 +7129,49 @@ pub fn timerfd_gettime(fd: i32) TimerFdGetError!linux.itimerspec {
71297129
7130pub const PtraceError = error{7130pub const PtraceError = error{
7131 DeviceBusy,7131 DeviceBusy,
7132 InputOutput,
7133 Overflow,
7132 ProcessNotFound,7134 ProcessNotFound,
7133 PermissionDenied,7135 PermissionDenied,
7134} || UnexpectedError;7136} || UnexpectedError;
71357137
7136/// TODO on other OSes7138pub fn ptrace(request: u32, pid: pid_t, addr: usize, signal: usize) PtraceError!void {
7137pub fn ptrace(request: i32, pid: pid_t, addr: ?[*]u8, signal: i32) PtraceError!void {7139 if (builtin.os.tag == .windows or builtin.os.tag == .wasi)
7138 switch (builtin.os.tag) {7140 @compileError("Unsupported OS");
7139 .macos, .ios, .tvos, .watchos => {},7141
7140 else => @compileError("TODO implement ptrace"),7142 return switch (builtin.os.tag) {
7141 }7143 .linux => switch (errno(linux.ptrace(request, pid, addr, signal, 0))) {
7142 return switch (errno(system.ptrace(request, pid, addr, signal))) {7144 .SUCCESS => {},
7143 .SUCCESS => {},7145 .SRCH => error.ProcessNotFound,
7144 .SRCH => error.ProcessNotFound,7146 .FAULT => unreachable,
7145 .INVAL => unreachable,7147 .INVAL => unreachable,
7146 .PERM => error.PermissionDenied,7148 .IO => return error.InputOutput,
7147 .BUSY => error.DeviceBusy,7149 .PERM => error.PermissionDenied,
7148 else => |err| return unexpectedErrno(err),7150 .BUSY => error.DeviceBusy,
7151 else => |err| return unexpectedErrno(err),
7152 },
7153
7154 .macos, .ios, .tvos, .watchos => switch (errno(darwin.ptrace(
7155 math.cast(i32, request) orelse return error.Overflow,
7156 pid,
7157 @intToPtr(?[*]u8, addr),
7158 math.cast(i32, signal) orelse return error.Overflow,
7159 ))) {
7160 .SUCCESS => {},
7161 .SRCH => error.ProcessNotFound,
7162 .INVAL => unreachable,
7163 .PERM => error.PermissionDenied,
7164 .BUSY => error.DeviceBusy,
7165 else => |err| return unexpectedErrno(err),
7166 },
7167
7168 else => switch (errno(system.ptrace(request, pid, addr, signal))) {
7169 .SUCCESS => {},
7170 .SRCH => error.ProcessNotFound,
7171 .INVAL => unreachable,
7172 .PERM => error.PermissionDenied,
7173 .BUSY => error.DeviceBusy,
7174 else => |err| return unexpectedErrno(err),
7175 },
7149 };7176 };
7150}7177}