From 02b15b3e72baa25c1441df6a924c0729591bf2f3 Mon Sep 17 00:00:00 2001 From: Sander Hautvast Date: Thu, 20 Jun 2024 20:55:42 +0200 Subject: [PATCH] stil WIP --- .gitignore | 35 +++++++++ README.md | 1 + pom.xml | 43 +++++++++++ .../github/shautvast/exceptional/Agent.java | 73 +++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/com/github/shautvast/exceptional/Agent.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b425f09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d5c557f --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Experiment with java22 and Rust to monitor exceptions in a JVM \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..aee9829 --- /dev/null +++ b/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + org.example + Exceptional + 1.0-SNAPSHOT + + + 21 + 21 + UTF-8 + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 22 + 22 + --enable-preview + + + + maven-jar-plugin + 3.1.0 + + + + com.github.shautvast.exceptional.Agent + true + true + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/github/shautvast/exceptional/Agent.java b/src/main/java/com/github/shautvast/exceptional/Agent.java new file mode 100644 index 0000000..e2cd75a --- /dev/null +++ b/src/main/java/com/github/shautvast/exceptional/Agent.java @@ -0,0 +1,73 @@ +package com.github.shautvast.exceptional; + +import java.io.InputStream; +import java.lang.classfile.*; +import java.lang.constant.ClassDesc; +import java.lang.constant.MethodTypeDesc; +import java.lang.instrument.ClassDefinition; +import java.lang.instrument.ClassFileTransformer; +import java.lang.instrument.IllegalClassFormatException; +import java.lang.instrument.Instrumentation; +import java.security.ProtectionDomain; + +@SuppressWarnings("ALL") +public class Agent { + + private static final String MESSAGE = "--- Exceptional agent active"; + + public static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { + instrumentation.addTransformer(new ClassFileTransformer() { + @Override + public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { + return instrumentTheThrowable(className, classfileBuffer); + } + + @Override + public byte[] transform(Module module, ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { + return instrumentTheThrowable(className, classfileBuffer); + } + }, true); + + InputStream stream = Agent.class.getResourceAsStream("/java/lang/Throwable.class"); + byte[] b = new byte[stream.available()]; + stream.read(b); + + instrumentation.redefineClasses(new ClassDefinition(Throwable.class, b)); + } + + private static byte[] instrumentTheThrowable(String className, byte[] classfileBuffer) { + if (className.equals("java/lang/Throwable")) { + + ClassFile classFile = ClassFile.of(); + ClassModel classModel = classFile.parse(classfileBuffer); + + return classFile.build(classModel.thisClass().asSymbol(), classBuilder -> { + for (ClassElement ce : classModel) { + if (ce instanceof MethodModel mm) { + String signature = mm.methodName() + "." + mm.methodType().stringValue(); + if (signature.equals(".()V")) { + classBuilder.withMethod(mm.methodName(), mm.methodType(), 1, + methodBuilder -> methodBuilder.withCode( + codebuilder -> +// codebuilder.invokeInstruction(Opcode.LDC, ); + codebuilder.invokeInstruction(Opcode.INVOKESTATIC, + ClassDesc.of("com.github.shautvast.exceptional.Agent"), "foo", + MethodTypeDesc.ofDescriptor("()V"), + false) + )); + continue; + } + } + classBuilder.with(ce); + } + }); + } else { + return classfileBuffer; + } + } + + public static void foo() { + System.out.println("foo"); + } + +} \ No newline at end of file