Skip to content

Commit ec2697b

Browse files
committed
darwin: add even more wrappers for Mach syscalls
Rename `ThreadId` to `MachThread`.
1 parent f5336a8 commit ec2697b

3 files changed

Lines changed: 38 additions & 10 deletions

File tree

lib/std/c/darwin.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ pub const MACH_EXCEPTION_MASK = MACH_EXCEPTION_CODES |
102102

103103
pub const TASK_NULL: task_t = 0;
104104
pub const THREAD_NULL: thread_t = 0;
105+
pub const MACH_PORT_NULL: mach_port_t = 0;
106+
pub const MACH_MSG_TIMEOUT_NONE: mach_msg_timeout_t = 0;
105107

106108
pub const MACH_MSG_OPTION_NONE = 0x00000000;
107109

@@ -406,6 +408,7 @@ pub extern "c" fn task_resume(target_task: task_read_t) kern_return_t;
406408
pub extern "c" fn task_suspend(target_task: task_read_t) kern_return_t;
407409

408410
pub extern "c" fn task_for_pid(target_tport: mach_port_name_t, pid: pid_t, t: *mach_port_name_t) kern_return_t;
411+
pub extern "c" fn pid_for_task(target_tport: mach_port_name_t, pid: *pid_t) kern_return_t;
409412
pub extern "c" fn mach_vm_read(
410413
target_task: vm_map_read_t,
411414
address: mach_vm_address_t,
@@ -3054,11 +3057,20 @@ pub const _POSIX_SPAWN_RESLIDE = 0x0800;
30543057
pub const POSIX_SPAWN_CLOEXEC_DEFAULT = 0x4000;
30553058

30563059
pub const PT_TRACE_ME = 0;
3060+
pub const PT_READ_I = 1;
3061+
pub const PT_READ_D = 2;
3062+
pub const PT_READ_U = 3;
3063+
pub const PT_WRITE_I = 4;
3064+
pub const PT_WRITE_D = 5;
3065+
pub const PT_WRITE_U = 6;
30573066
pub const PT_CONTINUE = 7;
30583067
pub const PT_KILL = 8;
30593068
pub const PT_STEP = 9;
30603069
pub const PT_DETACH = 11;
3070+
pub const PT_SIGEXC = 12;
3071+
pub const PT_THUPDATE = 13;
30613072
pub const PT_ATTACHEXC = 14;
3073+
pub const PT_FORCEQUOTA = 30;
30623074
pub const PT_DENY_ATTACH = 31;
30633075

30643076
pub const caddr_t = ?[*]u8;

lib/std/os/darwin.zig

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ const mach_task = if (builtin.target.isDarwin()) struct {
2424
return self.port != std.c.TASK_NULL;
2525
}
2626

27+
pub fn pidForTask(self: MachTask) MachError!std.os.pid_t {
28+
var pid: std.os.pid_t = undefined;
29+
switch (std.c.getKernError(std.c.pid_for_task(self.port, &pid))) {
30+
.SUCCESS => return pid,
31+
.FAILURE => return error.PermissionDenied,
32+
else => |err| {
33+
log.err("pid_for_task kernel call failed with error code: {s}", .{@tagName(err)});
34+
return error.Unexpected;
35+
},
36+
}
37+
}
38+
2739
pub fn allocatePort(self: MachTask, right: std.c.MACH_PORT_RIGHT) MachError!MachTask {
2840
var out_port: std.c.mach_port_name_t = undefined;
2941
switch (std.c.getKernError(std.c.mach_port_allocate(
@@ -440,7 +452,7 @@ const mach_task = if (builtin.target.isDarwin()) struct {
440452
}
441453

442454
const ThreadList = struct {
443-
buf: []ThreadId,
455+
buf: []MachThread,
444456

445457
pub fn deinit(list: ThreadList) void {
446458
const self_task = machTaskForSelf();
@@ -456,7 +468,7 @@ const mach_task = if (builtin.target.isDarwin()) struct {
456468
var thread_list: std.c.mach_port_array_t = undefined;
457469
var thread_count: std.c.mach_msg_type_number_t = undefined;
458470
switch (std.c.getKernError(std.c.task_threads(task.port, &thread_list, &thread_count))) {
459-
.SUCCESS => return ThreadList{ .buf = @ptrCast([*]ThreadId, thread_list)[0..thread_count] },
471+
.SUCCESS => return ThreadList{ .buf = @ptrCast([*]MachThread, thread_list)[0..thread_count] },
460472
else => |err| {
461473
log.err("task_threads kernel call failed with error code: {s}", .{@tagName(err)});
462474
return error.Unexpected;
@@ -465,14 +477,18 @@ const mach_task = if (builtin.target.isDarwin()) struct {
465477
}
466478
};
467479

468-
pub const ThreadId = extern struct {
469-
id: std.c.thread_act_t,
480+
pub const MachThread = extern struct {
481+
port: std.c.mach_port_t,
482+
483+
pub fn isValid(thread: MachThread) bool {
484+
return thread.port != std.c.THREAD_NULL;
485+
}
470486

471-
pub fn getBasicInfo(thread_id: ThreadId) MachError!std.c.thread_basic_info {
487+
pub fn getBasicInfo(thread: MachThread) MachError!std.c.thread_basic_info {
472488
var info: std.c.thread_basic_info = undefined;
473489
var count = std.c.THREAD_BASIC_INFO_COUNT;
474490
switch (std.c.getKernError(std.c.thread_info(
475-
thread_id.id,
491+
thread.port,
476492
std.c.THREAD_BASIC_INFO,
477493
@ptrCast(std.c.thread_info_t, &info),
478494
&count,
@@ -485,11 +501,11 @@ const mach_task = if (builtin.target.isDarwin()) struct {
485501
}
486502
}
487503

488-
pub fn getIdentifierInfo(thread_id: ThreadId) MachError!std.c.thread_identifier_info {
504+
pub fn getIdentifierInfo(thread: MachThread) MachError!std.c.thread_identifier_info {
489505
var info: std.c.thread_identifier_info = undefined;
490506
var count = std.c.THREAD_IDENTIFIER_INFO_COUNT;
491507
switch (std.c.getKernError(std.c.thread_info(
492-
thread_id.id,
508+
thread.port,
493509
std.c.THREAD_IDENTIFIER_INFO,
494510
@ptrCast(std.c.thread_info_t, &info),
495511
&count,

lib/std/os/ptrace.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const ptrace = if (builtin.target.isDarwin()) struct {
1616
PermissionDenied,
1717
} || UnexpectedError;
1818

19-
pub fn ptrace(request: i32, pid: pid_t) PtraceError!void {
20-
switch (errno(system.ptrace(request, pid, null, 0))) {
19+
pub fn ptrace(request: i32, pid: pid_t, addr: ?[*]u8, signal: i32) PtraceError!void {
20+
switch (errno(system.ptrace(request, pid, addr, signal))) {
2121
.SUCCESS => return,
2222
.SRCH => return error.ProcessNotFound,
2323
.INVAL => unreachable,

0 commit comments

Comments
 (0)