Skip to content

Commit 8c11538

Browse files
authored
ref(android): Extract ANR culprit exceptions into AnrCulpritEventHelper (#6593)
* ref(android): Extract ANR culprit exceptions into AnrCulpritEventHelper * ref(android): Trim a redundant Javadoc sentence in AnrCulpritEventHelper * ref(android): Rename AnrCulpritEventHelper methods and take exceptions directly * ref(android): Move AnrCulpritEventHelper into the anr package * license(android): Complete the ANR-WatchDog attribution for ApplicationNotResponding
1 parent c503bd6 commit 8c11538

6 files changed

Lines changed: 188 additions & 50 deletions

File tree

‎THIRD_PARTY_NOTICES.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ limitations under the License.
455455

456456
### Scope
457457

458-
The Sentry Java SDK includes an adapted version of the ANR-WatchDog library for Application Not Responding (ANR) detection on Android. The code resides in `io.sentry.android.core.ANRWatchDog`.
458+
The Sentry Java SDK includes an adapted version of the ANR-WatchDog library for Application Not Responding (ANR) detection on Android. The code resides in `io.sentry.android.core.ANRWatchDog` and `io.sentry.android.core.ApplicationNotResponding` (adapted from `ANRError`).
459459

460460
```
461461
MIT License

‎sentry-android-core/api/sentry-android-core.api‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ public final class io/sentry/android/core/ApplicationExitInfoEventProcessor : io
244244
public fun process (Lio/sentry/protocol/SentryTransaction;Lio/sentry/Hint;)Lio/sentry/protocol/SentryTransaction;
245245
}
246246

247+
public final class io/sentry/android/core/ApplicationNotResponding : java/lang/RuntimeException {
248+
public fun <init> (Ljava/lang/String;)V
249+
public fun getThread ()Ljava/lang/Thread;
250+
}
251+
247252
public final class io/sentry/android/core/BuildConfig {
248253
public static final field BUILD_TYPE Ljava/lang/String;
249254
public static final field DEBUG Z
@@ -716,6 +721,12 @@ public final class io/sentry/android/core/anr/AggregatedStackTrace {
716721
public fun getStack ()[Ljava/lang/StackTraceElement;
717722
}
718723

724+
public final class io/sentry/android/core/anr/AnrCulpritEventHelper {
725+
public fun <init> (Lio/sentry/SentryExceptionFactory;)V
726+
public static fun hasOnlySystemFrames (Ljava/util/List;)Z
727+
public fun toSentryExceptions (Lio/sentry/android/core/anr/AggregatedStackTrace;Lio/sentry/protocol/Mechanism;)Ljava/util/List;
728+
}
729+
719730
public final class io/sentry/android/core/anr/AnrCulpritIdentifier {
720731
public fun <init> ()V
721732
public static fun identify (Ljava/util/List;)Lio/sentry/android/core/anr/AggregatedStackTrace;

‎sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java‎

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.sentry.SentryStackTraceFactory;
4141
import io.sentry.SpanContext;
4242
import io.sentry.android.core.anr.AggregatedStackTrace;
43+
import io.sentry.android.core.anr.AnrCulpritEventHelper;
4344
import io.sentry.android.core.anr.AnrCulpritIdentifier;
4445
import io.sentry.android.core.anr.AnrProfile;
4546
import io.sentry.android.core.anr.AnrProfileManager;
@@ -48,7 +49,6 @@
4849
import io.sentry.android.core.internal.util.CpuInfoUtils;
4950
import io.sentry.cache.PersistingOptionsObserver;
5051
import io.sentry.cache.PersistingScopeObserver;
51-
import io.sentry.exception.ExceptionMechanismException;
5252
import io.sentry.hints.AbnormalExit;
5353
import io.sentry.hints.Backfillable;
5454
import io.sentry.hints.NativeCrashExit;
@@ -64,7 +64,6 @@
6464
import io.sentry.protocol.SdkVersion;
6565
import io.sentry.protocol.SentryException;
6666
import io.sentry.protocol.SentryId;
67-
import io.sentry.protocol.SentryStackFrame;
6867
import io.sentry.protocol.SentryStackTrace;
6968
import io.sentry.protocol.SentryThread;
7069
import io.sentry.protocol.SentryTransaction;
@@ -100,6 +99,7 @@ public final class ApplicationExitInfoEventProcessor
10099
private final @NotNull BuildInfoProvider buildInfoProvider;
101100

102101
private final @NotNull SentryExceptionFactory sentryExceptionFactory;
102+
private final @NotNull AnrCulpritEventHelper culpritEventHelper;
103103

104104
private final @Nullable PersistingScopeObserver persistingScopeObserver;
105105

@@ -121,6 +121,7 @@ public ApplicationExitInfoEventProcessor(
121121
new SentryStackTraceFactory(this.options);
122122

123123
sentryExceptionFactory = new SentryExceptionFactory(sentryStackTraceFactory);
124+
culpritEventHelper = new AnrCulpritEventHelper(sentryExceptionFactory);
124125
}
125126

126127
private @Nullable HintEnricher findEnricher(final @NotNull Object hint) {
@@ -859,7 +860,8 @@ private void setDefaultAnrFingerprint(
859860
return;
860861
}
861862

862-
if (options.isEnableAnrFingerprinting() && hasOnlySystemFrames(event)) {
863+
if (options.isEnableAnrFingerprinting()
864+
&& AnrCulpritEventHelper.hasOnlySystemFrames(event.getExceptions())) {
863865
// If the stacktrace only contains system frames, we want to statically group these events
864866
// to avoid ANR noise due to {{ default }} stacktrace grouping
865867
event.setFingerprints(
@@ -1002,28 +1004,17 @@ private void applyAnrProfile(
10021004
// Capture profile chunk
10031005
final @Nullable SentryId profilerId =
10041006
captureAnrProfile(anrTimestamp, anrProfile, optionsSource);
1005-
final @NotNull StackTraceElement[] stack = culprit.getStack();
1006-
1007-
if (stack.length > 0) {
1008-
final StackTraceElement stackTraceElement = stack[0];
1009-
final String message =
1010-
stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
1011-
final ApplicationNotResponding exception = new ApplicationNotResponding(message);
1012-
exception.setStackTrace(stack);
1013-
1014-
final Mechanism mechanism = new Mechanism();
1015-
mechanism.setType("ANR");
1016-
final ExceptionMechanismException error =
1017-
new ExceptionMechanismException(mechanism, exception, null, false);
1018-
1019-
final @NotNull List<SentryException> sentryException =
1020-
sentryExceptionFactory.getSentryExceptions(error);
1007+
final Mechanism mechanism = new Mechanism();
1008+
mechanism.setType("ANR");
1009+
final @Nullable List<SentryException> sentryExceptions =
1010+
culpritEventHelper.toSentryExceptions(culprit, mechanism);
10211011

1012+
if (sentryExceptions != null) {
10221013
// Replace the original ANR exception with the profile-derived one,
10231014
// as we assume the profiling culprit identification is more valuable
10241015
// the event threads are kept as-is, so the original main thread stacktrace is still
10251016
// available
1026-
event.setExceptions(sentryException);
1017+
event.setExceptions(sentryExceptions);
10271018

10281019
if (profilerId != null) {
10291020
event.getContexts().setProfile(new ProfileContext(profilerId));
@@ -1057,32 +1048,6 @@ private SentryId captureAnrProfile(
10571048
}
10581049
}
10591050

1060-
private boolean hasOnlySystemFrames(@NotNull SentryEvent event) {
1061-
final List<SentryException> exceptions = event.getExceptions();
1062-
if (exceptions == null || exceptions.isEmpty()) {
1063-
return false;
1064-
}
1065-
1066-
for (final SentryException exception : exceptions) {
1067-
final SentryStackTrace stacktrace = exception.getStacktrace();
1068-
if (stacktrace != null) {
1069-
final List<SentryStackFrame> frames = stacktrace.getFrames();
1070-
if (frames != null && !frames.isEmpty()) {
1071-
for (final SentryStackFrame frame : frames) {
1072-
if (frame.isInApp() != null && frame.isInApp()) {
1073-
return false;
1074-
}
1075-
final String module = frame.getModule();
1076-
if (module != null && !AnrCulpritIdentifier.isSystemFrame(module)) {
1077-
return false;
1078-
}
1079-
}
1080-
}
1081-
}
1082-
}
1083-
return true;
1084-
}
1085-
10861051
/**
10871052
* Creates debug metadata for an ANR profile chunk using the build metadata selected for the ANR
10881053
* event.

‎sentry-android-core/src/main/java/io/sentry/android/core/ApplicationNotResponding.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// Adapted from ANR-WatchDog.
2+
// Copyright (c) 2016 Salomon BRYS.
3+
// Licensed under the MIT License.
14
// https://github.com/SalomonBrys/ANR-WatchDog/blob/1969075f75f5980e9000eaffbaa13b0daf282dcb/anr-watchdog/src/main/java/com/github/anrwatchdog/ANRError.java
2-
// Based on the class above. The API unnecessary here was removed.
5+
// The API unnecessary here was removed.
36
package io.sentry.android.core;
47

58
import io.sentry.util.Objects;
9+
import org.jetbrains.annotations.ApiStatus;
610
import org.jetbrains.annotations.NotNull;
711
import org.jetbrains.annotations.Nullable;
812

@@ -14,14 +18,15 @@
1418
* really the cause of the exception. Each "Caused by" is the stack trace of a running thread. Note
1519
* that the main thread always comes first.
1620
*/
17-
final class ApplicationNotResponding extends RuntimeException {
21+
@ApiStatus.Internal
22+
public final class ApplicationNotResponding extends RuntimeException {
1823
private static final long serialVersionUID = 252541144579117016L;
1924

2025
// temp fix for JDK 21: Never serialized.
2126
@SuppressWarnings("serial")
2227
private final @Nullable Thread thread;
2328

24-
ApplicationNotResponding(final @Nullable String message) {
29+
public ApplicationNotResponding(final @Nullable String message) {
2530
super(message);
2631
this.thread = null;
2732
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.sentry.android.core.anr;
2+
3+
import io.sentry.SentryExceptionFactory;
4+
import io.sentry.android.core.ApplicationNotResponding;
5+
import io.sentry.exception.ExceptionMechanismException;
6+
import io.sentry.protocol.Mechanism;
7+
import io.sentry.protocol.SentryException;
8+
import io.sentry.protocol.SentryStackFrame;
9+
import io.sentry.protocol.SentryStackTrace;
10+
import java.util.List;
11+
import org.jetbrains.annotations.ApiStatus;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
14+
15+
@ApiStatus.Internal
16+
public final class AnrCulpritEventHelper {
17+
18+
private final @NotNull SentryExceptionFactory exceptionFactory;
19+
20+
public AnrCulpritEventHelper(final @NotNull SentryExceptionFactory exceptionFactory) {
21+
this.exceptionFactory = exceptionFactory;
22+
}
23+
24+
/**
25+
* The exception message is the class and method of the top frame of the culprit stack.
26+
*
27+
* @return the exceptions, or {@code null} if the culprit stack is empty
28+
*/
29+
public @Nullable List<SentryException> toSentryExceptions(
30+
final @NotNull AggregatedStackTrace culprit, final @NotNull Mechanism mechanism) {
31+
final @NotNull StackTraceElement[] stack = culprit.getStack();
32+
if (stack.length == 0) {
33+
return null;
34+
}
35+
final @NotNull StackTraceElement top = stack[0];
36+
final @NotNull ApplicationNotResponding exception =
37+
new ApplicationNotResponding(top.getClassName() + "." + top.getMethodName());
38+
exception.setStackTrace(stack);
39+
40+
return exceptionFactory.getSentryExceptions(
41+
new ExceptionMechanismException(mechanism, exception, null, false));
42+
}
43+
44+
/**
45+
* A frame is a system frame if it is not in app and its module is a Java, Kotlin, or Android
46+
* framework package, see {@link AnrCulpritIdentifier#isSystemFrame(String)}.
47+
*
48+
* @return {@code true} if there is at least one exception and none of their frames is an app
49+
* frame
50+
*/
51+
public static boolean hasOnlySystemFrames(final @Nullable List<SentryException> exceptions) {
52+
if (exceptions == null || exceptions.isEmpty()) {
53+
return false;
54+
}
55+
56+
for (final @NotNull SentryException exception : exceptions) {
57+
final @Nullable SentryStackTrace stacktrace = exception.getStacktrace();
58+
if (stacktrace != null) {
59+
final @Nullable List<SentryStackFrame> frames = stacktrace.getFrames();
60+
if (frames != null && !frames.isEmpty()) {
61+
for (final @NotNull SentryStackFrame frame : frames) {
62+
if (frame.isInApp() != null && frame.isInApp()) {
63+
return false;
64+
}
65+
final @Nullable String module = frame.getModule();
66+
if (module != null && !AnrCulpritIdentifier.isSystemFrame(module)) {
67+
return false;
68+
}
69+
}
70+
}
71+
}
72+
}
73+
return true;
74+
}
75+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package io.sentry.android.core.anr
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import io.sentry.SentryExceptionFactory
5+
import io.sentry.SentryOptions
6+
import io.sentry.SentryStackTraceFactory
7+
import io.sentry.protocol.Mechanism
8+
import io.sentry.protocol.SentryException
9+
import io.sentry.protocol.SentryStackFrame
10+
import io.sentry.protocol.SentryStackTrace
11+
import kotlin.test.Test
12+
13+
class AnrCulpritEventHelperTest {
14+
15+
private val helper =
16+
AnrCulpritEventHelper(SentryExceptionFactory(SentryStackTraceFactory(SentryOptions())))
17+
18+
@Test
19+
fun `creates exception with class and method of the top frame as message`() {
20+
val stack =
21+
arrayOf(
22+
StackTraceElement("com.example.Foo", "bar", "Foo.java", 12),
23+
StackTraceElement("android.os.Looper", "loop", "Looper.java", 200),
24+
)
25+
val culprit = AggregatedStackTrace(stack, 0, stack.size - 1, 1000L, 0.5f)
26+
27+
val exceptions = helper.toSentryExceptions(culprit, Mechanism().apply { type = "ANR" })
28+
29+
assertThat(exceptions).isNotNull()
30+
assertThat(exceptions!!).hasSize(1)
31+
val exception = exceptions[0]
32+
assertThat(exception.type).isEqualTo("ApplicationNotResponding")
33+
assertThat(exception.module).isEqualTo("io.sentry.android.core")
34+
assertThat(exception.value).isEqualTo("com.example.Foo.bar")
35+
assertThat(exception.mechanism!!.type).isEqualTo("ANR")
36+
assertThat(exception.stacktrace!!.frames!!.map { it.function }).containsExactly("loop", "bar")
37+
}
38+
39+
@Test
40+
fun `returns null for an empty culprit stack`() {
41+
val culprit = AggregatedStackTrace(emptyArray(), 0, -1, 1000L, 0f)
42+
43+
assertThat(helper.toSentryExceptions(culprit, Mechanism())).isNull()
44+
}
45+
46+
@Test
47+
fun `hasOnlySystemFrames is true for java and android frames only`() {
48+
val exceptions = exceptionsWithFrames(frame("java.lang.Object"), frame("android.os.Looper"))
49+
50+
assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(exceptions)).isTrue()
51+
}
52+
53+
@Test
54+
fun `hasOnlySystemFrames is false if one frame is in app`() {
55+
val exceptions =
56+
exceptionsWithFrames(frame("android.os.Looper"), frame("android.os.Handler", inApp = true))
57+
58+
assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(exceptions)).isFalse()
59+
}
60+
61+
@Test
62+
fun `hasOnlySystemFrames is false if one frame is not a system frame`() {
63+
val exceptions = exceptionsWithFrames(frame("android.os.Looper"), frame("com.example.Foo"))
64+
65+
assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(exceptions)).isFalse()
66+
}
67+
68+
@Test
69+
fun `hasOnlySystemFrames is false without exceptions`() {
70+
assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(null)).isFalse()
71+
assertThat(AnrCulpritEventHelper.hasOnlySystemFrames(emptyList())).isFalse()
72+
}
73+
74+
private fun frame(module: String, inApp: Boolean? = null) =
75+
SentryStackFrame().apply {
76+
this.module = module
77+
this.isInApp = inApp
78+
}
79+
80+
private fun exceptionsWithFrames(vararg frames: SentryStackFrame) =
81+
listOf(SentryException().apply { stacktrace = SentryStackTrace(frames.toList()) })
82+
}

0 commit comments

Comments
 (0)