本文主要参考慕课网若鱼老师课程Java生产环境下性能监控与调优详解
请支持正版,抵制盗版,维护每一位辛苦付出的人的合法权益!
本章关键词:BTrace、拦截、注意事项
一、入门
1.做什么的?
- 可以在应用程序不重启,不修改的情况下,正在运行的情况下,动态的修改字节码,达到监控调试的目的
- 可以动态的向目标应用程序的字节码注入追踪代码
- 用到的技术
JavaComplierApi、JVMTI、Agent、Instrumentation+ASM
2.安装
- 到官网中下载相应版本
- 新建环境变量BTRACE_HOME,添加Path:%BTRACE_HOME%\bin
环境变量的添加请自行百度
- 两种运行脚本方式
- JVisualVM中添加Btrace插件(参见上一章),添加classpath
2.使用命令行btrace
3.使用详解
-
代码示例
btrace需要引入三个jar,就是下载的btrace程序中,build目录下的三个
引入jar包
com.sun.btrace
btrace-boot
1.3.11
jar
system
D:\btrace-bin-1.3.11\build\btrace-boot.jar
com.sun.btrace
btrace-agent
1.3.11
jar
system
D:\btrace-bin-1.3.11\build\btrace-agent.jar
com.sun.btrace
btrace-client
1.3.11
jar
system
D:\btrace-bin-1.3.11\build\btrace-client.jar
将要被拦截的代码
package com.qinxianyun.monitor_tuning.chapter4;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Qinxianyun
* @version V1.0
* @time 2018/7/21.12:50
* @description 演示btrace
*/
@RestController
@RequestMapping("ch4")
public class Ch4Controller {
@RequestMapping("/arg1")
public String arg1(@RequestParam("name")String name){
return "hello," + name;
}
}
btrace脚本(脚本和代码没有必要关联性,脚本可以随便写在哪里,没有必要写在方法的一个项目里面)
package com.qinxianyun.monitor_tuning.chapter4;
import com.sun.btrace.AnyType;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;
@BTrace
public class PrintArgSimple {
@OnMethod(
clazz = "com.qinxianyun.monitor_tuning.chapter4.Ch4Controller",
method = "arg1",
location = @Location(Kind.ENTRY)
)
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, AnyType[] args){
BTraceUtils.printArray(args);
BTraceUtils.println(pcn + "," + pmn);
BTraceUtils.println();
}
}
@OnMethod表示我们要拦截哪个类的哪个方法,在什么时候进行拦截
Kind.ENTRY表示在入口的时候拦截(也就是一进入方法的时候)
@param pcn 拦截方法类的类名
@param pmn 拦截方法的方法名
@param args 参数
-
进入到btrace脚本的目录下
-
查看进程id
-
运行脚本文件
-
浏览器中输入http://localhost:8080/ch4/arg1?name=qinxianyun
出现如下处理结果
-
Java VisualVM打开btrace(需要已经安装了插件,详情见上一章)
二、拦截构造函数、同名函数
1.拦截方法(普通方法、构造方法、同名方法)
- 普通方法 @OnMethod(clazz=" ",method=" ")
参考上面的例子
- 构造函数@OnMethod(clazz=" ",method="
")
因为构造函数在字节码层面是叫init,所以用
@RequestMapping("/constructor")
public User arg1(User user){
return user;
}
package com.qinxianyun.monitor_tuning.chapter2;
/**
* @author Qinxianyun
* @version V1.0
* @time 2018/7/15.14:52
* @description 用户实体
*/
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.qinxianyun.monitor_tuning.chapter4;
import com.sun.btrace.AnyType;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
@BTrace
public class PrintConstructor {
@OnMethod(
clazz = "com.qinxianyun.monitor_tuning.chapter2.User",
method = ""
)
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, AnyType[] args){
BTraceUtils.println(pcn + "," + pmn);
BTraceUtils.printArray(args);
BTraceUtils.println();
}
}
- 同名方法(用参数区分)
@RequestMapping("/same1")
public String same(@RequestParam("name")String name){
return "hello," + name;
}
@RequestMapping("/same2")
public String same(@RequestParam("name")String name,@RequestParam("id")int id){
return "hello," + name + "," +id;
}
package com.qinxianyun.monitor_tuning.chapter4;
import com.sun.btrace.AnyType;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
@BTrace
public class PrintSame {
@OnMethod(
clazz = "com.qinxianyun.monitor_tuning.chapter4.Ch4Controller",
method = "same"
)
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, String name ,int id){
BTraceUtils.println(pcn + "," + pmn + "," + name + "," + id);
BTraceUtils.println();
}
}
拦截重载的方法,是根据参数的类型区分的,脚本中的名字可以和代码中参数名不一致
运行结果:
三、拦截返回值、异常行号
1.拦截时机
- Kind.ENTRY : 入口,默认值
参考第一个例子
- Kind.RETURN : 返回
package com.qinxianyun.monitor_tuning.chapter4;
import com.sun.btrace.AnyType;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;
@BTrace
public class PrintReturn {
@OnMethod(
clazz = "com.qinxianyun.monitor_tuning.chapter4.Ch4Controller",
method = "arg1",
location = @Location(Kind.RETURN)
)
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, @Return AnyType result){
BTraceUtils.println(pcn + "," + pmn + "," + result);
BTraceUtils.println();
}
}
- Kind.THROW : 异常
package com.qinxianyun.monitor_tuning.chapter4;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;
@BTrace
public class PrintOnThrow {
@TLS
static Throwable currentException;
@OnMethod(
clazz = "java.lang.Throwable",
method = ""
)
public static void onthrow(@Self Throwable self){//new Throwable()
currentException = self;
}
@OnMethod(
clazz = "java.lang.Throwable",
method = ""
)
public static void onthrow1(@Self Throwable self,String s){//new Throwable(String msg)
currentException = self;
}
@OnMethod(
clazz = "java.lang.Throwable",
method = ""
)
public static void onthrow1(@Self Throwable self,String s,Throwable cause){//new Throwable(String msg,Throwable cause)
currentException = self;
}
@OnMethod(
clazz = "java.lang.Throwable",
method = ""
)
public static void onthrow2(@Self Throwable self,Throwable cause){//new Throwable(Throwable cause)
currentException = self;
}
@OnMethod(
clazz = "java.lang.Throwable",
method = "",
location = @Location(Kind.RETURN)
)
public static void onthrowreturn(){
if (currentException != null){
BTraceUtils.Threads.jstack(currentException);
BTraceUtils.println("========================");
currentException = null;
}
}
}
- Kind.Line :行
package com.qinxianyun.monitor_tuning.chapter4;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;
@BTrace
public class PrintLine {
@OnMethod(
clazz = "com.qinxianyun.monitor_tuning.chapter4.Ch4Controller",
location = @Location(value = Kind.LINE,line = 41)
)
public static void anyRead(@ProbeClassName String pcn,@ProbeMethodName String pmn,int line){
BTraceUtils.println(pcn + "," + pmn + "," + line);
BTraceUtils.println();
}
}
四、拦截复杂参数、环境变量、正则匹配拦截
1.拦截this、入参、返回
- this:@Self
- 入参:可以用AnyType,也可以用真实类型,同名的用真实的
- 返回:@Return
2.获取对象的值
- 简单类型:直接获取
- 复杂类型:反射,类名+属性名
package com.qinxianyun.monitor_tuning.chapter4;
import com.qinxianyun.monitor_tuning.chapter2.User;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;
import java.lang.reflect.Field;
@BTrace
public class PrintArgComplex {
@OnMethod(
clazz = "com.qinxianyun.monitor_tuning.chapter4.Ch4Controller",
method = "arg2",
location = @Location(Kind.ENTRY)
)
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, User user){
BTraceUtils.printFields(user);
Field field2 = BTraceUtils.field("com.qinxianyun.monitor_tuning.chapter2.User","name");
BTraceUtils.println(BTraceUtils.get(field2,user));
BTraceUtils.println(pcn + "," + pmn);
BTraceUtils.println();
}
}
注意:
引用的需要加上-cp
3.正则表达式
- 拦截任意方法
package com.qinxianyun.monitor_tuning.chapter4;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.*;
@BTrace
public class PrintRegex {
@OnMethod(
clazz = "com.qinxianyun.monitor_tuning.chapter4.Ch4Controller",
method = "/.*/"
)
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn){
BTraceUtils.println(pcn + "," + pmn);
BTraceUtils.println();
}
}
4.打印环境变量
- 打印(输出类似Jinfo命令)
package com.qinxianyun.monitor_tuning.chapter4;
import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.BTrace;
@BTrace
public class PrintJinfo {
static {
BTraceUtils.println("System Properties:");
BTraceUtils.printProperties();
BTraceUtils.println("VM Flags:");
BTraceUtils.printVmArguments();
BTraceUtils.println("OS Enviroment:");
BTraceUtils.printEnv();
BTraceUtils.exit();
}
}
-
输出结果
注意事项:
1.默认只能本地运行
2.生产环境下可以使用,但是被修改的字节码不会被还原(即使btrace退出)
因此,脚本中不要有太耗系统资源的东西,否则会影响服务器。