3
3
// See license.txt file in the project root for full license information.
4
4
5
5
using System . Diagnostics . CodeAnalysis ;
6
+ using System . Runtime . CompilerServices ;
6
7
using System . Runtime . InteropServices ;
7
- using static Ultra . Sampler . libSystem ;
8
+ using static Ultra . Sampler . MacOS . MacOSLibSystem ;
8
9
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
9
10
10
- namespace Ultra . Sampler ;
11
+ namespace Ultra . Sampler . MacOS ;
11
12
12
- using task_t = mach_port_t ;
13
- using task_name_t = mach_port_t ;
14
- using task_policy_set_t = mach_port_t ;
15
- using task_policy_get_t = mach_port_t ;
16
13
using task_inspect_t = mach_port_t ;
17
- using task_read_t = mach_port_t ;
18
- using task_suspension_token_t = mach_port_t ;
19
- using thread_t = mach_port_t ;
20
14
using thread_act_t = mach_port_t ;
21
15
using thread_inspect_t = mach_port_t ;
22
- using thread_read_t = mach_port_t ;
23
- using ipc_space_t = mach_port_t ;
24
- using ipc_space_read_t = mach_port_t ;
25
- using ipc_space_inspect_t = mach_port_t ;
26
- using coalition_t = mach_port_t ;
27
- using host_t = mach_port_t ;
28
- using host_priv_t = mach_port_t ;
29
- using host_security_t = mach_port_t ;
30
- using processor_t = mach_port_t ;
31
- using processor_set_t = mach_port_t ;
32
- using processor_set_control_t = mach_port_t ;
33
- using semaphore_t = mach_port_t ;
34
- using lock_set_t = mach_port_t ;
35
- using ledger_t = mach_port_t ;
36
- using alarm_t = mach_port_t ;
37
- using clock_serv_t = mach_port_t ;
38
- using clock_ctrl_t = mach_port_t ;
39
- using arcade_register_t = mach_port_t ;
40
- using ipc_eventlink_t = mach_port_t ;
41
- using suid_cred_t = mach_port_t ;
42
- using task_id_token_t = mach_port_t ;
43
16
44
17
[ SuppressMessage ( "ReSharper" , "InconsistentNaming" ) ]
45
- internal static partial class libSystem
18
+ internal static partial class MacOSLibSystem
46
19
{
47
20
public const int EPERM = 1 ;
48
21
@@ -78,10 +51,16 @@ internal static partial class libSystem
78
51
79
52
[ LibraryImport ( LibSystem ) ]
80
53
public static unsafe partial return_t task_info ( mach_port_t host , uint flavor , task_dyld_info * task_info , ref /*uint*/ int task_info_count ) ;
81
-
54
+
82
55
[ LibraryImport ( LibSystem ) ]
83
56
public static unsafe partial return_t task_threads ( task_inspect_t target_task , thread_act_t * * act_list , out uint act_list_count ) ;
84
57
58
+ [ LibraryImport ( LibSystem ) ]
59
+ public static unsafe partial return_t task_suspend ( task_inspect_t target_task ) ;
60
+
61
+ [ LibraryImport ( LibSystem ) ]
62
+ public static unsafe partial return_t task_resume ( task_inspect_t target_task ) ;
63
+
85
64
[ LibraryImport ( LibSystem ) ]
86
65
public static partial return_t thread_suspend ( thread_act_t target_act ) ;
87
66
@@ -93,10 +72,10 @@ internal static partial class libSystem
93
72
94
73
[ LibraryImport ( LibSystem ) ]
95
74
public static partial return_t thread_get_state ( thread_inspect_t target_act , uint flavor , /*uint**/ nint old_state , ref /*uint*/ int old_state_count ) ;
96
-
75
+
97
76
[ LibraryImport ( LibSystem ) ]
98
77
public static partial nint pthread_from_mach_thread_np ( thread_act_t target_act ) ;
99
-
78
+
100
79
[ LibraryImport ( LibSystem ) ]
101
80
public static partial return_t pthread_threadid_np ( nint thread , out ulong threadId ) ;
102
81
@@ -112,6 +91,18 @@ public static partial int mach_vm_region(int target_task, ref /*UIntPtr*/ulong a
112
91
out /*UIntPtr*/ ulong size , int flavor , out /*int*/ vm_region_basic_info_64 info , ref /*uint*/ int info_count ,
113
92
out int object_name ) ;
114
93
94
+ [ LibraryImport ( LibSystem ) ]
95
+ public static partial return_t mach_vm_remap (
96
+ int target_task ,
97
+ ref /*UIntPtr*/ ulong address ,
98
+ /*UIntPtr*/ ulong size ,
99
+ /*UIntPtr*/ ulong mask ,
100
+ int flags ,
101
+ int src_task ,
102
+ /*UIntPtr*/ ulong src_address ,
103
+ int copy ,
104
+ out /*UIntPtr*/ ulong new_address ) ;
105
+
115
106
[ LibraryImport ( LibSystem ) ]
116
107
public static partial int mach_vm_deallocate ( int target_task , /*UIntPtr*/ ulong address , /*UIntPtr*/ ulong size ) ;
117
108
@@ -121,6 +112,23 @@ public static partial int mach_vm_region(int target_task, ref /*UIntPtr*/ulong a
121
112
[ LibraryImport ( LibSystem ) ]
122
113
public static partial int waitpid ( int pid , IntPtr status , int options ) ;
123
114
115
+ public unsafe delegate void dyld_register_callback ( mach_header * mh , nint vmaddr_slide ) ;
116
+
117
+ [ LibraryImport ( LibSystem ) ]
118
+ public static partial void _dyld_register_func_for_add_image ( nint callback ) ;
119
+
120
+ [ LibraryImport ( LibSystem ) ]
121
+ public static partial void _dyld_register_func_for_remove_image ( nint callback ) ;
122
+
123
+ [ LibraryImport ( LibSystem ) ]
124
+ public static partial int dladdr ( nint address , out dl_info info ) ;
125
+
126
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
127
+ public static nint mach_vm_trunk_page ( nint addr ) => addr & ~ ( Environment . SystemPageSize - 1 ) ;
128
+
129
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
130
+ public static nint mach_vm_round_page ( nint addr ) => ( addr + Environment . SystemPageSize - 1 ) & ~ ( Environment . SystemPageSize - 1 ) ;
131
+
124
132
public unsafe struct dyld_all_image_infos
125
133
{
126
134
public readonly uint version ;
@@ -179,7 +187,7 @@ public readonly struct dyld_image_info
179
187
180
188
public readonly unsafe struct task_dyld_info
181
189
{
182
- public readonly dyld_all_image_infos * all_image_info_addr ;
190
+ public readonly nint all_image_info_addr ;
183
191
public readonly nint all_image_info_size ;
184
192
public readonly int all_image_info_format ;
185
193
}
@@ -278,4 +286,65 @@ public struct MemoryRegion //: IRegion
278
286
279
287
public bool IsReadable => ( Permission & PROT_READ ) != 0 ;
280
288
}
289
+
290
+ public const uint MH_MAGIC_64 = 0xfeedfacf ;
291
+ public const uint MH_MAGIC = 0xfeedface ;
292
+
293
+ /*
294
+ * Structure filled in by dladdr().
295
+ */
296
+ public struct dl_info
297
+ {
298
+ public nint dli_fname ; /* Pathname of shared object */
299
+ public nint dli_fbase ; /* Base address of shared object */
300
+ public nint dli_sname ; /* Name of nearest symbol */
301
+ public nint dli_saddr ; /* Address of nearest symbol */
302
+ }
303
+
304
+ /*
305
+ * The 32-bit mach header appears at the very beginning of the object file for
306
+ * 32-bit architectures.
307
+ */
308
+ public struct mach_header {
309
+ public uint magic ; /* mach magic number identifier */
310
+ public int cputype ; /* cpu specifier */
311
+ public int cpusubtype ; /* machine specifier */
312
+ public uint filetype ; /* type of file */
313
+ public uint ncmds ; /* number of load commands */
314
+ public uint sizeofcmds ; /* the size of all the load commands */
315
+ public uint flags ; /* flags */
316
+ } ;
317
+
318
+ /*
319
+ * The 64-bit mach header appears at the very beginning of object files for
320
+ * 64-bit architectures.
321
+ */
322
+ public struct mach_header_64 {
323
+ public uint magic ; /* mach magic number identifier */
324
+ public int cputype ; /* cpu specifier */
325
+ public int cpusubtype ; /* machine specifier */
326
+ public uint filetype ; /* type of file */
327
+ public uint ncmds ; /* number of load commands */
328
+ public uint sizeofcmds ; /* the size of all the load commands */
329
+ public uint flags ; /* flags */
330
+ public uint reserved ; /* reserved */
331
+ } ;
332
+
333
+ public struct load_command {
334
+ public uint cmd ; /* type of load command */
335
+ public uint cmdsize ; /* total size of command in bytes */
336
+ } ;
337
+
338
+ /*
339
+ * The uuid load command contains a single 128-bit unique random number that
340
+ * identifies an object produced by the static link editor.
341
+ */
342
+ public struct uuid_command {
343
+ public uint cmd ; /* LC_UUID */
344
+ public uint cmdsize ; /* sizeof(struct uuid_command) */
345
+ public Guid uuid ; /* the 128-bit uuid */
346
+ } ;
347
+
348
+ public const uint LC_UUID = 0x1b ;
349
+
281
350
}
0 commit comments