Is it possible to build custom agent (or reuse existing one) that would enable JFR features in HotSpot VM?.
My plan is to build simple agent loader that uses com.sun.tools.attach.*
package to attach to VM and load custom agent.
I don't use jcmd or other JDK tools intentionally as I don't have control over environment, because applications run in containers where I don't have control about JRE/JDK and ABI for jcmd (so I cannot copy parts of JDK there just to run commands).
I know that agents can do almost anything and since jcmd is written in java and it is capable of starting FlightRecording on fly (I presume via some JMX) then it should be possible also via java agent, but I cannot find documentation for that.
Any lead would help. Thanks
Dynamic Attach API (com.sun.tools.attach) will not work with JRE 8, since it requires tools.jar
and libattach.so
which are included in JDK, but not in JRE.
jattach is a small standalone utility written in C; it provides dynamic attach functionality for JRE and can be used in place of jcmd:
jattach <pid> jcmd "JFR.start ..."
If you prefer Java API, you may use ByteBuddyAgent. It also provides dynamic attach capability without dependency on com.sun.tools.attach.
try (VirtualMachine.ForHotSpot.Connection conn =
VirtualMachine.ForHotSpot.Connection.ForJnaPosixSocket.Factory
.withDefaultTemporaryFolder(30, 100, TimeUnit.MILLISECONDS)
.connect(pid)) {
conn.execute("1", "jcmd", "JFR.start", null, null);
}