0

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);
}
AstroCB
  • 12,337
  • 20
  • 57
  • 73
Malik Dz
  • 11
  • 5

2 Answers2

0

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

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.

Jan Gassen
  • 3,406
  • 2
  • 26
  • 44