zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    使用openssl实现ECDSA签名以及验证功能(附完整测试源码)
    【转载】浅谈Linux内存管理机制
    【转载】Abstract Factory Step by Step --- 抽象工厂
    【转载】动态规划:从新手到专家
    Windows Container 和 Docker:你需要知道的5件事
    十年
    Docker,容器,虚拟机和红烧肉
    新的用户故事待办列表就是一副地图
    MarkDown/reST 文档发布流水线
    docker4dotnet #5 使用VSTS/TFS搭建基于容器的持续交付管道
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/12144482.html
Copyright © 2011-2022 走看看