Arthas初体验

  |   0 评论   |   0 浏览

背景

Arthas 是Alibaba开源的Java诊断工具,基本使用场景是定位复现一些生产环境比较难以定位问题。可以在线排查问题,以及动态追踪Java代码,实时监控JVM状态等等。

用户场景

  1. 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
  2. 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
  3. 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
  4. 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
  5. 是否有一个全局视角来查看系统的运行状况?
  6. 有什么办法可以监控到JVM的实时运行状态?
  7. 怎么快速定位应用的热点,生成火焰图?
  8. 怎样直接从JVM内查找某个类的实例?

快速入门

下载

本文写作时,最新版本为arthas-all-3.6.3:13M。

wget https://github.com/alibaba/arthas/releases/download/arthas-all-3.6.3/arthas-bin.zip
unzip arthas-bin.zip -d arthas

或者

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar

或者

curl -L https://arthas.aliyun.com/install.sh | sh

卸载方式

rm -rf ~/.arthas/
rm -rf ~/logs/arthas/

使用

这里我先启动了一个不停的输出'Hello World'的程序。

然后运行 sh as.sh,结果如下:

Attaching to 72007 using version /home/abeffect/share/arthas...

real	0m2.348s
user	0m0.260s
sys	0m0.143s
Attach success.
telnet connecting to arthas server... current timestamp is 1656836059
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'

wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.6.3
main_class MainHelloWorld
pid        72007
time       2022-07-03 16:14:19

指令 dashboard

展示当前进程的信息,按<span class="pre">ctrl+c</span>可以中断执行。

指令 thread

获取主进程,thread 1。

[arthas@72007]$ thread 1
"main" Id=1 TIMED_WAITING
    at java.lang.Thread.sleep(Native Method)
    at MainHelloWorld.main(MainHelloWorld.java:7)

指令 jad 反编译

ClassLoader:
+-sun.misc.Launcher$AppClassLoader@18b4aac2
  +-sun.misc.Launcher$ExtClassLoader@34ac45b2

Location:
/Users/80357528/IdeaProjects/helloWorld/target/classes/

       /*
        * Decompiled with CFR.
        */
       public class MainHelloWorld {
           public static void main(String[] args) throws InterruptedException {
/* 3*/         int count = 0;
               while (true) {
/* 5*/             count = MainHelloWorld.getNext(count);
/* 6*/             System.out.println("hello world: " + count);
/* 7*/             Thread.sleep(3000L);
               }
           }

           public static int getNext(int count) {
/*12*/         return ++count;
           }
       }

Affect(row-cnt:1) cost in 901 ms.

watch 查看返回值

watch函数的返回值。

[arthas@89522]$ watch MainHelloWorld getNext returnObj
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 177 ms, listenerId: 1
method=MainHelloWorld.getNext location=AtExit
ts=2022-07-03 17:08:45; [cost=1.439195ms] result=@Integer[51]
method=MainHelloWorld.getNext location=AtExit
ts=2022-07-03 17:08:48; [cost=0.016171ms] result=@Integer[52]
method=MainHelloWorld.getNext location=AtExit
ts=2022-07-03 17:08:51; [cost=0.030117ms] result=@Integer[53]

其它指令

tt 时空隧道

tt

profiler 火焰图

profiler

高级指令

基础命令

  • help——查看命令帮助信息
  • cat——打印文件内容,和linux里的cat命令类似
  • echo–打印参数,和linux里的echo命令类似
  • grep——匹配查找,和linux里的grep命令类似
  • base64——base64编码转换,和linux里的base64命令类似
  • tee——复制标准输入到标准输出和指定的文件,和linux里的tee命令类似
  • pwd——返回当前的工作目录,和linux命令类似
  • cls——清空当前屏幕区域
  • session——查看当前会话的信息
  • reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类
  • version——输出当前目标 Java 进程所加载的 Arthas 版本号
  • history——打印命令历史
  • quit——退出当前 Arthas 客户端,其他 Arthas 客户端不受影响
  • stop——关闭 Arthas 服务端,所有 Arthas 客户端全部退出
  • keymap——Arthas快捷键列表及自定义快捷键

jvm相关

  • dashboard——当前系统的实时数据面板
  • thread——查看当前 JVM 的线程堆栈信息
  • jvm——查看当前 JVM 的信息
  • sysprop——查看和修改JVM的系统属性
  • sysenv——查看JVM的环境变量
  • vmoption——查看和修改JVM里诊断相关的option
  • perfcounter——查看当前 JVM 的Perf Counter信息
  • logger——查看和修改logger
  • getstatic——查看类的静态属性
  • ognl——执行ognl表达式
  • mbean——查看 Mbean 的信息
  • heapdump——dump java heap, 类似jmap命令的heap dump功能
  • vmtool——从jvm里查询对象,执行forceGc

参考

  1. Arthas 用户文档