一、入门

1.做什么的?

  • 可以在应用程序不重启,不修改的情况下,正在运行的情况下,动态的修改字节码,达到监控调试的目的
  • 可以动态的向目标应用程序的字节码注入追踪代码
  • 用到的技术

2.安装

  • 官网中下载相应版本
  • 新建环境变量BTRACE_HOME,添加Path:%BTRACE_HOME%\bin
  • 两种运行脚本方式
  1. JVisualVM中添加Btrace插件(参见上一章),添加classpath

2.使用命令行btrace <pid> <trace_script>

3.使用详解

  • 代码示例
    btrace需要引入三个jar,就是下载的btrace程序中,build目录下的三个

     

    jar包.png

引入jar包

<!--btrace -->
        <dependency>
            <groupId>com.sun.btrace</groupId>
            <artifactId>btrace-boot</artifactId>
            <version>1.3.11</version>
            <type>jar</type>
            <scope>system</scope>
            <systemPath>D:\btrace-bin-1.3.11\build\btrace-boot.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.sun.btrace</groupId>
            <artifactId>btrace-agent</artifactId>
            <version>1.3.11</version>
            <type>jar</type>
            <scope>system</scope>
            <systemPath>D:\btrace-bin-1.3.11\build\btrace-agent.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:\btrace-bin-1.3.11\build\btrace-client.jar</systemPath>
        </dependency>

将要被拦截的代码

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();
    }
}
  • 进入到btrace脚本的目录下

     

    进入目录.png

  • 查看进程id

     

    查看进程id.png

  • 运行脚本文件

     

    运行中.png

  • 浏览器中输入http://localhost:8080/ch4/arg1?name=qinxianyun
    出现如下处理结果

    处理结果.png

     

  • Java VisualVM打开btrace(需要已经安装了插件,详情见上一章)

     

    vm打开btrace.png

     

    界面详解.png

     

    输出.png

二、拦截构造函数、同名函数

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 = "<init>"
    )
    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.拦截时机

  • 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 = "<init>"
    )
    public static void onthrow(@Self Throwable self){//new Throwable()
        currentException = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "<init>"
    )
    public static void onthrow1(@Self Throwable self,String s){//new Throwable(String msg)
        currentException = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "<init>"
    )
    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 = "<init>"
    )
    public static void onthrow2(@Self Throwable self,Throwable cause){//new Throwable(Throwable cause)
        currentException = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "<init>",
            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();
    }
}

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();
    }
}
  • 输出结果

     

    输出.png



 

10-04 12:40