| ... | @@ -24,6 +24,18 @@ const mach_task = if (builtin.target.isDarwin()) struct { | ... | @@ -24,6 +24,18 @@ const mach_task = if (builtin.target.isDarwin()) struct { |
| 24 | return self.port != std.c.TASK_NULL; | 24 | return self.port != std.c.TASK_NULL; |
| 25 | } | 25 | } |
| 26 | | 26 | |
| | 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 | |
| 27 | pub fn allocatePort(self: MachTask, right: std.c.MACH_PORT_RIGHT) MachError!MachTask { | 39 | pub fn allocatePort(self: MachTask, right: std.c.MACH_PORT_RIGHT) MachError!MachTask { |
| 28 | var out_port: std.c.mach_port_name_t = undefined; | 40 | var out_port: std.c.mach_port_name_t = undefined; |
| 29 | switch (std.c.getKernError(std.c.mach_port_allocate( | 41 | switch (std.c.getKernError(std.c.mach_port_allocate( |
| ... | @@ -440,7 +452,7 @@ const mach_task = if (builtin.target.isDarwin()) struct { | ... | @@ -440,7 +452,7 @@ const mach_task = if (builtin.target.isDarwin()) struct { |
| 440 | } | 452 | } |
| 441 | | 453 | |
| 442 | const ThreadList = struct { | 454 | const ThreadList = struct { |
| 443 | buf: []ThreadId, | 455 | buf: []MachThread, |
| 444 | | 456 | |
| 445 | pub fn deinit(list: ThreadList) void { | 457 | pub fn deinit(list: ThreadList) void { |
| 446 | const self_task = machTaskForSelf(); | 458 | const self_task = machTaskForSelf(); |
| ... | @@ -456,7 +468,7 @@ const mach_task = if (builtin.target.isDarwin()) struct { | ... | @@ -456,7 +468,7 @@ const mach_task = if (builtin.target.isDarwin()) struct { |
| 456 | var thread_list: std.c.mach_port_array_t = undefined; | 468 | var thread_list: std.c.mach_port_array_t = undefined; |
| 457 | var thread_count: std.c.mach_msg_type_number_t = undefined; | 469 | var thread_count: std.c.mach_msg_type_number_t = undefined; |
| 458 | switch (std.c.getKernError(std.c.task_threads(task.port, &thread_list, &thread_count))) { | 470 | 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] }, |
| 460 | else => |err| { | 472 | else => |err| { |
| 461 | log.err("task_threads kernel call failed with error code: {s}", .{@tagName(err)}); | 473 | log.err("task_threads kernel call failed with error code: {s}", .{@tagName(err)}); |
| 462 | return error.Unexpected; | 474 | return error.Unexpected; |
| ... | @@ -465,14 +477,18 @@ const mach_task = if (builtin.target.isDarwin()) struct { | ... | @@ -465,14 +477,18 @@ const mach_task = if (builtin.target.isDarwin()) struct { |
| 465 | } | 477 | } |
| 466 | }; | 478 | }; |
| 467 | | 479 | |
| 468 | pub const ThreadId = extern struct { | 480 | pub const MachThread = extern struct { |
| 469 | id: std.c.thread_act_t, | 481 | port: std.c.mach_port_t, |
| | 482 | |
| | 483 | pub fn isValid(thread: MachThread) bool { |
| | 484 | return thread.port != std.c.THREAD_NULL; |
| | 485 | } |
| 470 | | 486 | |
| 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 { |
| 472 | var info: std.c.thread_basic_info = undefined; | 488 | var info: std.c.thread_basic_info = undefined; |
| 473 | var count = std.c.THREAD_BASIC_INFO_COUNT; | 489 | var count = std.c.THREAD_BASIC_INFO_COUNT; |
| 474 | switch (std.c.getKernError(std.c.thread_info( | 490 | switch (std.c.getKernError(std.c.thread_info( |
| 475 | thread_id.id, | 491 | thread.port, |
| 476 | std.c.THREAD_BASIC_INFO, | 492 | std.c.THREAD_BASIC_INFO, |
| 477 | @ptrCast(std.c.thread_info_t, &info), | 493 | @ptrCast(std.c.thread_info_t, &info), |
| 478 | &count, | 494 | &count, |
| ... | @@ -485,11 +501,11 @@ const mach_task = if (builtin.target.isDarwin()) struct { | ... | @@ -485,11 +501,11 @@ const mach_task = if (builtin.target.isDarwin()) struct { |
| 485 | } | 501 | } |
| 486 | } | 502 | } |
| 487 | | 503 | |
| 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 { |
| 489 | var info: std.c.thread_identifier_info = undefined; | 505 | var info: std.c.thread_identifier_info = undefined; |
| 490 | var count = std.c.THREAD_IDENTIFIER_INFO_COUNT; | 506 | var count = std.c.THREAD_IDENTIFIER_INFO_COUNT; |
| 491 | switch (std.c.getKernError(std.c.thread_info( | 507 | switch (std.c.getKernError(std.c.thread_info( |
| 492 | thread_id.id, | 508 | thread.port, |
| 493 | std.c.THREAD_IDENTIFIER_INFO, | 509 | std.c.THREAD_IDENTIFIER_INFO, |
| 494 | @ptrCast(std.c.thread_info_t, &info), | 510 | @ptrCast(std.c.thread_info_t, &info), |
| 495 | &count, | 511 | &count, |