我的应用程序在jni本机代码中崩溃,核心文件中的堆栈为:
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffc6d478000
Core was generated by `java -jar -Xms8g -Xmx8g -XX:+UseG1GC -XX:MaxDirectMemorySize=6g -XX:ErrorFile=/'.
Program terminated with signal 6, Aborted.
#0 0x00007f21dad7d495 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install jdk-1.8.0-45.x86_64
(gdb) bt
#0 0x00007f21dad7d495 in raise () from /lib64/libc.so.6
#1 0x00007f21dad7ec75 in abort () from /lib64/libc.so.6
#2 0x00007f21da6926b5 in os::abort(bool) () from /usr/local/jdk1.8.0_45/jre/lib/amd64/server/libjvm.so
#3 0x00007f21da82fda3 in VMError::report_and_die() () from /usr/local/jdk1.8.0_45/jre/lib/amd64/server/libjvm.so
#4 0x00007f21da697bdf in JVM_handle_linux_signal () from /usr/local/jdk1.8.0_45/jre/lib/amd64/server/libjvm.so
#5 0x00007f21da68e493 in signalHandler(int, siginfo*, void*) () from /usr/local/jdk1.8.0_45/jre/lib/amd64/server/libjvm.so
#6 <signal handler called>
#7 0x00007f21da45fb82 in jni_SetObjectField () from /usr/local/jdk1.8.0_45/jre/lib/amd64/server/libjvm.so
#8 0x00007f2037c426c2 in JNIEnv_::SetObjectField(_jobject*, _jfieldID*, _jobject*) ()
我可以看到方法jni_SetObjectField中的0x00007f21da45fb82行是:
0x00007f21da45fb5b <+123>: jne 0x7f21da45fb68 <jni_SetObjectField+136>
0x00007f21da45fb5d <+125>: mov 0x30(%r14),%eax
0x00007f21da45fb61 <+129>: test $0x30000000,%eax
0x00007f21da45fb66 <+134>: je 0x7f21da45fb70 <jni_SetObjectField+144>
0x00007f21da45fb68 <+136>: mov %r14,%rdi
0x00007f21da45fb6b <+139>: callq 0x7f21da7dc360 <_ZN10JavaThread44check_safepoint_and_suspend_for_native_transEPS_>
0x00007f21da45fb70 <+144>: mov 0x876ed9(%rip),%rax # 0x7f21dacd6a50
0x00007f21da45fb77 <+151>: movl $0x6,0x270(%r14)
0x00007f21da45fb82 <+162>: mov (%r15),%rdx
0x00007f21da45fb85 <+165>: cmpb $0x0,(%rax)
0x00007f21da45fb88 <+168>: je 0x7f21da45fd30 <jni_SetObjectField+592>
似乎check_safepoint_and_suspend_for_native_trans抛出信号6,然后应用程序崩溃了。
我的jni代码是:
const char* strA = env->GetStringUTFChars(A, NULL);
const char* strN = env->GetStringUTFChars(N, NULL);
struct Result result = ... // read data from Shared memory
jclass objectClass = (env)->FindClass("com/example/Result");
jfieldID str = (env)->GetFieldID(objectClass,"data_","Ljava/lang/String;");
jfieldID ival = (env)->GetFieldID(objectClass,"error_","I");
jobject jResult = env->AllocObject(objectClass);
const char* strValue = result.txt_.c_str();
jstring value = (env)->NewStringUTF(strValue);
(env)->SetObjectField(jResult,str,value);
(env)->SetIntField(jResult,ival,result.error_);
(env)->ReleaseStringUTFChars(A, strA);
(env)->ReleaseStringUTFChars(N, strN);
return jResult;
我该如何解决? JNI代码中是否有错误?还是jdk错误?我的jdk版本是1.8.0-45。