OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base; | 5 package org.chromium.base; |
6 | 6 |
7 import android.support.annotation.UiThread; | 7 import android.support.annotation.UiThread; |
8 | 8 |
9 import org.chromium.base.annotations.CalledByNative; | 9 import org.chromium.base.annotations.CalledByNative; |
10 import org.chromium.base.annotations.JNINamespace; | 10 import org.chromium.base.annotations.JNINamespace; |
11 import org.chromium.base.annotations.MainDex; | 11 import org.chromium.base.annotations.MainDex; |
12 | 12 |
13 /** | 13 /** |
14 * This UncaughtExceptionHandler will create a breakpad minidump when there is a
n uncaught | 14 * This UncaughtExceptionHandler will create a breakpad minidump when there is a
n uncaught |
15 * exception. | 15 * exception. |
16 * | 16 * |
17 * The exception's stack trace will be added to the minidump's data. This allows
java-only crashes | 17 * The exception's stack trace will be added to the minidump's data. This allows
java-only crashes |
18 * to be reported in the same way as other native crashes. | 18 * to be reported in the same way as other native crashes. |
19 */ | 19 */ |
20 @JNINamespace("base::android") | 20 @JNINamespace("base::android") |
21 @MainDex | 21 @MainDex |
22 public class JavaExceptionReporter implements Thread.UncaughtExceptionHandler { | 22 public class JavaExceptionReporter implements Thread.UncaughtExceptionHandler { |
23 private Thread.UncaughtExceptionHandler mParent; | 23 private final Thread.UncaughtExceptionHandler mParent; |
| 24 private final boolean mCrashAfterReport; |
24 private boolean mHandlingException; | 25 private boolean mHandlingException; |
25 | 26 |
26 private JavaExceptionReporter(Thread.UncaughtExceptionHandler parent) { | 27 private JavaExceptionReporter( |
| 28 Thread.UncaughtExceptionHandler parent, boolean crashAfterReport) { |
27 mParent = parent; | 29 mParent = parent; |
| 30 mCrashAfterReport = crashAfterReport; |
28 } | 31 } |
29 | 32 |
30 @Override | 33 @Override |
31 public void uncaughtException(Thread t, Throwable e) { | 34 public void uncaughtException(Thread t, Throwable e) { |
32 if (!mHandlingException) { | 35 if (!mHandlingException) { |
33 mHandlingException = true; | 36 mHandlingException = true; |
34 nativeReportJavaException(e); | 37 nativeReportJavaException(mCrashAfterReport, e); |
35 } | 38 } |
36 if (mParent != null) { | 39 if (mParent != null) { |
37 mParent.uncaughtException(t, e); | 40 mParent.uncaughtException(t, e); |
38 } | 41 } |
39 } | 42 } |
40 | 43 |
41 /** | 44 /** |
42 * Report and upload the stack trace as if it was a crash. This is very expe
nsive and should | 45 * Report and upload the stack trace as if it was a crash. This is very expe
nsive and should |
43 * be called rarely and only on the UI thread to avoid corrupting other cras
h uploads. Ideally | 46 * be called rarely and only on the UI thread to avoid corrupting other cras
h uploads. Ideally |
44 * only called in idle handlers. | 47 * only called in idle handlers. |
45 * | 48 * |
46 * @param stackTrace The stack trace to report. | 49 * @param stackTrace The stack trace to report. |
47 */ | 50 */ |
48 @UiThread | 51 @UiThread |
49 public static void reportStackTrace(String stackTrace) { | 52 public static void reportStackTrace(String stackTrace) { |
50 assert ThreadUtils.runningOnUiThread(); | 53 assert ThreadUtils.runningOnUiThread(); |
51 nativeReportJavaStackTrace(stackTrace); | 54 nativeReportJavaStackTrace(stackTrace); |
52 } | 55 } |
53 | 56 |
54 @CalledByNative | 57 @CalledByNative |
55 private static void installHandler() { | 58 private static void installHandler(boolean crashAfterReport) { |
56 Thread.setDefaultUncaughtExceptionHandler( | 59 Thread.setDefaultUncaughtExceptionHandler(new JavaExceptionReporter( |
57 new JavaExceptionReporter(Thread.getDefaultUncaughtExceptionHand
ler())); | 60 Thread.getDefaultUncaughtExceptionHandler(), crashAfterReport)); |
58 } | 61 } |
59 | 62 |
60 private static native void nativeReportJavaException(Throwable e); | 63 private static native void nativeReportJavaException(boolean crashAfterRepor
t, Throwable e); |
61 private static native void nativeReportJavaStackTrace(String stackTrace); | 64 private static native void nativeReportJavaStackTrace(String stackTrace); |
62 } | 65 } |
OLD | NEW |