How to create a jvmti agent to see all the loaded classes, objects and their field details
Asked Answered
U

1

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.

Uncouple answered 25/7, 2011 at 3:39 Comment(0)
H
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

Hippocrene answered 25/7, 2011 at 6:55 Comment(4)
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.Uncouple
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.Uncouple
@Ankit: please provide some concrete examples what you want then I'll provide you sample solutions. And ofc links :)Hippocrene
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.Uncouple

© 2022 - 2024 — McMap. All rights reserved.