Skip to content

Commit 1a400c0

Browse files
committed
feat: experiment with javaagent
1 parent 36628a3 commit 1a400c0

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

classport-introspection/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
We were trying `javaagent` instead of `jvmti` because we ended
2+
up using the same APIs in JVMTI
3+
1. getAllStackTraces
4+
2. getAnnotations
5+
6+
However, upon trying to use `javaagent` we realized that we
7+
were not able to get the `Class<?>` instance in the running JVM.
8+
Thus, we can neither get its bytecode nor its annotations.
9+
10+
Some advantages of using `jvmti` are:
11+
1. It uses `AsyncGetCallTrace` which is immune to [safepoint bias](https://seethawenner.medium.com/java-safepoint-and-async-profiling-cdce0818cd29) and does
12+
not stop the JVM for long periods of time.
13+
2. It gives us the `jclass` instance so we can call `getAnnotations` on it.

classport-introspection/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.github.chains-project</groupId>
8+
<artifactId>classport</artifactId>
9+
<version>0.1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>classport-introspection</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<groupId>org.apache.maven.plugins</groupId>
24+
<artifactId>maven-shade-plugin</artifactId>
25+
<configuration>
26+
<createDependencyReducedPom>false</createDependencyReducedPom>
27+
<transformers>
28+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
29+
<manifestEntries>
30+
<Agent-Class>io.github.chains_project.classport.introspector.Agent</Agent-Class>
31+
<Can-Retransform-Classes>false</Can-Retransform-Classes>
32+
</manifestEntries>
33+
</transformer>
34+
</transformers>
35+
</configuration>
36+
<executions>
37+
<execution>
38+
<id>shade</id>
39+
<goals>
40+
<goal>shade</goal>
41+
</goals>
42+
<phase>package</phase>
43+
</execution>
44+
</executions>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.chains_project.classport.introspector;
2+
3+
import java.lang.instrument.Instrumentation;
4+
import java.util.Arrays;
5+
6+
public class Agent {
7+
public static void agentmain(String agentArgs, Instrumentation inst) {
8+
Thread.getAllStackTraces().forEach((thread, stackTrace) -> {
9+
System.out.println("Thread: " + thread.getName());
10+
for (StackTraceElement stackTraceElement : stackTrace) {
11+
String className = stackTraceElement.getClassName();
12+
System.out.println("Target JVM " + stackTraceElement.getClassLoaderName());
13+
System.out.println("this " + Agent.class.getClassLoader().getName());
14+
try {
15+
Class<?> c = Class.forName(className);
16+
c.getAnnotations();
17+
System.out.println(className + Arrays.toString(c.getAnnotations()));
18+
} catch (ClassNotFoundException e) {
19+
System.out.println("Class not found: " + className);
20+
}
21+
}
22+
});
23+
}
24+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>maven-plugin</module>
3737
<module>classport-agent</module>
3838
<module>classport-analyser</module>
39+
<module>classport-introspection</module>
3940
</modules>
4041

4142
<scm>

0 commit comments

Comments
 (0)