This crate supports dynamic attach via the Agent_OnAttach entry point.
Implement Agent::on_attach and use export_agent! — the macro generates
the correct native entry points automatically.
use jvmti_bindings::prelude::*;
#[derive(Default)]
struct AttachLogger;
impl Agent for AttachLogger {
fn on_attach(&self, vm: *mut jni::JavaVM, options: &str) -> jni::jint {
println!("[AttachLogger] attached with options: {}", options);
let _jvmti = Jvmti::new(vm).expect("get JVMTI");
jni::JNI_OK
}
}
export_agent!(AttachLogger);on_attachis called when the agent is loaded via the JVM Attach API.- You can request capabilities and enable JVMTI events inside
on_attach. - Thread and JNI safety rules still apply (see
docs/SAFETY.md).