Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling An Already Loaded Class

I am developing a plugin for a service. For it to function, it needs some data that the service does not provide.

A plugin has a strict loading/unloading specification. A bare plugin looks like this:

public class Plugin extends JavaPlugin 
{    
    @Override
    public void onEnable() {} //Plugin enters here. Comparable to main(String[] args)

    @Override
    public void onDisable() {} //Plugin exits here, when service shuts down.
}

There is a package called org.service.aClass. Inside it there is aMethod. aMethod looks like this:

public boolean aMethod(boolean bool) {
return bool;
}

An oversimplified scenario, but it works. My plugin needs to know that value of bool whenever aMethod is called. This is absolutely critical o my program; I have no other way of obtaining that value.

I would advise the aMethod, but since my plugin is loaded after the service this would not work. Load time weaving, from what I understand, won't be suitable here either, due to being loaded AFTER.

Despite it doesn't work, here is the aspect I was using, in case it may be of any use:

public aspect LogAspect {

    pointcut publicMethodExecuted(): execution(* org.service.aClass.aMethod(..));

    after(): publicMethodExecuted(){
    cat(String.format("Entered method: %s.",
        thisJoinPoint.getSignature()));

    List<Object> arguments = Arrays.asList(thisJoinPoint.getArgs());
    List<Object> argTypes = new ArrayList<Object>();
    for (Object o: arguments) {  
        argTypes.add(o.getClass().toString());

    }

    cat(String.format("With argument types: %s and values: %s.",
            argTypes, arguments));


    cat(String.format("Exited method: %s.", thisJoinPoint.getSignature()));
    }

 public void cat(Object dog) {
    System.out.println("[TEST] " + dog);
    }

}

I have the AspectJ: In Action book open beside me right now, and on all the load-time weaving examples it mentions that the program must be started with a -javaagent flag. Since my proram is a plugin, there is no way this can happen.

I also looked into ASM. I found a wonderful tutorial on building a profiler (basically what I wish to do) here.

Problem with that is that it once again uses the -javaagent flag when it starts, as well as the public static premain, so it is unsuitable, as I only have onEnable and onDisable.

Then I found out about Java's Attach API. Which from the looks of it would allow me to attach my agent, the profiler, after a class has been loaded. It seems perfect, but after half an hour searching I could not find a good example of this that I could understand.

Could anyone help? This is a two-in-one question: can AspectJ be used for this? And if so, how? Also, in the case that it cannot, could someone point me in the right direction for using the Attach API with an ASM profiler?

Thanks in advance!

like image 500
Xyene Avatar asked Dec 17 '25 03:12

Xyene


1 Answers

I did it!

I created a runtime attacher for the profiler outlined here. Its basically that, with the premain renamed to 'agentmain'.

I made a Util class that has the attacher along with other useful functions. The attacher works by creating a jar with the agent, with a manifest stating that it can profile. The Util class looks like this:

    public class Util {

    /**
     * Gets the current JVM PID
     * @return
     * Returns the PID
     * @throws Exception
     */

    public static String getPidFromRuntimeMBean() {
    String jvm = ManagementFactory.getRuntimeMXBean().getName();
    String pid = jvm.substring(0, jvm.indexOf('@'));
    return pid;
    }

    /**
     * Attaches given agent classes to JVM
     * 
     * @param agentClasses
     * A Class<?>[] of classes to be included in agent
     * @param JVMPid
     * The PID of the JVM to attach to
     */

    public static void attachAgentToJVM(Class<?>[] agentClasses, String JVMPid) {

    try {


    File jarFile = File.createTempFile("agent", ".jar");
    jarFile.deleteOnExit();

    Manifest manifest = new Manifest();
    Attributes mainAttributes = manifest.getMainAttributes();
    mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
    mainAttributes.put(new Attributes.Name("Agent-Class"),
        Agent.class.getName());
    mainAttributes.put(new Attributes.Name("Can-Retransform-Classes"),
        "true");
    mainAttributes.put(new Attributes.Name("Can-Redefine-Classes"), "true");

    JarOutputStream jos = new JarOutputStream(new FileOutputStream(
        jarFile), manifest);


    for(Class<?> clazz: agentClasses) {         
        JarEntry agent = new JarEntry(clazz.getName().replace('.',
            '/')
            + ".class");
        jos.putNextEntry(agent);

    jos.write(getBytesFromIS(clazz.getClassLoader()
        .getResourceAsStream(
            clazz.getName().replace('.', '/') + ".class")));
    jos.closeEntry();
    }

    jos.close();
    VirtualMachine vm = VirtualMachine.attach(JVMPid);
    vm.loadAgent(jarFile.getAbsolutePath());
    vm.detach();
    } catch (Exception e) {
        e.printStackTrace();
    }

    }

    /**
     * Gets bytes from InputStream
     * 
     * @param stream
     * The InputStream
     * @return 
     * Returns a byte[] representation of given stream
     */

    public static byte[] getBytesFromIS(InputStream stream) {

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    try {
        int nRead;
        byte[] data = new byte[16384];

        while ((nRead = stream.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
        }

        buffer.flush();
    } catch (Exception e) {
        System.err.println("Failed to convert IS to byte[]!");
        e.printStackTrace();
    }

    return buffer.toByteArray();

    }

    /**
     * Gets bytes from class
     * 
     * @param clazz    
     * The class
     * @return
     * Returns a byte[] representation of given class
     */

    public static byte[] getBytesFromClass(Class<?> clazz) {            
    return getBytesFromIS(clazz.getClassLoader().getResourceAsStream( clazz.getName().replace('.', '/') + ".class"));   
    }

}

I included JavaDoc comments for clarity.

An example of using it would be:

Util.attachAgentToJVM(new Class<?>[] { Agent.class, Util.class,
        Profile.class, ProfileClassAdapter.class,
        ProfileMethodAdapter.class }, Util.getPidFromRuntimeMBean());   

Remember that the attacher wants Agent.class to be the main agent. You can change this easily. The rest of the Class[] are classes to be included in the temporary agent.jar

If your IDE complains about "UnsatisfiedLinkError"s, it is because the attach.(dll|so) needed for this only comes with the JDK. Just copy paste it into your %JAVA_PATH%/jre/lib. Also, add a reference to your JDK's tools.jar, because it contains all the com.sun imports.

EDIT: I have a working github example is anyone might think this is useful. Its here.

like image 100
Xyene Avatar answered Dec 19 '25 18:12

Xyene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!