From b31a923a5eb0f071d079a3ff4fe1e292bc26c741 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 18 Dec 2023 20:30:24 +0100 Subject: [PATCH 1/7] compile stuff with the game --- package-dev/Runtime/SentryInitialization.cs | 34 ++++++++- src/Sentry.Unity/ISentryUnityInfo.cs | 2 + src/Sentry.Unity/Il2CppEventProcessor.cs | 75 +++++++++---------- .../SentryUnityOptionsExtensions.cs | 2 +- test/SharedClasses/TestUnityInfo.cs | 7 +- 5 files changed, 77 insertions(+), 43 deletions(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index 84fae5034..1b9ffeb6e 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -328,6 +328,38 @@ public bool IsNativeSupportEnabled(SentryUnityOptions options, RuntimePlatform p default: return false; } - } + + public bool IsSupportedBySentryNative(RuntimePlatform platform) + { + return platform == + RuntimePlatform.Android + || RuntimePlatform.LinuxPlayer + || RuntimePlatform.WindowsPlayer +#if UNITY_2021_3_OR_NEWER + || RuntimePlatform.WindowsServer + || RuntimePlatform.OSXServer + || RuntimePlatform.LinuxServer +#endif + ; + } + + public string GetDebugImageType(RuntimePlatform platform) + { + platform switch + { + RuntimePlatform.Android => "elf", + RuntimePlatform.IPhonePlayer => "macho", + RuntimePlatform.OSXPlayer => "macho", + RuntimePlatform.LinuxPlayer => "elf", + RuntimePlatform.WindowsPlayer => "pe", +#if UNITY_2021_3_OR_NEWER + RuntimePlatform.WindowsServer => "pe", + RuntimePlatform.OSXServer => "macho", + RuntimePlatform.LinuxServer => "elf", +#endif + _ => "unknown" + }; + } + } } } diff --git a/src/Sentry.Unity/ISentryUnityInfo.cs b/src/Sentry.Unity/ISentryUnityInfo.cs index 50eb18874..51792851a 100644 --- a/src/Sentry.Unity/ISentryUnityInfo.cs +++ b/src/Sentry.Unity/ISentryUnityInfo.cs @@ -10,6 +10,8 @@ public interface ISentryUnityInfo public bool IsKnownPlatform(); public bool IsLinux(); public bool IsNativeSupportEnabled(SentryUnityOptions options, RuntimePlatform platform); + public bool IsSupportedBySentryNative(RuntimePlatform platform); + public string GetDebugImageType(RuntimePlatform platform); } public class Il2CppMethods diff --git a/src/Sentry.Unity/Il2CppEventProcessor.cs b/src/Sentry.Unity/Il2CppEventProcessor.cs index 5e2ac549e..e9c707a9e 100644 --- a/src/Sentry.Unity/Il2CppEventProcessor.cs +++ b/src/Sentry.Unity/Il2CppEventProcessor.cs @@ -13,12 +13,14 @@ namespace Sentry.Unity internal class UnityIl2CppEventExceptionProcessor : ISentryEventExceptionProcessor { private readonly SentryUnityOptions _options; + private static ISentryUnityInfo UnityInfo = null!; // private static will be initialized in the constructor private readonly Il2CppMethods _il2CppMethods; - public UnityIl2CppEventExceptionProcessor(SentryUnityOptions options, Il2CppMethods il2CppMethods) + public UnityIl2CppEventExceptionProcessor(SentryUnityOptions options, ISentryUnityInfo unityInfo) { _options = options; - _il2CppMethods = il2CppMethods; + UnityInfo = unityInfo; + _il2CppMethods = unityInfo.Il2CppMethods ?? throw new ArgumentNullException(nameof(unityInfo.Il2CppMethods), "Unity IL2CPP methods are not available."); _options.SdkIntegrationNames.Add("IL2CPPLineNumbers"); } @@ -94,7 +96,7 @@ public void Process(Exception incomingException, SentryEvent sentryEvent) var instructionAddress = (ulong)nativeFrame.ToInt64(); // We cannot determine whether this frame is a main library frame just from the address - // because even relative address on the frame may correspond to an absolute addres of a loaded library. + // because even relative address on the frame may correspond to an absolute address of a loaded library. // Therefore, if the frame package matches known prefixes, we assume it's a GameAssembly frame. var isMainLibFrame = frame.Package is not null && ( frame.Package.StartsWith("UnityEngine.", StringComparison.InvariantCultureIgnoreCase) || @@ -129,18 +131,10 @@ public void Process(Exception incomingException, SentryEvent sentryEvent) } // First, try to find the image among the loaded ones, otherwise create a dummy one. - mainLibImage ??= DebugImagesSorted.Value.Find((info) => string.Equals(NormalizeUuid(info.Image.DebugId), mainImageUUID))?.Image; + mainLibImage ??= _debugImagesSorted.Value.Find((info) => string.Equals(NormalizeUuid(info.Image.DebugId), mainImageUUID))?.Image; mainLibImage ??= new DebugImage { - Type = Application.platform switch - { - RuntimePlatform.Android => "elf", - RuntimePlatform.IPhonePlayer => "macho", - RuntimePlatform.OSXPlayer => "macho", - RuntimePlatform.LinuxPlayer => "elf", - RuntimePlatform.WindowsPlayer => "pe", - _ => "unknown" - }, + Type = UnityInfo.GetDebugImageType(Application.platform), // NOTE: il2cpp in some circumstances does not return a correct `ImageName`. // A null/missing `CodeFile` however would lead to a processing error in sentry. // Since the code file is not strictly necessary for processing, we just fall back to @@ -224,48 +218,49 @@ public DebugImageInfo(DebugImage image) private static IDiagnosticLogger? Logger; - private static readonly Lazy> DebugImagesSorted = new(() => + private readonly Lazy> _debugImagesSorted = new(() => { var result = new List(); // Only on platforms where we actually use sentry-native. - if (ApplicationAdapter.Instance.Platform - is RuntimePlatform.WindowsPlayer or RuntimePlatform.Android or RuntimePlatform.LinuxPlayer) + if (!UnityInfo.IsSupportedBySentryNative(Application.platform)) { - var nativeDebugImages = C.DebugImages.Value; - foreach (var image in nativeDebugImages) + return result; + } + + var nativeDebugImages = C.DebugImages.Value; + foreach (var image in nativeDebugImages) + { + if (image.ImageSize is null) { - if (image.ImageSize is null) - { - Logger?.Log(SentryLevel.Debug, - "Skipping debug image '{0}' (CodeId {1} | DebugId: {2}) because its size is NULL", - null, image.CodeFile, image.CodeId, image.DebugId); - continue; - } + Logger?.Log(SentryLevel.Debug, + "Skipping debug image '{0}' (CodeId {1} | DebugId: {2}) because its size is NULL", + null, image.CodeFile, image.CodeId, image.DebugId); + continue; + } - var info = new DebugImageInfo(image); - var i = 0; - for (; i < result.Count; i++) + var info = new DebugImageInfo(image); + var i = 0; + for (; i < result.Count; i++) + { + if (info.StartAddress < result[i].StartAddress) { - if (info.StartAddress < result[i].StartAddress) - { - // insert at index `i`, all the rest have a larger start address - break; - } + // insert at index `i`, all the rest have a larger start address + break; } - result.Insert(i, info); - - Logger?.Log(SentryLevel.Debug, - "Found debug image '{0}' (CodeId {1} | DebugId: {2}) with addresses between {3:X8} and {4:X8}", - null, image.CodeFile, image.CodeId, image.DebugId, info.StartAddress, info.EndAddress); } + result.Insert(i, info); + + Logger?.Log(SentryLevel.Debug, + "Found debug image '{0}' (CodeId {1} | DebugId: {2}) with addresses between {3:X8} and {4:X8}", + null, image.CodeFile, image.CodeId, image.DebugId, info.StartAddress, info.EndAddress); } return result; }); - private static DebugImage? FindDebugImageContainingAddress(ulong instructionAddress) + private DebugImage? FindDebugImageContainingAddress(ulong instructionAddress) { - var list = DebugImagesSorted.Value; + var list = _debugImagesSorted.Value; // Manual binary search implementation on "value in range". var lowerBound = 0; diff --git a/src/Sentry.Unity/SentryUnityOptionsExtensions.cs b/src/Sentry.Unity/SentryUnityOptionsExtensions.cs index 6a5cea69c..e8d5b13ee 100644 --- a/src/Sentry.Unity/SentryUnityOptionsExtensions.cs +++ b/src/Sentry.Unity/SentryUnityOptionsExtensions.cs @@ -67,7 +67,7 @@ internal static void AddIl2CppExceptionProcessor(this SentryUnityOptions options { if (unityInfo.Il2CppMethods is not null) { - options.AddExceptionProcessor(new UnityIl2CppEventExceptionProcessor(options, unityInfo.Il2CppMethods)); + options.AddExceptionProcessor(new UnityIl2CppEventExceptionProcessor(options, unityInfo)); } else { diff --git a/test/SharedClasses/TestUnityInfo.cs b/test/SharedClasses/TestUnityInfo.cs index c7bcb3e58..277bbdab7 100644 --- a/test/SharedClasses/TestUnityInfo.cs +++ b/test/SharedClasses/TestUnityInfo.cs @@ -6,20 +6,25 @@ public class TestUnityInfo : ISentryUnityInfo private readonly bool _isKnownPlatform; private readonly bool _isLinux; private readonly bool _isNativeSupportEnabled; + private readonly bool _isSupportedBySentryNative; public bool IL2CPP { get; set; } public string? Platform { get; } public Il2CppMethods? Il2CppMethods { get; } - public TestUnityInfo(bool isKnownPlatform = true, bool isLinux = false, bool isNativeSupportEnabled = true) + public TestUnityInfo(bool isKnownPlatform = true, bool isLinux = false, bool isNativeSupportEnabled = true, bool isSupportedBySentryNative = true) { _isKnownPlatform = isKnownPlatform; _isLinux = isLinux; _isNativeSupportEnabled = isNativeSupportEnabled; + _isSupportedBySentryNative = isSupportedBySentryNative; } public bool IsKnownPlatform() => _isKnownPlatform; public bool IsLinux() => _isLinux; public bool IsNativeSupportEnabled(SentryUnityOptions options, RuntimePlatform platform) => _isNativeSupportEnabled; + public bool IsSupportedBySentryNative(RuntimePlatform platform) => _isSupportedBySentryNative; + + public string GetDebugImageType(RuntimePlatform platform) => "debug"; } From d200d7dc1e795b02a3740a58aba45443cb94ee98 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 18 Dec 2023 20:41:58 +0100 Subject: [PATCH 2/7] revert refactor --- src/Sentry.Unity/Il2CppEventProcessor.cs | 48 ++++++++++++------------ 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/Sentry.Unity/Il2CppEventProcessor.cs b/src/Sentry.Unity/Il2CppEventProcessor.cs index e9c707a9e..0048e804f 100644 --- a/src/Sentry.Unity/Il2CppEventProcessor.cs +++ b/src/Sentry.Unity/Il2CppEventProcessor.cs @@ -223,37 +223,35 @@ public DebugImageInfo(DebugImage image) var result = new List(); // Only on platforms where we actually use sentry-native. - if (!UnityInfo.IsSupportedBySentryNative(Application.platform)) + if (UnityInfo.IsSupportedBySentryNative(Application.platform)) { - return result; - } - - var nativeDebugImages = C.DebugImages.Value; - foreach (var image in nativeDebugImages) - { - if (image.ImageSize is null) + var nativeDebugImages = C.DebugImages.Value; + foreach (var image in nativeDebugImages) { - Logger?.Log(SentryLevel.Debug, - "Skipping debug image '{0}' (CodeId {1} | DebugId: {2}) because its size is NULL", - null, image.CodeFile, image.CodeId, image.DebugId); - continue; - } + if (image.ImageSize is null) + { + Logger?.Log(SentryLevel.Debug, + "Skipping debug image '{0}' (CodeId {1} | DebugId: {2}) because its size is NULL", + null, image.CodeFile, image.CodeId, image.DebugId); + continue; + } - var info = new DebugImageInfo(image); - var i = 0; - for (; i < result.Count; i++) - { - if (info.StartAddress < result[i].StartAddress) + var info = new DebugImageInfo(image); + var i = 0; + for (; i < result.Count; i++) { - // insert at index `i`, all the rest have a larger start address - break; + if (info.StartAddress < result[i].StartAddress) + { + // insert at index `i`, all the rest have a larger start address + break; + } } - } - result.Insert(i, info); + result.Insert(i, info); - Logger?.Log(SentryLevel.Debug, - "Found debug image '{0}' (CodeId {1} | DebugId: {2}) with addresses between {3:X8} and {4:X8}", - null, image.CodeFile, image.CodeId, image.DebugId, info.StartAddress, info.EndAddress); + Logger?.Log(SentryLevel.Debug, + "Found debug image '{0}' (CodeId {1} | DebugId: {2}) with addresses between {3:X8} and {4:X8}", + null, image.CodeFile, image.CodeId, image.DebugId, info.StartAddress, info.EndAddress); + } } return result; }); From 28a76dfe9c96734ca3336d6dd97d392733fba732 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 18 Dec 2023 20:45:26 +0100 Subject: [PATCH 3/7] revert refactor --- src/Sentry.Unity/Il2CppEventProcessor.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Sentry.Unity/Il2CppEventProcessor.cs b/src/Sentry.Unity/Il2CppEventProcessor.cs index 0048e804f..0aa1485de 100644 --- a/src/Sentry.Unity/Il2CppEventProcessor.cs +++ b/src/Sentry.Unity/Il2CppEventProcessor.cs @@ -131,7 +131,7 @@ public void Process(Exception incomingException, SentryEvent sentryEvent) } // First, try to find the image among the loaded ones, otherwise create a dummy one. - mainLibImage ??= _debugImagesSorted.Value.Find((info) => string.Equals(NormalizeUuid(info.Image.DebugId), mainImageUUID))?.Image; + mainLibImage ??= DebugImagesSorted.Value.Find((info) => string.Equals(NormalizeUuid(info.Image.DebugId), mainImageUUID))?.Image; mainLibImage ??= new DebugImage { Type = UnityInfo.GetDebugImageType(Application.platform), @@ -218,7 +218,7 @@ public DebugImageInfo(DebugImage image) private static IDiagnosticLogger? Logger; - private readonly Lazy> _debugImagesSorted = new(() => + private static readonly Lazy> DebugImagesSorted = new(() => { var result = new List(); @@ -258,7 +258,7 @@ public DebugImageInfo(DebugImage image) private DebugImage? FindDebugImageContainingAddress(ulong instructionAddress) { - var list = _debugImagesSorted.Value; + var list = DebugImagesSorted.Value; // Manual binary search implementation on "value in range". var lowerBound = 0; From 4a7760c3a17890d830b89d2761c9974b095ac5da Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 18 Dec 2023 20:47:02 +0100 Subject: [PATCH 4/7] revert refactor --- src/Sentry.Unity/Il2CppEventProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry.Unity/Il2CppEventProcessor.cs b/src/Sentry.Unity/Il2CppEventProcessor.cs index 0aa1485de..a365fb4de 100644 --- a/src/Sentry.Unity/Il2CppEventProcessor.cs +++ b/src/Sentry.Unity/Il2CppEventProcessor.cs @@ -256,7 +256,7 @@ public DebugImageInfo(DebugImage image) return result; }); - private DebugImage? FindDebugImageContainingAddress(ulong instructionAddress) + private static DebugImage? FindDebugImageContainingAddress(ulong instructionAddress) { var list = DebugImagesSorted.Value; From e09e1c9d46a093a520b532f1784f7d80d548f05e Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 18 Dec 2023 20:51:11 +0100 Subject: [PATCH 5/7] Updated CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f56822e16..60ebc3c62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes +- The SDK no longer fails to resolve the debug symbol type on dedicated server builds ([#1522](https://github.com/getsentry/sentry-unity/pull/1522)) - Fixed screenshots not being attached to iOS native crashes ([#1517](https://github.com/getsentry/sentry-unity/pull/1517)) ### Dependencies From b98892de34523df64807962f2a17a26ea25cfca4 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Mon, 18 Dec 2023 21:08:13 +0100 Subject: [PATCH 6/7] fix --- package-dev/Runtime/SentryInitialization.cs | 50 ++++++++++----------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index 1b9ffeb6e..b4cd87cbc 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -328,38 +328,38 @@ public bool IsNativeSupportEnabled(SentryUnityOptions options, RuntimePlatform p default: return false; } + } - public bool IsSupportedBySentryNative(RuntimePlatform platform) - { - return platform == - RuntimePlatform.Android - || RuntimePlatform.LinuxPlayer - || RuntimePlatform.WindowsPlayer + + public bool IsSupportedBySentryNative(RuntimePlatform platform) + { + return platform == RuntimePlatform.Android + || platform == RuntimePlatform.LinuxPlayer + || platform == RuntimePlatform.WindowsPlayer #if UNITY_2021_3_OR_NEWER - || RuntimePlatform.WindowsServer - || RuntimePlatform.OSXServer - || RuntimePlatform.LinuxServer + || platform == RuntimePlatform.WindowsServer + || platform == RuntimePlatform.OSXServer + || platform == RuntimePlatform.LinuxServer #endif - ; - } + ; + } - public string GetDebugImageType(RuntimePlatform platform) + public string GetDebugImageType(RuntimePlatform platform) + { + return platform switch { - platform switch - { - RuntimePlatform.Android => "elf", - RuntimePlatform.IPhonePlayer => "macho", - RuntimePlatform.OSXPlayer => "macho", - RuntimePlatform.LinuxPlayer => "elf", - RuntimePlatform.WindowsPlayer => "pe", + RuntimePlatform.Android => "elf", + RuntimePlatform.IPhonePlayer => "macho", + RuntimePlatform.OSXPlayer => "macho", + RuntimePlatform.LinuxPlayer => "elf", + RuntimePlatform.WindowsPlayer => "pe", #if UNITY_2021_3_OR_NEWER - RuntimePlatform.WindowsServer => "pe", - RuntimePlatform.OSXServer => "macho", - RuntimePlatform.LinuxServer => "elf", + RuntimePlatform.WindowsServer => "pe", + RuntimePlatform.OSXServer => "macho", + RuntimePlatform.LinuxServer => "elf", #endif - _ => "unknown" - }; - } + _ => "unknown" + }; } } } From 9a8189763eef6e547d93fe518d604f0115622f2d Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Tue, 19 Dec 2023 09:36:21 +0100 Subject: [PATCH 7/7] language downgrade --- package-dev/Runtime/SentryInitialization.cs | 31 +++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/package-dev/Runtime/SentryInitialization.cs b/package-dev/Runtime/SentryInitialization.cs index b4cd87cbc..7cefc142b 100644 --- a/package-dev/Runtime/SentryInitialization.cs +++ b/package-dev/Runtime/SentryInitialization.cs @@ -346,20 +346,29 @@ public bool IsSupportedBySentryNative(RuntimePlatform platform) public string GetDebugImageType(RuntimePlatform platform) { - return platform switch + switch (platform) { - RuntimePlatform.Android => "elf", - RuntimePlatform.IPhonePlayer => "macho", - RuntimePlatform.OSXPlayer => "macho", - RuntimePlatform.LinuxPlayer => "elf", - RuntimePlatform.WindowsPlayer => "pe", + case RuntimePlatform.Android: + return "elf"; + case RuntimePlatform.IPhonePlayer: + return "macho"; + case RuntimePlatform.OSXPlayer: + return "macho"; + case RuntimePlatform.LinuxPlayer: + return "elf"; + case RuntimePlatform.WindowsPlayer: + return "pe"; #if UNITY_2021_3_OR_NEWER - RuntimePlatform.WindowsServer => "pe", - RuntimePlatform.OSXServer => "macho", - RuntimePlatform.LinuxServer => "elf", + case RuntimePlatform.WindowsServer: + return "pe"; + case RuntimePlatform.OSXServer: + return "macho"; + case RuntimePlatform.LinuxServer: + return "elf"; #endif - _ => "unknown" - }; + default: + return "unknown"; + } } } }