In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to use BTrace to diagnose problems in Java. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
What is BTrace?
BTrace is a safe, dynamic tracing tool for the Java platform. BTrace can be used to dynamically trace a running Java program (similar to DTrace for OpenSolaris applications and OS). BTrace dynamically instruments the classes of the target application to inject tracing code ("bytecode tracing"). Tracing code is expressed in Java programming language. There is also integration with DTrace for the OpenSolaris platform. Admins were sundararajana and Jaroslov Bachorik
To sum up, BTrace is a dynamic problem analysis and diagnosis tool, which is similar to DTrace on Solaris. Based on the Instrument feature provided by JDK, retransform of classes is carried out during the operation of JVM, so that the defined Trace script takes effect.
For example, analyze the following system problems:
There are a large number of Socket connections in the application, resulting in excessive occupation of Socket handles and system anomalies. You can directly analyze the places where Socket is opened and closed in the application code and count the number of times. Similar can be applied to database connections and so on.
The GC log of the application shows frequent calls to System.gc (). You can directly analyze the caller and print the call Stack.
Apply online to count the call time of a method
The return value of the online application view method call
...
For more information, please refer to UserGuide or the sample included in the download package.
Basic principles
There is a concept of Probe in BTrace, and all defined OnMethod can be treated as a probe, similar to the concept in today's APM tool, triggering at a specific node. Temporarily added Trace scripts that are provided by JVM TI and dynamically added to the target class by Instrument. This is equivalent to adding a new trace method, and then calling the trace method in the source method.
In our previous article, we wrote about the HotSwap implementation of class, and at that time, several diagrams showed the concrete form of the analyzed BTrace after adding a probe to the target class. (class loader and hot replacement of classes (Hotswap)
And the overall Instrument also implements the Retransform of the class through JVMTI, which was also written when analyzing the implementation principle of Debug: what are we talking about when we talk about Debug (Debug implementation principle)
Use
When it comes to the use of BTrace, it is actually very easy. Let's introduce two common ways:
1. Download https://github.com/btraceio/btrace/releases, download it here, and then write a Trace script to execute btrace PID script.java on the command line.
two。 Directly add the BTrace plug-in to JvisualVM, and then analyze the corresponding application, directly right-click the process, select Trace application, you can open a window similar to IDE, in which you can write Trace scripts and execute start.
It is also easy to write a Trace script. Declare the probe through OnMethod in any method, which is called Probe in BTrace, then specify the class name and method name of the trace, and directly write the logic to be executed when the corresponding Probe is triggered in the method body. For more details, please refer to UserGuide or some of the accompanying sample.
Comparatively speaking, the second is more convenient to use, but the disadvantage is that it does not support remote Trace.
Examples
For example, it is observed that System.gc () is sometimes executed in the application. In order to check whether it is called directly in the application, you can use BTrace
For the acquisition of database connection, you can directly judge the call of its getConnection.
I am lazy to write, these are on the gist to find a few cut pictures, the method to understand its syntax and some commonly used usage. For the constructor calls of some objects, you can directly specify the method as
Even for the execution time of a method, there is no need to increase it when the code is written without adding Log, it can be done directly through the script, and it takes time to print when the method exits:
In addition, for the parameters passed in the method body, the printing of the call chain can be easily carried out.
You can also get the property value of a class, as well as the call of a method within the class to another specified method.
For the above operations, the most important thing is that the application does not need to be stopped and can be modified dynamically, so as to realize the analysis and diagnosis of the application.
Of course, after the Trace script is executed, the content will remain in the loaded class until the application restarts or the class transform again.
On how to use BTrace in Java to achieve problem diagnosis is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.