Arthas真是好用,项目地址:https://github.com/alibaba/arthas , 回想btrace时代真是辛苦。以下文字来自官方文档摘录。
Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
- 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
- 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
- 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
- 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
- 是否有一个全局视角来查看系统的运行状况?
- 有什么办法可以监控到JVM的实时运行状态?
Arthas支持JDK 6+,支持Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。
快速开始
使用arthas-boot(推荐)
下载arthas-boot.jar,然后用java -jar的方式启动:
wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
打印帮助信息:
java -jar arthas-boot.jar -h
- 如果下载速度比较慢,可以使用aliyun的镜像:java -jar arthas-boot.jar –repo-mirror aliyun –use-http
使用as.sh
Arthas 支持在 Linux/Unix/Mac 等平台上一键安装,请复制以下内容,并粘贴到命令行中,敲 回车 执行即可:
curl -L https://alibaba.github.io/arthas/install.sh | sh
上述命令会下载启动脚本文件 as.sh 到当前目录,你可以放在任何地方或将其加入到 $PATH 中。
直接在shell下面执行./as.sh,就会进入交互界面。
也可以执行./as.sh -h来获取更多参数信息。
快速入门
1. 启动Demo
|
wget https://alibaba.github.io/arthas/arthas-demo.jar
|
|
java -jar arthas-demo.jar
|
arthas-demo是一个简单的程序,每隔一秒生成一个随机数,再执行质因式分解,并打印出分解结果。
arthas-demo源代码:查看
2. 启动arthas
在命令行下面执行(使用和目标进程一致的用户启动,否则可能attach失败):
|
wget https://alibaba.github.io/arthas/arthas-boot.jar
|
|
java -jar arthas-boot.jar
|
- 执行该程序的用户需要和目标进程具有相同的权限。比如以admin用户来执行:sudo su admin && java -jar arthas-boot.jar 或 sudo -u admin -EH java -jar arthas-boot.jar。
- 如果attach不上目标进程,可以查看~/logs/arthas/ 目录下的日志。
- 如果下载速度比较慢,可以使用aliyun的镜像:java -jar arthas-boot.jar –repo-mirror aliyun –use-http
- java -jar arthas-boot.jar -h 打印更多参数信息。
选择应用java进程:
|
$ $ java -jar arthas-boot.jar
|
|
* [1]: 35542
|
|
[2]: 71560 arthas-demo.jar
|
Demo进程是第2个,则输入2,再输入回车/enter。Arthas会attach到目标进程上,并输出日志:
|
[INFO] Try to attach process 71560
|
|
[INFO] Attach process 71560 success.
|
|
[INFO] arthas-client connect 127.0.0.1 3658
|
|
,---. ,------. ,--------.,--. ,--. ,---. ,---.
|
|
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
|
|
| .-. || '--'.' | | | .--. || .-. |`. `-.
|
|
| | | || |\ \ | | | | | || | | |.-' |
|
|
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
|
|
|
|
wiki: https://alibaba.github.io/arthas
|
|
version: 3.0.5.20181127201536
|
|
pid: 71560
|
|
time: 2018-11-28 19:16:24
|
|
|
|
$
|
3. 查看dashboard
输入dashboard,按enter/回车,会展示当前进程的信息,按ctrl+c可以中断执行。
|
$ dashboard
|
|
ID NAME GROUP PRIORI STATE %CPU TIME INTERRU DAEMON
|
|
17 pool-2-thread-1 system 5 WAITIN 67 0:0 false false
|
|
27 Timer-for-arthas-dashb system 10 RUNNAB 32 0:0 false true
|
|
11 AsyncAppender-Worker-a system 9 WAITIN 0 0:0 false true
|
|
9 Attach Listener system 9 RUNNAB 0 0:0 false true
|
|
3 Finalizer system 8 WAITIN 0 0:0 false true
|
|
2 Reference Handler system 10 WAITIN 0 0:0 false true
|
|
4 Signal Dispatcher system 9 RUNNAB 0 0:0 false true
|
|
26 as-command-execute-dae system 10 TIMED_ 0 0:0 false true
|
|
13 job-timeout system 9 TIMED_ 0 0:0 false true
|
|
1 main main 5 TIMED_ 0 0:0 false false
|
|
14 nioEventLoopGroup-2-1 system 10 RUNNAB 0 0:0 false false
|
|
18 nioEventLoopGroup-2-2 system 10 RUNNAB 0 0:0 false false
|
|
23 nioEventLoopGroup-2-3 system 10 RUNNAB 0 0:0 false false
|
|
15 nioEventLoopGroup-3-1 system 10 RUNNAB 0 0:0 false false
|
|
Memory used total max usage GC
|
|
heap 32M 155M 1820M 1.77% gc.ps_scavenge.count 4
|
|
ps_eden_space 14M 65M 672M 2.21% gc.ps_scavenge.time(m 166
|
|
ps_survivor_space 4M 5M 5M s)
|
|
ps_old_gen 12M 85M 1365M 0.91% gc.ps_marksweep.count 0
|
|
nonheap 20M 23M -1 gc.ps_marksweep.time( 0
|
|
code_cache 3M 5M 240M 1.32% ms)
|
|
Runtime
|
|
os.name Mac OS X
|
|
os.version 10.13.4
|
|
java.version 1.8.0_162
|
|
java.home /Library/Java/JavaVir
|
|
tualMachines/jdk1.8.0
|
|
_162.jdk/Contents/Hom
|
|
e/jre
|
4. 通过thread命令来获取到arthas-demo进程的Main Class
thread 1会打印线程ID 1的栈,通常是main函数的线程。
|
$ thread 1 | grep 'main('
|
|
at demo.MathGame.main(MathGame.java:17)
|
5. 通过jad来反编绎Main Class
|
$ jad demo.MathGame
|
|
|
|
ClassLoader:
|
|
[email protected]3d4eac69
|
|
[email protected]66350f69
|
|
|
|
Location:
|
|
/tmp/arthas-demo.jar
|
|
|
|
/*
|
|
* Decompiled with CFR 0_132.
|
|
*/
|
|
package demo;
|
|
|
|
import java.io.PrintStream;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
public class MathGame {
|
|
private static Random random = new Random();
|
|
private int illegalArgumentCount = 0;
|
|
|
|
public static void main(String[] args) throws InterruptedException {
|
|
MathGame game = new MathGame();
|
|
do {
|
|
game.run();
|
|
TimeUnit.SECONDS.sleep(1L);
|
|
} while (true);
|
|
}
|
|
|
|
public void run() throws InterruptedException {
|
|
try {
|
|
int number = random.nextInt();
|
|
List<Integer> primeFactors = this.primeFactors(number);
|
|
MathGame.print(number, primeFactors);
|
|
}
|
|
catch (Exception e) {
|
|
System.out.println(String.format("illegalArgumentCount:%3d, ", this.illegalArgumentCount) + e.getMessage());
|
|
}
|
|
}
|
|
|
|
public static void print(int number, List<Integer> primeFactors) {
|
|
StringBuffer sb = new StringBuffer("" + number + "=");
|
|
Iterator<Integer> iterator = primeFactors.iterator();
|
|
while (iterator.hasNext()) {
|
|
int factor = iterator.next();
|
|
sb.append(factor).append('*');
|
|
}
|
|
if (sb.charAt(sb.length() - 1) == '*') {
|
|
sb.deleteCharAt(sb.length() - 1);
|
|
}
|
|
System.out.println(sb);
|
|
}
|
|
|
|
public List<Integer> primeFactors(int number) {
|
|
if (number < 2) {
|
|
++this.illegalArgumentCount;
|
|
throw new IllegalArgumentException("number is: " + number + ", need >= 2");
|
|
}
|
|
ArrayList<Integer> result = new ArrayList<Integer>();
|
|
int i = 2;
|
|
while (i <= number) {
|
|
if (number % i == 0) {
|
|
result.add(i);
|
|
number /= i;
|
|
i = 2;
|
|
continue;
|
|
}
|
|
++i;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
Affect(row-cnt:1) cost in 970 ms.
|
常用命令举例:
watch
通过watch命令来查看demo.MathGame#primeFactors函数的返回值:
|
$ watch demo.MathGame primeFactors returnObj
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 107 ms.
|
|
ts=2018-11-28 19:22:30; [cost=1.715367ms] result=null
|
|
ts=2018-11-28 19:22:31; [cost=0.185203ms] result=null
|
|
ts=2018-11-28 19:22:32; [cost=19.012416ms] [email protected][
|
|
@Integer[5],
|
|
@Integer[47],
|
|
@Integer[2675531],
|
|
]
|
|
ts=2018-11-28 19:22:33; [cost=0.311395ms] [email protected][
|
|
@Integer[2],
|
|
@Integer[5],
|
|
@Integer[317],
|
|
@Integer[503],
|
|
@Integer[887],
|
|
]
|
|
ts=2018-11-28 19:22:34; [cost=10.136007ms] [email protected][
|
|
@Integer[2],
|
|
@Integer[2],
|
|
@Integer[3],
|
|
@Integer[3],
|
|
@Integer[31],
|
|
@Integer[717593],
|
|
]
|
|
ts=2018-11-28 19:22:35; [cost=29.969732ms] [email protected][
|
|
@Integer[5],
|
|
@Integer[29],
|
|
@Integer[7651739],
|
|
]
|
更多的功能可以查看进阶使用。
5. 退出arthas
如果只是退出当前的连接,可以用quit或者exit命令。Attach到目标进程上的arthas还会继续运行,端口会保持开放,下次连接时可以直接连接上。
如果想完全退出arthas,可以执行shutdown命令。
watch
方法执行数据观测
让你能方便的观察到指定方法的调用情况。能观察到的范围为:返回值、抛出异常、入参,通过编写 OGNL 表达式进行对应变量的查看。
参数说明
watch 的参数比较多,主要是因为它能在 4 个不同的场景观察对象
参数名称 | 参数说明 |
---|---|
class-pattern | 类名表达式匹配 |
method-pattern | 方法名表达式匹配 |
express | 观察表达式 |
condition-express | 条件表达式 |
[b] | 在方法调用之前观察 |
[e] | 在方法异常之后观察 |
[s] | 在方法返回之后观察 |
[f] | 在方法结束之后(正常返回和异常返回)观察 |
[E] | 开启正则表达式匹配,默认为通配符匹配 |
[x:] | 指定输出结果的属性遍历深度,默认为 1 |
这里重点要说明的是观察表达式,观察表达式的构成主要由 ognl 表达式组成,所以你可以这样写”{params,returnObj}”,只要是一个合法的 ognl 表达式,都能被正常支持。
观察的维度也比较多,主要体现在参数 advice 的数据结构上。Advice 参数最主要是封装了通知节点的所有信息。请参考表达式核心变量中关于该节点的描述。
- 特殊用法请参考:https://github.com/alibaba/arthas/issues/71
- OGNL表达式官网:https://commons.apache.org/proper/commons-ognl/language-guide.html
特别说明:
- watch 命令定义了4个观察事件点,即 -b 方法调用前,-e 方法异常后,-s 方法返回后,-f 方法结束后
- 4个观察事件点 -b、-e、-s 默认关闭,-f 默认打开,当指定观察点被打开后,在相应事件点会对观察表达式进行求值并输出
- 这里要注意方法入参和方法出参的区别,有可能在中间被修改导致前后不一致,除了 -b 事件点 params 代表方法入参外,其余事件都代表方法出参
- 当使用 -b 时,由于观察事件点是在方法调用前,此时返回值或异常均不存在
使用参考
启动 Demo
启动快速入门里的arthas-demo。
观察方法出参和返回值
|
$ watch demo.MathGame primeFactors "{params,returnObj}" -x 2
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 44 ms.
|
|
ts=2018-12-03 19:16:51; [cost=1.280502ms] [email protected][
|
|
@Object[][
|
|
@Integer[535629513],
|
|
],
|
|
@ArrayList[
|
|
@Integer[3],
|
|
@Integer[19],
|
|
@Integer[191],
|
|
@Integer[49199],
|
|
],
|
|
]
|
观察方法入参
|
$ watch demo.MathGame primeFactors "{params,returnObj}" -x 2 -b
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 50 ms.
|
|
ts=2018-12-03 19:23:23; [cost=0.0353ms] [email protected][
|
|
@Object[][
|
|
@Integer[-1077465243],
|
|
],
|
|
null,
|
|
]
|
- 对比前一个例子,返回值为空(事件点为方法执行前,因此获取不到返回值)
同时观察方法调用前和方法返回后
|
$ watch demo.MathGame primeFactors "{params,target,returnObj}" -x 2 -b -s -n 2
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 46 ms.
|
|
ts=2018-12-03 19:29:54; [cost=0.01696ms] [email protected][
|
|
@Object[][
|
|
@Integer[1544665400],
|
|
],
|
|
@MathGame[
|
|
|
|
[email protected][13038],
|
|
],
|
|
null,
|
|
]
|
|
ts=2018-12-03 19:29:54; [cost=4.277392ms] [email protected][
|
|
@Object[][
|
|
@Integer[1544665400],
|
|
],
|
|
@MathGame[
|
|
|
|
[email protected][13038],
|
|
],
|
|
@ArrayList[
|
|
@Integer[2],
|
|
@Integer[2],
|
|
@Integer[2],
|
|
@Integer[5],
|
|
@Integer[5],
|
|
@Integer[73],
|
|
@Integer[241],
|
|
@Integer[439],
|
|
],
|
|
]
|
- 参数里-n 2,表示只执行两次
- 这里输出结果中,第一次输出的是方法调用前的观察表达式的结果,第二次输出的是方法返回后的表达式的结果
- 结果的输出顺序和事件发生的先后顺序一致,和命令中 -s -b 的顺序无关
调整-x的值,观察具体的方法参数值
|
$ watch demo.MathGame primeFactors "{params,target}" -x 3
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 58 ms.
|
|
ts=2018-12-03 19:34:19; [cost=0.587833ms] [email protected][
|
|
@Object[][
|
|
@Integer[47816758],
|
|
],
|
|
@MathGame[
|
|
|
|
[email protected][3905348978240129619],
|
|
[email protected][3133719055989],
|
|
[email protected][25214903917],
|
|
[email protected][11],
|
|
[email protected][281474976710655],
|
|
[email protected][1.1102230246251565E-16],
|
|
[email protected][bound must be positive],
|
|
[email protected][bound must be greater than origin],
|
|
[email protected][size must be non-negative],
|
|
[email protected][-3282039941672302964],
|
|
[email protected][0.0],
|
|
[email protected][false],
|
|
[email protected][][isEmpty=false;size=3],
|
|
|
|
[email protected][24],
|
|
],
|
|
[email protected][13159],
|
|
],
|
|
]
|
- -x表示遍历深度,可以调整来打印具体的参数和结果内容,默认值是1。
条件表达式的例子
|
$ watch demo.MathGame primeFactors "{params[0],target}" "params[0]<0"
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 68 ms.
|
|
ts=2018-12-03 19:36:04; [cost=0.530255ms] [email protected][
|
|
@Integer[-18178089],
|
|
@MathGame[[email protected]],
|
|
]
|
- 只有满足条件的调用,才会有响应。
观察异常信息的例子
|
$ watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 62 ms.
|
|
ts=2018-12-03 19:38:00; [cost=1.414993ms] [email protected][
|
|
@Integer[-1120397038],
|
|
java.lang.IllegalArgumentException: number is: -1120397038, need >= 2
|
|
at demo.MathGame.primeFactors(MathGame.java:46)
|
|
at demo.MathGame.run(MathGame.java:24)
|
|
at demo.MathGame.main(MathGame.java:16)
|
|
,
|
|
]
|
- -e表示抛出异常时才触发
- express中,表示异常信息的变量是throwExp
按照耗时进行过滤
|
$ watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 66 ms.
|
|
ts=2018-12-03 19:40:28; [cost=2112.168897ms] [email protected][
|
|
@Object[][
|
|
@Integer[2141897465],
|
|
],
|
|
@ArrayList[
|
|
@Integer[5],
|
|
@Integer[428379493],
|
|
],
|
|
]
|
- #cost>200(单位是ms)表示只有当耗时大于200ms时才会输出,过滤掉执行时间小于200ms的调用
观察当前对象中的属性
如果想查看方法运行前后,当前对象中的属性,可以使用target关键字,代表当前对象
|
$ watch demo.MathGame primeFactors 'target'
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 52 ms.
|
|
ts=2018-12-03 19:41:52; [cost=0.477882ms] [email protected][
|
|
|
|
[email protected][13355],
|
|
]
|
然后使用target.field_name访问当前对象的某个属性
|
$ watch demo.MathGame primeFactors 'target.illegalArgumentCount'
|
|
Press Ctrl+C to abort.
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 67 ms.
|
|
ts=2018-12-03 20:04:34; [cost=131.303498ms] [email protected][8]
|
|
ts=2018-12-03 20:04:35; [cost=0.961441ms] [email protected][8]
|