Skip to content

Commit 87acc70

Browse files
feat: add native agent to intercept class and read annotations (#26)
* added first version of native agent * example added * add test app * add files to reproduce prototype * up * fix: add JVM option to compiler plugin * initialise capabilities * set capabilities * attach all potential capabilities * feat: get stack trace at runtime and print methodids * small changes in test project * first version rust agent * improving code safety * constant pool parsing in progress * agennt c test * update c native * small change toy project * refactor and fix problem in using getAnnotations() * update commands * rust agent in progress (fix of getAnnotations * fix bug recursion onMethodEntry * fix recursion problem with onMethodEntry event * update to filter just for classportInfo annotation * path fixing * readme update * code cleaning * code cleaning --------- Co-authored-by: serenacofano <[email protected]>
1 parent 1a400c0 commit 87acc70

File tree

15 files changed

+1038
-0
lines changed

15 files changed

+1038
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ target/
99

1010
# vscode settings
1111
settings.json
12+
13+
# Project relate files
14+
*.so
15+
classport-files/
16+

.idea/runConfigurations/AgentLoader.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo_agent/cargo_agent/Cargo.lock

Lines changed: 286 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo_agent/cargo_agent/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "cargo_agent"
3+
version = "0.1.0"
4+
edition = "2021"
5+
build = "build.rs"
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
12+
[build-dependencies]
13+
bindgen = "0.71.0"
14+

cargo_agent/cargo_agent/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Rust Agent (WIP)
2+
Here is an agent in Rust.
3+
4+
* lib.rs is the agent (for the moment just intercept onMethodEntry event and print "capitalize" when this method is intercepted);
5+
* build.rs is the code that generate the binding of jvmti.h. It uses [rust-bindgen](https://rust-lang.github.io/rust-bindgen/)
6+
7+
### To use it
8+
9+
Requirements: see [bindgen guide](https://rust-lang.github.io/rust-bindgen/requirements.html).
10+
11+
- set the required environment variables
12+
13+
```
14+
export JVM_LIB_PATH=<path-to-your-jdk-instalaltion>/libexec/openjdk.jdk/Contents/Home/lib/server
15+
export JVM_INCLUDE_PATH=<path-to-jvmti.h>
16+
export JNI_INCLUDE_PATH=<path-to-jni.h>
17+
```
18+
19+
- compile the project:
20+
21+
`cargo build --release`
22+
23+
- run the agent:
24+
25+
`java -agentpath:/path/to/target/release/libmy_java_agent.dylib -jar your_app.jar`
26+
27+

cargo_agent/cargo_agent/build.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
4+
fn main() {
5+
println!("OUT_DIR: {}", env::var("OUT_DIR").unwrap());
6+
7+
let jvm_lib_path = env::var("JVM_LIB_PATH").unwrap_or_else(|_| "/usr/local/Cellar/openjdk/23.0.2/libexec/openjdk.jdk/Contents/Home/lib/server".to_string());
8+
let jvm_include_path = env::var("JVM_INCLUDE_PATH").unwrap_or_else(|_| "/usr/local/opt/openjdk@23/include/jvmti.h".to_string());
9+
let jni_include_path = env::var("JNI_INCLUDE_PATH").unwrap_or_else(|_| "/usr/local/opt/openjdk@23/include/jni.h".to_string());
10+
11+
println!("cargo:rustc-link-search={}", jvm_lib_path);
12+
println!("cargo:rustc-link-lib=jvm");
13+
14+
let bindings = bindgen::Builder::default()
15+
.header(jvm_include_path)
16+
.header(jni_include_path)
17+
.blocklist_function("Agent_OnLoad")
18+
.blocklist_function("Agent_OnUnload")
19+
// Tell cargo to invalidate the built crate whenever any of the
20+
// included header files changed.
21+
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
22+
.generate()
23+
.expect("Unable to generate bindings");
24+
25+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
26+
bindings
27+
.write_to_file(out_path.join("bindings.rs"))
28+
.expect("Couldn't write bindings!");
29+
}

0 commit comments

Comments
 (0)