1

谁能向我解释为什么当 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 中恰好有一个该类的实例。有什么想法吗???

尼基塔

4

2 回答 2

9

出于性能原因,JVMTI 仅支持无法通过字节码检测 (BCI) 检测到的对象的分配事件,如 JVMTIVMObjectAlloc 事件文档中所述。这意味着大多数对象分配不会触发该事件。我假设您缺少的分配属于该类别。

幸运的是,使用 BCI 拦截所有对象分配并不难。HeapTracker演示准确地说明了如何使用 java_crw_demo 拦截 JVMTI 代理中的所有对象分配以及事件VMObjectAlloc

于 2012-07-15T22:35:18.737 回答
0

也许你只是没有打你的支票?如果这是您实际运行的代码,那么您有一个错误;你错过了;在对象的末尾您的比较应该是这样的:

if (strcmp(class_name,"Ljavax/swing/JFrame;") == 0) {
    printf("Got the sucker!!!");
}
于 2012-03-25T19:14:13.970 回答