jni异常处理
在android ndk开发过程中,调用java对象方法可能会抛异常,如果在ndk层中不做任何处理,那么程序就会直接崩溃。例如我们要在jni层获取apk的签名,代码如下
**jclass native_class = env->GetObjectClass(const_cast(contextObject));
jmethodID pm_id = env->GetMethodID(native_class, "getPackageManager", "()Landroid/content/pm/PackageManager;");
jobject pm_obj = env->CallObjectMethod(const_cast(contextObject), pm_id);
jclass pm_clazz = env->GetObjectClass(pm_obj);
jmethodID package_info_id = env->GetMethodID(pm_clazz, "getPackageInfo","(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
// 获得应用包的信息
jobject pi_obj = NULL;
//GET_SIGNATURES 64
pi_obj = env->CallObjectMethod(pm_obj, package_info_id, package_name, 64);
...**
上述代码如果package_name
查找不到就会报如下异常
NameNotFoundException
捕获异常
jthowable except = env -> ExceptionOccurred();
if(except){
env -> ExceptionDescribe();
env -> ExceptionClear()
}
释放资源
template
void clearRef(JNIEnv* env, const T& t){
env -> DeleteLocalRef(t);
}
template
void clearRef(JNIEnv *env, const T& t, const Args&... args){
env -> DeleteLocalRef(t);
clearRef(env, args...);
}
Native 提供了 ExceptionOccurred
和 ExceptionCheck
方法来检测是否有异常发生,前者返回的是 jthrowable
类型,后者返回的是 jboolean
类型。如果有异常,会通过 ExceptionDescribe
方法来打印异常信息,方便我们在 LogCat 中看到对应的信息。而 ExceptionClear
方法则是关键的不会让应用直接崩溃的方法,类似于 Java 的 catch
捕获异常处理,它会消除这次异常。这样就把由 Native 调用 Java 时的一个异常进行了处理,当处理完异常之后,别忘了释放对应的资源。
不过,我们这样仅仅是消除了这次异常,还应该让调用者有异常的发生,那么就需要通过 Native 来抛出一个异常告诉 Java 调用者了。
void throwByName(JNIEnv *env, const char *name, const char *msg) {
jclass cls = env->FindClass(name);
if (cls != NULL) {
env->ThrowNew(cls, msg);
}
env->DeleteLocalRef(cls);
}
// 调用抛出异常
extern "C"
JNIEXPORT void JNICALL
Java_com_glumes_cppso_jnioperations_ExceptionOps_nativeThrowException(JNIEnv *env, jobject instance) {
throwByName(env, "java/lang/IllegalArgumentException", "native throw exception");
}
jni错误日志分析
ndk发生崩溃的时候会在/data/tombstones文件夹下生成一个墓碑文件,形如tombstone_xx。谷歌也提供了addr2line/ objdump/ndk-stack工具。
ndk崩溃信号表
一个典型的墓碑文件内容如下
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'Android-x86/android_x86/x86:5.1.1/LMY48W/woshijpf04211939:eng/test-keys'
Revision: '0'
ABI: 'x86'
pid: 1019, tid: 1019, name: surfaceflinger >>> /system/bin/surfaceflinger <<<
signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x4
eax a6265c06 ebx b7467d88 ecx b7631a22 edx a6265c06
esi 00000000 edi b6867140
xcs 00000073 xds 0000007b xes 0000007b xfs 00000000 xss 0000007b
eip b745a639 ebp bfcfc1e8 esp bfcfc150 flags 00010282
backtrace:
#00 pc 00006639 /system/lib/libui.so (android::Fence::waitForever(char const*)+41)
#01 pc 00034b86 /system/lib/libsurfaceflinger.so
#02 pc 0003229e /system/lib/libsurfaceflinger.so
#03 pc 0002cb9c /system/lib/libgui.so (android::BufferQueue::ProxyConsumerListener::onFrameAvailable(android::BufferItem const&)+652)
#04 pc 000342f4 /system/lib/libgui.so (android::BufferQueueProducer::queueBuffer(int, android::IGraphicBufferProducer::QueueBufferInput const&, android::IGraphicBufferProducer::QueueBufferOutput*)+2580)
#05 pc 0004eafb /system/lib/libgui.so (android::Surface::queueBuffer(ANativeWindowBuffer*, int)+411)
#06 pc 0004ce06 /system/lib/libgui.so (android::Surface::hook_queueBuffer(ANativeWindow*, ANativeWindowBuffer*, int)+38)
#07 pc 00014bc6 /system/lib/egl/libGLES_android.so
#08 pc 00017f73 /system/lib/egl/libGLES_android.so (eglSwapBuffers+163)
#09 pc 00015fdb /system/lib/libEGL.so (eglSwapBuffers+203)
#10 pc 000013ea /system/lib/hw/hwcomposer.x86.so
#11 pc 00034730 /system/lib/libsurfaceflinger.so
#12 pc 000256d4 /system/lib/libsurfaceflinger.so
#13 pc 00024bf4 /system/lib/libsurfaceflinger.so
#14 pc 000236fb /system/lib/libsurfaceflinger.so
#15 pc 0002338a /system/lib/libsurfaceflinger.so
#16 pc 0001e0ff /system/lib/libsurfaceflinger.so
#17 pc 0001d9ce /system/lib/libutils.so (android::Looper::pollInner(int)+926)
#18 pc 0001db73 /system/lib/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+67)
#19 pc 0001e561 /system/lib/libsurfaceflinger.so
#20 pc 00022ce7 /system/lib/libsurfaceflinger.so (android::SurfaceFlinger::run()+39)
#21 pc 00000ca3 /system/bin/surfaceflinger
#22 pc 0001365a /system/lib/libc.so (__libc_init+106)
#23 pc 00000da8 /system/bin/surfaceflinger
stack:
bfcfc110 00000000
bfcfc114 b6839270
bfcfc118 00000000
bfcfc11c 00000000
bfcfc120 b68394e0
bfcfc124 00000002
bfcfc128 00000002
bfcfc12c b75d8185 /system/lib/libutils.so (android::RefBase::incStrong(void const*) const+53)
bfcfc130 b6839270
bfcfc134 bfcfc1e8 [stack]
bfcfc138 00000002
bfcfc13c a6265c06
bfcfc140 b7467d88 /system/lib/libui.so
bfcfc144 00000000
bfcfc148 b6867140
bfcfc14c b745a639 /system/lib/libui.so (android::Fence::waitForever(char const*)+41)
#00 bfcfc150 b683af18
bfcfc154 bfcfc1e8 [stack]
bfcfc158 00000000
bfcfc15c 00000000
bfcfc160 00000000
bfcfc164 b683af18
bfcfc168 b75ec9c4 /system/lib/libutils.so
bfcfc16c b75d8285 /system/lib/libutils.so (android::RefBase::weakref_type::decWeak(void const*)+37)
bfcfc170 00000000
bfcfc174 00000000
bfcfc178 00000000
bfcfc17c 00000000
bfcfc180 b7642968 /system/lib/libsurfaceflinger.so
bfcfc184 bfcfc1e8 [stack]
bfcfc188 b6867140
bfcfc18c b7622b87 /system/lib/libsurfaceflinger.so
文件结构
Build fingerprint
Crashed process and PIDs
Terminated signal and fault address
CPU registers
Call stack
Stack content of each call
定位工具
- addr2line
qinqundeMacBook-Pro% ./aarch64-linux-android-addr2line -C -f -e ~/Documents/DProtect2/mylibrary/build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so 0000000000157efc
Java_com_dofun_dprotect_lib_LocalWork_start
/Users/qinqun/Documents/DProtect2/mylibrary/src/main/cpp/native-lib.cpp:147
- ndk-stack
jni内存泄漏分析
Abort message: 'JNI DETECTED ERROR IN APPLICATION: a thread (tid 30253 is making JNI calls without being attached
JavaVM* vm ;
env -> GetJavaVM(&vm);
if (vm -> AttachCurrentThread(&env, NULL) != JNI_OK){
return Error::error_sdk_inner;
}
vm -> DetachCurrentThread();