2

I want to write a java agent to instrument some applications. I am interested in getting the details of the objects, (i.e. their fields) instantiated by the applications. I would also like to catch any read and write access to any of those objects/their fields while running.

Can you please guide me in writing the agents and let me know what classes and methods should I explore. I just know about java.lang.instrument class. But I could not find anything there that could catch these events.

I am also open to any other java instrumentation techniques that you think can help me.

Ankit
  • 6,772
  • 11
  • 48
  • 84

1 Answers1

4

You can use the AspectJ with load-time weaving (javaagent). You can e.g. write aspects to monitor constructor calls (call/execution pointcuts) and monitor field access (set/get pointcuts).

I'm using annotation-based development. For example to monitor setting all nonstatic nonfinal and nontransient fields in all classes in given package you can create aspect:

@Aspect
public class MonitorAspect {   
     @Around(" set(!static !final !transient * (*) . *) && args(newVal) && target(t) && within(your.target.package.*) ")
    public void aroundSetField(ProceedingJoinPoint jp, Object t, Object newVal) throws Throwable{
        Signature signature = jp.getSignature();
        String fieldName = signature.getName();
        Field field = t.getClass().getDeclaredField(fieldName);
        field.setAccessible(true);
        Object oldVal = field.get(t);
        System.out.println("Before set field. "
                + "oldVal=" + oldVal + " newVal=" + newVal + " target.class=" + t.getClass());
        jp.proceed();
    }
}

in META-INF place aop.xml:

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <aspects>
        <aspect name="your.package.MonitorAspect" />
    </aspects>
</aspectj>

Place acpectjrt.jar and aspectjweaver.jar on classpath and run your JVM with -javaagent:lib/aspectjweaver.jar parameter. Here are some examples and documentation http://www.eclipse.org/aspectj/doc/released/adk15notebook/ataspectj.html

zacheusz
  • 8,750
  • 3
  • 36
  • 60
  • ok i am a newbie and getting confused. can u point me to some basic examples for aspectj as the ones that I found really confuse me. – Ankit Jul 26 '11 at 01:37
  • Hey Zach, I did not completely get the idea of using aspectj with javaagent. can you point me to page where I can get to look at some basic examples and try them out myself to get a feel of all this. – Ankit Jul 26 '11 at 01:43
  • @Ankit: please provide some concrete examples what you want then I'll provide you sample solutions. And ofc links :) – zacheusz Jul 26 '11 at 07:18
  • What I am looking for is that I have a couple of Java applications (jar files only). I want to be able to catch all reads and writes to any objects/fields in the program. Basically, I am looking for the pattern in which the access to the fields and objects in the program are made. Hope this clarifies the problem better. – Ankit Jul 26 '11 at 11:22