当前位置:网站首页>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
边栏推荐
- Fabric. JS upper dash, middle dash (strikethrough), underline
- A white hole formed by antineutrons produced by particle accelerators
- Reuse and distribution
- Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
- 实现一个多进程并发的服务器
- kityformula-editor 配置字号和间距
- taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
- Actual combat sharing of shutter screen acquisition
- taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
- 4. Array pointer and pointer array
猜你喜欢
【NOI模拟赛】刮痧(动态规划)
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
Uniapp automated test learning
STM32库函数进行GPIO初始化
Obsidian installs third-party plug-ins - unable to load plug-ins
There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
jmeter脚本参数化
kityformula-editor 配置字号和间距
LeetCode 2310. 个位数字为 K 的整数之和
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
随机推荐
MFC 定时器使用
qml 弹窗框架,可定制
广州市应急管理局发布7月高温高湿化工安全提醒
Fabric.js 橡皮擦的用法(包含恢复功能)
华为面试题: 没有回文串
记一次报错解决经历依赖重复
天猫商品详情接口(APP,H5端)
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
kityformula-editor 配置字号和间距
871. 最低加油次数 : 简单优先队列(堆)贪心题
taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
LeetCode 2320. Count the number of ways to place the house
Threejs controller cube space basic controller + inertia control + flight control
Have you learned the wrong usage of foreach
2. Const pointer
蜻蜓低代码安全工具平台开发之路
C RichTextBox controls the maximum number of lines displayed
Actual combat sharing of shutter screen acquisition
forEach的错误用法,你都学废了吗
Find the maximum inscribed circle of the contour