1

如何获取 JVM TI _jclass 的名称?我想显示在 JVMTI 代理中加​​载的类的名称,但是对我来说如何从 _jclass 实例中获取类的名称并不明显。

4

2 回答 2

3

这是你想要的吗?

#include <stdlib.h>
#include "jvmti.h"

jvmtiEnv  *globalJVMTIInterface;

void JNICALL vmInit(jvmtiEnv *jvmti_env,JNIEnv* jni_env,jthread thread) {

    printf("VMStart\n");

    jint numberOfClasses;
    jclass *classes;

    jint returnCode =  (*globalJVMTIInterface)->GetLoadedClasses(globalJVMTIInterface, &numberOfClasses, &classes);
    if (returnCode != JVMTI_ERROR_NONE) {
        fprintf(stderr, "Unable to get a list of loaded classes (%d)\n", returnCode);
        exit(-1);
    }

    int i;

    for(i=0;i<numberOfClasses;i++) {

        char* signature = NULL;
        char* generic = NULL;

        (*globalJVMTIInterface)->GetClassSignature(globalJVMTIInterface, classes[i], &signature, &generic);

        printf("%d) %s %s\n", i+1, signature, generic);

        if(signature) {
            returnCode = (*globalJVMTIInterface)->Deallocate(globalJVMTIInterface, (unsigned char*) signature);
        }
        if(generic) {
            returnCode = (*globalJVMTIInterface)->Deallocate(globalJVMTIInterface, (unsigned char*) generic);
        }


    }


    if(classes) {
        returnCode = (*globalJVMTIInterface)->Deallocate(globalJVMTIInterface, (unsigned char*) classes);
    }

}

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {

    jint returnCode = (*jvm)->GetEnv(jvm, (void **)&globalJVMTIInterface, JVMTI_VERSION_1_0);

    if (returnCode != JNI_OK) {
            fprintf(stderr, "The version of JVMTI requested (1.0) is not supported by this JVM.\n");
            return JVMTI_ERROR_UNSUPPORTED_VERSION;
    }


    jvmtiEventCallbacks *eventCallbacks;

    eventCallbacks = calloc(1, sizeof(jvmtiEventCallbacks));
    if (!eventCallbacks) {
            fprintf(stderr, "Unable to allocate memory\n");
            return JVMTI_ERROR_OUT_OF_MEMORY;
    }


    eventCallbacks->VMInit = &vmInit;


    returnCode = (*globalJVMTIInterface)->SetEventCallbacks(globalJVMTIInterface, eventCallbacks, (jint) sizeof(*eventCallbacks));
    if (returnCode != JNI_OK) {
        fprintf(stderr, "JVM does not have the required capabilities (%d)\n", returnCode);
        exit(-1);
    }


    returnCode = (*globalJVMTIInterface)->SetEventNotificationMode(globalJVMTIInterface, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, (jthread) NULL);
    if (returnCode != JNI_OK) {
        fprintf(stderr, "JVM does not have the required capabilities, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT (%d)\n", returnCode);
        exit(-1);
    }

    return JVMTI_ERROR_NONE;
}
于 2011-01-12T17:51:56.507 回答
1

我相信你可以从GetClassSignature(不是我试过)来确定它。

于 2010-12-29T17:02:32.517 回答