标签:ide ase 参考 tac nbsp exec nsf sim his
btrace 是一个类型安全的java 平台动态追踪工具(类似dtrace,bpf。。。),以下是一个简单的试用
备注: 目前btrace 对于jdk 9 以及以上版本的支持有问题,而且团队暂时也没有支持的打算
JAVA_HOME
以及BTRACE_HOME
java_home 的很简单,BTRACE_HOME 类似,同时配置好path 路径├── README.md
├── btrace-app
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── com
│ └── dalong
│ └── Application.java
└── btrace-scripts
└── Simple.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
btrace -v <pid> Simple.java
以上是一个简单的使用,btrace 的功能还是很强大的,支持对于新版本jdk 暂不支持,对于的agent boot client 都已经提供maven 包了
我们可以方便在项目中使用,阿里的arthas 也是一个很不错的工具,强大,系统开销比较小
https://github.com/btraceio/btrace
标签:ide ase 参考 tac nbsp exec nsf sim his
原文地址:https://www.cnblogs.com/rongfengliang/p/12144482.html