我正在将橡皮擦数据竞争检测算法实现为 JVMTI 代理。当我尝试运行一些示例输入来测试我的代码时,JVM 崩溃,转储如下(也可能显示相同错误的其他堆栈跟踪):
FATAL ERROR in native method: Using JNIEnv in the wrong thread
at Proxy.monitor_enter_(Native Method)
at Proxy.monitor_enter(Proxy.java:30)
at ex1.LifeThreads.setNeighborThreadChange(LifeThreads.java:36)
at ex1.LifeThreads.neighbor(LifeThreads.java:425)
at ex1.LifeThreads.standardItr(LifeThreads.java:321)
at ex1.LifeThreads.run(LifeThreads.java:462)
(这种事后跟踪可以通过 Sun JVM 的 -Xcheck:jni 选项获得)
在代码中,我执行了与各种 JDK 示例(heapViewer、heapTracker 等通过具有本机方法的一些代理 java 类)中所示相同的检测。在每条指令Proxy.monitor_enter_
之后调用本机方法。monitorenter
这是 monitor_enter_ 的代码:
void native_monitor_exit(JNIEnv *jni, jclass klass, jthread thread_id, jobject obj)
{
scoped_lock( agent::instance()->jvmti(), agent::instance()->monitor_ );
if( agent::instance()->death_active_)
return;
std::string name = agent::instance()->thread_name( thread_id );
thread_t* thread = get_thread( thread_id );
if( thread == 0 )
return;
jobject global_ref = agent::instance()->jni()->NewGlobalRef( obj );
if( global_ref == 0 )
fatal_error("Out of memory while trying to create new global ref.");
logger::instance()->level(1) << "MONITOR ENTER"
<< "\n\t" << "jthread name= " << name
<< "\n\t" << "thread_t= " << thread << " " << *thread
<< "\n\t" << "monitor gl= " << global_ref
<< std::endl;
thread->lock( lock(global_ref) );
}
,其中scoped_lock
基本上是 JVMTI Raw Monitor 进入/退出的范围锁,
thread_t
只是一个包装 some 的结构std::vector
,其中存储类的实例lock
(它本身仅包装jobject
全局引用global_ref
),当thread->lock( lock(global_ref))
被调用时。
JVMTI 环境。在代理单例中全局缓存,而线程本地的 JNI env 在每次使用之前都会重新加载(效率不高,但现在我不在乎),如下所示:
JNIEnv* jni()
{
jint res;
JNIEnv* env = 0;
res = jvm()->GetEnv( (void**)&env, JNI_VERSION_1_2 );
if( res == JNI_EDETACHED )
{
res = jvm()->AttachCurrentThread( (void **)&env, 0 );
if( res != JNI_OK || env == 0 )
fatal_error( "ERROR: Unable to create JNIEnv by attach, error=%d\n", res );
}
else if( res != JNI_OK || env == 0 )
fatal_error( "ERROR: Unable to create JNIEnv, error=%d\n", res );
}
return env;
}