BTrace tool Introduction
BTrace tool Introduction
Java Process diagnosis and analysis tools
Secure tools
Non-invasive
No application data is modified.
Restrict tracking behaviors, and there is no loop
Dependent components
Use the OjbectWeb ASM component to track and analyze bytecode
Open-source components
Project home: http://btrace.dev.java.net
GPLv2 + CLASSPATH Exception
VisualVM plug-ins and Development plug-ins
The reason why BTrace is widely used is that it is secure and non-invasive, and hot interaction technology has enabled us to dynamically track and analyze without starting the Agent, its security does not cause any destructive impact on the target Java Process, making BTrace a powerful tool for locating problems in our online products. No intrusion. We do not need to make any modifications to the original code to reduce launch risks and test costs, and do not need to restart the target Java Process for Agent loading to dynamically analyze and track the target program, it can be said that BTrace can meet most application scenarios.
Support tracking types through annotations
L method call
L method return
L The capture method is abnormal.
L row number
L field get/set
L method call/Return (in the specified method)
L before and after an exception is thrown
L synchronous entry/exit
L Timer
BTrace script
To ensure that the trace statement is read-only, BTrace has some restrictions on the trace script (for example, it cannot change the state in the trace code). A static method must be provided in the trace class, the BTraceUtils class is called to complete some actions. These actions are safe and a subset specified by BTrace. For details, see the following description:
L BTrace prohibits new classes and arrays, throws exceptions, and captures exceptions.
L do not call other instance methods and static methods except the com. sun. btrace. BTraceUtil class.
L no instance fields or methods are available before BTrace1.2. Only static methods with or without return values must be available. All fields must also be static.
L defining external, internal, anonymous, and local classes is prohibited
L prohibit synchronization blocks and Methods
L prohibit loops (for, while, do... while)
L implementing interfaces is prohibited, and classes cannot be extended. The direct superclass must be java. lang. Object.
L The assert statement cannot be used, and the class literal value cannot be used.
L class bytecode prohibited
Method Annotation
L @ OnMethod is used to specify the target class, method, and location of the trace. The annotated method is called when the matching method is executed to the specified location. The "clazz" attribute is used to specify the target class name. You can specify a fully qualified class name, for example, "java. awt. it can also be a regular expression (the expression must be written in "//", for example, "/java \\. awt \\.. + /"). The "method" attribute is used to specify the method to be traced. for the expression, see the built-in example (NewComponent. java and Classload. java. For method annotations, see MultiClass. java ). some trace classes and methods may also use annotations. for usage reference, see the built-in example WebServiceTracker. java. you can also use regular expressions for annotations, such as @/com \\. acme \\.. +/", you can also specify a superclass to match multiple classes, such as" + java. lang. runnable "can match all implemented java. lang. runnable interface class. for more information, see the built-in example SubtypeTracer. java.
L @ OnTimer: The Trace is triggered on a regular basis. The time can be specified in milliseconds. For details, refer to the built-in example Histogram. java.
L @ OnError when the trace code throws an exception or error, the annotation method will be executed. If other methods in the same trace script throws an exception, the annotation method will also be executed.
L @ OnExit when the trace method calls the built-in exit (int) method (used to end the entire trace program), the annotation method will be executed. See the built-in example ProbeExit. java.
L @ OnEvent is used to intercept events triggered by the "external" btrace client. For example, press Ctrl-C to interrupt the execution of btrace and select 2, or enter the event name, the method using the annotation is executed. The value of the annotation is the name of a specific event. For more information, see HistoOnEvent. java.
L @ OnLowMemory the annotation method will be triggered when the memory exceeds a set value. For details, refer to MemAlerter. java
L @ OnProbe use the external file XML to define the trace method and the specific location. For details, refer to the example SocketTracker1.java and java.net. socket. xml.
Parameter Annotation
L @ Self is used to specify this for the trace method. For example, AWTEventTracer. java and AllCalls1.java
L @ Return is used to specify the Return value of the trace method. For details, refer to the Classload. java example.
L @ ProbeClassName (since 1.1) is used to specify the class name to be traced. See the example AllMethods. java
L @ ProbeMethodName (since 1.1) is used to specify the method name to be traced. See the example WebServiceTracker. java.
L @ TargetInstance (since 1.1) is used to specify the instance internally called by the trace method. See the example AllCalls2.java
L @ TargetMethodOrField (since 1.1) is used to specify the name of the method called inside the trace method. For details, refer to the examples of AllCalls1.java and AllCalls2.java.
Non-Annotated method parameters
Method parameters without annotations are generally used for method Signature Matching. They are generally consistent with the order in which parameters appear in the trace method. however, they can also be staggered with the annotation method. If a parameter type is declared as * AnyType [] *, it indicates that all parameters of the method are "eaten" in order. the unannotated method must be used with * Location:
L Kind. ENTRY-trace method parameter
L Kind. RETURN-returned value by the trace Method
L Kind. THROW-throwing an exception
L Kind. ARRAY_SET, Kind. ARRAY_GET-array index
L Kind. CATCH-CATCH exceptions
L Kind. FIELD_SET-attribute value
L Kind. LINE-row number
L Kind. NEW-class name
L Kind. ERROR-throwing an exception
Attribute Annotation
L @ Export the static attribute of this annotation is mainly used to associate with the jvmstat counter. with this annotation, The btrace program can expose the execution times of the trace program to the jvmstat client (which can be used to count the memory usage in the jvm heap). For details, refer to the example ThreadCounter. java
L @ Property the trace script using this annotation will be used as an attribute of MBean. Once this annotation is used, the trace script will create an MBean and register it with the MBean server, in this way, the JMX client, such as VisualVM, can see these BTrace mbeans in jconsole. if these annotated attributes are associated with those of the trace program, you can view these attributes through VisualVM and jconsole. for more information, see ThreadCounterBean. java and HistogramBean. java.
L @ TLS is used to associate a script variable with a ThreadLocal variable. because the ThreadLocal variable is thread-related, it is generally used to check whether trace methods are executed in the same thread call. for more information, see OnThrow. java and WebServiceTracker. java
Class Annotation
L @ com. sun. btrace. annotations. DTrace is used to specify the association between the btrace script and the D language script embedded in the script. For details, see the example DTraceInline. java.
L @ com. sun. btrace. annotations. DTraceRef is used to specify the association between the btrace script and another D language script file. For details, see the example DTraceRefDemo. java.
L @ com. sun. btrace. annotations. BTrace is used to specify the java class as a btrace script file.
Related instance description
The sample provided by BTrace is the final material for learning BTrace. It is proficient in using the sample provided by BTrace and can be verified manually, so that you can quickly familiarize yourself with BTrace and load applications, A large part of the built-in sample can be directly or slightly modified to become our positioning script for ease of use.
L AWTEventTracer. java-demonstrate the trace Method for EventQueue. dispatchEvent () events. You can use instanceof to filter events. For example, here only focus event trace is used.
L AllLines. java-demonstrate how to execute the specified operation when the trace program reaches the class specified by probe and the specified row number (in this example, the specified row number is-1 to indicate any row ).
L AllSync. java-demonstrate how to trace the synchronization block during/out.
L ArgArray. java-demonstrate the input parameters for printing the readXXX method of all classes in the java. io package.
L Classload. java-demonstrate that the specified class and stack information are successfully loaded.
L CommandArg. java-demonstrate how to obtain btrace command line parameters.
L Deadlock. java-demonstrate the @ OnTimer annotation and built-in deadlock () method usage
L DTraceInline. java-demonstrate @ DTrace annotation usage
L DTraceDemoRef. java-demonstrate @ DTraceRef annotation usage.
L FileTracker. java-demonstrate how to perform trace operations on the File {Input/Output} Stream constructor to initialize and open a File's read/write File.
L FinalizeTracker. java-demonstrates how to print all the attributes of a class, which is useful in debugging and fault analysis. the example here is to print the information when the close ()/finalize () method of the FileInputStream class is called.
L Histogram. java-demonstrate how many times javax. swing. JComponent was created in an application.
L HistogramBean. java-the preceding example demonstrates how to integrate with JMX. The map attribute is exposed as an MBean by using the @ Property annotation.
L HistoOnEvent. java-the same example above demonstrates how to print the number of times of creation when the current script is interrupted by pressing ctrl + c, rather than timed printing.
L JdbcQueries. java-demonstrate the aggregation function. For details about the aggregation function, see DTrace.
L JInfo. java-demonstrate the built-in Methods printVmArguments (), printProperties () and printEnv ()
L JMap. java-demonstrate the usage of the built-in method dumpHeap (). It dumps the heap information of the target application in binary format.
L JStack. java-demonstrate the use of the built-in method jstackAll (), that is, to print the stack information of all threads.
L LogTracer. java-demonstrate how to go deep into the instance method (Logger. log) and call the built-in method (field () to print private attribute content.
L MemAlerter. java-demonstrate the use of @ OnLowMememory annotation to monitor memory usage. That is, the memory information is printed when the old generation in the heap memory reaches the specified value.
L Memory. java-demonstrate printing Memory statistics every 4s.
L MultiClass. java-demonstrate trace multiple methods of multiple classes by using regular expressions.
L NewComponent. java-use the counter to check the number of java. awt. Component created in the current application at intervals.
L OnThrow. java-when an exception is thrown, the exception stack information is printed.
L ProbeExit. java-demonstrate @ OnExit annotation and built-in exit (int) method usage
L Profiling. java-demonstrate the support for profile. // I failed to execute the program, and an error occurred in BTrace.
L Sizeof. java-demonstrate the use of the built-in sizeof method.
L SocketTracker. java-demonstrate the trace of the socket creation/bind method.
L SocketTracker1.java-Same as above, only @ OnProbe is used.
L SysProp. java-demonstrate using the built-in method to obtain System attributes. Here we trace the getProperty method of java. lang. System.
L SubtypeTracer. java-demonstrate how to trace the specified methods of all subclasses of the specified superclass.
L ThreadCounter. java-demonstrate how to use the jvmstat counter in the script. (jstat-J-Djstat.showUnsupported = true-name btrace.com. sun. btrace. samples. threadCounter. count needs to be accessed from outside through jstat)
L ThreadCounterBean. java-Same as above, only JMX.
L ThreadBean. java-demonstrate the use of the pre-compiler (combined with JMX ).
L ThreadStart. java-demonstrate the usage of DTrace in the script.
L Timers. java-demonstrate using multiple @ OnTimer in one script at the same time
L URLTracker. java-demonstrate printing the URL every time the url. openConnection returns successfully. The D language script is also used here.
L WebServiceTracker. java-demonstrate how to trace based on annotations.
Other related blog posts
BTrace tools overview http://mgoann.iteye.com/blog/1409667
BTrace instance application http://mgoann.iteye.com/blog/1409676
BTrace case analysis http://mgoann.iteye.com/blog/1409685