0

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.

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.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127

2 Answers2

5

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

//#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}");
}
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

By the way, you are able to throw an exception from native layer to Java:) Something like this:

jint throwOutOfMemoryError( JNIEnv *env, char *message ){
    jclass       exClass;
    char *className = "java/lang/OutOfMemoryError" ;

    exClass = (*env)->FindClass( env, className );
    if ( exClass == NULL ){
        return throwNoClassDefError( env, className );
    }
    return (*env)->ThrowNew( env, exClass, message );
}

Or if you have an instance of Exception, just throw it into Java layer, and then get a stack trace in Java.

Taras
  • 2,526
  • 3
  • 33
  • 63