JVM Advent

The JVM Programming Advent Calendar

How to write a java agent

For vmlens, a lightweight java race condition catcher, we are using a java agent to trace field accesses. Here are the lessons we learned implementing such an agent.

The Start

Create an agent class with a “static public static void premain(String args, Instrumentation inst)” method. Put this class into a jar file with a manifest pointing to the Agent class. The premain method will be called before the main method of the application.

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.2
Created-By: 1.8.0_05-b13 (Oracle Corporation)
Built-By: Thomas Krieger
Implementation-Vendor: Anarsoft
Implementation-Title: VMLens Agent
Implementation-Version: 2.0.0.201511181111
Can-Retransform-Classes: true
Premain-Class: com.anarsoft.trace.agent.Agent
Boot-Class-Path: agent_bootstrap.jar

The MANIFEST.MF file from vmlens.

Class loader magic part 1

The agent class will be loaded by the system class loader. But we have to avoid version conflicts between the classes used by the agent and the application. Especially the frameworks used in the agent should not be visible to the application classes. So we use a dedicated URLClassLoader to load all other agent classes:

// remember the currently used classloader
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        
// Create and set a special URLClassLoader
URLClassLoader classloader = new URLClassLoader(urlList.toArray(new URL[]{}) , null );
Thread.currentThread().setContextClassLoader(classloader);
    
// Load and execute the agent
String agentName = "com.anarsoft.trace.agent.runtime.AgentRuntimeImpl";
AgentRuntime agentRuntime  =  (AgentRuntime) classloader.loadClass(agentName).newInstance();
    
// reset the classloader
Thread.currentThread().setContextClassLoader(contextClassLoader);

Class loader magic part 2

Now we use asm to add our static callbacks methods when a field is accessed. To make sure that the classes are visible in every other class, they have to be loaded by the bootstrap classloader. To do this they have to be in a java package and the jar containing them have to be in the boot class path.

package java.anarsoft.trace.agent.bootstrap.callback;

public class FieldAccessCallback {

public static  void getStaticField(int field,int methodId) {
 }

}

A callback class from vmlens. It has to be in the java package namespace to be visible in all classes.

Boot-Class-Path: agent_bootstrap.jar

The boot class path entry in the MANIFEST.MF file from vmlens.

VMLens, a lightweight java race condition catcher, is built as a java agent. We know, writing java agents can be a tricky business. So, if you have any questions, just ask them in a comment below.

Author: ThomasKrieger

I work for now more than 15 years as Java Developer, JEE Architect and Scum Product Owner. I am founder of http://vmlens.com, a lightweight java race condition catcher.

Next Post

Previous Post

2 Comments

  1. josefB December 19, 2015

    “Scum Product Owner”?

  2. erez October 24, 2017

    what is urlList?
    I thought you mean that “magic1 ” is for initializing ClassFileTransformer in the premain method. but that is in the same jar as my agent, so what do i do with this UrlClassLoader?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2024 JVM Advent | Powered by Jetbrains LogoJetBrains & steinhauer.software Logosteinhauer.software

Theme by Anders Norén

Cookie Consent with Real Cookie Banner