Calling JVMTI outside JVM threads

Hi folks,

I'm writing a profiler-type application in C with the JVMTI. In Agent_OnLoad, I create a new thread which accept()s connection and passes them to another new thread which handles data from the network. I want to be able to control my agent over the network, specifically using SetEventNotificationMode. However, I find that calling any of the jvmti methods from my network thread does not work. Can anyone explain why this might be and if there is a workaround other than creating a new JVMTI environment for each network thread?

This is based on the heapTracker example in demo/jvmti/heapTracker/src/heapTracker.c which keeps a global copy of the jvmtiEnv*, although it is only used in native methods which were called from java.

Many thanks

Bruce Duncan

[797 byte] By [b_duncana] at [2007-9-25]
# 1
You can't call jvmti functions from threads that aren't attached to the VM. Have you looked at the RunAgentThread function? Alternatively, you could use JNI's AttachCurrentThread to attach your native thread to the VM. In either case you don't need an environment per thread.
alan.batemana at 2007-7-14 > top of java,Developer Tools,Debugging and Profiling Tool APIs...
# 2

Alan,

Thanks very much, that clears things up for me! I will attach my agent thread and write the necessary code to exclude it from the thread list I collect.

Alternatively, I could attach and detach the thread for short periods when I need to use the TI. Do you know if AttachCurrentThread and DetachCurrentThread are expensive?

Thanks again,

Bruce

b_duncana at 2007-7-14 > top of java,Developer Tools,Debugging and Profiling Tool APIs...
# 3
Attach/Detach is not that expensive but it may be an issue if you are doing it hundreds of times a second. I don't know the details of your agent but it might be an easy to maintain an agent thread.
alan.batemana at 2007-7-14 > top of java,Developer Tools,Debugging and Profiling Tool APIs...