Skip to content

Commit d9add41

Browse files
committed
Add Ultra.ProcessHook
1 parent bd538c3 commit d9add41

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/Ultra.Core/Ultra.Core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
<PrivateAssets>all</PrivateAssets>
3939
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4040
</PackageReference>
41+
<ProjectReference Include="..\Ultra.ProcessHook\Ultra.ProcessHook.csproj" />
4142
</ItemGroup>
4243
</Project>

src/Ultra.ProcessHook/StartupHook.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
using System;
5+
using System.Runtime.InteropServices;
6+
7+
/// <summary>
8+
/// This class is used to load the native library <c>libUltraSamplerIndirect.dylib</c> on macOS ARM64
9+
/// and allows to attach to an existing process by PID and profile it with ultra.
10+
/// The native library is responsible for loading the native Ultra.Sampler.UltraSampler to profile the target process.
11+
/// </summary>
12+
// ReSharper disable once CheckNamespace
13+
internal static class StartupHook
14+
{
15+
private const string NativeLibraryName = "libUltraSamplerIndirect.dyld";
16+
private static nint _libraryHandle;
17+
18+
public static void Initialize()
19+
{
20+
// We don't need to re-load the library once it is loaded.
21+
// Only load the library on macOS ARM64
22+
if (_libraryHandle == 0 && OperatingSystem.IsMacOS() && RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
23+
{
24+
NativeLibrary.TryLoad(Path.Combine(AppContext.BaseDirectory, NativeLibraryName), out _libraryHandle);
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
</Project>

src/Ultra.Sampler/UltraSampler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Ultra.Sampler;
66

7-
public abstract class UltraSampler
7+
internal abstract class UltraSampler
88
{
99
private static readonly object Lock = new();
1010

src/ultra.sln

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentat
3232
EndProject
3333
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ultra.Sampler", "Ultra.Sampler\Ultra.Sampler.csproj", "{F06B2925-6916-4CA1-BFBB-ADAFA59BE835}"
3434
EndProject
35+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ultra.ProcessHook", "Ultra.ProcessHook\Ultra.ProcessHook.csproj", "{250C55EA-E6A7-4743-AD04-B07B412832CA}"
36+
EndProject
3537
Global
3638
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3739
Debug|Any CPU = Debug|Any CPU
@@ -58,6 +60,10 @@ Global
5860
{F06B2925-6916-4CA1-BFBB-ADAFA59BE835}.Debug|Any CPU.Build.0 = Debug|Any CPU
5961
{F06B2925-6916-4CA1-BFBB-ADAFA59BE835}.Release|Any CPU.ActiveCfg = Release|Any CPU
6062
{F06B2925-6916-4CA1-BFBB-ADAFA59BE835}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{250C55EA-E6A7-4743-AD04-B07B412832CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64+
{250C55EA-E6A7-4743-AD04-B07B412832CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
65+
{250C55EA-E6A7-4743-AD04-B07B412832CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
66+
{250C55EA-E6A7-4743-AD04-B07B412832CA}.Release|Any CPU.Build.0 = Release|Any CPU
6167
EndGlobalSection
6268
GlobalSection(SolutionProperties) = preSolution
6369
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)