stack

输出当前方法被调用的调用路径

很多时候我们都知道一个方法被执行,但这个方法被执行的路径非常多,或者你根本就不知道这个方法是从那里被执行了,此时你需要的是 stack 命令。

命令选项 描述
--exclude-class-pattern <pattern> 排除class模式
-n, --limits <number> 监控执行次数
--listenerId <id> 特殊的监控id
-E, --regex 开启正则匹配,默认为通配符匹配
-v, --verbose 打印debug信息
-h, --help 帮助
<class-pattern> 类匹配模式
<method-pattern> 方法匹配模式
<condition-express> ognl条件表达式

示例

# 监控primeFactors方法调用路径
[arthas@2200]$ stack demo.MathGame primeFactors
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 15 ms, listenerId: 5
ts=2023-03-30 16:14:25;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=jdk.internal.loader.ClassLoaders$AppClassLoader@15db9742
    @demo.MathGame.primeFactors()
        at demo.MathGame.run(MathGame.java:24)
        at demo.MathGame.main(null:16)

ts=2023-03-30 16:14:26;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=jdk.internal.loader.ClassLoaders$AppClassLoader@15db9742
    @demo.MathGame.primeFactors()
        at demo.MathGame.run(MathGame.java:24)
        at demo.MathGame.main(null:16)
        
# 根据条件表达式过滤
[arthas@2200]$ stack demo.MathGame primeFactors 'params[0] < 0' -n 2
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 18 ms, listenerId: 6
ts=2023-03-30 16:15:55;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=jdk.internal.loader.ClassLoaders$AppClassLoader@15db9742
    @demo.MathGame.primeFactors()
        at demo.MathGame.run(MathGame.java:24)
        at demo.MathGame.main(null:16)

ts=2023-03-30 16:16:01;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=jdk.internal.loader.ClassLoaders$AppClassLoader@15db9742
    @demo.MathGame.primeFactors()
        at demo.MathGame.run(MathGame.java:24)
        at demo.MathGame.main(null:16)

Command execution times exceed limit: 2, so command will exit. You can set it with -n option.

# 根据rt过滤
[arthas@20276]$ stack demo.MathGame primeFactors '#cost > 0.5' -n 2
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 10 ms, listenerId: 4
ts=2023-03-30 16:27:00;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=jdk.internal.loader.ClassLoaders$AppClassLoader@6d06d69c
    @demo.MathGame.primeFactors()
        at demo.MathGame.run(MathGame.java:24)
        at demo.MathGame.main(null:16)