如何与正在运行的JVM上附加的jvmti代理进行通信 [英] How to communicate with jvmti agent attached on a running JVM

查看:152
本文介绍了如何与正在运行的JVM上附加的jvmti代理进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用attach API与运行JVM上附加的jvmti代理进行通信。当我说沟通时,这就是我的意思:我想调用位于我的jvmti代理上的本机函数,这些函数将返回我之前与代理感染的正在运行的JVM的数据(如字段值)。

I wanted to know how would I communicate with the jvmti agent I attached on a running JVM using attach API. When I say communicate ,here's what I meant : I want to call native functions located on my jvmti agent , theses function will return me data (like field values) of the running JVM that I "infected" earlier with the agent.

这是代理商;我还没有添加原生函数:

Here's the agent; I did not add the native functions yet:

#include <jvmti.h>

JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved);
jvmtiEnv* create_jvmti_env(JavaVM* vm);
JNIEnv* create_jni_env(JavaVM* vm);
void init_jvmti_capabilities(jvmtiEnv* env);

JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
    jvmtiEnv* jvmti = create_jvmti_env(vm);
    init_jvmti_capabilities(jvmti);
    JNIEnv* jni = create_jni_env(vm);
    return JNI_OK;
}

jvmtiEnv* create_jvmti_env(JavaVM* vm) {
    jvmtiEnv* env;
    vm->GetEnv((void **) &env, JVMTI_VERSION_1_2);
    return env;
}

JNIEnv* create_jni_env(JavaVM* vm) {
    JNIEnv* env;
    vm->GetEnv( (void **) &env, JNI_VERSION_1_8);
    return env;
}

void init_jvmti_capabilities(jvmtiEnv* env) {
    jvmtiCapabilities capabilities;
    env->GetPotentialCapabilities( &capabilities);
    env->AddCapabilities( &capabilities);
}


推荐答案

您可以尝试注册原生使用JNI的方法。我还没有测试过这个,但你可能会尝试这样的事情:

You could try to register native methods using the JNI. I haven't tested this yet, but you might try something like this:

将一个本机方法添加到应该与你的JVMTI代理通信的类中:

Add a native method to the class that is supposed to communicate with your JVMTI agent:

public native MyResponseType myNativeMethod (MyRequestType obj);

然后使用JNI将此Java方法绑定到JVMTI代理的某些方法:

Then use the JNI to bind this Java method to some method of your JVMTI agent:

static JNINativeMethod methods[] = {
    {"myNativeMethod", "(Lmy/package/MyRequestType;)Lmy/package/MyResponseType;", (void *)&native_method}
}; 

jni->RegisterNatives(cls, methods, 1);

其中 cls 是对jclass的jclass引用包含您的原生方法的类。

where cls is a jclass reference to the class containing your native method.

这篇关于如何与正在运行的JVM上附加的jvmti代理进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