diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2022-12-10 23:36:36 +0100 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2022-12-10 23:36:36 +0100 |
| commit | 35e3069ab7af0a5fd65c5fe8e35b01458dbcb132 (patch) | |
| tree | 95b3efb3d74726be1da81f11fbf4dbecb35952fa /lib/std | |
| parent | c9d763502fb1ff9ee76f0745c493e65c1333b1b1 (diff) | |
| download | zig-35e3069ab7af0a5fd65c5fe8e35b01458dbcb132.tar.gz zig-35e3069ab7af0a5fd65c5fe8e35b01458dbcb132.zip | |
darwin: expose ptrace with errno handling
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/os.zig | 1 | ||||
| -rw-r--r-- | lib/std/os/ptrace.zig | 28 |
2 files changed, 29 insertions, 0 deletions
diff --git a/lib/std/os.zig b/lib/std/os.zig index f13ee03a96..df03903731 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -42,6 +42,7 @@ pub const uefi = @import("os/uefi.zig"); pub const wasi = @import("os/wasi.zig"); pub const windows = @import("os/windows.zig"); pub const posix_spawn = @import("os/posix_spawn.zig"); +pub const ptrace = @import("os/ptrace.zig"); comptime { assert(@import("std") == std); // std lib tests require --zig-lib-dir diff --git a/lib/std/os/ptrace.zig b/lib/std/os/ptrace.zig new file mode 100644 index 0000000000..4168ed0032 --- /dev/null +++ b/lib/std/os/ptrace.zig @@ -0,0 +1,28 @@ +const std = @import("std"); +const builtin = @import("builtin"); + +const os = @import("../os.zig"); +const system = os.system; +const errno = system.getErrno; +const pid_t = system.pid_t; +const unexpectedErrno = os.unexpectedErrno; +const UnexpectedError = os.UnexpectedError; + +pub usingnamespace ptrace; + +const ptrace = if (builtin.target.isDarwin()) struct { + pub const PtraceError = error{ + ProcessNotFound, + PermissionDenied, + } || UnexpectedError; + + pub fn ptrace(request: i32, pid: pid_t) PtraceError!void { + switch (errno(system.ptrace(request, pid, null, 0))) { + .SUCCESS => return, + .SRCH => return error.ProcessNotFound, + .INVAL => unreachable, + .BUSY, .PERM => return error.PermissionDenied, + else => |err| return unexpectedErrno(err), + } + } +} else struct {}; |
