Home  >  Article  >  Java  >  Detailed explanation of BTrace for Java online troubleshooting

Detailed explanation of BTrace for Java online troubleshooting

黄舟
黄舟Original
2017-08-23 11:33:522366browse

What is BTrace

BTrace is a killer tool for checking and solving online problems. BTrace can obtain all the information during the execution of the program by writing scripts, and, please note, No need to restart the service, yes, no need to restart the service. After writing the script, you can execute it directly with the command without touching the original program code.

Principle

Generally speaking, BTrace is based on dynamic bytecode modification technology (Hotswap) to realize the tracking and execution of runtime java programs. replace. The general principle can be described by the following formula: Client(Java compile api + attach api) + Agent (script parsing engine + ASM + JDK6 Instrumentation) + SocketIn fact, BTrace is used java attach api attaches agent.jar, then uses the script parsing engine + asm to rewrite the bytecode of the specified class, and then uses instrument to replace the original class.

Installation and configuration

This installation and configuration is performed under Linux Ubuntu 14.04. The latest version of BTrace is currently 1.3.9, and the code is hosted on [github]. The first step is to download the releases version btrace-bin-1.3.9.tgz from github. The zip version does not have a build directory. The second step is to unzip btrace-bin-1.3.9.tgz to a directory, such as /home/fengzheng/soft/btrace. At this step, it is actually ready to use. Just execute When scripting, you need to add the absolute path before the btrace command. If you want it to be executable in any directory, proceed to the next step The third step is to configure the environment variables. The configured environment variables include JAVA_HOME and BTRACE_HOME, for example, my configuration is as follows:


##

export JAVA_HOME=/home/fengzheng/soft/jdk1.8.0_111
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  
export PATH=${JAVA_HOME}/bin:$PATH
export BTRACE_HOME=/home/fengzheng/soft/btrace
export PATH=$PATH:$BTRACE_HOME/bin

Then execute the commandsource /etc/profile to make the environment variables take effect immediately. Next, execute the btrace command in any directory, and it will be executed successfully.

Simple test case

btrace The simplest syntax is btrace $pid script.java, so you need to know the Java to be detected The process id of the program, and then write a detection script.

1. Write a memory-resident Java program. Here, an infinite loop is written to output a set of calculation results every 5 seconds. The content is as follows:


package kite.lab.utils;
/**
 * NumberUtil
 *
 * @author fengzheng
 * @date 2017/2/15
 */
public class NumberUtil {

    public int sum(){
        int result = 0;
        for(int i = 0; i< 100; i++){
            result += i * i;
        }
        return result;
    }

    public static void main(String[] args){
        while (true) {
            Thread.currentThread().setName("计算");
            NumberUtil util = new NumberUtil();
            int result = util.sum();
            System.out.println(result);
            try {
                Thread.sleep(5000);
            }catch (InterruptedException e){

            }
        }
    }
}

By the way, the process of compiling and running Java from the command line:

Compile: javac -d . NumberUtil.java, navigate to the directory where NumberUtil.java is located, and then execute this command line, The directory structure shown in the package name will be generated in the current directory (. indicates the current directory), kite/lab/utils/NumberUtil.class

Executejava kite.lab.utils.NumberUtil 

2. After executing the above program, you can use the

jps command to check the pid (generally, which account is used to start the program will depend on which account is used to execute jps, except for the root account). Execute the jps command to see the following results:


root@ubuntu:/home/fengzheng/codes/btrace# jps
10906 Jps
10860 NumberUtil

3. You can see that the java process just executed is 10860.

4. Write a btrace script. The content of the script is as follows:


package kite;
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.Strings.strcat;
import static com.sun.btrace.BTraceUtils.jstack;
import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.str;

/**
 * NumberUtilBTrace
 *
 * @author fengzheng
 * @date 2017/6/20
 */
@BTrace
public class NumberUtilBTrace {

    @OnMethod(
            clazz="kite.lab.utils.NumberUtil",
            method="sum",
            location=@Location(Kind.RETURN)
    )
    public static void func(@Return int result) {
        println("trace: =======================");
        println(strcat("result:", str(result)));
        jstack();
    }
}

means that after the execution ends (location=@Location(Kind.RETURN) indicates the end of execution), the results and stack information are output

5. Preparation Compilation: You can use the precompilation command to check the correctness of the script before execution. The precompilation command is btracec, which is a javac-like command, btracec NumberUtilBTrace.java

# #6. Call the command line to execute, btrace 10860 NumberUtilBTrace.java, (if you want to save it to a local file, you can use the redirection command btrace 10860 NumberUtilBTrace.java > mylog.log). The printed information is as follows

trace: =======================
result:328350
kite.lab.utils.NumberUtil.sum(NumberUtil.java:16)
kite.lab.utils.NumberUtil.main(NumberUtil.java:27)

7.

Press ctrl + c, the exit prompt will be given, then press 1 to exitUse Scenes

BTrace 是一个事后工具,所谓事后工具就是在服务已经上线了,但是发现存在以下问题的时候,可以用 BTrace。

  1. 比如哪些方法执行太慢,例如监控执行时间超过1s的方法

  2. 查看哪些方法调用了 System.gc() ,调用栈是怎样的

  3. 查看方法参数或对象属性

  4. 哪些方法发生了异常

