Skip to content

Commit c4794ca

Browse files
committed
poc: custom disk transport for sentry-native
1 parent 0e11682 commit c4794ca

4 files changed

Lines changed: 74 additions & 13 deletions

File tree

integration-test/runtime.Tests.ps1

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SentrySdk.Init(options =>
2020
options.Dsn = args[0];
2121
options.Debug = true;
2222
options.Transport = new FakeTransport();
23+
options.CacheDirectoryPath = Path.GetTempPath();
2324
});
2425
2526
if (args.Length > 1 && !string.IsNullOrEmpty(args[1]))
@@ -164,15 +165,11 @@ internal class FakeTransport : ITransport
164165
# The first run triggers a native error. This error is captured by sentry-native and stored stored for the next run.
165166
runConsoleApp $true 'Native' | Should -AnyElementMatch 'Triggering a deliberate exception'
166167

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

scripts/build-sentry-native.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ try
6969
-D SENTRY_SDK_NAME=sentry.native.dotnet `
7070
-D SENTRY_BUILD_SHARED_LIBS=0 `
7171
-D SENTRY_BACKEND=inproc `
72+
-D SENTRY_TRANSPORT=none `
7273
$additionalArgs
7374

7475
cmake `

src/Sentry/Platforms/Native/CFunctions.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ public static bool Init(SentryOptions options)
139139
}
140140
}
141141

142+
unsafe
143+
{
144+
var cTransport = sentry_transport_new(&nativeTransport);
145+
sentry_transport_set_state(cTransport, GCHandle.ToIntPtr(GCHandle.Alloc(options)));
146+
sentry_transport_set_free_func(cTransport, &nativeTransportFree);
147+
sentry_options_set_transport(cOptions, cTransport);
148+
}
149+
142150
options.DiagnosticLogger?.LogDebug("Initializing sentry native");
143151
return 0 == sentry_init(cOptions);
144152
}
@@ -364,6 +372,66 @@ internal struct sentry_value_t
364372
[DllImport("sentry-native")]
365373
private static extern void sentry_options_set_auto_session_tracking(IntPtr options, int debug);
366374

375+
[DllImport("sentry-native")]
376+
private static extern void sentry_options_set_transport(IntPtr options, IntPtr transport);
377+
378+
[DllImport("sentry-native")]
379+
private static extern unsafe IntPtr sentry_transport_new(delegate* unmanaged/*[Cdecl]*/<IntPtr, IntPtr, void> sendFunc);
380+
381+
[DllImport("sentry-native")]
382+
private static extern void sentry_transport_set_state(IntPtr transport, IntPtr state);
383+
384+
[DllImport("sentry-native")]
385+
private static extern unsafe void sentry_transport_set_free_func(IntPtr transport, delegate* unmanaged/*[Cdecl]*/<IntPtr, void> freeFunc);
386+
387+
[DllImport("sentry-native")]
388+
private static extern int sentry_envelope_write_to_file(IntPtr envelope, string path);
389+
390+
[DllImport("sentry-native")]
391+
private static extern void sentry_envelope_free(IntPtr envelope);
392+
393+
[DllImport("sentry-native")]
394+
private static extern void sentry_free(IntPtr ptr);
395+
396+
[UnmanagedCallersOnly]
397+
private static void nativeTransport(IntPtr envelope, IntPtr state)
398+
{
399+
try
400+
{
401+
var options = GCHandle.FromIntPtr(state).Target as SentryOptions;
402+
var cacheDirectoryPath = options?.TryGetProcessSpecificCacheDirectoryPath();
403+
if (cacheDirectoryPath is null)
404+
{
405+
return;
406+
}
407+
408+
var envelopePath = Path.Combine(cacheDirectoryPath, SentryId.Create() + ".envelope");
409+
if (sentry_envelope_write_to_file(envelope, envelopePath) != 0)
410+
{
411+
options?.DiagnosticLogger?.LogError("Failed to write native envelope: {0}", envelopePath);
412+
}
413+
else
414+
{
415+
options?.DiagnosticLogger?.LogDebug("Wrote native envelope: {0}", envelopePath);
416+
}
417+
}
418+
catch
419+
{
420+
// never allow an exception back to native code - it would crash the app
421+
}
422+
finally
423+
{
424+
sentry_envelope_free(envelope);
425+
}
426+
}
427+
428+
[UnmanagedCallersOnly]
429+
private static void nativeTransportFree(IntPtr state)
430+
{
431+
var handle = GCHandle.FromIntPtr(state);
432+
handle.Free();
433+
}
434+
367435
[DllImport("sentry-native")]
368436
private static extern unsafe void sentry_options_set_logger(IntPtr options, delegate* unmanaged/*[Cdecl]*/<int, IntPtr, IntPtr, IntPtr, void> logger, IntPtr userData);
369437

src/Sentry/Platforms/Native/buildTransitive/Sentry.Native.targets

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
<ItemGroup Condition="'$(FrameworkSupportsNative)' == 'true' and ('$(RuntimeIdentifier)' == 'linux-x64' or '$(RuntimeIdentifier)' == 'linux-arm64')">
3737
<DirectPInvoke Include="sentry-native" />
3838
<NativeLibrary Include="$(MSBuildThisFileDirectory)..\sentry-native\$(RuntimeIdentifier)\libsentry-native.a" />
39-
<!-- See: https://github.com/dotnet/runtime/issues/97414 -->
40-
<NativeSystemLibrary Include="curl" />
4139
</ItemGroup>
4240

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

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

0 commit comments

Comments
 (0)