Skip to content

Commit

Permalink
feat: experiment with javaagent
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaster99 committed Jan 21, 2025
1 parent 36628a3 commit 1a400c0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
13 changes: 13 additions & 0 deletions classport-introspection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
We were trying `javaagent` instead of `jvmti` because we ended
up using the same APIs in JVMTI
1. getAllStackTraces
2. getAnnotations

However, upon trying to use `javaagent` we realized that we
were not able to get the `Class<?>` instance in the running JVM.
Thus, we can neither get its bytecode nor its annotations.

Some advantages of using `jvmti` are:
1. It uses `AsyncGetCallTrace` which is immune to [safepoint bias](https://seethawenner.medium.com/java-safepoint-and-async-profiling-cdce0818cd29) and does
not stop the JVM for long periods of time.
2. It gives us the `jclass` instance so we can call `getAnnotations` on it.
49 changes: 49 additions & 0 deletions classport-introspection/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.chains-project</groupId>
<artifactId>classport</artifactId>
<version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>classport-introspection</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Agent-Class>io.github.chains_project.classport.introspector.Agent</Agent-Class>
<Can-Retransform-Classes>false</Can-Retransform-Classes>
</manifestEntries>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<id>shade</id>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.chains_project.classport.introspector;

import java.lang.instrument.Instrumentation;
import java.util.Arrays;

public class Agent {
public static void agentmain(String agentArgs, Instrumentation inst) {
Thread.getAllStackTraces().forEach((thread, stackTrace) -> {
System.out.println("Thread: " + thread.getName());
for (StackTraceElement stackTraceElement : stackTrace) {
String className = stackTraceElement.getClassName();
System.out.println("Target JVM " + stackTraceElement.getClassLoaderName());
System.out.println("this " + Agent.class.getClassLoader().getName());
try {
Class<?> c = Class.forName(className);
c.getAnnotations();
System.out.println(className + Arrays.toString(c.getAnnotations()));
} catch (ClassNotFoundException e) {
System.out.println("Class not found: " + className);
}
}
});
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<module>maven-plugin</module>
<module>classport-agent</module>
<module>classport-analyser</module>
<module>classport-introspection</module>
</modules>

<scm>
Expand Down

0 comments on commit 1a400c0

Please sign in to comment.