4

We are developing an application for obtaining the information of a running java application using JDWP and JVMTI. Sun Java comes with a reference implementation of JDWP so using agentlib -jdwp: will use the reference implementation. Our aim is to study JVMTI and write JVMTI agents to get specific detail. Also creating a Front end using which the user can request for specific information about the running java application. For this we would have to write our implementation for JDWP. Though we were able to write JVMTI agents and use it to attach to the java application from the command line. But our aim is to send this information to another java process (Front End).

So we have a Java application- Front end querying for information from the JVMTI Agents (Back-End). These agents should be able to attach to the running java application we are going to inspect dynamically. Any ideas on how this can be done? Or has anyone tried this before?

2
  • What kind of information are you trying to grab? It sounds like JMX is not enough. Could you elaborate? Apr 5, 2011 at 19:37
  • I was trying to write JVMTI agents in C which would connect to a running Java Application and then send the performance data of the application to another GUI application. I somehow managed to implement this. And its been almost a year since I finished this and was unable to document my work. Apr 6, 2011 at 16:59

1 Answer 1

6

The JDK has a built-in Java api for remote/local debug (com.sun.jdi.).

To quickly start using the api, you can look at examples in $JDK_DIR\demo\jpda\examples.jar. [More details]


Since the official examples are not so clear, here is reusable example (inspired by this blog post):

VMAcquirer.java (Connection manager)

public class VMAcquirer {

    public VirtualMachine connect(String host,int port) throws IOException {
        String strPort = Integer.toString(port);
        AttachingConnector connector = getConnector();
        try {
            VirtualMachine vm = connect(connector,host, strPort);
            return vm;
        } catch (IllegalConnectorArgumentsException e) {
            throw new IllegalStateException(e);
        }
    }

    private AttachingConnector getConnector() {
        VirtualMachineManager vmManager = Bootstrap.virtualMachineManager();

        for (Connector connector : vmManager.attachingConnectors()) {
            if("com.sun.jdi.SocketAttach".equals(connector.name()))
                return (AttachingConnector) connector;
        }
        throw new IllegalStateException();
    }

    private VirtualMachine connect(AttachingConnector connector,String host,String port)
            throws IllegalConnectorArgumentsException, IOException {

        Map<String, Connector.Argument> args = connector.defaultArguments();
        Connector.Argument portArg = args.get("port");
        portArg.setValue(port);
        Connector.Argument addressArg = args.get("hostname");
        addressArg.setValue(host);

        return connector.attach(args);
    }
}

Monitor.java (Actual monitoring)

class Monitor {
    public static void main(String[] args){
        VirtualMachine vm = new VMAcquirer().connect("192.168.0.x", 2600);

        System.out.println("name="+vm.name()); //Info about the remote VM
        System.out.println("description="+vm.description());

        EventRequestManager erm = vm.eventRequestManager();
        [...] //Send request using erm instance

        loopEventQueue(vm); //Start a loop to listen to the events received
    }

    public static void loopEventQueue(VirtualMachine vm) throws InterruptedException {
        EventQueue eventQueue = vm.eventQueue();
        while (true) {
            EventSet eventSet = eventQueue.remove();
            for (Event ev : eventSet) {
                if(ev instanceof MethodEntryEvent) {
                    handleMethodEntryEvent(ev);
                }
                [...]
            }
        }
    }
}

Required by the application being monitor

java -Xdebug -Xrunjdwp:transport=dt_socket,address=2600,server=y,suspend=n ...

Informations available through JDWP

  • Tracing method call and return (possibly for doing profiling or logging)
  • Field value changes
  • VM information (see name() and description() in the example)
  • Evaluate expression to execute arbitrary code

  • 3
    • thanks a lot for the answer. my requirement was slightly different- the example above uses the refrence implementation for jvmti, jdwp which comes with the jdk. I was looking for help in implementing the jvmti specifications- writing jvmti agents in C and then using them in monitoring the java applications. Also setup a transport mechanism to communicate the data from the jvmti agent to the java front end which would then display the results. Jun 25, 2011 at 4:18
    • 4
      I'll keep the answer in case it can help somebody who need something similar.
      – h3xStream
      Jun 25, 2011 at 4:29
    • @h3xStream Excellent answer! do you have any ideas regarding my question at stackoverflow.com/questions/21212895/… ? Thanks!
      – isapir
      Jan 19, 2014 at 3:38

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.