From 60c1ae26ead4c77bab8823400f98010740c53beb Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 12 Apr 2024 13:47:56 +0200 Subject: [PATCH 1/8] check for no screen --- src/Sentry.Unity/ScreenshotAttachment.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Sentry.Unity/ScreenshotAttachment.cs b/src/Sentry.Unity/ScreenshotAttachment.cs index a4d5ec864..8918329d9 100644 --- a/src/Sentry.Unity/ScreenshotAttachment.cs +++ b/src/Sentry.Unity/ScreenshotAttachment.cs @@ -31,6 +31,13 @@ public Stream GetStream() return Stream.Null; } + if (Screen.width == 0 || Screen.height == 0) + { + _options.DiagnosticLogger?.LogDebug("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.",Screen.width, Screen.height); + // Returning an empty memory stream here so we can smoke-test the attempt to capture a screenshot in CI + return new MemoryStream(); + } + return new MemoryStream(CaptureScreenshot(Screen.width, Screen.height)); } From b614fab4e3e6e9945b1d624cbc860f23872abc70 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 12 Apr 2024 14:26:36 +0200 Subject: [PATCH 2/8] memory stream can't be empty --- src/Sentry.Unity/ScreenshotAttachment.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry.Unity/ScreenshotAttachment.cs b/src/Sentry.Unity/ScreenshotAttachment.cs index 8918329d9..14f531611 100644 --- a/src/Sentry.Unity/ScreenshotAttachment.cs +++ b/src/Sentry.Unity/ScreenshotAttachment.cs @@ -35,7 +35,7 @@ public Stream GetStream() { _options.DiagnosticLogger?.LogDebug("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.",Screen.width, Screen.height); // Returning an empty memory stream here so we can smoke-test the attempt to capture a screenshot in CI - return new MemoryStream(); + return new MemoryStream(1); } return new MemoryStream(CaptureScreenshot(Screen.width, Screen.height)); From 2de667389a210a54163f2d2d1783363b5c5d9f2a Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Fri, 12 Apr 2024 14:27:06 +0200 Subject: [PATCH 3/8] comment --- src/Sentry.Unity/ScreenshotAttachment.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry.Unity/ScreenshotAttachment.cs b/src/Sentry.Unity/ScreenshotAttachment.cs index 14f531611..ad965210a 100644 --- a/src/Sentry.Unity/ScreenshotAttachment.cs +++ b/src/Sentry.Unity/ScreenshotAttachment.cs @@ -34,7 +34,7 @@ public Stream GetStream() if (Screen.width == 0 || Screen.height == 0) { _options.DiagnosticLogger?.LogDebug("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.",Screen.width, Screen.height); - // Returning an empty memory stream here so we can smoke-test the attempt to capture a screenshot in CI + // Returning a memory stream with a capacity of 1 so we can smoke-test the attempt to capture a screenshot in CI return new MemoryStream(1); } From fb4f8505a230f6527dd5db0286e105e2a9648a8f Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Fri, 12 Apr 2024 13:14:35 +0000 Subject: [PATCH 4/8] Format code --- src/Sentry.Unity/ScreenshotAttachment.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry.Unity/ScreenshotAttachment.cs b/src/Sentry.Unity/ScreenshotAttachment.cs index ad965210a..cffb92abe 100644 --- a/src/Sentry.Unity/ScreenshotAttachment.cs +++ b/src/Sentry.Unity/ScreenshotAttachment.cs @@ -33,7 +33,7 @@ public Stream GetStream() if (Screen.width == 0 || Screen.height == 0) { - _options.DiagnosticLogger?.LogDebug("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.",Screen.width, Screen.height); + _options.DiagnosticLogger?.LogDebug("Can't capture screenshots on a screen with a resolution of '{0}x{1}'.", Screen.width, Screen.height); // Returning a memory stream with a capacity of 1 so we can smoke-test the attempt to capture a screenshot in CI return new MemoryStream(1); } From c7643483df3947e9a7300bb5e4def376e52d996d Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 15 Apr 2024 18:24:17 +0200 Subject: [PATCH 5/8] moved reinstallation back to scene loading --- package-dev/Runtime/SentryInitialization.cs | 11 +++++++++++ src/Sentry.Unity.Native/SentryNative.cs | 5 ----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index 7cefc142b..ed5e570a7 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -120,6 +120,17 @@ public static void Init() } } +#if SENTRY_NATIVE + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + public static void ReinstallBackend() + { + // At this point Unity has taken the signal handler and will not invoke our handler. So we register our + // backend once more to make sure user-defined data is available in the crash report and the SDK is able + // to capture the crash. + SentryNativeBridge.ReinstallBackend(); + } +#endif + public class SentryUnityInfo : ISentryUnityInfo { public bool IL2CPP diff --git a/src/Sentry.Unity.Native/SentryNative.cs b/src/Sentry.Unity.Native/SentryNative.cs index bcc5485ee..3e13ddc32 100644 --- a/src/Sentry.Unity.Native/SentryNative.cs +++ b/src/Sentry.Unity.Native/SentryNative.cs @@ -69,11 +69,6 @@ public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentry } } options.CrashedLastRun = () => crashedLastRun; - - // At this point Unity has taken the signal handler and will not invoke our handler. So we register our - // backend once more to make sure user-defined data is available in the crash report and the SDK is able - // to capture the crash. - SentryNativeBridge.ReinstallBackend(); } } } From fccdfd955959fc72be4d99b6b2aca85c04865d3f Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 15 Apr 2024 19:33:17 +0200 Subject: [PATCH 6/8] fix it --- package-dev/Runtime/SentryInitialization.cs | 19 ++++++++++--------- src/Sentry.Unity.Native/SentryNative.cs | 2 ++ src/Sentry.Unity.Native/SentryNativeBridge.cs | 1 - 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index ed5e570a7..1bcd39297 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -118,18 +118,19 @@ public static void Init() #endif } } - } #if SENTRY_NATIVE - [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] - public static void ReinstallBackend() - { - // At this point Unity has taken the signal handler and will not invoke our handler. So we register our - // backend once more to make sure user-defined data is available in the crash report and the SDK is able - // to capture the crash. - SentryNativeBridge.ReinstallBackend(); - } + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + public static void ReinstallBackend() + { + // At this point Unity has taken the signal handler and will not invoke our handler. So we register our + // backend once more to make sure user-defined data is available in the crash report and the SDK is able + // to capture the crash. + SentryNative.ReinstallBackend(); + } #endif + } + public class SentryUnityInfo : ISentryUnityInfo { diff --git a/src/Sentry.Unity.Native/SentryNative.cs b/src/Sentry.Unity.Native/SentryNative.cs index 3e13ddc32..9825619fe 100644 --- a/src/Sentry.Unity.Native/SentryNative.cs +++ b/src/Sentry.Unity.Native/SentryNative.cs @@ -70,5 +70,7 @@ public static void Configure(SentryUnityOptions options, ISentryUnityInfo sentry } options.CrashedLastRun = () => crashedLastRun; } + + public static void ReinstallBackend() => SentryNativeBridge.ReinstallBackend(); } } diff --git a/src/Sentry.Unity.Native/SentryNativeBridge.cs b/src/Sentry.Unity.Native/SentryNativeBridge.cs index eb2367342..bde377f36 100644 --- a/src/Sentry.Unity.Native/SentryNativeBridge.cs +++ b/src/Sentry.Unity.Native/SentryNativeBridge.cs @@ -14,7 +14,6 @@ namespace Sentry.Unity.Native /// public static class SentryNativeBridge { - public static bool CrashedLastRun; public static bool Init(SentryUnityOptions options, ISentryUnityInfo sentryUnityInfo) From ab6b235e539d6a810fc4dc06bcc066abe4a766ed Mon Sep 17 00:00:00 2001 From: Stefan Jandl Date: Mon, 15 Apr 2024 19:33:44 +0200 Subject: [PATCH 7/8] Update package-dev/Runtime/SentryInitialization.cs --- package-dev/Runtime/SentryInitialization.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index 1bcd39297..f44875c77 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -131,7 +131,6 @@ public static void ReinstallBackend() #endif } - public class SentryUnityInfo : ISentryUnityInfo { public bool IL2CPP From 29a511aed71b3b6edf18f56fd8c3c291c15a384b Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 15 Apr 2024 19:54:31 +0200 Subject: [PATCH 8/8] Updated CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d4302227..35335be27 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixes + +- Tweaked the SDK reinstalling the backend to capture native crashes on Windows. C++ exceptions are now getting properly captured again ([#1622](https://github.com/getsentry/sentry-unity/pull/1622)) + ### Dependencies - Bump Java SDK from v7.6.0 to v7.7.0 ([#1610](https://github.com/getsentry/sentry-unity/pull/1610))