当前位置:网站首页>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
边栏推荐
- fatal: unsafe repository is owned by someone else 的解决方法
- btrace-(字节码)动态跟踪工具
- STM32 library function for GPIO initialization
- [QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
- taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
- Huawei interview question: no palindrome string
- Simple verification code generator for 51 single chip microcomputer experiment
- kityformula-editor 配置字号和间距
- A white hole formed by antineutrons produced by particle accelerators
- C语言中的算术运算及相关练习题
猜你喜欢
LeetCode 2320. Count the number of ways to place the house
C语言习题---(数组)
【apipost】使用教程
广州市应急管理局发布7月高温高湿化工安全提醒
LeetCode 209. Minimum length subarray
Xilinx Vivado set *.svh as SystemVerilog Header
微信小程序使用towxml显示公式
Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
What is erdma? Popular science cartoon illustration
随机推荐
Threejs controller cube space basic controller + inertia control + flight control
LeetCode 2320. 统计放置房子的方式数
MFC 控制台打印,弹出对话框
广州市应急管理局发布7月高温高湿化工安全提醒
Obsidian installs third-party plug-ins - unable to load plug-ins
Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
STM32 standard firmware library function name memory (II)
fatal: unsafe repository is owned by someone else 的解决方法
Xilinx Vivado set *. svh as SystemVerilog Header
传感器数据怎么写入电脑数据库
[QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
【无标题】LeetCode 2321. 拼接数组的最大分数
Uniapp automated test learning
Factal: Unsafe repository is owned by someone else Solution
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
871. 最低加油次数 : 简单优先队列(堆)贪心题
天猫商品详情接口(APP,H5端)
数据库连接池和数据源
C # delay, start the timer in the thread, and obtain the system time
About text selection in web pages and counting the length of selected text