How to communicate with jvmti agent attached on a running JVM

六眼飞鱼酱① 提交于 2019-12-20 07:15:06

问题


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

回答1:


How would I communicate with the jvmti agent I attached on a running JVM using attach API.

If I understand what you are doing here correctly, it is entirely up to you how your external application communicates with the agent, but it is also up to you to implement it ... starting with designing or choosing the wire-protocol you are going to use.




回答2:


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

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

public native MyResponseType myNativeMethod (MyRequestType obj);

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);

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



来源:https://stackoverflow.com/questions/29585199/how-to-communicate-with-jvmti-agent-attached-on-a-running-jvm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!