We strongly recommend the btrace tool. Once used, we have to say that it is too powerful. btrace can simply be used without modifying the current program, the runtime monitors the execution status of Java programs, such as monitoring the memory status and method calls. There are many detailed examples on the official website, the following is a simple example to describe its function.
For more information about Guide, see http://kenai.com/projects/btrace/pages/userguide.
For a running Java program, especially a program with a problem, you need to track its execution status, such as the input parameter, execution time, and returned object, if any exception is thrown, the traditional method is to modify the program and add a bunch of logs. An example is provided to show the situation where btrace is used, how to track the execution time of a method:
@ Btrace public class methodresponsetime {
@ TLS Private Static long starttime;
@ Onmethod (clazz = "Class Name", method = "method name ")
Public static void oncall (){
Println ("enter this method ");
Starttime = timemillis ();
}
@ Onmethod (clazz = "Class Name", method = "method name", location = @ location (kind. Return ))
Public static void onreturn (){
Println ("method end! ");
Println (strcat ("time taken ms", STR (timemillis ()-starttime )));
}
}
With btrace, You can dynamically monitor the execution time of a certain method in any currently running Java program. The method for executing the above Code is as follows (JDK 6 + ):
Btrace [pid] methodresponsetime. Class
For example, to obtain the call parameters, the caller's object instance, and the return value, see user guide.
Btrace imposes many restrictions to ensure the security of JVM operations, such as not throwing exceptions, modifying input parameter values, and modifying return values, it is basically a read-only tool for dynamically analyzing the code running status, but it is still very useful. Its implementation mechanism is attach.
API + ASM + instrumentation.