Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

### Fixes

- Fixed IL2CPP line number support for 32-bit Windows and Linux ([#2514](https://github.com/getsentry/sentry-unity/pull/2514))
- The SDK now specifies the files and directories targeted for debug symbol upload instead of pointing sentry-cli at the build output directory ([#2485](https://github.com/getsentry/sentry-unity/pull/2485))
- The 'SceneManagerTracingIntegration' properly respects the `AutoSceneTracing` flag again ([#2496](https://github.com/getsentry/sentry-unity/pull/2496))
- When targeting Android, the capturing native SDK now has its name correctly set ([#2476](https://github.com/getsentry/sentry-unity/pull/2476))
Expand Down
10 changes: 5 additions & 5 deletions package-dev/Runtime/SentryInitialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define SENTRY_NATIVE_COCOA
#elif UNITY_ANDROID && ENABLE_IL2CPP
#define SENTRY_NATIVE_ANDROID
#elif UNITY_64 && (UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX)
#elif UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX
#define SENTRY_NATIVE
#elif UNITY_GAMECORE
#define SENTRY_NATIVE
Expand Down Expand Up @@ -176,20 +176,20 @@ void SwapHexByte(IntPtr buffer, Int32 offset1, Int32 offset2)

// Available in Unity `2013.3.12f1` (and later)
// Il2CppObject* il2cpp_gchandle_get_target(Il2CppGCHandle gchandle)
[DllImport("__Internal")]
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr il2cpp_gchandle_get_target(IntPtr gchandle);
#else
private static IntPtr Il2CppGcHandleGetTargetShim(IntPtr gchandle) => il2cpp_gchandle_get_target(gchandle.ToInt32());

// Available in Unity `2019.4.34f1` (and later)
// Il2CppObject* il2cpp_gchandle_get_target(uint32_t gchandle)
[DllImport("__Internal")]
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr il2cpp_gchandle_get_target(int gchandle);
#endif

// Available in Unity `2019.4.34f1` (and later)
// void il2cpp_free(void* ptr)
[DllImport("__Internal")]
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern void il2cpp_free(IntPtr ptr);

private static void Il2CppNativeStackTraceShim(IntPtr exc, out IntPtr addresses, out int numFrames, out string? imageUUID, out string? imageName)
Expand All @@ -212,7 +212,7 @@ private static void Il2CppNativeStackTraceShim(IntPtr exc, out IntPtr addresses,

// Definition from Unity `2021.3` (and later):
// void il2cpp_native_stack_trace(const Il2CppException * ex, uintptr_t** addresses, int* numFrames, char** imageUUID, char** imageName)
[DllImport("__Internal")]
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern void il2cpp_native_stack_trace(IntPtr exc, out IntPtr addresses, out int numFrames, out IntPtr imageUUID, out IntPtr imageName);

#pragma warning restore 8632
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ private void Start()
public void CrashInC() => crash_in_c();

// CppPlugin.cpp
[DllImport("__Internal")]
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern void throw_cpp();
[DllImport("__Internal")]
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern void crash_in_cpp();

// CPlugin.c
[DllImport("__Internal")]
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern void crash_in_c();

public void CatchViaCallback() => call_into_csharp(new callback_t(csharpCallback));

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate void callback_t(int code);

[DllImport("__Internal")]
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
private static extern void call_into_csharp(callback_t callback);

// This method is called from the C library.
Expand Down
52 changes: 28 additions & 24 deletions src/Sentry.Unity.Android/SentryNativeAndroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,104 +16,108 @@ public static class SentryNativeAndroid
// parameter on `Configure` due SentryNativeAndroid being public
internal static ISentryJava? SentryJava;

private static IDiagnosticLogger? Logger;

/// <summary>
/// Configures the native Android support.
/// </summary>
/// <param name="options">The Sentry Unity options to use.</param>
public static void Configure(SentryUnityOptions options)
{
options.DiagnosticLogger?.LogInfo("Attempting to configure native support via the Android SDK");
Logger = options.DiagnosticLogger;

Logger?.LogInfo("Attempting to configure native support via the Android SDK");

if (!options.AndroidNativeSupportEnabled)
{
options.DiagnosticLogger?.LogDebug("Native support is disabled for Android");
Logger?.LogDebug("Native support is disabled for Android");
return;
}

options.DiagnosticLogger?.LogDebug("Checking whether the Android SDK is present.");
Logger?.LogDebug("Checking whether the Android SDK is present.");

// If it's not been set (in a test)
SentryJava ??= new SentryJava(options.DiagnosticLogger);
SentryJava ??= new SentryJava(Logger);
if (!SentryJava.IsSentryJavaPresent())
{
options.DiagnosticLogger?.LogError("Android Native Support has been enabled but the " +
Logger?.LogError("Android Native Support has been enabled but the " +
"Android SDK is missing. This could have been caused by a mismatching" +
"build time / runtime configuration. Please make sure you have " +
"Android Native Support enabled during build time.");
return;
}

options.DiagnosticLogger?.LogDebug("Checking whether the Android SDK has already been initialized");
Logger?.LogDebug("Checking whether the Android SDK has already been initialized");

if (SentryJava.IsEnabled() is true)
{
options.DiagnosticLogger?.LogDebug("The Android SDK is already initialized");
Logger?.LogDebug("The Android SDK is already initialized");
}
else
{
options.DiagnosticLogger?.LogInfo("Initializing the Android SDK");
Logger?.LogInfo("Initializing the Android SDK");

SentryJava.Init(options);

options.DiagnosticLogger?.LogDebug("Validating Android SDK initialization");
Logger?.LogDebug("Validating Android SDK initialization");

if (SentryJava.IsEnabled() is not true)
{
options.DiagnosticLogger?.LogError("Failed to initialize Android Native Support");
Logger?.LogError("Failed to initialize Android Native Support");
return;
}
}

options.DiagnosticLogger?.LogDebug("Configuring scope sync");
Logger?.LogDebug("Configuring scope sync");

options.NativeContextWriter = new NativeContextWriter(SentryJava);
options.ScopeObserver = new AndroidJavaScopeObserver(options, SentryJava);
options.EnableScopeSync = true;
options.NativeDebugImageProvider = new Native.NativeDebugImageProvider();
options.CrashedLastRun = () =>
{
options.DiagnosticLogger?.LogDebug("Checking for 'CrashedLastRun'");
Logger?.LogDebug("Checking for 'CrashedLastRun'");

var crashedLastRun = SentryJava.CrashedLastRun();
if (crashedLastRun is null)
{
// Could happen if the Android SDK wasn't initialized before the .NET layer.
options.DiagnosticLogger?
Logger?
.LogWarning(
"Unclear from the native SDK if the previous run was a crash. Assuming it was not.");
crashedLastRun = false;
}
else
{
options.DiagnosticLogger?.LogDebug("Native SDK reported: 'crashedLastRun': '{0}'", crashedLastRun);
Logger?.LogDebug("Native SDK reported: 'crashedLastRun': '{0}'", crashedLastRun);
}

return crashedLastRun.Value;
};

try
{
options.DiagnosticLogger?.LogDebug("Reinstalling native backend.");
Logger?.LogDebug("Reinstalling native backend.");

// At this point Unity has taken the signal handler and will not invoke the original handler (Sentry)
// So we register our backend once more to make sure user-defined data is available in the crash report.
SentryNative.ReinstallBackend();
}
catch (Exception e)
{
options.DiagnosticLogger?.LogError(
Logger?.LogError(
e, "Failed to reinstall backend. Captured native crashes will miss scope data and tag.");
}

options.NativeSupportCloseCallback = () => Close(options);

options.DiagnosticLogger?.LogDebug("Fetching installation ID");
Logger?.LogDebug("Fetching installation ID");

options.DefaultUserId = SentryJava.GetInstallationId();
if (string.IsNullOrEmpty(options.DefaultUserId))
{
// In case we can't get an installation ID we create one and sync that down to the native layer
options.DiagnosticLogger?.LogDebug(
Logger?.LogDebug(
"Failed to fetch 'Installation ID' from the native SDK. Creating new 'Default User ID'.");

// We fall back to Unity's Analytics Session Info: https://docs.unity3d.com/ScriptReference/Analytics.AnalyticsSessionInfo-userId.html
Expand All @@ -126,33 +130,33 @@ public static void Configure(SentryUnityOptions options)
}
else
{
options.DiagnosticLogger?.LogDebug("Failed to create new 'Default User ID'.");
Logger?.LogDebug("Failed to create new 'Default User ID'.");
}
}

options.DiagnosticLogger?.LogInfo("Successfully configured the Android SDK");
Logger?.LogInfo("Successfully configured the Android SDK");
}

/// <summary>
/// Closes the native Android support.
/// </summary>
public static void Close(SentryUnityOptions options)
{
options.DiagnosticLogger?.LogInfo("Attempting to close the Android SDK");
Logger?.LogInfo("Attempting to close the Android SDK");

if (!options.IsNativeSupportEnabled())
{
options.DiagnosticLogger?.LogDebug("Android Native Support is not enabled. Skipping closing the Android SDK");
Logger?.LogDebug("Android Native Support is not enabled. Skipping closing the Android SDK");
return;
}

if (SentryJava?.IsSentryJavaPresent() is not true)
{
options.DiagnosticLogger?.LogDebug("Failed to find Sentry Java. Skipping closing the Android SDK");
Logger?.LogDebug("Failed to find Sentry Java. Skipping closing the Android SDK");
return;
}

options.DiagnosticLogger?.LogDebug("Closing the Android SDK");
Logger?.LogDebug("Closing the Android SDK");
SentryJava.Close();
}
}
12 changes: 7 additions & 5 deletions src/Sentry.Unity.Editor/Il2CppBuildPreProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Sentry.Unity.Editor;
internal class Il2CppBuildPreProcess : IPreprocessBuildWithReport
{
internal const string SourceMappingArgument = "--emit-source-mapping";
private static IDiagnosticLogger? Logger;

public int callbackOrder => 0;

Expand All @@ -20,14 +21,15 @@ public void OnPreprocessBuild(BuildReport report)
return;
}


var options = SentryScriptableObject.LoadOptions(isBuilding: true);

if (options is null)
{
return;
}

Logger = options.DiagnosticLogger;
Logger?.LogInfo("IL2CPP build detected. Handling additional IL2CPP arguments.");

SetAdditionalIl2CppArguments(options,
PlayerSettings.GetAdditionalIl2CppArgs,
PlayerSettings.SetAdditionalIl2CppArgs);
Expand All @@ -37,12 +39,12 @@ internal static void SetAdditionalIl2CppArguments(SentryUnityOptions options, Fu
{
if (options.Il2CppLineNumberSupportEnabled)
{
options.DiagnosticLogger?.LogDebug("IL2CPP line number support enabled - Adding additional IL2CPP arguments.");
Logger?.LogDebug("IL2CPP line number support enabled - Adding additional IL2CPP arguments.");

var arguments = getArguments.Invoke();
if (arguments.Contains(SourceMappingArgument))
{
options.DiagnosticLogger?.LogDebug("Additional argument '{0}' already present.", SourceMappingArgument);
Logger?.LogDebug("Additional argument '{0}' already present.", SourceMappingArgument);
return;
}

Expand All @@ -53,7 +55,7 @@ internal static void SetAdditionalIl2CppArguments(SentryUnityOptions options, Fu
var arguments = getArguments.Invoke();
if (arguments.Contains(SourceMappingArgument))
{
options.DiagnosticLogger?.LogDebug("IL2CPP line number support disabled - Removing additional IL2CPP arguments.");
Logger?.LogDebug("IL2CPP line number support disabled - Removing additional IL2CPP arguments.");

arguments = arguments.Replace(SourceMappingArgument, "");
setArguments.Invoke(arguments);
Expand Down
Loading
Loading