Skip to content

Commit 7a457db

Browse files
committed
Add support for native modules events
1 parent 829faa8 commit 7a457db

9 files changed

+538
-168
lines changed

src/Directory.Packages.props

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<PackageVersion Include="Spectre.Console" Version="0.49.1" />
1414
<PackageVersion Include="Verify.DiffPlex" Version="3.1.2" />
1515
<PackageVersion Include="Verify.MSTest" Version="28.4.0" />
16+
<PackageVersion Include="XenoAtom.Collections" Version="1.2.0" />
1617
<PackageVersion Include="XenoAtom.CommandLine" Version="1.0.1" />
1718
</ItemGroup>
1819
</Project>

src/Ultra.Sampler/libSystem.cs src/Ultra.Sampler/MacOS/MacOSLibSystem.cs

+104-35
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,19 @@
33
// See license.txt file in the project root for full license information.
44

55
using System.Diagnostics.CodeAnalysis;
6+
using System.Runtime.CompilerServices;
67
using System.Runtime.InteropServices;
7-
using static Ultra.Sampler.libSystem;
8+
using static Ultra.Sampler.MacOS.MacOSLibSystem;
89
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
910

10-
namespace Ultra.Sampler;
11+
namespace Ultra.Sampler.MacOS;
1112

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;
1613
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;
2014
using thread_act_t = mach_port_t;
2115
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;
4316

4417
[SuppressMessage("ReSharper", "InconsistentNaming")]
45-
internal static partial class libSystem
18+
internal static partial class MacOSLibSystem
4619
{
4720
public const int EPERM = 1;
4821

@@ -78,10 +51,16 @@ internal static partial class libSystem
7851

7952
[LibraryImport(LibSystem)]
8053
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+
8255
[LibraryImport(LibSystem)]
8356
public static unsafe partial return_t task_threads(task_inspect_t target_task, thread_act_t** act_list, out uint act_list_count);
8457

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+
8564
[LibraryImport(LibSystem)]
8665
public static partial return_t thread_suspend(thread_act_t target_act);
8766

@@ -93,10 +72,10 @@ internal static partial class libSystem
9372

9473
[LibraryImport(LibSystem)]
9574
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+
9776
[LibraryImport(LibSystem)]
9877
public static partial nint pthread_from_mach_thread_np(thread_act_t target_act);
99-
78+
10079
[LibraryImport(LibSystem)]
10180
public static partial return_t pthread_threadid_np(nint thread, out ulong threadId);
10281

@@ -112,6 +91,18 @@ public static partial int mach_vm_region(int target_task, ref /*UIntPtr*/ulong a
11291
out /*UIntPtr*/ulong size, int flavor, out /*int*/vm_region_basic_info_64 info, ref /*uint*/int info_count,
11392
out int object_name);
11493

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+
115106
[LibraryImport(LibSystem)]
116107
public static partial int mach_vm_deallocate(int target_task, /*UIntPtr*/ulong address, /*UIntPtr*/ulong size);
117108

@@ -121,6 +112,23 @@ public static partial int mach_vm_region(int target_task, ref /*UIntPtr*/ulong a
121112
[LibraryImport(LibSystem)]
122113
public static partial int waitpid(int pid, IntPtr status, int options);
123114

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+
124132
public unsafe struct dyld_all_image_infos
125133
{
126134
public readonly uint version;
@@ -179,7 +187,7 @@ public readonly struct dyld_image_info
179187

180188
public readonly unsafe struct task_dyld_info
181189
{
182-
public readonly dyld_all_image_infos* all_image_info_addr;
190+
public readonly nint all_image_info_addr;
183191
public readonly nint all_image_info_size;
184192
public readonly int all_image_info_format;
185193
}
@@ -278,4 +286,65 @@ public struct MemoryRegion //: IRegion
278286

279287
public bool IsReadable => (Permission & PROT_READ) != 0;
280288
}
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+
281350
}

0 commit comments

Comments
 (0)