当前位置:网站首页>Btrace tells you how to debug online without restarting the JVM
Btrace tells you how to debug online without restarting the JVM
2022-07-04 11:24:00 【Java geek Technology】
Hello Hello everyone , I'm fan , I don't know if you've ever come across a scene like this , After running online services for a period of time, there will be problems occasionally , Code and data analysis alone cannot find the reason , And this situation is not very common, so the corresponding code does not add log output , If we add logs again to publish , It will destroy the scene. We can only wait for a while , Or sometimes I want to see the parameters of the interface , So as to determine whether there is a problem with the interface parameters .
At this time, I wonder if there is a good way , You can add some logs to see the running status and entry parameters without modifying the source code or publishing the upgrade ? The answer, of course, is yes , Now let's take a look at the artifact BTrace
!
Let's simulate a scene , This scenario is that there is a problem with an online service , An error will be reported when some requests are triggered , Now we want to see the details of the input parameters of the method interface when an error is reported .
Write a first demo
Code , An internal loop calls a method , As follows , This code simulates calling a method all the time through a loop , This method print
The input parameter of is a person
Packaging , The internal processing logic of the method is ignored here , And every time 3 Once per second . Now our needs are very simple , I just want to know the parameters of each run person
What is the value of each attribute of , In other words age
and name
What's the value of , Of course, we can't modify the source code to add printing and republishing .
package com.javajeek.demo;
public class BtraceTest {
public void print(Person person) {
System.out.println("-------- Handle Person ing------");
try {
//... Other logic
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void test(Person person) {
while (true) {
this.print(person);
}
}
public static void main(String[] args) {
BtraceTest btraceTest = new BtraceTest();
Person person = new Person(18, "ziyou");
btraceTest.test(person);
}
static class Person {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
There is nothing to say about the running results , Every time 3 One sentence per second , as follows .
At first glance, everyone may be a little confused , I find it difficult to achieve , This thing is not local debug
, Can you interrupt . At this time, we need to use our artifact , Although not local breakpoint debugging , But output the attribute value of the parameter and some JVM
The state of can still be realized .
BTrace
Before providing a solution , Let's take a look at what BTrace
,BTrace
yes sun
The company launched a Java
dynamic 、 Safe tracking ( monitor ) Tools , You can do it without restarting JVM
Monitor the operation of the system , Convenient access to the data information when the program is running , Such as method parameters 、 Return value 、 Global variables and stack information . In a nutshell BTrace
It's a tool , It can help us get some data of runtime in real time .
Download and install
First, let's download the compressed package wget https://github.com/btraceio/btrace/releases/download/v2.2.0/btrace-v2.2.0-bin.tar.gz
Download the latest version of the compressed package , After downloading, unzip it , Ah fan is here macOS
Shown above , If it is Windows
You can go directly to GitHub
It is the same to download it and decompress it manually .GitHub
Address https://github.com/btraceio/btrace/releases/tag/v2.2.0
.
wget https://github.com/btraceio/btrace/releases/download/v2.2.0/btrace-v2.2.0-bin.tar.gz
tar xzvf btrace-v2.2.0-bin.tar.gz
- 1.
- 2.
After decompression , We can see the directory structure as follows , among samples
There is a lot of sample code in the directory. Interested partners can go and have a look :
Use
In the use of BTrace
We need to write a Java
Script , Express the things we need to deal with in this script , If you want to know JVM
How is the operation of , What is the return value of a method of a class , What is the method input parameter and any information you want to know . Here is the script code of the solution in our case , As follows :
import org.openjdk.btrace.core.annotations.*;
import org.openjdk.btrace.core.types.*;
import static org.openjdk.btrace.core.BTraceUtils.*;
@BTrace
public class AllMethodsTest {
@OnMethod(
clazz = "com.lazada.marketing.search.ads.diamond.BtraceTest",
method = "print",
location = @Location(Kind.ENTRY)
)
public static void m(Object request) {
printFields(request);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
You can see that the core code is just a few lines , Write with us Java
The code is very similar , It is also an import package , Write a class , Redefine the method , At the same time, I added some comments of my own . Through the content of the annotation, we can deal with which class and which class's methods . About BTrace
What are the notes , And how each annotation is used. For details, please refer to this article https://ningyu1.github.io/site/post/39-btrace/ Very detailed , Ah fan thinks it's very good , Mention annotations with classes , Comment on method , And comments related to parameters .
Briefly, let's talk about a few comments we used in the above code ,@BTrace
,OnMethod
,@Location
The meaning of representation :
@BTrace
: Indicates that this class is a BTrace
Program ,BTrace
The compiler will force the lookup of this annotation ,BTrace
The agent will also check whether this annotation exists . without , Then the prompt is wrong , And will not execute .
OnMethod
: This annotation can be used to specify the target class , The target method , And in the target method “ Location ”. among clazz
and method
Respectively represent the class and method to be executed , It can be expressed by regularity .
@Location
: Represents a specific location in the method . For values, please refer to the table in the above article .
perform
With the above running program and BTrace
We can meet our requirements with the sample code of , First of all, our demo
The code is running , We go through jps
The command finds the corresponding pid
, The operation is as follows , Corresponding pid
yes 84287
.
We found pid
After that, we can execute BTrace
Procedure , Through the command ./bin/btrace 84287 samples/AllMethodsTest.java
, The results are shown in the figure below . You can see that the details of the input parameters are output , So far, our requirements have been met . Are the friends very happy , Very excited , Very excited ? This use is just BTrace
Powerful tip of iceberg , There are many better examples in samples
Under the table of contents , Interested partners can study .
summary
The above example , A fan is just a very simple but common way to use it , You can study more powerful applications by yourself , Many friends think there must be a doubt when they see this function , That's it BTrace
How to realize this function , A fan shares with you a meituan technology blog post , The introduction is quite detailed and easy to understand , You can go and have a look https://tech.meituan.com/2019/02/28/java-dynamic-trace.html,BTrace
Although easy to use , But it is relatively troublesome to use , I believe everyone knows that a better tool is Alibaba open source Arthas
, You can study .
< END >
Here's good news ,Java Geek technology reader group ( Mainly fishing ), Time interval 2 Years later, it opened up again , Interested friends , You can reply on the public account :999
边栏推荐
- Configure SSH certificate login
- Some summaries of the 21st postgraduate entrance examination 823 of network security major of Shanghai Jiaotong University and ideas on how to prepare for the 22nd postgraduate entrance examination pr
- Performance features focus & JMeter & LoadRunner advantages and disadvantages
- Common built-in modules
- iptables导致Heartbeat脑裂
- Function introduction of canbedded component
- Introduction to canoe automatic test system
- Ternsort model integration summary
- Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
- Dos and path
猜你喜欢
Some summaries of the 21st postgraduate entrance examination 823 of network security major of Shanghai Jiaotong University and ideas on how to prepare for the 22nd postgraduate entrance examination pr
Simple understanding of seesion, cookies, tokens
Canoe - the third simulation project - bus simulation-1 overview
QQ group collection
Deepmind proposed a Zuan AI, which specially outputs network attack language
Canoe: what is vtsystem
Function introduction of canbedded component
Canoe - the third simulation project - bus simulation - 2 function introduction, network topology
Reptile learning 4 winter vacation series (3)
2018 meisai modeling summary +latex standard meisai template sharing
随机推荐
Configure SSH certificate login
Cacti主机模板之定制版
Iptables cause heartbeat brain fissure
os. Path built-in module
Canoe: the difference between environment variables and system variables
三立期货安全么?期货开户怎么开?目前期货手续费怎么降低?
Elevator dispatching (pairing project) ③
2020 Summary - Magic year, magic me
Install freeradius3 in the latest version of openwrt
Ten key performance indicators of software applications
OSI seven layer reference model
Configure SSH key to realize login free
About the use of URL, href, SRC attributes
R built in data set
Reptile learning 4 winter vacation learning series (1)
OSI model notes
MBG combat zero basis
Getting started with window functions
2021 annual summary - it seems that I have done everything except studying hard
Lvs+kept realizes four layers of load and high availability