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.
Asked
Active
Viewed 556 times
0

Philippe Marschall
- 4,452
- 1
- 34
- 52
1 Answers
0
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...

Jan Gassen
- 3,406
- 2
- 26
- 44
-
I want to communicate with the agent. The agent collects a bunch of data (iterates over several objects looking for specific ones). The attaching VM (not the one where the agent runs) needs this information. – Philippe Marschall Dec 23 '14 at 12:44
-
1To my best knowledge, you cannot directly communicate with the agent from your VM, especially if it's a different process. I think the easiest thing would be just to create a network socket in your agent and connect to this from your other VM. Then you are free to exchange whatever information you like – Jan Gassen Dec 23 '14 at 14:29