Check out https://github.com/ShiftLeftSecurity/bctrace instead. A more mature and stable version of this project
An extensible java agent framework that instruments programs running on the JVM (modifying the bytecode at class loading time), with the purpose of capturing method invocation events (start, finish, errors ...) and notifying custom listeners.
Table of Contents
The java instrumentation package introduced in JDK1.5, provides a simple way to transform java-class definition at loading time, consisting basically in a byte[]
to byte[]
transformation, by the so called "java agents".
This module provides an agent (org.brutusin.instrumentation.Agent) that creates an execution listener instance (from the name of a concrete class extending org.brutusin.instrumentation.Interceptor passed from the JVM agent arguments) and, making use of the ASM library, introduces a series of instructions in the method definitions of the classes to be loaded (classes and methods can be skipped) to notify these execution events to the listener.
From a simplified point of view, the dynamic transformation turns a method like this:
public Object foo(Object bar){
return new Object();
}
into that:
public Object foo(Object bar){
onStart(bar);
try{
Object ret = new Object();
onFinished(ret);
return ret;
} catch(Throwable th){
onThrowable(th);
throw th; // at bytecode level this is legal
}
}
allowing your custom listener to be notified.
<dependency>
<groupId>org.brutusin</groupId>
<artifactId>instrumentation</artifactId>
</dependency>
Click here to see the latest available version released to the Maven Central Repository.
If you are not using maven and need help you can ask here.
See logging-instrumentation for a complete working example.
Create the following listener implementation:
package mypackage;
public class MyInterceptor extends Interceptor {
@Override
public void init(String arg) {
System.out.println("Interceptor args: " + arg);
}
@Override
public boolean interceptClass(String className, byte[] byteCode) {
return true; // all classes can be intrumented
}
@Override
public boolean interceptMethod(ClassNode cn, MethodNode mn) {
return true; // all methods are instrumented
}
@Override
protected void doOnStart(Method m, Object[] arg, String executionId) {
System.out.println("doOnStart " + m + " " + executionId);
}
@Override
protected void doOnThrowableThrown(Method m, Throwable throwable, String executionId) {
System.out.println("doOnThrowableThrown " + m + " " + executionId);
}
@Override
protected void doOnThrowableUncatched(Method m, Throwable throwable, String executionId) {
System.out.println("doOnThrowableUncatched " + m + " " + executionId);
}
@Override
protected void doOnFinish(Method m, Object result, String executionId) {
System.out.println("doOnFinish " + m + " " + executionId);
}
}
Create a fat-jar with the previous class and its dependencies. Add the following attribute to the manifest of the agent JAR:
Premain-Class: org.brutusin.instrumentation.Agent
Suppose this jar to be named myagent.jar
Run (at least JRE 1.5) the desired java application with the following JVM options: (suppossing myagent.jar located in the working directory)
-javaagent:myagent.jar=mypackage.MyInterceptor;an_interceptor_optional_parameter
This module could not be possible without:
https://github.com/brutusin/instrumentation/issues
- Ignacio del Valle Alles (https://github.com/idelvall/)
Contributions are always welcome and greatly appreciated!
Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0