Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions integration-test/runtime.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SentrySdk.Init(options =>
options.Dsn = args[0];
options.Debug = true;
options.Transport = new FakeTransport();
options.CacheDirectoryPath = Path.GetTempPath();
});

if (args.Length > 1 && !string.IsNullOrEmpty(args[1]))
Expand Down Expand Up @@ -164,15 +165,11 @@ internal class FakeTransport : ITransport
# The first run triggers a native error. This error is captured by sentry-native and stored stored for the next run.
runConsoleApp $true 'Native' | Should -AnyElementMatch 'Triggering a deliberate exception'

# On the next run, we use a mock Sentry HTTP server to receive the native crash.
$result = Invoke-SentryServer {
Param([string]$url)
runConsoleApp $true '' ($url.Replace('http://', 'http://key@') + '/0')
}
$result.HasErrors() | Should -BeFalse
$result.ScriptOutput | Should -AnyElementMatch "Native SDK reported: 'crashedLastRun': 'True'"
# On the next run, we receive the native crash.
$output = runConsoleApp $true ''
$output | Should -AnyElementMatch "Native SDK reported: 'crashedLastRun': 'True'"
$type = $IsWindows ? 'EXCEPTION_ACCESS_VIOLATION' : 'SIGSEGV'
$result.Envelopes() | Should -AnyElementMatch "`"exception`":{`"values`":\[{`"type`":`"$type`""
$output | Should -AnyElementMatch "`"exception`":{`"values`":\[{`"type`":`"$type`""
}
}

Expand Down
1 change: 1 addition & 0 deletions scripts/build-sentry-native.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ try
-D SENTRY_SDK_NAME=sentry.native.dotnet `
-D SENTRY_BUILD_SHARED_LIBS=0 `
-D SENTRY_BACKEND=inproc `
-D SENTRY_TRANSPORT=none `
$additionalArgs

cmake `
Expand Down
68 changes: 68 additions & 0 deletions src/Sentry/Platforms/Native/CFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ public static bool Init(SentryOptions options)
}
}

unsafe
{
var cTransport = sentry_transport_new(&nativeTransport);
sentry_transport_set_state(cTransport, GCHandle.ToIntPtr(GCHandle.Alloc(options)));
sentry_transport_set_free_func(cTransport, &nativeTransportFree);
sentry_options_set_transport(cOptions, cTransport);
}

options.DiagnosticLogger?.LogDebug("Initializing sentry native");
return 0 == sentry_init(cOptions);
}
Expand Down Expand Up @@ -364,6 +372,66 @@ internal struct sentry_value_t
[DllImport("sentry-native")]
private static extern void sentry_options_set_auto_session_tracking(IntPtr options, int debug);

[DllImport("sentry-native")]
private static extern void sentry_options_set_transport(IntPtr options, IntPtr transport);

[DllImport("sentry-native")]
private static extern unsafe IntPtr sentry_transport_new(delegate* unmanaged/*[Cdecl]*/<IntPtr, IntPtr, void> sendFunc);

[DllImport("sentry-native")]
private static extern void sentry_transport_set_state(IntPtr transport, IntPtr state);

[DllImport("sentry-native")]
private static extern unsafe void sentry_transport_set_free_func(IntPtr transport, delegate* unmanaged/*[Cdecl]*/<IntPtr, void> freeFunc);

[DllImport("sentry-native")]
private static extern int sentry_envelope_write_to_file(IntPtr envelope, string path);

[DllImport("sentry-native")]
private static extern void sentry_envelope_free(IntPtr envelope);

[DllImport("sentry-native")]
private static extern void sentry_free(IntPtr ptr);

[UnmanagedCallersOnly]
private static void nativeTransport(IntPtr envelope, IntPtr state)
{
try
{
var options = GCHandle.FromIntPtr(state).Target as SentryOptions;
var cacheDirectoryPath = options?.TryGetProcessSpecificCacheDirectoryPath();
if (cacheDirectoryPath is null)
{
return;
}

var envelopePath = Path.Combine(cacheDirectoryPath, SentryId.Create() + ".envelope");
if (sentry_envelope_write_to_file(envelope, envelopePath) != 0)
{
options?.DiagnosticLogger?.LogError("Failed to write native envelope: {0}", envelopePath);
}
else
{
options?.DiagnosticLogger?.LogDebug("Wrote native envelope: {0}", envelopePath);
}
}
catch
{
// never allow an exception back to native code - it would crash the app
}
finally
{
sentry_envelope_free(envelope);
}
}

[UnmanagedCallersOnly]
private static void nativeTransportFree(IntPtr state)
{
var handle = GCHandle.FromIntPtr(state);
handle.Free();
}

[DllImport("sentry-native")]
private static extern unsafe void sentry_options_set_logger(IntPtr options, delegate* unmanaged/*[Cdecl]*/<int, IntPtr, IntPtr, IntPtr, void> logger, IntPtr userData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
<ItemGroup Condition="'$(FrameworkSupportsNative)' == 'true' and ('$(RuntimeIdentifier)' == 'linux-x64' or '$(RuntimeIdentifier)' == 'linux-arm64')">
<DirectPInvoke Include="sentry-native" />
<NativeLibrary Include="$(MSBuildThisFileDirectory)..\sentry-native\$(RuntimeIdentifier)\libsentry-native.a" />
<!-- See: https://github.com/dotnet/runtime/issues/97414 -->
<NativeSystemLibrary Include="curl" />
</ItemGroup>

<ItemGroup Condition="'$(FrameworkSupportsNative)' == 'true' and '$(RuntimeIdentifier)' == 'linux-musl-x64'">
Expand All @@ -46,13 +44,10 @@
<LinkerArg Include="-Wl,-Bstatic -Wl,--whole-archive -lunwind -Wl,--no-whole-archive -Wl,-Bdynamic" />
<NativeSystemLibrary Include="lzma" />
<NativeLibrary Include="$(MSBuildThisFileDirectory)..\sentry-native\linux-musl-x64\libsentry-native.a" />
<!-- See: https://github.com/dotnet/runtime/issues/97414 -->
<NativeSystemLibrary Include="curl" />
</ItemGroup>

<ItemGroup Condition="'$(FrameworkSupportsNative)' == 'true' and ('$(RuntimeIdentifier)' == 'osx-x64' or '$(RuntimeIdentifier)' == 'osx-arm64')">
<DirectPInvoke Include="sentry-native" />
<NativeLibrary Include="$(MSBuildThisFileDirectory)..\sentry-native\osx\libsentry-native.a" />
<NativeSystemLibrary Include="curl" />
</ItemGroup>
</Project>
Loading