JVMTI communicate with attaching vm

589 views Asked by At

I have a native JVMTI agent that I attach with the Java Attach API. The agent basically just runs Agent_OnAttach and then exists. I would like to pass information from the agent to the VM that attached the agent. Even just writing to stdout of the VM that attached the agent would be fine. I'm aware that I could use out of band means like sockets or named pipes but I'm looking for something built in.

1

There are 1 answers

2
Jan Gassen On

Do you really want to communicate with the JVM or with the application running inside? If you want to talk to your application, Agent_Onload would be to early because your app has not yet been loaded. Instead, you can use the VMInit event:

void JNICALL
VMInit(jvmtiEnv *jvmti_env,
        JNIEnv* jni_env,
        jthread thread)

This provides you access to the JNI and thus lets you execute java code. You could use this for example to set a system property that can later be read by your classes. If you want to communicate with any particular class, this event might still be to early and you might wait for the respective ClassPrepare event and check when your class becomes available:

void JNICALL
ClassPrepare(jvmtiEnv *jvmti_env,
        JNIEnv* jni_env,
        jthread thread,
        jclass klass)

If you want to write to stdout, you can of course use the JNI to call System.println...