Btrace 入门 (一)

概述

    Btrace是java的调试利器,它可以通过自己编写脚本,来获取应用的一切调试信息。他的工作原理是通过 instrument + asm 来对正在运行的java程序中的class类进行动态增强,可以在不用重启的情况下监控系统运行情况,方便的获取程序运行时的数据信息,如方法参数、返回值、全局变量和堆栈信息等,并且做到最少的侵入,占用最少的系统资源。 既可支持线上链路监控排查,也可支持单机版问题定位。根据官方声明,不当地使用BTrace可能导致JVM崩溃,如BTrace使用错误的.class文件,所以,可以先在本地验证BTrace脚本的正确性再使用。

 

使用限制

1、不能创建对象 

2、不能使用数组 

3、不能抛出或捕获异常 

4、不能使用循环 

5、不能使用synchronized关键字 

6、属性和方法必须使用static修饰 

简单来说,除了BtraceUtils里面提供的方法(基本就是打印调试信息),其他的就不要乱用了。

 

使用场景

  • 线上出现了一个问题,需要入参才能完成定位

  • 接口调用慢,是哪个接口方法慢?

  • 分析任意接口或者方法的耗时情况 

  • 统计接口的调用情况

  • ......

安装

使用

  • 编写脚本    搭建一个简易的maven项目即可开始编写脚本(用IDEA不容易出错)

    /**
     * 通过打印调试信息,跟踪方法的调用
     * Created by cxy on 2018/1/30
     * @author cxy
     */
    @BTrace
    public class PrintTrace {
    
      @OnMethod(clazz = "com.service.common.biz.controller.UserController",method = "/find.*/",location = @Location(value = Kind.ENTRY))
      public static void print(long id){
        println(strcat("step into userConroller findById" ,str(id)));
      }
    }
  • 脚本参数说明
    root@cxy-VirtualBox:~# btrace --help
    Usage: btrace <options> <pid> <btrace source or .class file> <btrace arguments>
    where possible options include:
      --version             Show the version  (btrace 版本号)
      -v                    Run in verbose mode  (会打印出Btrace debug级别的调试信息)
      -o <file>             The path to store the probe output (will disable showing the output in console 
    Btrace打印的调试信息,会输出到这个目录)
    -u                    Run in trusted mode
      -d <path>             Dump the instrumented classes to the specified path
      -pd <path>            The search path for the probe XML descriptors
      -classpath <path>     Specify where to find user class files and annotation processors
      -cp <path>            Specify where to find user class files and annotation processors
      -I <path>             Specify where to find include files
      -p <port>             Specify port to which the btrace agent listens for clients
      -statsd <host[:port]> Specify the statsd server, if any
    root@cxy-VirtualBox:~#
  • 运行脚本   
    root@cxy-VirtualBox:/usr/local# btrace -v -o mylog.txt -cp btrace/build/ 3981 PrintTrace.java 
    DEBUG: outputFile is mylog.txt
    DEBUG: accepting classpath btrace/build/
    DEBUG: assuming default port 2020
    DEBUG: compiling PrintTrace.java
    DEBUG: compiled PrintTrace.java
    DEBUG: attaching to 3981
    DEBUG: checking port availability: 2020
    DEBUG: attached to 3981
    DEBUG: loading /usr/local/btrace/build/btrace-agent.jar
    DEBUG: agent args: port=2020,statsd=,debug=true,bootClassPath=btrace/build/,systemClassPath=/usr/lib/jvm/java-8-oracle/jre/../lib/tools.jar,probeDescPath=.
    DEBUG: loaded /usr/local/btrace/build/btrace-agent.jar
    DEBUG: registering shutdown hook
    DEBUG: registering signal handler for SIGINT
    DEBUG: submitting the BTrace program
    DEBUG: opening socket to 2020
    DEBUG: setting up client settings
    DEBUG: sending instrument command
    DEBUG: entering into command loop
    DEBUG: received com.sun.btrace.comm.RenameCommand@1d29cf23
    DEBUG: received com.sun.btrace.comm.OkayCommand@5f282abb
    DEBUG: received com.sun.btrace.comm.RetransformationStartNotification@167fdd33
    DEBUG: received com.sun.btrace.comm.OkayCommand@1e965684

        可以发现应用也有相应的debug信息打印

btrace DEBUG: client accepted Socket[addr=/127.0.0.1,port=57804,localport=2020]
btrace INFO: Redirecting output to mylog.txt
btrace INFO: Redirecting output to mylog.txt
btrace DEBUG: got instrument command
btrace DEBUG: loading BTrace class
btrace DEBUG: verifying BTrace class ...
btrace DEBUG: BTrace class PrintTrace$2 verified
btrace DEBUG: preprocessing BTrace class PrintTrace$2 ...
btrace DEBUG: ... preprocessed
btrace DEBUG: loaded 'PrintTrace$2' successfully
btrace DEBUG: class renamed to PrintTrace$2
btrace DEBUG: client PrintTrace$2: got com.sun.btrace.comm.RenameCommand@3ee68ed5
btrace DEBUG: creating BTraceRuntime instance for PrintTrace$2
btrace DEBUG: created BTraceRuntime instance for PrintTrace$2
btrace DEBUG: sending Okay command
btrace DEBUG: client PrintTrace$2: got com.sun.btrace.comm.OkayCommand@329a79ca
btrace DEBUG: about to defineClass PrintTrace$2
btrace DEBUG: defineClass succeeded for PrintTrace$2
btrace DEBUG: starting client command handler thread
btrace DEBUG: new Client created com.sun.btrace.agent.RemoteClient@1fa46219
btrace DEBUG: retransforming loaded classes
btrace DEBUG: filtering loaded classes
btrace DEBUG: candidate class com.service.common.biz.controller.UserController added
btrace DEBUG: client PrintTrace$2: got com.sun.btrace.comm.RetransformationStartNotification@698fd690
btrace DEBUG: calling retransformClasses (1 classes to be retransformed)
btrace DEBUG: Attempting to retransform class: com.service.common.biz.controller.UserController
btrace DEBUG: transformed class com.service.common.biz.controller.UserController
btrace DEBUG: waiting for clients
btrace DEBUG: client PrintTrace$2: got com.sun.btrace.comm.OkayCommand@79639c01

      mylog.txt 也有相应的日志输出

转载于:https://my.oschina.net/u/2392566/blog/1618248

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值