首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JVMTI _jclass toString

JVMTI _jclass toString
EN

Stack Overflow用户
提问于 2010-12-30 00:40:15
回答 2查看 640关注 0票数 1

如何获取JVM _jclass的名称?我想要显示加载到JVMTI代理中的类的名称,但是我不清楚如何从_jclass实例中获取类的名称。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-13 01:51:57

这是你想要的吗?

代码语言:javascript
复制
#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;
}
票数 3
EN

Stack Overflow用户

发布于 2010-12-30 01:02:33

我相信你可以从GetClassSignature中确定它(我没有试过)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4555915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档