BTrace线上不重启实现织入诊断代码实例

BTrace线上不重启实现织入诊断代码实例

460发表于2019-06-27

经常会遇到这样的问题,本调代码测试没有问题。线上代码出现bug,或者hung住。重启后问题又消失了。

这个时候只能通过线上调试代码且不能重启。BTrace是一个java调试线上代码神奇器,可以通过向目标代码动态的织入调试代码,获取参数、返回值,响应时间,gc等情况。这样快速的找到问题,提高 解决问题的效率 。

BTrace原理主要是Attach API技术。

BTrace是基于动态字节码修改技术(Hotswap)来实现运行时java程序的跟踪和替换。

大体的原理可以用下面的公式描述:
Client(Java compile api + attach api) + Agent(脚本解析引擎 + ASM + Instumentation) + Socket

被测试类:

一、NumberUtil

package com.lanhusoft;

/**
 * Copyright@www.lanhusoft.com.
 * Author:lanhusoft
 * Date:2019-05-28
 * Description:
 */
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){

            }
        }
    }
}

二、maven:pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lanhusoft</groupId>
    <artifactId>btrace-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>btrace-start</module>
    </modules>

    <properties>
        <!-- 文件拷贝时的编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 编译时的编码 -->
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    </properties>

    <dependencies>
    </dependencies>

    <build>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <!-- 此处指定main方法入口的class -->
                            <mainClass>com.lanhusoft.NumberUtil</mainClass>
                        </manifest>
                    </archive>
                    <archiverConfig>
                        <encoding>utf-8</encoding>
                    </archiverConfig>
                    <encoding>UTF-8</encoding><!-- 指定编码格式,否则在DOS下运行mvn命令时当发生文件资源copy时将使用系统默认使用GBK编码 -->
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

三、打包

mvn clean package

四、运行

[root@bogon /]# java -jar btrace-demo-1.0-SNAPSHOT.jar

from clipboard



4.1、下载:btrace并解压放到目录root下

链接: https://pan.baidu.com/s/1f4B9rmTBETDjjd84_TI_-Q 提取码: xnnj


4.2、环境变量
[root@bogon ~]# vi /etc/profile
编辑环境变量添加如下代码:
export BTRACE_HOME=/root/btrace-bin-1.3.10
export PATH=$PATH:$BTRACE_HOME/bin
生效:
source /etc/profile

4.3、添加权限
[root@bogon ~]# chmod +x /root/btrace-bin-1.3.10/bin/btrace

4.4、查询java要织入代码的java进程id

jps -l


五、编写BTrace调试代码

1、NumberUtilBTrace

package com.lanhusoft;

import com.sun.btrace.BTraceUtils;
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;
/**
 * Copyright@www.lanhusoft.com.
 * Author:lanhusoft
 * Date:2019-05-28
 * Description:
 */
@BTrace
public class NumberUtilBTrace {

    @OnMethod(
            clazz="com.lanhusoft.NumberUtil",
            method="sum",
            location=@Location(Kind.RETURN)
    )
    public static void func(@Return int result) {

        println("trace: =======================");
        println(strcat("result:", str(result)));
        jstack();
    }
}
2、pom文件


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>btrace-demo</artifactId>
        <groupId>com.lanhusoft</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lanhusoft</groupId>
    <artifactId>btrace-start</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.sun.tools.btrace</groupId>
            <artifactId>btrace-agent</artifactId>
            <version>1.3.9</version>
            <scope>system</scope>
            <systemPath>D:/btrace-bin-1.3.10/build/btrace-agent.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.sun.tools.btrace</groupId>
            <artifactId>btrace-boot</artifactId>
            <version>1.3.9</version>
            <scope>system</scope>
            <systemPath>D:/btrace-bin-1.3.10/build/btrace-boot.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.sun.tools.btrace</groupId>
            <artifactId>btrace-client</artifactId>
            <version>1.3.9</version>
            <scope>system</scope>
            <systemPath>D:/btrace-bin-1.3.10/build/btrace-client.jar</systemPath>
        </dependency>
    </dependencies>
    <build>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <!-- 此处指定main方法入口的class -->
                            <mainClass>com.lanhusoft.NumberUtilBTrace</mainClass>
                        </manifest>
                    </archive>
                    <archiverConfig>
                        <encoding>utf-8</encoding>
                    </archiverConfig>
                    <encoding>UTF-8</encoding><!-- 指定编码格式,否则在DOS下运行mvn命令时当发生文件资源copy时将使用系统默认使用GBK编码 -->
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
注意:上面BTrace的依赖包是通过system本地目录的形式,你要替换成你们本地路径。这是为了在idea中调试。


3、线上启动btrace

这个调试工程不需要打包成jar到服务器,直接拷贝这个NumberUtilBTrace.java就行。执行以下命令:

[root@bogon ~]# btrace 29516 /root/NumberUtilBTrace.java
from clipboard


小编蓝狐