From 48760f6960ffa2e7dc8411d6a28c47bc961a2000 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Mon, 12 Jun 2017 10:43:05 -0700 Subject: [PATCH] Binder: Add more logging on Error Try to add the Error's message to the abort, so that it shows up when the logcat is missing. Sample message: 'jni_internal.cc:508] JNI FatalError called: java.lang.Error thrown during binder transaction: java.lang.LinkageError: This is a test.' (cherry picked from commit 58383ba095f552a58ed416736fe049126c647ffd) Bug: 62514767 Test: m Change-Id: I12026bb7a8ec9438db493f135ed5d0177187f702 --- core/jni/android_util_Binder.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/core/jni/android_util_Binder.cpp b/core/jni/android_util_Binder.cpp index de67c50e71959..26b00344b789d 100644 --- a/core/jni/android_util_Binder.cpp +++ b/core/jni/android_util_Binder.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -194,10 +195,34 @@ static void report_exception(JNIEnv* env, jthrowable excep, const char* msg) /* * It's an Error: Reraise the exception and ask the runtime to abort. */ + + // Try to get the exception string. Sometimes logcat isn't available, + // so try to add it to the abort message. + std::string exc_msg = "(Unknown exception message)"; + { + ScopedLocalRef exc_class(env, env->GetObjectClass(excep)); + jmethodID method_id = env->GetMethodID(exc_class.get(), + "toString", + "()Ljava/lang/String;"); + ScopedLocalRef jstr( + env, + reinterpret_cast( + env->CallObjectMethod(excep, method_id))); + env->ExceptionClear(); // Just for good measure. + if (jstr.get() != nullptr) { + ScopedUtfChars jstr_utf(env, jstr.get()); + exc_msg = jstr_utf.c_str(); + } + } + env->Throw(excep); ALOGE("java.lang.Error thrown during binder transaction (stack trace follows) : "); env->ExceptionDescribe(); - env->FatalError("java.lang.Error thrown during binder transaction."); + + std::string error_msg = base::StringPrintf( + "java.lang.Error thrown during binder transaction: %s", + exc_msg.c_str()); + env->FatalError(error_msg.c_str()); } bail: