Arthas watch 命令使用指南

Posted by agentd on 05-02,2020

2020-06-23 UPDATE
新增对 list 的常用操作

Arthas 是我很喜欢的一款 Java 领域的开发调试工具。

每次测试遇到问题的时候,当别人为了加一条日志而重发代码,我都会欣慰地拿出我的 Arthas 并且告诉他们:少年,你不用再为了加日志就重发代码而烦恼了。Arthas,你值得拥有。

这次我要介绍的是我使用最多的一个功能:watch。Arthas 功能虽多,但我最喜欢的还是这一个。使用 watch 之后,我再也不用为了观察函数调用而加日志了。

Arthas 是什么

Arthas 官网是这么介绍自己的:

Arthas 是 Alibaba 开源的 Java 诊断工具,深受开发者喜爱。
当你遇到以下类似问题而束手无策时,Arthas 可以帮助你解决:

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

一键安装并启动 Arthas

curl -O https://alibaba.github.io/arthas/arthas-boot.jar && java -Dfile.encoding=UTF-8 -jar arthas-boot.jar

稍微解释一下上面这条 shell 命令。命令分为两部分,&& 之前的部分是下载 Arthas,之后的部分是启动 Arthas。

你可能会疑惑下载文件为什么不用 wget 而是用 curl?这是因为有些服务器是没有预装 wget 的,但是基本都预装了 curl。如果你的服务器预装了 wget 的话完全可以把 'curl' 改成 wget

如果使用 wget 的话命令可以改成:

# wget 版命令
wget https://alibaba.github.io/arthas/arthas-boot.jar && java -Dfile.encoding=UTF-8 -jar arthas-boot.jar

另外一个需要解释的点是 -Dfile.encoding=UTF-8,这个 Java 设置是为了让 Arthas 输出中文的时候不会乱码,这一点可以看一下我以前的文章 由 Arthas 中文乱码引发的 Java 默认编码思考

Arthas watch 命令

watch 让你能方便地观察到指定方法的调用情况。能观察到的范围为:返回值抛出异常入参(还能观察执行函数的对象本身,不知道为什么官方介绍的时候没说这个」,通过编写 OGNL 表达式进行对应变量的查看。

# watch -h
# USAGE
watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-pattern method-pattern express [condition-express]

1 观察方法返回结果 returnObj

使用方式看着复杂,其实很简单。来个最简单的示例:
假设我们要观察下面这段代码中字符串的 contains 方法。

public class App {
    public static void main(String[] args) throws IOException {
        String hello = "Hello Arthas";
        while (true) {
            boolean contains = StringUtils.contains(hello, "Arthas");
            System.out.println(contains);
        }
    }
}

可以使用如下语句

## 观察 contains 返回结果
[arthas@11939]$ watch org.apache.commons.lang3.StringUtils contains returnObj -n 3
# Press Q or Ctrl+C to abort.
# Affect(class-cnt:1 , method-cnt:2) cost in 68 ms.
# ts=2020-05-02 16:46:04; [cost=2.424254ms] result=@Boolean[true]
# ts=2020-05-02 16:46:05; [cost=0.21033ms] result=@Boolean[true]
# ts=2020-05-02 16:46:06; [cost=0.165514ms] result=@Boolean[true]

-n 3 表示只执行三次,这参数挺常用,不然很容易被输出刷屏。

2 过滤不关心的调用 condition-express

显然,真实的案例肯定不会如上面的示例那么简单。
真实的服务代码中,肯定不止一个地方调用了 String 的 contains 方法。我们需要把无关的调用过滤掉。

## 观察 contains 返回结果,并且过滤掉无关调用
[arthas@11939]$ watch org.apache.commons.lang3.StringUtils contains returnObj 'params[1]=="Arthas"'
# Press Q or Ctrl+C to abort.
# Affect(class-cnt:1 , method-cnt:2) cost in 29 ms.
# ts=2020-05-02 16:48:50; [cost=0.331109ms] result=@Boolean[true]
# ts=2020-05-02 16:48:51; [cost=0.175224ms] result=@Boolean[true]
# ts=2020-05-02 16:48:52; [cost=0.138984ms] result=@Boolean[true]

入参是一个很容易把不同调用区分开的方法,通过 params[1]=="Arthas" 这个 condition-express,我们可以只保留第二个入参是 Arthas 的函数调用。

3 同时观察入参和结果

[arthas@11939]$ watch org.apache.commons.lang3.StringUtils contains {params,returnObj} 'params[1]=="Arthas"'
# Press Q or Ctrl+C to abort.
# Affect(class-cnt:1 , method-cnt:2) cost in 33 ms.
# ts=2020-05-02 16:51:27; [cost=0.507486ms] result=@ArrayList[
#     @Object[][isEmpty=false;size=2],
#     @Boolean[true],
# ]

通过 {} 把字段包起来,可以同时观察想观察的字段。可以注意到一个点,params 是一个数组,但是打印 params 的时候并没有把具体内容打印出来,这个时候可以使用 -x 2 来指定打印对象的属性遍历深度。

arthas@11939]$ watch org.apache.commons.lang3.StringUtils contains  {params,returnObj} 'params[1]=="Arthas"' -x 2
# Press Q or Ctrl+C to abort.
# Affect(class-cnt:1 , method-cnt:2) cost in 35 ms.
# ts=2020-05-02 16:51:33; [cost=0.391218ms] result=@ArrayList[
#     @Object[][
#         @String[Hello Arthas],
#         @String[Arthas],
#     ],
#     @Boolean[true],
# ]

