improved project structure, no more hardcoded absolute path for bootstrap lib in agent

This commit is contained in:
Shautvast 2024-07-03 15:59:12 +02:00
parent 600589edc3
commit beb078d4de
12 changed files with 80 additions and 94 deletions

View file

@ -28,7 +28,7 @@ cd api; RUST_LOG=info cargo run
Start your java with
```
java22 -javaagent:$EXCEPTIONAL_PROJECT/exceptional/agent/target/exceptional-agent-1.0-SNAPSHOT.jar
-Dagentlib="$EXCEPTIONAL_PROJECT/rustlib/target/debug/librustlib.dylib"
-Dagentlib="$EXCEPTIONAL_PROJECT/rustlib/target/release/librustlib.dylib"
--enable-preview --enable-native-access=ALL-UNNAMED
-classpath $YOUR_CLASSPATH YourMain
```

View file

@ -6,18 +6,10 @@
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>exceptional-lib</artifactId>
<artifactId>exceptional-agent</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>22</source>
<target>22</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
@ -31,6 +23,34 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>22</source>
<target>22</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifestEntries>
<Premain-Class>com.github.shautvast.exceptional.Agent</Premain-Class>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<argLine>-Dagentlib=${project.basedir}/rustlib/target/release/librustlib.dylib</argLine>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>

View file

@ -21,14 +21,35 @@
<dependencies>
<dependency>
<groupId>org.github.shautvast.exceptional</groupId>
<artifactId>exceptional-lib</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -47,14 +68,21 @@
<Premain-Class>com.github.shautvast.exceptional.Agent</Premain-Class>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Boot-Class-Path>
/Users/Shautvast/dev/exceptional/lib/target/exceptional-lib-1.0-SNAPSHOT.jar
</Boot-Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<argLine>-Dagentlib=${project.basedir}/rustlib/target/release/librustlib.dylib</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -21,13 +21,22 @@ public class Agent {
// add transformer
instrumentation.addTransformer(new ClassFileTransformer() {
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
return injectExceptionLoggerBeforeThrow(className, classfileBuffer);
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) {
return bytecode(className, classfileBuffer);
}
@Override
public byte[] transform(Module module, ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) {
return injectExceptionLoggerBeforeThrow(className, classfileBuffer);
return bytecode(className, classfileBuffer);
}
private static byte[] bytecode(String className, byte[] classfileBuffer) {
if (!className.startsWith("com/fasterxml/jackson")) {
return injectExceptionLoggerBeforeThrow(className, classfileBuffer);
} else {
return classfileBuffer;
}
}
}, true);
}

View file

@ -1,70 +0,0 @@
<?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>org.github.shautvast.exceptional</groupId>
<artifactId>exceptional-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>exceptional-lib</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.6.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>22</source>
<target>22</target>
<compilerArgs>--enable-preview</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View file

@ -10,9 +10,8 @@
<packaging>pom</packaging>
<modules>
<module>agent</module>
<module>rustlib</module>
<module>lib</module>
<module>agent</module>
</modules>
</project>

View file

@ -31,6 +31,7 @@
<workingDirectory>${project.basedir}/rustlib</workingDirectory>
<arguments>
<argument>build</argument>
<argument>--release</argument>
</arguments>
</configuration>
</execution>

View file

@ -3,7 +3,6 @@ use std::ffi::c_char;
use std::slice;
use reqwest::blocking::Client;
use tracing::error;
// same value, but different meanings
// TODO find a way to set the buffer size from java.
@ -45,7 +44,7 @@ pub extern "C" fn buffer_updated(buffer: *mut c_char) {
let result = std::str::from_utf8_unchecked(slice::from_raw_parts(buffer.offset(read_pos).cast::<u8>(), len as usize));
_ = client.post("http://localhost:3000/api/stacktraces")
.body(result)
.send()
.send();
}
read_pos += len;
} else {
@ -57,7 +56,7 @@ pub extern "C" fn buffer_updated(buffer: *mut c_char) {
s.push_str(s2);
_ = client.post("http://localhost:3000/api/stacktraces")
.body(s)
.send()
.send();
}
read_pos = len - remaining;
}