| ... | @@ -17,11 +17,11 @@ const mach_task = if (builtin.target.isDarwin()) struct { | ... | @@ -17,11 +17,11 @@ const mach_task = if (builtin.target.isDarwin()) struct { |
| 17 | Unexpected, | 17 | Unexpected, |
| 18 | }; | 18 | }; |
| 19 | | 19 | |
| 20 | pub const MachTask = struct { | 20 | pub const MachTask = extern struct { |
| 21 | port: std.c.mach_port_name_t, | 21 | port: std.c.mach_port_name_t, |
| 22 | | 22 | |
| 23 | pub fn isValid(self: MachTask) bool { | 23 | pub fn isValid(self: MachTask) bool { |
| 24 | return self.port != 0; | 24 | return self.port != std.c.TASK_NULL; |
| 25 | } | 25 | } |
| 26 | | 26 | |
| 27 | pub fn allocatePort(self: MachTask, right: std.c.MACH_PORT_RIGHT) MachError!MachTask { | 27 | pub fn allocatePort(self: MachTask, right: std.c.MACH_PORT_RIGHT) MachError!MachTask { |
| ... | @@ -439,11 +439,11 @@ const mach_task = if (builtin.target.isDarwin()) struct { | ... | @@ -439,11 +439,11 @@ const mach_task = if (builtin.target.isDarwin()) struct { |
| 439 | } | 439 | } |
| 440 | } | 440 | } |
| 441 | | 441 | |
| 442 | pub fn getThreads(task: MachTask) MachError![]std.c.mach_port_t { | 442 | pub fn getThreads(task: MachTask) MachError![]ThreadId { |
| 443 | var thread_list: std.c.mach_port_array_t = undefined; | 443 | var thread_list: std.c.mach_port_array_t = undefined; |
| 444 | var thread_count: std.c.mach_msg_type_number_t = undefined; | 444 | var thread_count: std.c.mach_msg_type_number_t = undefined; |
| 445 | switch (std.c.getKernError(std.c.task_threads(task.port, &thread_list, &thread_count))) { | 445 | switch (std.c.getKernError(std.c.task_threads(task.port, &thread_list, &thread_count))) { |
| 446 | .SUCCESS => return thread_list[0..thread_count], | 446 | .SUCCESS => return @ptrCast([*]ThreadId, thread_list)[0..thread_count], |
| 447 | else => |err| { | 447 | else => |err| { |
| 448 | log.err("task_threads kernel call failed with error code: {s}", .{@tagName(err)}); | 448 | log.err("task_threads kernel call failed with error code: {s}", .{@tagName(err)}); |
| 449 | return error.Unexpected; | 449 | return error.Unexpected; |
| ... | @@ -452,6 +452,44 @@ const mach_task = if (builtin.target.isDarwin()) struct { | ... | @@ -452,6 +452,44 @@ const mach_task = if (builtin.target.isDarwin()) struct { |
| 452 | } | 452 | } |
| 453 | }; | 453 | }; |
| 454 | | 454 | |
| | 455 | pub const ThreadId = extern struct { |
| | 456 | id: std.c.thread_act_t, |
| | 457 | |
| | 458 | pub fn getBasicInfo(thread_id: ThreadId) MachError!std.c.thread_basic_info { |
| | 459 | var info: std.c.thread_basic_info = undefined; |
| | 460 | var count = std.c.THREAD_BASIC_INFO_COUNT; |
| | 461 | switch (std.c.getKernError(std.c.thread_info( |
| | 462 | thread_id.id, |
| | 463 | std.c.THREAD_BASIC_INFO, |
| | 464 | @ptrCast(std.c.thread_info_t, &info), |
| | 465 | &count, |
| | 466 | ))) { |
| | 467 | .SUCCESS => return info, |
| | 468 | else => |err| { |
| | 469 | log.err("thread_info kernel call failed with error code: {s}", .{@tagName(err)}); |
| | 470 | return error.Unexpected; |
| | 471 | }, |
| | 472 | } |
| | 473 | } |
| | 474 | |
| | 475 | pub fn getIdentifierInfo(thread_id: ThreadId) MachError!std.c.thread_identifier_info { |
| | 476 | var info: std.c.thread_identifier_info = undefined; |
| | 477 | var count = std.c.THREAD_IDENTIFIER_INFO_COUNT; |
| | 478 | switch (std.c.getKernError(std.c.thread_info( |
| | 479 | thread_id.id, |
| | 480 | std.c.THREAD_IDENTIFIER_INFO, |
| | 481 | @ptrCast(std.c.thread_info_t, &info), |
| | 482 | &count, |
| | 483 | ))) { |
| | 484 | .SUCCESS => return info, |
| | 485 | else => |err| { |
| | 486 | log.err("thread_info kernel call failed with error code: {s}", .{@tagName(err)}); |
| | 487 | return error.Unexpected; |
| | 488 | }, |
| | 489 | } |
| | 490 | } |
| | 491 | }; |
| | 492 | |
| 455 | pub fn machTaskForPid(pid: std.os.pid_t) MachError!MachTask { | 493 | pub fn machTaskForPid(pid: std.os.pid_t) MachError!MachTask { |
| 456 | var port: std.c.mach_port_name_t = undefined; | 494 | var port: std.c.mach_port_name_t = undefined; |
| 457 | switch (std.c.getKernError(std.c.task_for_pid(std.c.mach_task_self(), pid, &port))) { | 495 | switch (std.c.getKernError(std.c.task_for_pid(std.c.mach_task_self(), pid, &port))) { |