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

有帮助吗?

解决方案

这是你想要的吗?

#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;
}

其他提示

我相信你可以从 GetClassSignature (不是我尝试过的)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top