当前位置:网站首页>Btrace- (bytecode) dynamic tracking tool
Btrace- (bytecode) dynamic tracking tool
2022-07-02 14:56:00 【Carefree hero】
Source download :https://gitee.com/hong99/spring.git (springboot_btrace)
btrace What is it? ?
github:https://github.com/btraceio/btrace
btrace Is a Java Platform's secure dynamic tracking tool .BTrace It can be used to dynamically track running Java Program ( Be similar to DTrace be used for OpenSolaris Applications and operating systems ).BTrace Dynamically detect the class injection tracking code of the target application (“ Bytecode tracking ”).
Personal understanding : It is used for bytecode tracking , Used to solve the specific implementation of what , So the log concept is just a record of the results of implementation .
Be careful :btrace The bottom layer is based on ASM Interested students, look here :http://asm.ow2.org
Download address :
https://github.com/btraceio/btrace-maven (maven)
https://github.com/btraceio/btrace.git (gradle)
Using document :https://github.com/btraceio/btrace/wiki#btrace
https://github.com/btraceio/btrace/releases/tag/v2.2.2 ( Run the tool )
btrace Some of the conventions of
Object creation is not allowed
Creating arrays is not allowed
Throwing exceptions is not allowed
Don't allow catch abnormal
Do not call methods of other objects or classes at will , Only calls are allowed com.sun.btrace.BTraceUtils Static methods provided in ( Some data processing and information output tools )
Changing the properties of a class is not allowed
Member variables and methods are not allowed , Only allowed static public void Method
Inner classes are not allowed 、 Nested class
Synchronization method and synchronization block are not allowed
Loop not allowed
It is not allowed to inherit other classes at will ( Of course ,java.lang.Object With the exception of )
Interface implementation is not allowed
Not allowed assert
Not allowed Class object
So many restrictions , It's understandable .BTrace What we need to do is , Although the bytecode has been modified , But in addition to outputting the required information , It has no effect on the normal operation of the whole program .
btrace What are the specific scenes ?
btrace It can be used for code log follow-up , And the analysis during the implementation of the method ;
btrace It can be used to monitor the slow performance of the interface , Analyzing the time-consuming of each method ;
btrace Can be used for analysis gc And call stack information ;
btrace It can be used to analyze abnormal information ;
btrace It can be used to collect system related information ;
....
btrace Use
download btrace Running package :
https://github.com/btraceio/btrace/releases
Configuration environment
mac:
#jdk To configure
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home"
CLASS_PATH="$JAVA_HOME/lib"
PATH=".$PATH:$JAVA_HOME/bin"
# Key configurations btrace Environmental Science
BTRACE_HOME=/Users/csh/Desktop/tools/btrace
export BTRACE_HOME
# This is the key
export PATH=${PATH}:${BTRACE_HOME}/bin
View version information
windows:
BTRACE_HOME: Your path
PATH newly added :%BTRACE_HOME%\bin
View environment variables
Introduce client package
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.tools.btrace</groupId>
<artifactId>btrace-client</artifactId>
<scope>system</scope>
<type>jar</type>
<systemPath>D:/tools/btrace/libs/btrace-client.jar</systemPath>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.sun.tools.btrace</groupId>
<artifactId>btrace-agent</artifactId>
<scope>system</scope>
<systemPath>D:/tools/btrace/libs/btrace-agent.jar</systemPath>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.sun.tools.btrace</groupId>
<artifactId>btrace-boot</artifactId>
<scope>system</scope>
<type>jar</type>
<systemPath>D:/tools/btrace/libs/btrace-boot.jar</systemPath>
<version>2.2.2</version>
</dependency>
Be careful :systemPath Lu Jin downloads the package for you
Project configuration | edition | remarks |
springboot | 2.4.0 | |
btrace | 2.2.2 | |
junit | 3.8.1 | |
port | 8386 |
home page
package com.hong.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author: csh
* @Date: 2021/1/12 10:16
* @Description:
*/
@RestController
public class IndexController {
@RequestMapping("/")
public String index() {
return " success !";
}
@RequestMapping("/1")
public String index1(@RequestParam String name) {
return name;
}
}
package com.hong.springboot.config;
import org.openjdk.btrace.core.annotations.*;
import org.openjdk.btrace.core.types.AnyType;
import static org.openjdk.btrace.core.BTraceUtils.Reflective.printFields;
import static org.openjdk.btrace.core.BTraceUtils.println;
@BTrace
public class GetIndexMethod {
@OnMethod(clazz = "com.hong.springboot.controller.IndexController",method = "index1")
public static void pre(@Self Object self, AnyType params){
println("classPath:"+self);
printFields(params);
println("======end========");
}
@OnMethod(clazz = "com.hong.springboot.controller.IndexController",method = "index1",location = @Location(value = Kind.RETURN))
public static void getReturn(@Return Object obj, AnyType params, @Duration long time){
printFields(obj);
printFields(params);
println("time:"+time);
println("======end========");
}
}
Start project ~
obtain pid by :15040 ( Yours pid)
Start the monitoring , Pay attention to your road strength here
btrace.bat 15040 ..\samples\GetIndexMethod.java
Pay special attention : there GetIndexMethod.java I moved it to the project , So you can also run on the direct open path of your project , Don't Scribble
Request path :http://localhost:8386/1?name=test2
give the result as follows :
Compile monitoring duration
package com.hong.springboot.config;
import org.openjdk.btrace.core.annotations.*;
import static org.openjdk.btrace.core.BTraceUtils.Strings.str;
import static org.openjdk.btrace.core.BTraceUtils.println;
import static org.openjdk.btrace.core.BTraceUtils.timeMillis;
/**
* @author: csh
* @Date: 2022/6/25 16:29
* @Description: Monitoring time
*/
@BTrace
public class TlsBtrace {
@TLS
private static long startTime = 0;
@OnMethod(clazz = "com.hong.springboot.controller.IndexController", method="index1")
public static void startMethod(){
startTime = timeMillis();
}
@OnMethod(clazz = "com.hong.springboot.controller.IndexController", method="index1", [email protected](Kind.RETURN))
public static void endMethod(@ProbeClassName String pcm, @ProbeMethodName String pmn) {
println(pcm + "." + pmn + " [Time taken: " + str(timeMillis() - startTime) + "ms]");
}
}
Is it with arthas It's kind of like ~, It's just that you need to code it manually . Not so convenient . Of course, the official provides a lot of demo You can parameter the path :\samples Below .
Yes, of course , There's another one usage , Start through proxy mode .( There is no list of )
java -javaagent:btrace-agent.jar=[<agent-arg>[,<agent-arg>]*]? <launch-args>
Last
In the use of btrace Pay attention to the configuration of path and environment variables , This is probably the only place where mistakes are most likely , Especially running .java The path when , It must be there .java To execute or point to this java Execute again at the path address where the file is located , Otherwise, you will not find .btrace Very simple and practical , But with arthas Compared with ease of use, there may be a big gap , however arthas There is a very big problem is that downloading will lead to frequent FULL GC So looking at the scene requires , If those special scenes , Company monitoring is very strict FULL GC You can consider using this tool to see what you want to see , However, it is troublesome to introduce the package first when using it ~~~~.
Reference article
Official website :https://github.com/btraceio/btrace
https://tech.meituan.com/2019/02/28/java-dynamic-trace.html
https://www.oschina.net/p/btrace?hmsr=aladdin1e1
https://www.jianshu.com/p/1b52561e3848
https://www.cnblogs.com/danny-djy/p/9990566.html#:~:text=%20BTrace%E6%98%AF%E4%B8%80%E7%A7%8D%E5%AE%89%E5%85%A8%EF%BC%8C%E5%8A%A8%E6%80%81%E7%9A%84Java%E8%B7%9F%E8%B8%AA%E5%B7%A5%E5%85%B7%E3%80%82,BTrace%E9%80%9A%E8%BF%87%E5%8A%A8%E6%80%81%EF%BC%88%E5%AD%97%E8%8A%82%E7%A0%81%EF%BC%89%E6%A3%80%E6%B5%8B%E6%AD%A3%E5%9C%A8%E8%BF%90%E8%A1%8C%E7%9A%84Java%E7%A8%8B%E5%BA%8F%E7%9A%84%E7%B1%BB%E6%9D%A5%E5%B7%A5%E4%BD%9C%E3%80%82%20BTrace%E5%B0%86%E8%B7%9F%E8%B8%AA%E6%93%8D%E4%BD%9C%E6%8F%92%E5%85%A5%E5%88%B0%E6%AD%A3%E5%9C%A8%E8%BF%90%E8%A1%8C%E7%9A%84Java%E7%A8%8B%E5%BA%8F%E7%9A%84%E7%B1%BB%E4%B8%AD%EF%BC%8C%E5%B9%B6%E5%AF%B9%E8%B7%9F%E8%B8%AA%E7%9A%84%E7%A8%8B%E5%BA%8F%E7%B1%BB%E8%BF%9B%E8%A1%8C%E7%83%AD%E4%BA%A4%E6%8D%A2%E3%80%82
边栏推荐
- Makefile separates file names and suffixes
- Fabric.js 橡皮擦的用法(包含恢复功能)
- Actual combat sharing of shutter screen acquisition
- Introduction to mathjax (web display of mathematical formulas, vector)
- 使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
- Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
- mathML转latex
- LeetCode 2320. Count the number of ways to place the house
- Huawei interview question: no palindrome string
- STM32标准固件库函数名(一)
猜你喜欢
为什么只会编程的程序员无法成为优秀的开发者?
【apipost】使用教程
Introduction to C language -- array
Obsidian installs third-party plug-ins - unable to load plug-ins
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
Socket and socket address
微信小程序使用towxml显示公式
Large top heap, small top heap and heap sequencing
MFC 定时器使用
JMeter script parameterization
随机推荐
Why can't browsers read JSX?
Introduction to C language -- array
What is erdma? Popular science cartoon illustration
c语言入门--数组
A white hole formed by antineutrons produced by particle accelerators
数据库内容输出有问题怎么解决
【C语音】详解指针进阶和注意点(2)
Huawei interview question: no palindrome string
Slashgear shares 2021 life changing technology products, which are somewhat unexpected
Check password
Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
LeetCode 2320. Count the number of ways to place the house
C# richTextBox控制显示最大行数
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
4、数组指针和指针数组
forEach的错误用法,你都学废了吗
Fabric. Usage of JS eraser (including recovery function)
[untitled] leetcode 2321 Maximum score of concatenated array
数据库连接池和数据源
PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)