tonglin0325的个人主页

arthas使用方法

arthas是阿里开发的一个java诊断工具,官网

1
2
https://arthas.aliyun.com/zh-cn/

下载地址

1
2
https://github.com/alibaba/arthas/releases

 

启动arthas-boot.jar#

选择java程序的pid,注意启动arthas-boot.jar的用户需要和所监控的java进程的用户是同一个,否则会报下面的错误

1
2
3
4
5
6
7
8
9
10
[ERROR] Start arthas failed, exception stack trace:
java.io.IOException: well-known file /tmp/.java_pid29923 is not secure: file should be owned by the current user (which is 0) but is owned by 987
at sun.tools.attach.LinuxVirtualMachine.checkPermissions(Native Method)
at sun.tools.attach.LinuxVirtualMachine.<init>(LinuxVirtualMachine.java:117)
at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:78)
at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:250)
at com.taobao.arthas.core.Arthas.attachAgent(Arthas.java:102)
at com.taobao.arthas.core.Arthas.<init>(Arthas.java:27)
at com.taobao.arthas.core.Arthas.main(Arthas.java:161)

如果切换用户的时候遇到

1
2
This account is currently not available.

则可以使用下面命令来切换

1
2
sudo su -l xxx -s /bin/bash

启动arthas

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
java -jar ./arthas-boot.jar
[INFO] arthas-boot version: 3.6.0
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 23235
[2]: 66309 math-game.jar
[3]: 78485
[4]: 85979
[5]: 16061 org.jd.gui.OsxApp
[6]: 90607 org.jetbrains.jps.cmdline.Launcher
[7]: 88623
2
[INFO] arthas home: /Users/lintong/Downloads/arthas-bin
[INFO] Try to attach process 66309
[INFO] Attach process 66309 success.
[INFO] arthas-client connect 127.0.0.1 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'

wiki https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version 3.6.0
main_class demo.MathGame
pid 66309
time 2022-04-10 17:08:50

[arthas@66309]$

  

1.查看仪表盘#

输入dashboard,退出control+C

2.查看主线程#

1
2
3
4
5
6
7
[arthas@66309]$ thread 1
"main" Id=1 TIMED_WAITING
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at demo.MathGame.main(MathGame.java:17)

3.watch命令监控方法#

监控返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
watch demo.MathGame primeFactors returnObj
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 119 ms, listenerId: 1
method=demo.MathGame.primeFactors location=AtExit
ts=2022-04-10 17:16:30; [cost=1.662911ms] result=@ArrayList[
@Integer[2],
@Integer[2],
@Integer[2],
@Integer[2],
@Integer[2],
@Integer[2],
@Integer[2],
@Integer[3],
@Integer[419],
]

监控参数值

1
2
3
4
5
6
7
8
watch demo.MathGame primeFactors params
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 23 ms, listenerId: 2
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2022-04-10 17:16:44; [cost=0.266471ms] result=@Object[][
@Integer[-150713],
]

同时监控多个值

1
2
watch org.xx.xx Func "{params[0],params[1],target,returnObj}" -x 2 -b -s -n 2

查看匿名内部类

1
2
watch org.xx.yy$zz Func "{params[0],params[1],target,returnObj}" -x 2 -b -s -n 2

4.监控静态变量#

1
2
getstatic org.xx.yy.ZZ var 

5.反编译#

1
2
jad demo.MathGame

  

其他参考:https://arthas.aliyun.com/doc/advanced-use.html