Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of agentmain method in java instrumentation

I done some java bytecode instrumentation with -javaagent argument and premain method. But this is the first time i hear about agentmain method. I have some questions about this method. Here follows it.

Both premain and agentmain method have same use?
When agentmain method invoked?
What is the use of agentmain method in java instrumentation?

like image 415
Anish Antony Avatar asked Nov 05 '13 09:11

Anish Antony


1 Answers

premain is invoked when an agent is started before the application. Agents invoked using premain are specified with the -javaagent switch.

agentmain is invoked when an agent is started after the application is already running. Agents started with agentmain can be attached programatically using the Sun tools API (for Sun/Oracle JVMs only -- the method for introducing dynamic agents is implementation-dependent).

An agent can have both a premain and an agentmain, but only one of the two will be called in a particular JVM invocation. In other words, your agent will either start with premain or agentmain, but not both.

You can find more information about this in the answer to the question Starting a Java agent after program start.

like image 196
Charles Forsythe Avatar answered Nov 08 '22 08:11

Charles Forsythe