Arthas 基础教程
Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪Java代码;实时监控JVM状态。
Arthas 支持JDK 6+,支持Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。
Github: https://github.com/alibaba/arthas
文档: https://arthas.aliyun.com/doc/
启动arthas-demo
下载arthas-demo.jar,再用java -jar命令启动:
wget https://arthas.aliyun.com/arthas-demo.jar;java -jar arthas-demo.jar
arthas-demo是一个很简单的程序,它随机生成整数,再执行因式分解,把结果打印出来。如果生成的随机数是负数,则会打印提示信息。
shell@Alicloud:~$ wget https://arthas.aliyun.com/arthas-demo.jar;java -jar arthas-demo.jar
--2021-03-15 22:00:06-- https://arthas.aliyun.com/arthas-demo.jar
Resolving arthas.aliyun.com (arthas.aliyun.com)... 203.119.214.116
Connecting to arthas.aliyun.com (arthas.aliyun.com)|203.119.214.116|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4497 (4.4K) [application/java-archive]
Saving to: ‘arthas-demo.jar’
arthas-demo.jar 100%[=======================================================>] 4.39K --.-KB/s in 0s
2021-03-15 22:00:07 (40.1 MB/s) - ‘arthas-demo.jar’ saved [4497/4497]
169788=2*2*3*14149
illegalArgumentCount: 1, number is: -168533, need >= 2
illegalArgumentCount: 2, number is: -214707, need >= 2
138028=2*2*11*3137
628=2*2*157
启动arthas-boot
在新的Terminal 2里,下载arthas-boot.jar,再用java -jar命令启动:
wget https://arthas.aliyun.com/arthas-boot.jar;java -jar arthas-boot.jar
arthas-boot是Arthas的启动程序,它启动后,会列出所有的Java进程,用户可以选择需要诊断的目标进程。
选择第一个进程,输入 1 ,再Enter/回车:
$1
Attach成功之后,会打印Arthas LOGO。输入 help 可以获取到更多的帮助信息
$help
image.png
Dashboard
dashboard 命令可以查看当前系统的实时数据面板。
$dashboard
输入 q 或者 Ctrl+C 可以退出dashboard命令。
$q
image.png
Thread
thread 1 命令会打印线程ID 1的栈。
[arthas@7467]$ 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)
Ctrl+C Arthas支持管道,可以用 thread 1 | grep 'main(' 查找到main class。
$ thread 1 | grep 'main('
可以看到main class是demo.MathGame:
$ thread 1 | grep 'main('
at demo.MathGame.main(MathGame.java:17)
Sc
可以通过 sc 命令来查找JVM里已加载的类:
[arthas@7467]$ sc -d *MathGame
class-info demo.MathGame
code-source /root/arthas-demo.jar
name demo.MathGame
isInterface false
isAnnotation false
isEnum false
isAnonymousClass false
isArray false
isLocalClass false
isMemberClass false
isPrimitive false
isSynthetic false
simple-name MathGame
modifier public
annotation
interfaces
super-class +-java.lang.Object
class-loader +-sun.misc.Launcher$AppClassLoader@70dea4e
+-sun.misc.Launcher$ExtClassLoader@4bc00ff2
classLoaderHash 70dea4e
Affect(row-cnt:1) cost in 32 ms.
Jad
可以通过 jad 命令来反编译代码:
$ jad demo.MathGame
Watch
通过watch命令可以查看函数的参数/返回值/异常信息。
[arthas@7467]$ watch demo.MathGame primeFactors returnObj
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 64 ms, listenerId: 1
method=demo.MathGame.primeFactors location=AtExit
ts=2021-03-15 22:12:03; [cost=1.677656ms] result=@ArrayList[
@Integer[2],
@Integer[3],
@Integer[7],
@Integer[3463],
]
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2021-03-15 22:12:04; [cost=0.161071ms] result=null
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2021-03-15 22:12:05; [cost=0.067096ms] result=null
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2021-03-15 22:12:06; [cost=0.073109ms] result=null
网友评论