谁能解释一下为什么当jvm分配一些java对象时,我会得到一个回调,而不是其他的?下面是我正在做的事情:
static jvmtiCapabilities capa;
static jvmtiEnv* jvmti = NULL;
static const char* fileName = "C:\\temp\\ObjectInitCallbackDump.txt";
static ofstream outFileStream;
void JNICALL callbackObjectAllocation ( jvmtiEnv* jvmti_env,
JNIEnv* jni_env,
jthread thread,
jobject object,
jclass object_klass,
jlong size )
{
char* generic_ptr_class;
char* class_name;
jvmtiError error;
error = jvmti_env->GetClassSignature(object_klass, &class_name, &generic_ptr_class);
if (check_jvmti_error(jvmti_env, error, "Failed to get class signature")) {
return;
}
outFileStream << class_name << std::endl;
}
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
jint result;
jvmtiError error;
jvmtiEventCallbacks callbacks;
outFileStream.open(fileName,ios::trunc);
result = jvm->GetEnv((void**) &jvmti, JVMTI_VERSION_1_1);
if (result != JNI_OK || jvmti == NULL) {
printf("error\n");
return JNI_ERR;
} else {
printf("loaded agent\n");
}
(void) memset(&capa, 0, sizeof(jvmtiCapabilities));
capa.can_generate_vm_object_alloc_events = 1;
error = jvmti->AddCapabilities(&capa);
if (check_jvmti_error(jvmti, error, "Unable to set capabilities") != JNI_OK) {
return JNI_ERR;
}
(void) memset(&callbacks, 0, sizeof(callbacks));
callbacks.VMObjectAlloc = &callbackObjectAllocation;
error = jvmti->SetEventCallbacks(&callbacks, (jint) sizeof(callbacks));
if (check_jvmti_error(jvmti, error, "Unable to set callbacks") != JNI_OK) {
return JNI_ERR;
}
error = jvmti->SetEventNotificationMode( JVMTI_ENABLE,
JVMTI_EVENT_VM_OBJECT_ALLOC,
(jthread) NULL);
if (check_jvmti_error(jvmti, error,
"Unable to set method entry notifications") != JNI_OK) {
return JNI_ERR;
}
return JNI_OK;
}
JNIEXPORT void JNICALL Agent_OnUnload(JavaVM *vm) {
outFileStream.close();
}当我检查我创建的文件时,我没有看到我感兴趣的类,尽管我知道它们在那里,并且NetBeans告诉我在jvm中只有一个该类的实例。有什么想法吗?
尼基塔
发布于 2012-07-16 06:35:19
出于性能方面的原因,JVMTI仅支持通过字节码规范( bytecode instrumentation,BCI)无法检测到的对象的分配事件,如JVMTI VMObjectAlloc事件文档中所述。这意味着大多数对象分配都不会触发该事件。我假设您错过的分配属于这一类。
幸运的是,使用BCI截取所有对象分配并不是很困难。HeapTracker演示准确地演示了如何使用java_crw_demo以及VMObjectAlloc事件拦截JVMTI代理中的所有对象分配。
发布于 2012-03-26 03:14:14
也许你只是没有拿到你的支票?如果这是您实际运行的代码,那么您就有一个bug;您错过了对象末尾的;,您的比较应该是这样的:
if (strcmp(class_name,"Ljavax/swing/JFrame;") == 0) {
printf("Got the sucker!!!");
}https://stackoverflow.com/questions/9856861
复制相似问题