`
mingren135
  • 浏览: 69006 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

btrace-实战线上问题

阅读更多

BTrace:https://kenai.com/projects/btrace,BTrace is a safe, dynamic tracing tool for the Java platform. BTrace can be used to dynamically trace a running Java program (similar to DTrace for OpenSolaris applications and OS). BTrace dynamically instruments the classes of the target application to inject tracing code ("bytecode tracing"). Tracing code is expressed in Java programming language. 

 

BTrace介绍:一个Java的动态跟踪工具,BTrace的工作的基本原理是把跟踪的代码动态替换到被跟踪的Java程序内,其实现机制是Sun Attach API + BTrace脚本解析引擎 + Objectweb ASM + JDK6 Instumentation,项目主页是在这里,用户指南点击这里。

 

BTrace使用:

1)解压并加可执行权限

tar xvf btrace-bin.tar.gz
chmod +x bin/btrace

 

2)查找java的进程号pid

ps aux | grep "java"

3)编写btrace脚本:需要看到调用参数,因此import额外的class

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import com.xxx.OrderQuery;

@BTrace
public class TestMethod {
    @OnMethod(
        clazz="com.xxx.impl.OrderServiceImpl",
        method="getSingleOrderById"
    )
    public static void test(OrderQuery orderQuery) {
                println(strcat("parameter tid: ",str(orderQuery.tid)));
    }
}

 4)执行脚本:注意build目录下新增了com目录,包含接口参数类型的class

[qinde@sirius049090.cm4 build]$ ll
total 772
-rw-r--r-- 1 qinde users 273544 Jul 31 12:37 btrace-agent.jar
-rw-r--r-- 1 qinde users 171502 Jul 31 12:37 btrace-boot.jar
-rw-r--r-- 1 qinde users 325121 Jul 31 12:37 btrace-client.jar
drwxr-xr-x 3 qinde users   4096 Jan 14 16:08 com

[qinde@sirius049090.cm4 btrace]$ ls
aa.txt  bin  build  COPYRIGHT  docs  LICENSE.txt  README.txt  samples  TestMethod.java  THIRDPARTYLICENSEREADME.txt

[qinde@sirius049090.cm4 btrace]$ sudo -u admin ./bin/btrace -cp build/ 17794 TestMethod.java

 

PS

1)Please set JAVA_HOME before running this script,需要EXPORT环境变量

export JAVA_HOME=/opt/xxx/java

2)案例记录:beta发布,打开新业务开关时,发现日志中有npe,怀疑和请求参数有关,但日志中看不到参数,因此准备以上的btrace脚本,发现确实有未预料到的参数哈。。真好用啊!

3)返回值,location=@Location(Kind.RETURN)  

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics