从JNI调用exception.printStackTrace()JNI、exception、printStackTrace

由网友(用生命在耍帅)分享简介:现在的问题是找出哪些Java函数调用一些JNI函数。在Java中,这将与新Exception.printStackTrace()实现,但必须从本地进行(JNI)的功能。The problem is to find out which Java functions call some JNI function. In...

现在的问题是找出哪些Java函数调用一些JNI函数。在Java中,这将与新Exception.printStackTrace()实现,但必须从本地进行(JNI)的功能。

The problem is to find out which Java functions call some JNI function. In Java, this would be achieved with new Exception.printStackTrace(), but this must be done from a native (JNI) function.

由于后来发现自己的code中的最简单方法是在网络出版,我张贴这两个问题的答案。

Since the easiest way to find your own code later is to publish it in the 'net, I post both the question and the answer.

推荐答案

的JNI模拟新Exception.printStackTrace()是:

//#include <android/log.h>
//#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , "~~~~~~", __VA_ARGS__)
//#define DLOG(...) __android_log_print(ANDROID_LOG_DEBUG  , "~~~~~~", __VA_ARGS__)
void printStackTrace(JNIEnv *env) {
LOGD("###################################################################################printStackTrace{");
    jclass cls = env->FindClass("java/lang/Exception");
    if (cls != NULL) {
        jmethodID constructor = env->GetMethodID(cls, "<init>", "()V");
        if(constructor != NULL) {
            jobject exc = env->NewObject(cls, constructor);
            if(exc != NULL) {
                jmethodID printStackTrace = env->GetMethodID(cls, "printStackTrace", "()V");
                if(printStackTrace != NULL) {
                    env->CallObjectMethod(exc, printStackTrace);
                } else { DLOG("err4"); }
            } else { DLOG("err3"); }
            env->DeleteLocalRef(exc);
        } else { DLOG("err2"); }
    } else { DLOG("err1"); }
    /* free the local ref */
    env->DeleteLocalRef(cls);
LOGD("###################################################################################printStackTrace}");
}
阅读全文

相关推荐

最新文章