多说一点,为了更好解决问题,最好还要配合事前准备和进行中监控,事前准备就是埋点嘛,在一些可能出现问题的方法中进行日志输出,进行中监控就是利用一些实时监控工具,例如 VisualVM 、jmc 这些带界面的工具或者 jdk 提供的命令行工具等,再高级一点的就是利用 Graphite 这样的Metrics 工具配合 web 界面展示出来。

使用限制

 

为了保证trace语句只读,最小化对被检测程序造成影响, BTrace对trace脚本有一些限制(比如不能改变被trace代码中的状态)

  • BTrace class不能新建类, 新建数组, 抛异常, 捕获异常,

  • 不能调用实例方法以及静态方法(com.sun.btrace.BTraceUtils除外)

  • 不能将目标程序和对象赋值给BTrace的实例和静态field

  • 不能定义外部, 内部, 匿名, 本地类

  • 不能有同步块和方法

  • 不能有循环

  • 不能实现接口, 不能扩展类

  • 不能使用assert语句, 不能使用class字面值

拦截方法定义

@OnMethod 可以指定 clazz 、method、location。由此组成了在什么时机(location 决定)监控某个类/某些类(clazz 决定)下的某个方法/某些方法(method 决定)。

如何定位

1. 精准定位

直接定位到一个类下的一个方法,上面测试用的例子就是

2. 正则表达式定位

正则表达式在两个"/" 之间,例如下面的例子,监控 javax.swing 包下的所有方法,注意正式环境中,范围尽可能小一点,太大了性能会有影响。

 


@OnMethod(clazz="/javax\\.swing\\..*/", method="/.*/")
public static void swingMethods( @ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
   print("entered " + probeClass + "."  + probeMethod);
}

通过在拦截函数的定义里注入@ProbeClassName String probeClass, @ProbeMethodName String probeMethod 参数,告诉脚本实际匹配到的类和方法名。

3. 按接口或继承类定位

例如要匹配继承或实现了 com.kite.base 的接口或基类的,只要在类前加上 + 号就可以了,例如

@OnMethod(clazz="+com.kite.base", method="doSome")

4. 按注解定位

在前面加上 @ 即可,例如@OnMethod(clazz="@javax.jws.WebService", method="@javax.jws.WebMethod")  

拦截时机

拦截时机由 location 决定,当然也可为同一个定位加入多个拦截时机,即可以在进入方法时拦截、方法返回时拦截、抛出异常时拦截

1. Kind.Entry与Kind.Return

分别表示函数的开始和返回,不写 location 的情况下,默认为 Kind.Entry,仅获取参数值,可以用 Kind.Entry ,要获取返回值或执行时间就要用 Kind.Return

2. Kind.Error, Kind.Throw和 Kind.Catch

表示异常被 throw 、异常被捕获还有异常发生但是没有被捕获的情况,在拦截函数的参数定义里注入一个Throwable的参数,代表异常



@OnMethod(clazz = "com.kite.demo", location = @Location(value = Kind.LINE, line = 20))
public static void onBind() {
   println("执行到第20行");
}
@OnMethod(clazz = "java.net.ServerSocket", method = "bind", location =@Location(Kind.ERROR)) 
public static void onBind(Throwable exception, @Duration long duration){ }

3. Kind.Call 和 Kind.Line  

Kind.Call 表示被监控的方法调用了哪些其他方法,例如:


@OnMethod(clazz = "com.kite",
            method = "login",
            location = @Location(value = Kind.CALL, clazz = "/.*/", method = "/.*/", where = Where.AFTER))
    public static void onBind(@Self Object self, @TargetInstance Object instance, @TargetMethodOrField String method, @Duration long duration){
        println(strcat("self: ", str(self)));
        println(strcat("instance: ", str(instance)));
        println(strcat("method: ", str(method)));
        println(strcat("duration(ms): ", str(duration / 1000000)));
    }

@Self 表示当前监控的函数所在类,如果是静态类则为空,@TargetInstance 表示函数中调用的方法或属性所在的类,如果是静态方法则为空,@TargetMethodOrField 表示调用的方法或属性,如果要获取执行时间,那么 where 必须设置为 Where.AFTER

Kind.Line 监测类是否执行到了设置的行数,例如:


@OnMethod(clazz = "com.kite.demo", location = @Location(value = Kind.LINE, line = 20))
public static void onBind() {

   println("执行到第20行");

}

几个例子

查看谁调用了GC


@OnMethod(clazz = "java.lang.System", method = "gc")
    public static void onSystemGC() {
        println("entered System.gc()");
        jstack();
    }

打印耗时超过100ms的方法


@OnMethod(clazz = "/com\\.kite\\.controller\\..*/",method = "/.*/",location = @Location(Kind.RETURN))
    public static void slowQuery(@ProbeClassName String pcn,@ProbeMethodName String probeMethod, @Duration long duration){
        if(duration > 1000000 * 100){
            println(strcat("类:", pcn));
            println(strcat("方法:", probeMethod));
            println(strcat("时长:", str(duration / 1000000)));
        }
    }

BTrace 提供了一系列的 sample, 可到 github 上查看。

注意问题

如果出现 Unable to open socket file: target process not responding or HotSpot VM not loaded 这个问题,可能的原因是执行 BTrace 脚本的用户和 Java 进程运行的用户不是同一个,使用 ps -aux | grep $pid查看一下 Java 进程的执行用户,保证和 BTrace 脚本执行用户相同即可 

The above is the detailed content of Detailed explanation of BTrace for Java online troubleshooting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn