0

我正在用本机代码编写一个函数,并想从函数内部了解其他函数调用它的内容,所以我决定java.lang.Thread.dumpStack在我的 c 代码中使用,但看起来不起作用。请问有人知道其他可能的方法吗?非常感谢!

下面是代码:

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
    __android_log_print(ANDROID_LOG_INFO, "TIM", "JNI_OnLoad Called");

    JNIEnv *env = NULL;

    if (vm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK)
    {
            __android_log_print(ANDROID_LOG_INFO, "TIM", "GetEnv Failed");
            return JNI_ERR;
    }

    __android_log_print(ANDROID_LOG_INFO, "TIM", "GetEnv Called");

    // public static void dumpStack ()

    jclass cls2 = env->FindClass("java/lang/Thread");
    if(!cls2)
    {
        __android_log_print(ANDROID_LOG_INFO, "TIM", "FindClass  Failed");
        return JNI_ERR;
    }

    __android_log_print(ANDROID_LOG_INFO, "TIM", "FinClass Called");


    jmethodID mid = env->GetStaticMethodID(cls2, "dumpStack", "()V");
    if(!mid)
    {
        __android_log_print(ANDROID_LOG_INFO, "TIM", "GetStaticMethodID loadLibrary Failed");
        return JNI_ERR;
    }

    __android_log_print(ANDROID_LOG_INFO, "TIM", "GetStaticMethodID Called");

    env->CallStaticVoidMethod(cls2, mid);

    return JNI_VERSION_1_6;
}

这是我得到的错误:

04-24 11:44:48.217: D/dalvikvm(4140): Trying to load lib /data/data/com.tim.myjni/lib/libmyjni.so 0x413486d8
04-24 11:44:48.267: D/dalvikvm(4140): Added shared lib /data/data/com.tim.myjni/lib/libmyjni.so 0x413486d8
04-24 11:44:48.267: I/TIM(4140): JNI_OnLoad Called
04-24 11:44:48.277: I/TIM(4140): GetEnv Called
04-24 11:44:48.277: I/TIM(4140): FinClass Called
04-24 11:44:48.277: I/TIM(4140): GetStaticMethodID Called
04-24 11:44:48.287: W/System.err(4140): java.lang.Throwable: stack dump
04-24 11:44:48.348: W/System.err(4140):     at java.lang.Thread.dumpStack(Thread.java:496)
04-24 11:44:48.348: W/System.err(4140):     at java.lang.Runtime.nativeLoad(Native Method)
04-24 11:44:48.377: W/System.err(4140):     at java.lang.Runtime.loadLibrary(Runtime.java:368)
04-24 11:44:48.377: W/System.err(4140):     at java.lang.System.loadLibrary(System.java:535)
04-24 11:44:48.407: W/System.err(4140):     at com.tim.myjni.MainActivity.<clinit>(MainActivity.java:53)
04-24 11:44:48.407: W/System.err(4140):     at java.lang.Class.newInstanceImpl(Native Method)
04-24 11:44:48.417: W/System.err(4140):     at java.lang.Class.newInstance(Class.java:1319)
04-24 11:44:48.417: W/System.err(4140):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
04-24 11:44:48.417: W/System.err(4140):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
04-24 11:44:48.417: W/System.err(4140):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-24 11:44:48.417: W/System.err(4140):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-24 11:44:48.417: W/System.err(4140):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-24 11:44:48.447: W/System.err(4140):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 11:44:48.447: W/System.err(4140):     at android.os.Looper.loop(Looper.java:137)
04-24 11:44:48.467: W/System.err(4140):     at android.app.ActivityThread.main(ActivityThread.java:4340)
04-24 11:44:48.467: W/System.err(4140):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 11:44:48.477: W/System.err(4140):     at java.lang.reflect.Method.invoke(Method.java:511)
04-24 11:44:48.477: W/System.err(4140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 11:44:48.477: W/System.err(4140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 11:44:48.477: W/System.err(4140):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

那不是错误,那是堆栈跟踪。它只是碰巧像一个例外一样被打印出来。

线程中的代码是:

public static void dumpStack() {
    new Throwable("stack dump").printStackTrace();
}

这就是你在日志中的内容——注意第一行:

W/System.err(4140): java.lang.Throwable: stack dump

于 2013-04-24T17:16:00.470 回答