calling exception.printStackTrace() from JNI

前端 未结 2 1947
孤独总比滥情好
孤独总比滥情好 2021-01-26 00:29

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

2条回答
  •  甜味超标
    2021-01-26 01:23

    The JNI analog of new Exception.printStackTrace() is:

    //#include 
    //#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, "", "()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}");
    }
    

提交回复
热议问题