本章关键词:BTrace、拦截、注意事项
1.做什么的?
JavaComplierApi、JVMTI、Agent、Instrumentation+ASM
2.安装
环境变量的添加请自行百度
2.使用命令行btrace
3.使用详解
引入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脚本的目录下
进入目录.png
查看进程id
查看进程id.png
运行脚本文件
运行中.png
浏览器中输入http://localhost:8080/ch4/arg1?name=qinxianyun
出现如下处理结果
处理结果.png
Java VisualVM打开btrace(需要已经安装了插件,详情见上一章)
vm打开btrace.png
界面详解.png
输出.png
1.拦截方法(普通方法、构造方法、同名方法)
参考上面的例子
因为构造函数在字节码层面是叫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();
}
}
运行结果.png
@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.png
1.拦截时机
参考第一个例子
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();
}
}
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;
}
}
}
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、入参、返回
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();
}
}
注意:
复杂类型.png
引用的需要加上-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.打印环境变量
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退出)
因此,脚本中不要有太耗系统资源的东西,否则会影响服务器。