使用btrace 分析java 应用

btrace 是一个类型安全的java 平台动态追踪工具(类似dtrace,bpf。。。),以下是一个简单的试用
备注: 目前btrace 对于jdk 9 以及以上版本的支持有问题,而且团队暂时也没有支持的打算

项目准备

 
├── README.md
├── btrace-app
├── pom.xml
└── src
└── main
└── java
└── com
└── dalong
└── Application.java
└── btrace-scripts
    └── Simple.java
  • 代码说明
    btrace-app 为需要追踪的项目,一个maven 项目,使用flat 模式的部署
    Application.java 代码
 
package com.dalong;
import java.io.IOException;
import java.util.Random;
public class Application {
    private String name;
    public Application(String name) {
        super();
        this.name = name;
    }
    public String toString() {
        return this.name;
    }
    public int add(int a, int b) {
        Test test = new Test();
        int result = 0;
        try {
            result = test.add(a, b);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    class Test {
        public int add(int a, int b) throws IOException {
            if (a > 50 && a < 80) throw new IOException("this is a exception!");
            return a + b;
        }
    }
    public static void main(String[] args) {
        Random random = new Random();
        Application demo = new Application("this is a Demo1 instace");
        while (true) {
            int a = random.nextInt(100);
            int b = random.nextInt(100);
            int c = demo.add(a, b);
            System.out.println("a:" + a);
            System.out.println("b:" + b);
            System.out.println("a+b:" + c);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

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.dalong</groupId>
    <artifactId>btrace-learning</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <finalName>btrace-learning-app</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <!-- Run shade goal on package phase -->
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <!-- add Main-Class to manifest file -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.dalong.Application</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

btrace script 代码,这个实际上就是一个普通的java 代码(也可可以maven 进行代码管理,同时使用ide 自动提示)
Simple.java

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
/**
 * This script traces method entry into every method of 
 * every class in com.dalong.Application! Think before using 
 * this script -- this will slow down your app significantly!!
 */
@BTrace public class Simple {
    @OnMethod(
        clazz="/.*/",
        method="/.*/",
        location=@Location(value=Kind.CALL, clazz="/.*/", method="/.*/"))
    public static void m(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
        print(Strings.strcat("entered ", probeClass));
        println(Strings.strcat(".", probeMethod));
    }
}

运行

  • 构建项目
cd btrace-app
mvn clean package
java -jar target/btrace-learning-app.jar
  • 查看进程id
    可以使用jcmd
  • 运行btrace 脚本
btrace -v <pid> Simple.java
  • 效果

 

 

说明

以上是一个简单的使用,btrace 的功能还是很强大的,支持对于新版本jdk 暂不支持,对于的agent boot client 都已经提供maven 包了
我们可以方便在项目中使用,阿里的arthas 也是一个很不错的工具,强大,系统开销比较小

参考资料

https://github.com/btraceio/btrace

posted on   荣锋亮  阅读(448)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2019-01-03 masterlab 敏捷项目管理工具
2019-01-03 luarocks 自定义包发布试用
2018-01-03 jest js 测试框架-简单方便人性化
2014-01-03 Nginx 安装成Windows 服务方法
2014-01-03 Windows环境下用jwplayer+Nginx搭建视频点播服务器
2014-01-03 我的nginx iis 负载均衡学习(环境搭建)
2014-01-03 windows 版nginx 的一些基础知识

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示