我正在研究一个包含Hotspot Profiler反馈的研究项目。目前,我正在研究JVMTI代理,该代理应具有以下功能:
侦听任何已编译的加载事件。
提取并分析具有热点方法的完整类文件。
修改/定义类的字节码。
我在JVMTI中有很多可用的API函数,以获取有关具有JIT正在编译的方法的类文件的信息。但是,我想要Java虚拟机规范中描述的方法的完整类文件。
如果无法获取整个类文件,则我至少希望使用以下格式的类文件:
typedef struct {
unsigned int magic;
unsigned short minor_version;
unsigned short major_version;
unsigned short constant_pool_count;
unsigned char *constant_pool;
unsigned short access_flags;
unsigned short this_class;
unsigned short super_class;
unsigned short interfaces_count;
unsigned char *interfaces;
unsigned short fields_count;
unsigned char *fields;
unsigned short methods_count;
unsigned char *methods;
unsigned short attributes_count;
unsigned char *attributes;
}ClassFile;
到目前为止,我有以下代码部分地达到了目的:
void JNICALL compiled_method_load(jvmtiEnv *jvmti, jmethodID method, jint code_size, const void* code_addr, jint map_length, const jvmtiAddrLocationMap* map, const void* compile_info)
{
static ClassFile *clazz;
jvmtiError err;
jclass klass;
jint constant_pool_count_pointer;
jint constant_pool_byte_count_pointer;
jint local_entry_count_ptr;
jint minor, major;
jint modifier_ptr;
jvmtiLocalVariableEntry* table_ptr;
unsigned char* constant_pool_bytes_ptr;
char* name = NULL;
char* signature = NULL;
char* generic_ptr = NULL;
unsigned char* bytecodes_ptr = NULL;
err = (*jvmti)->RawMonitorEnter(jvmti,lock);
check_jvmti_error(jvmti, err, "raw monitor enter");
clazz->magic = 0xCAFEBABE;
err = (*jvmti)->GetMethodDeclaringClass(jvmti,method, &klass);
check_jvmti_error(jvmti, err, "Get Declaring Class");
err = (*jvmti)->GetClassVersionNumbers(jvmti, klass, &minor, &major);
check_jvmti_error(jvmti, err, "Get Class Version Number");
clazz->minor_version = (u2_int)minor;
clazz->major_version = (u2_int)major;
err = (*jvmti)->GetConstantPool(jvmti, klass, &constant_pool_count_pointer,
&constant_pool_byte_count_pointer, &constant_pool_bytes_ptr);
check_jvmti_error(jvmti, err, "Get Constant Pool");
clazz->constant_pool_count = constant_pool_count_pointer;
clazz->constant_pool = constant_pool_bytes_ptr;
err = (*jvmti)->GetClassModifiers(jvmti,klass, &modifier_ptr);
check_jvmti_error(jvmti, err, "Get Access Flags");
clazz->access_flags = (u2_int)modifier_ptr;
err = (*jvmti)->GetBytecodes(jvmti,method, &code_size, &bytecodes_ptr);
check_jvmti_error(jvmti, err, "Get Bytecodes");
err = (*jvmti)->GetLocalVariableTable(jvmti,method, &local_entry_count_ptr, &table_ptr);
check_jvmti_error(jvmti, err, "Get Local Variable table");
if (constant_pool_bytes_ptr != NULL) {
err = (*jvmti)->Deallocate(jvmti,(unsigned char*)constant_pool_bytes_ptr);
check_jvmti_error(jvmti, err, "deallocate bytecodes pointer");
}
if (bytecodes_ptr != NULL) {
err = (*jvmti)->Deallocate(jvmti,(unsigned char*)bytecodes_ptr);
check_jvmti_error(jvmti, err, "deallocate bytecodes pointer");
}
if (name != NULL) {
err = (*jvmti)->Deallocate(jvmti,(unsigned char*)name);
check_jvmti_error(jvmti, err, "deallocate name");
}
if (signature != NULL) {
err = (*jvmti)->Deallocate(jvmti,(unsigned char*)signature);
check_jvmti_error(jvmti, err, "deallocate signature");
}
if (generic_ptr != NULL) {
err = (*jvmti)->Deallocate(jvmti,(unsigned char*)generic_ptr);
check_jvmti_error(jvmti, err, "deallocate generic_ptr");
}
err = (*jvmti)->RawMonitorExit(jvmti,lock);
}
我的问题是:
是否可以通过代理获取完整的类文件?
如果没有,如何使用
ClassFile
或JVMTI
API填充JNI
结构?还有其他实现我目标的策略吗?
局限性:
类加载后很长时间,我必须在运行时检查和操作字节码。因此,使用
ASM
和JAVASSIST
之类的库的AFAIK Java代理不会有任何帮助。任何帮助将不胜感激。
最佳答案
所以我终于让它工作了。 Holger在评论中的想法是此答案的主要来源。我认为没有人会需要这个,但是只是为了回答这个问题。
简而言之,要获取整个类文件,在JVMTI
API中只有一种可能性,那就是ClassFileLoadHook
事件。每当在JVM中加载新类或调用Retransformclasses
或RedefineClasses
函数时,都会触发此事件。因此,我将Retransformclasses
函数作为虚拟调用进行了调用,只是为了调用ClassFileLoadHookEvent
,然后最终获得了整个类。
除了添加功能和回调设置外,主要在我的源代码中添加了以下功能:
void JNICALL
Class_File_Load_Hook(jvmtiEnv *jvmti_env,
JNIEnv* jni_env,
jclass class_being_redefined,
jobject loader,
const char* name,
jobject protection_domain,
jint class_data_len,
const unsigned char* class_data,
jint* new_class_data_len,
unsigned char** new_class_data)
{
jvmtiError err;
unsigned char* jvmti_space = NULL;
char* args = "vop";
javab_main(3, args, class_data, class_data_len);
err = (*jvmti_env)->Allocate(jvmti_env, (jlong)global_pos, &jvmti_space);
check_jvmti_error(jvmti_env, err, "Allocate new class Buffer.");
(void)memcpy((void*)jvmti_space, (void*)new_class_ptr, (int)global_pos);
*new_class_data_len = (jint)global_pos;
*new_class_data = jvmti_space;
}
此处的
class_data
变量包含在其上调用ClassFileLoadHook
事件的完整类文件。我分析了该类文件,并使用char*
方法将其插入了新的javab_main
数组中,最后将新数组指向了new_class_data
变量。 new_class_ptr
是一个全局变量,其中包含更改的类定义。