JavaSe:-javaagent,-agentlib,-agentpath, javase-javaagent

Source: Internet
Author: User

JavaSe:-javaagent,-agentlib,-agentpath, javase-javaagent

Brief Description

  • -Javaagent,-agentlib,-agentpath description
  • -Javaagent example

 

-Javaagent,-agentlib, and-agentpath

-Agentlib: libname [= options] 

Used to load the local lib package;
Here, libname is the name of the local agent library, and the default search PATH is the PATH in the environment variable PATH. options is the parameter passed to the local database at startup. Multiple parameters are separated by commas. On the Windows platform, jvm searches for the local database named libname. dll file, in linux jvm search local library name libname. so files, search PATH environment variables vary on different systems, for example, Solaries, LD_LIBRARY_PATH is searched by default.
For example:-agentlib: hprof
It is used to obtain the jvm running status, including CPU, memory, thread, and other running data, and can be output to a specified file. In windows, the search path is JRE_HOME/bin/hprof. dll.

-Agentpath: pathname [= options] 
Load the local library by full PATH without searching the PATH in PATH. Other functions are the same as those in agentlib. More information is to be continued and will be detailed in the subsequent JVMTI sections.

-Javaagent: jarpath [= options] 

Specifies that jvm startup enters the java language infrastructure proxy. The mainfest file in the jarpath file must have the Premain-Class (required before binding) and Agent-Class (required during runtime binding) attributes. The proxy class must also implement the public static void premain (String agentArgs, Instrumentation inst) method (similar to the main method ). During jvm initialization, The premain method is called in the order described by the proxy class. For details, see the description of java. lang. instrument Software Package.

The specific description of the comments in the program below the parameter:

-Javaagent example

1 package com. fjn. jdk. jvm. options. java agent; 2 3 import java. io. IOException; 4 import java. lang. instrument. instrumentation; 5 6 import com. sun. tools. attach. attachNotSupportedException; 7 8 public class CustomAgent {9/** 10 * If the Agent is bound to a program using the JVM option, the premain method will be executed after the JVM Initialization is complete, after premain is executed, it is the main method of the program. 11 * in the configuration file, you must specify Premain-Class.
12 * <p> 13 * premain has two forms. By default, 1 is executed. If not 1), 2 is executed. 1) and 2 is executed) only one <br> 14 * <code> 15*1) public static void premain (String agentArgs, Instrumentation instrumentation) is executed. <br/> 16*2) public static void premain (String agentArgs) 17 * </code> </p> 18*19 * @ param agentArgs20 * @ param instrumentation21 */22 public static void premain (String agentArgs, instrumentation instrumentation) {23 System. out24. println ("Cu StomAgent # premain (String agentArgs, Instrumentation instrumentation) "); 25 parseAgentArgs (agentArgs); 26} 27 28 public static void premain (String agentArgs) {29 System. out. println ("CustomAgent # premain (String agentArgs)"); 30 parseAgentArgs (agentArgs); 31} 32 32 33/** 34 * If the Agent is running, dynamic bundling to the program is to execute the agentmain method. 35 * in the inventory file you need to specify the Agent-Class36 * <p> 37 * agentmain has two forms, 1 by default), 2 if not 1), 1) and 2) Only one <br> 38 * <code> 39*1) public static void agentmain (String agentArgs, Instrumentation instrumentation) is executed. <br/> 40*2) public static void agentmain (String agentArgs) 41 * </code> </p> 42*43 * code bundled by a program: <br/> 44 * <code> 45 * VirtualMachine vm = VirtualMachine. attach ("PID"); // bind the agent to the specified process <br/> 46 * after obtaining the vm of the target process, you can run M. loadAgent ("agentjar"), vm. loadAgentLibrary (dll), and loadAgentPath (dllPath) are bundled <br/> 48 * of them: <br> 49 * loadAgent is a jar file bundled with 50 * loadAgentLibrary, loadAgentPath is bound to the local keystore Library (Dynamic Connection Library) 51 * </code> 52*53 * @ param agentArgs54 * @ param inst55 */56 public static void agentmain (String agentArgs, instrumentation inst) {57 System. out58. println ("CustomAgent # agentmain (String agentArgs, Instrumentation instrume Ntation) "); 59 parseAgentArgs (agentArgs); 60} 61 62 public static void agentmain (String agentArgs) {63 System. out. println ("CustomAgent # agentmain (String agentArgs)"); 64 parseAgentArgs (agentArgs); 65} 66 67/** 68 * parameters can be specified for both premain and agentmain, the parameter is a string, and how to parse it is the program's own things 69 * @ param agentArgs70 * @ return71 * @ throws IOException 72 * @ throws AttachNotSupportedException 73 */74 private static boolean parseA GentArgs (String agentArgs) {75 boolean hasArgs = false; 76 if (agentArgs! = Null &&! AgentArgs. isEmpty () {77 System. out. println ("agentArgs is:" + agentArgs); 78 hasArgs = true; 79} else {80 System. out. println ("has no agentArgs. "); 81} 82 83 return hasArgs; 84} 85}

Package the class into customagent. jar and put it in the root directory of drive D,

Configuration file:

Manifest-Version: 1.0Premain-Class: com.fjn.jdk.jvm.options.javaagent.CustomAgentSealed: true

 

Test code:

Package com. fjn. jdk. jvm. options. javaagent; import org. junit. test; public class JavaAgentTest {/*** at startup, use the-javaagent method to add the agent * <code> *-javaagent: D:/customagent. jar = "Here, your can input agent arguments" * if you want to specify the parameter value * </code> */@ Test public void testPremain () {System. out. println ("test premain, sepcial inst... ");}}

Execution result:

CustomAgent#premain(String agentArgs, Instrumentation instrumentation)agentArgs is : Here, your can input agent argumentstest premain,  sepcial inst...

 

To learn about the dynamic binding mechanism, refer:

Http://ifeve.com/jvm-attach/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.