• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/android/java_exception_reporter.h"
6 
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/debug/dump_without_crashing.h"
10 #include "jni/JavaExceptionReporter_jni.h"
11 
12 using base::android::JavaParamRef;
13 
14 namespace base {
15 namespace android {
16 
17 namespace {
18 
19 void (*g_java_exception_callback)(const char*);
20 
21 }  // namespace
22 
InitJavaExceptionReporter()23 void InitJavaExceptionReporter() {
24   JNIEnv* env = base::android::AttachCurrentThread();
25   constexpr bool crash_after_report = false;
26   Java_JavaExceptionReporter_installHandler(env, crash_after_report);
27 }
28 
InitJavaExceptionReporterForChildProcess()29 void InitJavaExceptionReporterForChildProcess() {
30   JNIEnv* env = base::android::AttachCurrentThread();
31   constexpr bool crash_after_report = true;
32   Java_JavaExceptionReporter_installHandler(env, crash_after_report);
33 }
34 
SetJavaExceptionCallback(void (* callback)(const char *))35 void SetJavaExceptionCallback(void (*callback)(const char*)) {
36   DCHECK(!g_java_exception_callback);
37   g_java_exception_callback = callback;
38 }
39 
SetJavaException(const char * exception)40 void SetJavaException(const char* exception) {
41   DCHECK(g_java_exception_callback);
42   g_java_exception_callback(exception);
43 }
44 
JNI_JavaExceptionReporter_ReportJavaException(JNIEnv * env,const JavaParamRef<jclass> & jcaller,jboolean crash_after_report,const JavaParamRef<jthrowable> & e)45 void JNI_JavaExceptionReporter_ReportJavaException(
46     JNIEnv* env,
47     const JavaParamRef<jclass>& jcaller,
48     jboolean crash_after_report,
49     const JavaParamRef<jthrowable>& e) {
50   std::string exception_info = base::android::GetJavaExceptionInfo(env, e);
51   SetJavaException(exception_info.c_str());
52   if (crash_after_report) {
53     LOG(ERROR) << exception_info;
54     LOG(FATAL) << "Uncaught exception";
55   }
56   base::debug::DumpWithoutCrashing();
57   SetJavaException(nullptr);
58 }
59 
JNI_JavaExceptionReporter_ReportJavaStackTrace(JNIEnv * env,const JavaParamRef<jclass> & jcaller,const JavaParamRef<jstring> & stackTrace)60 void JNI_JavaExceptionReporter_ReportJavaStackTrace(
61     JNIEnv* env,
62     const JavaParamRef<jclass>& jcaller,
63     const JavaParamRef<jstring>& stackTrace) {
64   SetJavaException(ConvertJavaStringToUTF8(stackTrace).c_str());
65   base::debug::DumpWithoutCrashing();
66   SetJavaException(nullptr);
67 }
68 
69 }  // namespace android
70 }  // namespace base
71