gpt4 book ai didi

java - JVMTI _jclass toString

转载 作者:行者123 更新时间:2023-11-29 03:59:09 27 4
gpt4 key购买 nike

如何获取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;
}

关于java - JVMTI _jclass toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4555915/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com