watch

函数执行数据监测

让你能方便的观察到指定函数的调用情况。能观察到的范围为:返回值、抛出异常、入参, 通过编写 OGNL 表达式进行对应变量的查看。

命令选项 描述
-b, --before 在方法执行之前监测
-e, --exception 仅当抛出异常后监测
--exclude-class-pattern <pattern> 排除class模式
-x, --expand <level> 扩展对象层级,默认1
-f, --finish 在方法执行后监测,默认为方法执行后检测
-n, --limits <number> 监控执行次数
--listenerId <id> 特殊的监控id
-E, --regex 开启正则匹配,默认为通配符匹配
-s, --success 在方法执行成功监测
-v, --verbose 打印debug信息
-h, --help 帮助
<class-pattern> 类匹配模式
<method-pattern> 方法匹配模式
<watch-express> ognl观察表达式,默认值是{params, target, returnObj}
<condition-express> ognl条件表达式

示例

方法执行位置 描述
AtExit 方法执行完成
AtEnter 方法执行前
AtExceptionExit 方法执行异常退出
# 执行方法之后观察
[arthas@20276]$ watch demo.MathGame print -f
[arthas@20276]$ watch demo.MathGame print
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 71 ms, listenerId: 1
method=demo.MathGame.print location=AtExit
ts=2023-03-31 15:48:53; [cost=1.1698ms] result=@ArrayList[
    @Object[][isEmpty=false;size=2],
    null,
    null,
]

# 执行方法之前观察
[arthas@20276]$ watch demo.MathGame print -b
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 34 ms, listenerId: 2
method=demo.MathGame.print location=AtEnter
ts=2023-03-31 15:50:18; [cost=0.0594ms] result=@ArrayList[
    @Object[][isEmpty=false;size=2],
    null,
    null,
]

# 执行方法异常时
[arthas@20276]$ watch demo.MathGame primeFactors throwExp -e
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 36 ms, listenerId: 6
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2023-03-31 15:53:55; [cost=0.092ms] result=java.lang.IllegalArgumentException: number is: -91979, need >= 2
        at demo.MathGame.primeFactors(MathGame.java:46)
        at demo.MathGame.run(MathGame.java:24)
        at demo.MathGame.main(Unknown Source)
        
# 执行方法成功时
[arthas@20276]$ watch demo.MathGame primeFactors -s
Press Q or Ctrl+C to abort.method=demo.MathGame.primeFactors location=AtExit
ts=2023-03-31 16:06:19; [cost=0.2284ms] result=@ArrayList[
    @Object[][isEmpty=false;size=1],
    @MathGame[demo.MathGame@7d6f77cc],
    @ArrayList[isEmpty=false;size=2],
]

# 条件过滤异常执行 建议添加-v 用于辅助条件判断
[arthas@20276]$ watch demo.MathGame primeFactors '{params, throwExp}' 'isThrow' -v
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 33 ms, listenerId: 11
Condition express: isThrow , result: true
method=demo.MathGame.primeFactors location=AtExceptionExit
ts=2023-03-31 16:07:48; [cost=0.0976ms] result=@ArrayList[
    @Object[][isEmpty=false;size=1],
    @IllegalArgumentException[java.lang.IllegalArgumentException: number is: -137963, need >= 2],
]