Http://www.myexception.cn/program/729263.html
Btrace usage Summary
I. background
Various problems may occur frequently in the production environment. To locate the problem, you need to obtain the data information when the program is running, such as method parameters, return values, global variables, and stack information. To obtain the data information, we can rewrite the code, print the log information, and then release it to the production environment. In this way, on the one hand, it will increase the cost and cycle of problem locating, and fail to respond to urgent problems in a timely manner; on the other hand, the environment may be damaged after redeployment, and it is difficult to re-engineer the problem.
Ii. btrace Function
Btrace is born to solve such problems, and it can dynamically track Java running programs. With the hotswap technology, the tracking bytecode is dynamically injected into the running class, with little intrusion to the running code and negligible impact on performance.
Btrace has many restrictions on usage, such as the inability to create objects, arrays, throws and capture exceptions, and loops. For specific restrictions, see btrace restrictions in the user's document. User document address: http://kenai.com/projects/btrace/pages/userguide.
According to the official statement, improper use of btrace may cause JVM crash, such as the incorrect. Class file in btrace and the hotswap bug in hotspot JVM. You can verify the correctness of the btrace script locally before uploading it to the production environment to locate the problem.
Iii. Installation Steps
1. download and install the compressed package. The latest version is 1.2.1: http://kenai.com/projects/btrace/downloads/directory/releases.
2. decompress the package and place the Command Script in the bin directory.
3. Set the script environment variable.
4. added the script executable permission.
Iv. Usage
Btrace mainly includes btracec and btrace commands to compile and start btrace scripts:
1. btrace
Function: run the btrace program.
Command Format:
Btrace [-I <include-path>] [-P <port>] [-CP <classpath>] <pid> <btrace-script> [<ARGs>]
Example:
Btrace-CP build/1200 allcalls1.java
Parameter description:
Include-path specifies the path of the header file for script preprocessing. Optional;
Port: Specifies the port number of the btrace agent server to listen to clients. The default value is 2020. Optional;
Classpath is used to specify the class loading path. The default value is the current path. Optional;
PID indicates the process number, which can be obtained through the JPS command;
Btrace-script is a btrace script. If the btrace script ends with. Java, It is compiled and then submitted for execution. You can use the btracec command to pre-compile the script.
ARGs is an optional parameter of the btrace script. You can use "$" and "$ length" to obtain parameter information in the script.
2. btracec
Function: Used to pre-compile btrace scripts to verify script correctness during compilation.
Btracec [-I <include-path>] [-CP <classpath>] [-D <directory>] <one-or-more-btrace-. Java-files>
The parameter meaning is the same as that of the btrace command. Directory indicates the output directory of the compilation result.
3. btracer
Function: The btracer command starts the application and btrace script at the same time, that is, the btrace script is used when the application starts. The btrace command executes the btrace script for the running program.
Command Format:
Btracer <pre-compiled-btrace.class> <application-Main-class> <application-ARGs>
Parameter description:
The pre-compiled-btrace.class represents the btrace script compiled by btracec.
Application-Main-class indicates the application code;
Application-ARGs indicates the application parameters.
This command is equivalent to the following:
Java-javaagent: btrace-agent.jar = script = <pre-compiled-btrace-script1> [, <pre-compiled-btrace-script1>] * <mainclass> <apparguments>
4. jvisualvm plug-in
Btrace provides the jvisualvm plug-in. It is strongly recommended that you write and test btrace scripts in jvisualvm to enable, disable, send events, and add classpath.
V. btrace practices
1. Sample Code
The sample code defines the Counter. There is an add () method. Each time a random value is added, the total number is saved in the totalcount attribute.
Package COM. learnworld; import Java. util. random; public class btracetest {public static void main (string [] ARGs) throws exception {random = new random (); // Counter = new counter (); while (true) {// Add a Random counter each time. add (random. nextint (10); thread. sleep (1000 );}}}
Package COM. learnworld; public class counter {// total Private Static int totalcount = 0; Public int add (INT num) throws exception {totalcount + = num; sleep (); Return totalcount ;} public void sleep () throws exception {thread. sleep (1000 );}}
2. Common Use Cases
The following describes how to use the btrace script in several common scenarios.
1) obtain the value and return value of the add () method.
import com.sun.btrace.annotations.*;import static com.sun.btrace.BTraceUtils.*;@BTracepublic class TracingScript { @OnMethod( clazz="com.learnworld.Counter", method="add", location=@Location(Kind.RETURN) ) public static void traceExecute(int num,@Return int result){ println("====== "); println(strcat("parameter num: ",str(num))); println(strcat("return value:",str(result))); }}
2) regularly obtain the counter class attribute value totalcount.
import com.sun.btrace.annotations.*;import static com.sun.btrace.BTraceUtils.*;@BTracepublic class TracingScript { private static Object totalCount = 0; @OnMethod( clazz="com.learnworld.Counter", method="add", location=@Location(Kind.RETURN) ) public static void traceExecute(@Self com.learnworld.Counter counter){ totalCount = get(field("com.learnworld.Counter","totalCount"), counter); } @OnTimer(1000) public static void print(){ println("====== "); println(strcat("totalCount: ",str(totalCount))); }}
3) obtain the execution time of the add method.
import com.sun.btrace.annotations.*;import static com.sun.btrace.BTraceUtils.*;@BTracepublic class TracingScript { @TLS private static long startTime = 0; @OnMethod( clazz="com.learnworld.Counter", method="add" ) public static void startExecute(){ startTime = timeNanos(); } @OnMethod( clazz="com.learnworld.Counter", method="add", location=@Location(Kind.RETURN) ) public static void endExecute(@Duration long duration){ long time = timeNanos() - startTime; println(strcat("execute time(nanos): ", str(time))); println(strcat("duration(nanos): ", str(duration))); } }
4) obtain the number of times the add () method calls sleep.
import com.sun.btrace.annotations.*;import static com.sun.btrace.BTraceUtils.*;@BTracepublic class TracingScript { private static long count; @OnMethod( clazz="/.*/", method="add", location=@Location(value=Kind.CALL, clazz="/.*/", method="sleep") ) public static void traceExecute(@ProbeClassName String pcm, @ProbeMethodName String pmn, @TargetInstance Object instance, @TargetMethodOrField String method){ println("====== "); println(strcat("ProbeClassName: ",pcm)); println(strcat("ProbeMethodName: ",pmn)); println(strcat("TargetInstance: ",str(classOf(instance)))); println(strcat("TargetMethodOrField : ",str(method))); count++; } @OnEvent public static void getCount(){ println(strcat("count: ", str(count))); }}
Vi. References
1. userguide: http://kenai.com/projects/btrace/pages/UserGuide
2. Java Doc: http://btrace.kenai.com/javadoc/1.2/index.html
3. btrace user manual, http://macrochen.iteye.com/blog/838920
4. btrace usage, http://rdc.taobao.com/team/jm/archives/509
5. btrace memory, http://agapple.iteye.com/blog/962119
6. btrace something you don't know (source code), http://agapple.iteye.com/blog/1005918