想学JDK自带的工具,BTrace然后,同事说这个过时了,但是我不是很相信,因为是JDK自带的工具,他推荐这个,于是我就来看看这个到底是什么东西。
Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。
开源地址:git地址
官方文档:官方
当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:
Arthas采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。
待补充
arthas-agent:基于JavaAgent技术的代理
bin:一些启动脚本
arthas-boot:Java版本的一键安装启动脚本
arthas-client:telnet client代码
arthas-common:一些共用的工具类和枚举类
arthas-core:核心库,各种arthas命令的交互和实现
arthas-demo:示例代码
arthas-memorycompiler:内存编绎器代码,Fork from https://github.com/skalogs/SkaETL/tree/master/compiler
arthas-packaging:maven打包相关的
arthas-site:arthas站点
arthas-spy:编织到目标类中的各个切面
static:静态资源
arthas-testcase:测试
下面列举一些 Arthas 的常用命令
命令 | 介绍 |
---|---|
dashboard | 当前系统的实时数据面板 |
thread | 查看当前 JVM 的线程堆栈信息 |
watch | 方法执行数据观测 |
trace | 方法内部调用路径,并输出方法路径上的每个节点上耗时 |
stack | 输出当前方法被调用的调用路径 |
tt | 方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测 |
monitor | 方法执行监控 |
jvm | 查看当前 JVM 信息 |
vmoption | 查看,更新 JVM 诊断相关的参数 |
sc | 查看 JVM 已加载的类信息 |
sm | 查看已加载类的方法信息 |
jad | 反编译指定已加载类的源码 |
classloader | 查看 classloader 的继承树,urls,类加载信息 |
heapdump | 类似 jmap 命令的 heap dump 功能 |
Linux/Unix/Mac
安装Arthas:
[/Users/lcc]$ curl -L https://alibaba.github.io/arthas/install.sh | sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 961 100 961 0 0 1537 0 --:--:-- --:--:-- --:--:-- 1537
downloading... ./as.sh.1042
Arthas install successed.
[/Users/lcc]$
[/Users/lcc]$
[/Users/lcc]$ll
-rwxr-xr-x@ 1 lcc staff 28075 3 10 08:45 as.sh*
启动Arthas:
./as.sh
[/Users/lcc]$./as.sh
Error: telnet is not installed. Try to use java -jar arthas-boot.jar
[/Users/lcc]$
这里要去安装telnet
[/Users/lcc]$./as.sh
Arthas script version: 3.1.7
[INFO] JAVA_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home
Found existing java process, please choose one and hit RETURN.
* [1]: 304
安装telnet 参考:安装telnet
下载
wget https://alibaba.github.io/arthas/arthas-boot.jar
启动
$ java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.1.7
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 304
# 查看
# jps -ml
304
4740 sun.tools.jps.Jps -ml
4728 arthas-boot.jar
然后执行
[/Users/lcc]$./as.sh
Arthas script version: 3.1.7
[INFO] JAVA_HOME: /Library/Java/JavaVirtualMachines/jdk1.8.0_241.jdk/Contents/Home
Found existing java process, please choose one and hit RETURN.
* [1]: 304
[2]: 4757 arthas-boot.jar
2 # 这里输入你想要查看的PID
updating version 3.1.7 ...
Download arthas from: https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/3.1.7/arthas-packaging-3.1.7-bin.zip
############################################################################################################################################################# 100.0%
Archive: /tmp/temp_3.1.7_4829/arthas-3.1.7-bin.zip
creating: /tmp/temp_3.1.7_4829/async-profiler/
inflating: /tmp/temp_3.1.7_4829/arthas-spy.jar
inflating: /tmp/temp_3.1.7_4829/arthas-agent.jar
inflating: /tmp/temp_3.1.7_4829/arthas-client.jar
inflating: /tmp/temp_3.1.7_4829/arthas-boot.jar
inflating: /tmp/temp_3.1.7_4829/arthas-demo.jar
inflating: /tmp/temp_3.1.7_4829/install-local.sh
inflating: /tmp/temp_3.1.7_4829/as.sh
inflating: /tmp/temp_3.1.7_4829/as.bat
inflating: /tmp/temp_3.1.7_4829/as-service.bat
inflating: /tmp/temp_3.1.7_4829/async-profiler/libasyncProfiler-linux-x64.so
inflating: /tmp/temp_3.1.7_4829/async-profiler/libasyncProfiler-mac-x64.so
inflating: /tmp/temp_3.1.7_4829/arthas-core.jar
update completed.
Arthas home: /Users/lcc/.arthas/lib/3.1.7/arthas
Calculating attach execution time...
Attaching to 4757 using version /Users/lcc/.arthas/lib/3.1.7/arthas...
real 0m1.330s
user 0m0.319s
sys 0m0.050s
Attach success.
telnet connecting to arthas server... current timestamp is 1583807312
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://alibaba.github.io/arthas
tutorials https://alibaba.github.io/arthas/arthas-tutorials
version 3.1.7
pid 4757
time 2020-03-10 10:28:32
[arthas@4757]$
下载
wget https://alibaba.github.io/arthas/arthas-boot.jar
启动
$ java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.1.7
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 304
# 查看
# jps -ml
304
4740 sun.tools.jps.Jps -ml
4728 arthas-boot.jar
查看日志
cat ~/logs/arthas/arthas.log
Web控制台
http://127.0.0.1:8563
查看帮助
$ java -jar arthas-boot.jar -h
[INFO] arthas-boot version: 3.1.7
Usage: arthas-boot [-h] [--target-ip <value>] [--telnet-port <value>]
[--http-port <value>] [--session-timeout <value>] [--arthas-home <value>]
[--use-version <value>] [--repo-mirror <value>] [--versions] [--use-http]
[--attach-only] [-c <value>] [-f <value>] [--height <value>] [--width
<value>] [-v] [--tunnel-server <value>] [--agent-id <value>] [--stat-url
<value>] [pid]
Bootstrap Arthas
EXAMPLES:
java -jar arthas-boot.jar <pid>
java -jar arthas-boot.jar --target-ip 0.0.0.0
java -jar arthas-boot.jar --telnet-port 9999 --http-port -1
java -jar arthas-boot.jar --tunnel-server 'ws://192.168.10.11:7777/ws'
java -jar arthas-boot.jar --tunnel-server 'ws://192.168.10.11:7777/ws'
--agent-id bvDOe8XbTM2pQWjF4cfw
java -jar arthas-boot.jar --stat-url 'http://192.168.10.11:8080/api/stat'
java -jar arthas-boot.jar -c 'sysprop; thread' <pid>
java -jar arthas-boot.jar -f batch.as <pid>
java -jar arthas-boot.jar --use-version 3.1.7
java -jar arthas-boot.jar --versions
java -jar arthas-boot.jar --session-timeout 3600
java -jar arthas-boot.jar --attach-only
java -jar arthas-boot.jar --repo-mirror aliyun --use-http
WIKI:
https://alibaba.github.io/arthas
Options and Arguments:
-h,--help Print usage
--target-ip <value> The target jvm listen ip, default 127.0.0.1
--telnet-port <value> The target jvm listen telnet port, default 3658
--http-port <value> The target jvm listen http port, default 8563
--session-timeout <value> The session timeout seconds, default 1800
(30min)
--arthas-home <value> The arthas home
--use-version <value> Use special version arthas
--repo-mirror <value> Use special maven repository mirror, value is
center/aliyun or http repo url.
--versions List local and remote arthas versions
--use-http Enforce use http to download, default use https
--attach-only Attach target process only, do not connect
-c,--command <value> Command to execute, multiple commands separated
by ;
-f,--batch-file <value> The batch file to execute
--height <value> arthas-client terminal height
--width <value> arthas-client terminal width
-v,--verbose Verbose, print debug info.
--tunnel-server <value> The tunnel server url
--agent-id <value> The agent id register to tunnel server
--stat-url <value> The report stat url
<pid> Target pid
下载demo并启动
wget https://alibaba.github.io/arthas/arthas-demo.jar
java -jar arthas-demo.jar
启动arthas
(base) lcc@lcc arthas$ java -jar arthas-boot.jar --target-ip localhost
[INFO] arthas-boot version: 3.1.7
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 49290 org.apache.zookeeper.server.quorum.QuorumPeerMain
[2]: 51182 org.jetbrains.jps.cmdline.Launcher
1
[INFO] Start download arthas from remote server: https://maven.aliyun.com/repository/public/com/taobao/arthas/arthas-packaging/3.1.7/arthas-packaging-3.1.7-bin.zip
[INFO] File size: 10.33 MB, downloaded size: 1.67 MB, downloading ...
[INFO] File size: 10.33 MB, downloaded size: 6.13 MB, downloading ...
[INFO] Download arthas success.
[INFO] arthas home: /Users/lcc/.arthas/lib/3.1.7/arthas
[INFO] Try to attach process 49290
[INFO] Attach process 49290 success.
[INFO] arthas-client connect localhost 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://alibaba.github.io/arthas
tutorials https://alibaba.github.io/arthas/arthas-tutorials
version 3.1.7
pid 49290
time 2020-03-11 09:40:52
[arthas@49290]$
dashboard 命令可以查看当前系统的实时数据面板。
输入 Q 或者 Ctrl+C 可以退出dashboard命令。
thread 1 命令会打印线程ID 1的栈。
Arthas支持管道,可以用 thread 1 | grep ‘main(’ 查找到main class。
[arthas@49290]$ thread 1 | grep 'main('
at org.apache.zookeeper.server.ZooKeeperServerMain.main(ZooKeeperServerMain.java:55)
at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:81)
[arthas@49290]$
可以通过 sc 命令来查找JVM里已加载的类:
[arthas@49290]$ sc -d *ZooKeeperServerMain
class-info org.apache.zookeeper.server.ZooKeeperServerMain
code-source /Users/lcc/soft/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.jar
name org.apache.zookeeper.server.ZooKeeperServerMain
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name ZooKeeperServerMain
modifier public
annotation org.apache.yetus.audience.InterfaceAudience$Public
interfaces
super-class +-java.lang.Object
class-loader +-sun.misc.Launcher$AppClassLoader@764c12b6
+-sun.misc.Launcher$ExtClassLoader@14899482
classLoaderHash 764c12b6
Affect(row-cnt:1) cost in 50 ms.
可以通过 jad 命令来反编译代码:
通过watch命令可以查看函数的参数/返回值/异常信息。
watch 全限定类名 方法名 returnObj
输入 Q 或者 Ctrl+C 退出watch命令。
arthas@49290]$ watch org.apache.zookeeper.server.ZooKeeperServerMain runFromConfig returnObj
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 51 ms.
等待输入
退出Arthas
用 exit 或者 quit 命令可以退出Arthas。
退出Arthas之后,还可以再次用 java -jar arthas-boot.jar 来连接。
彻底退出Arthas
exit/quit命令只是退出当前session,arthas server还在目标进程中运行。
想完全退出Arthas,可以执行 stop 命令。