【问题标题】:JVMTI _jclass toStringJVMTI _jclass toString
【发布时间】:2010-12-29 16:40:15
【问题描述】:

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

【问题讨论】:

    标签: java jvm jvmti


    【解决方案1】:

    这是你想要的吗?

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

    【讨论】:

    • 您从哪里获得此代码?还是你自己写的?你用 JVMTI 做什么?
    • 我为您快速编写了它.....我在 IBM 工作,主要编写一个内部 Java 分析工具......我用 C 编写了一个 BCI 库......事实上我是今年打算重新编写它,可能是开源的......我已经“生活”了很长一段时间的JVMTI......只是想我会帮忙。
    • 听起来很有趣!一旦你开源了你的库,你能告诉我吗?如果您在重写这个库时需要任何帮助,请告诉我!我的邮箱是 paeloque@gmail.com
    • @Paul ping 相同的上述评论
    【解决方案2】:

    我相信你可以通过GetClassSignature来判断(不是我试过)。

    【讨论】:

      猜你喜欢
      • 2012-01-23
      • 2012-08-30
      • 1970-01-01
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多