1、Btrace簡介java
BTrace能夠動態的向目標應用程序的字節碼注入追蹤代碼git
用到的技術JavaComplierApi,JVMTI,Agent,Instrumentation+ASMgithub
2、Btrace安裝web
一、下載spring
Btrace的Github地址app
https://github.com/btraceio/btrace測試
進入Release Pageflex
Linux版本btrace-bin-1.3.11.3.tgzui
Window 版本 btrace-bin-1.3.11.3.zipspa
我這裏下載的是window版本
二、配置環境變量
1) 新建BTRACE_HOME
2) 修改Path
新建環境變量BTRACE_HOME
添加Path: %BTRACE_HOME%\bin
三、測試
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) 測試接口
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(); } }
將此文件移動到
4) 查看進程jps -l
5) 將腳本注入到進程 btrace 4584 PrintArgSimple.java。
而後訪問接口http://localhost:8080/monitor_tuning/ch4/arg1?name=Jack
最終能夠看到監控到了方法,參數等信息。