首页 技术 正文
技术 2022年11月8日
0 收藏 836 点赞 1,527 浏览 2207 个字

一、Btrace简介

BTrace可以动态的向目标应用程序的字节码注入追踪代码

用到的技术JavaComplierApi,JVMTI,Agent,Instrumentation+ASM

二、Btrace安装

1、下载

Btrace的Github地址

https://github.com/btraceio/btrace

进入Release Page

Btrace介绍

Linux版本btrace-bin-1.3.11.3.tgz

Window 版本 btrace-bin-1.3.11.3.zip

我这里下载的是window版本

2、配置环境变量

1) 新建BTRACE_HOME

Btrace介绍

2) 修改Path

新建环境变量BTRACE_HOME

添加Path: %BTRACE_HOME%\bin

3、测试

1) 创建接口

package com.example.monitor_tuning.chapter4;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/ch4")
public class Ch4Controller { @RequestMapping("/arg1")
public String arg1(@RequestParam("name")String name){
return "hello," + name;
}}

2) 测试接口 

Btrace介绍

3) 创建Btrace脚本

加入Btrace测试

增加引用

<dependency>
<groupId>com.sun.btrace</groupId>
<artifactId>btrace-agent</artifactId>
<version>1.3.11</version>
<type>jar</type>
<scope>system</scope>
<systemPath>D:/Study/javaMonitor/btrace-bin-1.3.11.3/build/btrace-agent.jar</systemPath>
</dependency><dependency>
<groupId>com.sun.btrace</groupId>
<artifactId>btrace-boot</artifactId>
<version>1.3.11</version>
<type>jar</type>
<scope>system</scope>
<systemPath>D:/Study/javaMonitor/btrace-bin-1.3.11.3/build/btrace-boot.jar</systemPath>
</dependency><dependency>
<groupId>com.sun.btrace</groupId>
<artifactId>btrace-client</artifactId>
<version>1.3.11</version>
<type>jar</type>
<scope>system</scope>
<systemPath>D:/Study/javaMonitor/btrace-bin-1.3.11.3/build/btrace-client.jar</systemPath>
</dependency>

然后

package com.example.monitor_tuning.chapter4;import com.sun.btrace.AnyType;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;/**
* 此Btrace脚本和要跟踪的代码不是放在同一个工程里的。这里演示方便,放在一起。
*/
@BTrace
public class PrintArgSimple { /*要拦截哪个类,哪个方法,什么时候拦截*/
@OnMethod(
clazz = "com.example.monitor_tuning.chapter4.Ch4Controller",
method="arg1",
location = @Location(Kind.ENTRY)
)
/*ProbeClassName 方法类名; ProbeMethodName 方法名 ; AnyType[] 方法参数*/
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, AnyType[] args)
{
BTraceUtils.printArray(args);
BTraceUtils.println(pcn + "," + pmn);
BTraceUtils.println();
}
}

  

将此文件移动到

Btrace介绍

  

4) 查看进程jps -l

Btrace介绍

5) 将脚本注入到进程 btrace 4584 PrintArgSimple.java。

然后访问接口http://localhost:8080/monitor_tuning/ch4/arg1?name=Jack

Btrace介绍

最终可以看到监控到了方法,参数等信息。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289