How can I add a Javaagent to a JVM without stopping the JVM?
Asked Answered
H

4

25

I wish to profile a Java application without stopping the application. Can I add a Javaagent somehow while the application is running?

Hyperplane answered 27/1, 2011 at 14:36 Comment(12)
The times I wished I could attach the debugger in production...Kizzee
@Kizzee ouchy :). Then run it w/ the debugger option, you wont see any real performance drawback, but debuggers can cause leaksFebri
So does the debugger option allow me to add a javaagent at runtime?Hyperplane
@bestsss: They also can be a security vulnerability.Autarch
@Software Monkey - hardly security vulnerability - block the port and allow it only for a few selected IPs prior attaching the debugger.Febri
@bestsss: That you have to take additional action to block the port illustrates it's a potential security vulnerability.Autarch
@Software Monkey: sure it is, but it's easily solved. Yet, running almost any Application Server on Java opens a good number of ports, so taking care of debugger is no different. That was my point, opening the debugger port to the world is asking for a trouble (although the port is not standard), so is RMI one and so on.Febri
@bestsss, debug ports are security holes if ever there was one!Bellow
@Zubair, debugger option does not allow for attaching a javaagent.Bellow
@Thorbjørn Ravn Andersen: you can call 'em like that (hole) but I, generally, don't consider a hole something that I have done on purpose and am very well aware what it does.Febri
@bestsss, perhaps you do not. In a world where the applications (note: not app servers) run on customer machinery which happens to have other responsibilities too, it is usually unacceptable to have this into code running in production.Bellow
@Thorbjørn Ravn Andersen: that's a weird statement, the remark about debugging was mentioned to omerkudat and no one has talked about client machines at all. I'd not even consider connecting to anything not exclusively owned, unless explicitly asked by a customer and even then it would be just sending back logs.Febri
A
7

It should be possible according the documentation of the java.lang.instrument package.

Starting Agents After VM Startup

An implementation may provide a mechanism to start agents sometime after the the VM has started. The details as to how this is initiated are implementation specific but typically the application has already started and its main method has already been invoked. In cases where an implementation supports the starting of agents after the VM has started the following applies:

1.The manifest of the agent JAR must contain the attribute Agent-Class. The value of this attribute is the name of the agent class.
2. The agent class must implement a public static agentmain method.
3. The system class loader ( ClassLoader.getSystemClassLoader) must support a mechanism to add an agent JAR file to the system class path.

but I have never tried it :-|

Acquit answered 27/1, 2011 at 20:59 Comment(0)
S
31

See Starting a Java agent after program start.

It links to http://dhruba.name/2010/02/07/creation-dynamic-loading-and-instrumentation-with-javaagents/ that under "Dynamic loading of a javaagent at runtime" provides working example:

public static void loadAgent() throws Exception {
    String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
    String pid = nameOfRunningVM.substring(0, nameOfRunningVM.indexOf('@'));
    VirtualMachine vm = VirtualMachine.attach(pid);
    vm.loadAgent(jarFilePath, "");
    vm.detach();
}

Note that Java 9 requires -Djdk.attach.allowAttachSelf=true to be present among JVM startup arguments.

Saloma answered 23/10, 2012 at 14:29 Comment(0)
D
13

You can use ea-agent-loader

With it loading an agent in runtime will look like:

public class HelloAgentWorld
{
    public static class HelloAgent
    {
        public static void agentmain(String agentArgs, Instrumentation inst)
        {
            System.out.println(agentArgs);
            System.out.println("Hi from the agent!");
            System.out.println("I've got instrumentation!: " + inst);
        }
    }

    public static void main(String[] args)
    {
        AgentLoader.loadAgentClass(HelloAgent.class.getName(), "Hello!");
    }
}
Diarmid answered 3/3, 2016 at 15:7 Comment(1)
I checked the ea-agent-loader, which looks pretty good. Sadly it is not working for me: #48679057 and here github.com/electronicarts/ea-agent-loader/issues/9Abnormality
A
7

It should be possible according the documentation of the java.lang.instrument package.

Starting Agents After VM Startup

An implementation may provide a mechanism to start agents sometime after the the VM has started. The details as to how this is initiated are implementation specific but typically the application has already started and its main method has already been invoked. In cases where an implementation supports the starting of agents after the VM has started the following applies:

1.The manifest of the agent JAR must contain the attribute Agent-Class. The value of this attribute is the name of the agent class.
2. The agent class must implement a public static agentmain method.
3. The system class loader ( ClassLoader.getSystemClassLoader) must support a mechanism to add an agent JAR file to the system class path.

but I have never tried it :-|

Acquit answered 27/1, 2011 at 20:59 Comment(0)
H
1

Here a library that initializes aspectj and spring-aspects at runtime by injecting instrumentation: https://github.com/subes/invesdwin-instrument

You can use it as a more elaborate sample.

Hecatomb answered 3/8, 2015 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.