Report native info when handling Java exceptions
Currently, when the Chromium JNI framework is faced with a Java
exception as control returns to native code, the exception is passed
back to Java to trigger a Java crash.
Sadly, this means that, from an Android (not Crashpad) crash reporting
perspective, the native stack trace is lost, since Android Java crash
reports only include the Java stack trace, not the native stack trace.
This CL fixes this problem by including the native stack trace in the
message of the Java exception that we crash with, thus ensuring that it
is included in the resulting crash report. Therefore, we get complete
Java *and* native stack information together in one crash report.
We also take the opportunity to log that same native stack trace to the
Android log, just in case something unexpected happens and it doesn't
get logged by the Android Java uncaught exception handler.
Bug: 1426888
Change-Id: I5d5a21ae415bd2704d6b4d967497ec97256e8fb5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4909845
Commit-Queue: Etienne Dechamps <[email protected]>
Reviewed-by: Richard (Torne) Coles <[email protected]>
Code-Coverage: [email protected] <[email protected]>
Reviewed-by: Stefano Duo <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1219487}
diff --git a/base/android/jni_android.cc b/base/android/jni_android.cc
index 8ab3f09..f44a688 100644
--- a/base/android/jni_android.cc
+++ b/base/android/jni_android.cc
@@ -342,12 +342,11 @@
<< "Uncaught Java exception in native code. Please include the Java "
"exception stack from the Android log in your crash report.";
- // TODO: this terminates the process from the Java side, which means the crash
- // is treated as a Java crash, as opposed to a native crash. This is what we
- // want (the true root cause of the crash is a Java exception, after all), but
- // that also means we don't get any information about the native side of the
- // stack. We should try to mitigate that in some way.
- Java_JniAndroid_handleException(env, throwable);
+ const std::string native_stack_trace = base::debug::StackTrace().ToString();
+ LOG(ERROR) << "Native stack trace:" << std::endl << native_stack_trace;
+
+ Java_JniAndroid_handleException(
+ env, throwable, ConvertUTF8ToJavaString(env, native_stack_trace));
// Ideally handleException() should have terminated the process and we should
// not get here. In the unlikely case it didn't, we need to do that ourselves.