Skip to content
Draft
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.java-version
.idea/
.gradle/
.run/
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Update Android targetSdk to API 36 (Android 16) ([#5016](https://github.com/getsentry/sentry-java/pull/5016))
- Merge Tombstone and Native SDK events into single crash event. ([#5037](https://github.com/getsentry/sentry-java/pull/5037))

### Internal

Expand Down
19 changes: 18 additions & 1 deletion sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,21 @@ public final class io/sentry/android/core/LoadClass : io/sentry/util/LoadClass {
public fun loadClass (Ljava/lang/String;Lio/sentry/ILogger;)Ljava/lang/Class;
}

public final class io/sentry/android/core/NativeEventCollector {
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;)V
public fun collect ()V
public fun deleteNativeEventFile (Lio/sentry/android/core/NativeEventCollector$NativeEventData;)Z
public fun findAndRemoveMatchingNativeEvent (JLjava/lang/String;)Lio/sentry/android/core/NativeEventCollector$NativeEventData;
}

public final class io/sentry/android/core/NativeEventCollector$NativeEventData {
public fun getCorrelationId ()Ljava/lang/String;
public fun getEnvelope ()Lio/sentry/SentryEnvelope;
public fun getEvent ()Lio/sentry/SentryEvent;
public fun getFile ()Ljava/io/File;
public fun getTimestampMs ()J
}

public final class io/sentry/android/core/NdkHandlerStrategy : java/lang/Enum {
public static final field SENTRY_HANDLER_STRATEGY_CHAIN_AT_START Lio/sentry/android/core/NdkHandlerStrategy;
public static final field SENTRY_HANDLER_STRATEGY_DEFAULT Lio/sentry/android/core/NdkHandlerStrategy;
Expand Down Expand Up @@ -339,6 +354,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
public fun getBeforeViewHierarchyCaptureCallback ()Lio/sentry/android/core/SentryAndroidOptions$BeforeCaptureCallback;
public fun getDebugImagesLoader ()Lio/sentry/android/core/IDebugImagesLoader;
public fun getFrameMetricsCollector ()Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;
public fun getNativeCrashCorrelationId ()Ljava/lang/String;
public fun getNativeSdkName ()Ljava/lang/String;
public fun getNdkHandlerStrategy ()I
public fun getStartupCrashDurationThresholdMillis ()J
Expand Down Expand Up @@ -392,6 +408,7 @@ public final class io/sentry/android/core/SentryAndroidOptions : io/sentry/Sentr
public fun setEnableSystemEventBreadcrumbs (Z)V
public fun setEnableSystemEventBreadcrumbsExtras (Z)V
public fun setFrameMetricsCollector (Lio/sentry/android/core/internal/util/SentryFrameMetricsCollector;)V
public fun setNativeCrashCorrelationId (Ljava/lang/String;)V
public fun setNativeHandlerStrategy (Lio/sentry/android/core/NdkHandlerStrategy;)V
public fun setNativeSdkName (Ljava/lang/String;)V
public fun setReportHistoricalAnrs (Z)V
Expand Down Expand Up @@ -500,7 +517,7 @@ public final class io/sentry/android/core/TombstoneIntegration$TombstoneHint : i
}

public class io/sentry/android/core/TombstoneIntegration$TombstonePolicy : io/sentry/android/core/ApplicationExitInfoHistoryDispatcher$ApplicationExitInfoPolicy {
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;)V
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;Landroid/content/Context;)V
public fun buildReport (Landroid/app/ApplicationExitInfo;Z)Lio/sentry/android/core/ApplicationExitInfoHistoryDispatcher$Report;
public fun getLabel ()Ljava/lang/String;
public fun getLastReportedTimestamp ()Ljava/lang/Long;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.sentry.android.core.NdkIntegration.SENTRY_NDK_CLASS_NAME;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
Expand Down Expand Up @@ -57,8 +58,10 @@
import io.sentry.util.Objects;
import io.sentry.util.thread.NoOpThreadChecker;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
Expand Down Expand Up @@ -244,6 +247,12 @@ static void initializeIntegrationsAndProcessors(
if (options.getSocketTagger() instanceof NoOpSocketTagger) {
options.setSocketTagger(AndroidSocketTagger.getInstance());
}

// Set native crash correlation ID before NDK integration is registered
if (buildInfoProvider.getSdkInfoVersion() >= Build.VERSION_CODES.R) {
setNativeCrashCorrelationId(context, options);
}

if (options.getPerformanceCollectors().isEmpty()) {
options.addPerformanceCollector(new AndroidMemoryCollector());
options.addPerformanceCollector(new AndroidCpuCollector(options.getLogger()));
Expand Down Expand Up @@ -497,4 +506,33 @@ private static void readDefaultOptionValues(
static @NotNull File getCacheDir(final @NotNull Context context) {
return new File(context.getCacheDir(), "sentry");
}

/**
* Sets a native crash correlation ID that can be used to associate native crash events (from
* sentry-native) with tombstone events (from ApplicationExitInfo). The ID is stored via
* ActivityManager.setProcessStateSummary() and passed to the native SDK.
*
* @param context the Application context
* @param options the SentryAndroidOptions
*/
private static void setNativeCrashCorrelationId(
final @NotNull Context context, final @NotNull SentryAndroidOptions options) {
final String correlationId = UUID.randomUUID().toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As anyone could call this API to store information, it might be a good idea to ensure it's our data we're reading back later. I guess a simple prefix could work e.g. sentry-app-run-<uuid>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, and this is only meant as a proposal that survived from 2022:

  • processStateSummary is global and generally owned by the app
  • If we write into this, we should first check if there is already something in there
  • Even if nothing was in there, any later code can overwrite it without us knowing anything about it
  • The maximum length is 128 bytes, and writing into it is free-form, so there is no way for binary stability except if we communicate that Sentry owns this if you use the SDK (not sure if that is a good idea).
  • If we prefix it with something generic like sentry-app-run-, it shows that this might be used beyond native crash correlation (it will be accessible from any ApplicationExitInfo).

I implemented this because we considered it a stable correlation mechanism back then, but I think timestamp correlation is stable enough and doesn't depend on app-owned storage.

It's likely a good idea to drop this altogether, and if we introduce it, maybe together with other ApplicationExitInfo handlers?

options.setNativeCrashCorrelationId(correlationId);

try {
final ActivityManager am =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (am != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
am.setProcessStateSummary(correlationId.getBytes(StandardCharsets.UTF_8));
options
.getLogger()
.log(SentryLevel.DEBUG, "Native crash correlation ID set: %s", correlationId);
}
} catch (Throwable e) {
options
.getLogger()
.log(SentryLevel.WARNING, "Failed to set process state summary for correlation ID", e);
}
}
}
Loading
Loading