4 给大家来几个个我实际用到的例子

在陌陌做动态推荐开发的时候,测试时经常会遇到查看某个用户是否开启了相应的业务开关,经常就会需要查看某个实验开关是否开启。

## 查看陌陌用户 1234567 是否开启 ElasticSearch 开关
watch com.momo.Experiment enableElasticSearch returnObj 'target.momoId=="1234567"'
# ts=2020-05-02 20:09:46; [cost=24.443527ms] result=@Boolean[true]

我还经常会根据入参的陌陌用户 id 进行判断,查看返回结果或者异常

## 查看 MorecControlFlow 类的 process 方法返回结果
## process 方法第一个参数的 momoId 属性值为 “123454567” 才生效
## 类路径和陌陌号都非真实数据
watch com.momo.MorecControlFlow process returnObj 'params[0].momoId=="123454567"'
# ts=2019-03-18 21:09:46; [cost=264.434972ms] result=@Boolean[true]

## 查看 IMorecShuffler 类的 shuffle 抛的异常
## process 方法第一个参数的 momoId 属性值为 “123454567” 才生效
watch com.momo.plugins.shuffler.IMorecShuffler shuffle throwExp 'params[0].morecRequest.momoId=="123454567"'
# ts=2019-03-27 20:54:29; [cost=46.642339ms] result=java.lang.IndexOutOfBoundsException: Index: 12, Size: 11
	at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:665)
	at java.util.ArrayList.add(ArrayList.java:477)
	at com.momo.plugin.shuffler.RoomShuffler.shuffle(RoomShuffler:45)

5 观察 List 类型数据的一些特殊用法

  • 查看第一个参数
$ watch com.taobao.container.Test test "params[0]"
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 34 ms.
@ArrayList[
    @Pojo[com.taobao.container.Test$Pojo@75a1cd57],

    @Pojo[com.taobao.container.Test$Pojo@3d012ddd],

    @Pojo[com.taobao.container.Test$Pojo@6f2b958e],

    @Pojo[com.taobao.container.Test$Pojo@1eb44e46],

    @Pojo[com.taobao.container.Test$Pojo@6504e3b2],
  • 查看第一个参数的size
$ watch com.taobao.container.Test test "params[0].size()"
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 22 ms.
@Integer[40]
  • 将结果按name属性投影
$ watch com.taobao.container.Test test "params[0].{ #this.name }"
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 25 ms.
@ArrayList[
    @String[name 0],

    @String[name 1],
  • 按条件过滤
$ watch com.taobao.container.Test test "params[0].{? #this.name == null }" -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 27 ms.
@ArrayList[
    @Pojo[
        name=null,
        age=@Integer[32],
        hobby=null,
    ],
]
@ArrayList[
    @Pojo[
        name=null,
        age=@Integer[31],
        hobby=null,
    ],
]

$ watch com.taobao.container.Test test "params[0].{? #this.name != null }" -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 24 ms.
@ArrayList[
    @Pojo[
        name=@String[name 1],
        age=@Integer[3],
        hobby=null,
    ],
  • 过滤后统计
$ watch com.taobao.container.Test test "params[0].{? #this.age > 10 }.size()" -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 29 ms.
@Integer[31]
@Integer[31]

一些小提示

上面我只是列了一下常用的观察方式和参数,watch 支持的命令还有很多,你可以查看 Arthas 的 watch 命令官方文档

还可以通过启动 arthas 命令之后使用 watch -h 查看。

使用 Arthas 的过程中很多人会觉得获取类的全限定名很费劲,其实这个可以通过 Idea 的 Copy Refrence 快捷键解决。我自己定义的快捷键是 ⌥⌘C

还有一点就是写代码的时候最好把代码拆细,尽量把小功能也封装成单独的函数,等你需要使用 Arthas 观察函数调用的时候,你会回来感谢自己的。

参考文档

Arthas watch 命令文档
由 Arthas 中文乱码引发的 Java 默认编码思考
Arthas:在线 Java 诊断工具