# stack [`stack`在线教程](https://arthas.aliyun.com/doc/arthas-tutorials.html?language=cn&id=command-stack) ::: tip 输出当前方法被调用的调用路径 ::: 很多时候我们都知道一个方法被执行,但这个方法被执行的路径非常多,或者你根本就不知道这个方法是从那里被执行了,此时你需要的是 stack 命令。 ## 参数说明 | 参数名称 | 参数说明 | | ------------------: | :----------------------------------------------------------------- | | _class-pattern_ | 类名表达式匹配 | | _method-pattern_ | 方法名表达式匹配 | | _condition-express_ | 条件表达式 | | [E] | 开启正则表达式匹配,默认为通配符匹配 | | `[n:]` | 执行次数限制 | | `[m ]` | 指定 Class 最大匹配数量,默认值为 50。长格式为`[maxMatch ]`。 | 这里重点要说明的是观察表达式,观察表达式的构成主要由 ognl 表达式组成,所以你可以这样写`"{params,returnObj}"`,只要是一个合法的 ognl 表达式,都能被正常支持。 观察的维度也比较多,主要体现在参数 `advice` 的数据结构上。`Advice` 参数最主要是封装了通知节点的所有信息。 请参考[表达式核心变量](advice-class.md)中关于该节点的描述。 - 特殊用法请参考:[https://github.com/alibaba/arthas/issues/71](https://github.com/alibaba/arthas/issues/71) - OGNL 表达式官网:[https://commons.apache.org/dormant/commons-ognl/language-guide.html](https://commons.apache.org/dormant/commons-ognl/language-guide.html) ## 使用例子 ### 启动 Demo 启动[快速入门](quick-start.md)里的`math-game`。 ### stack ```bash $ stack demo.MathGame primeFactors Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 36 ms. ts=2018-12-04 01:32:19;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 @demo.MathGame.run() at demo.MathGame.main(MathGame.java:16) ``` ### 指定 Class 最大匹配数量 ```bash $ stack demo.MathGame primeFactors -m 1 Press Q or Ctrl+C to abort. Affect(class count:1 , method count:1) cost in 561 ms, listenerId: 5. ts=2022-12-25 21:07:07;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@b4aac2 @demo.MathGame.primeFactors() at demo.MathGame.run(MathGame.java:46) at demo.MathGame.main(MathGame.java:38) ``` ### 据条件表达式来过滤 ```bash $ stack demo.MathGame primeFactors 'params[0]<0' -n 2 Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 30 ms. ts=2018-12-04 01:34:27;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 @demo.MathGame.run() at demo.MathGame.main(MathGame.java:16) ts=2018-12-04 01:34:30;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 @demo.MathGame.run() at demo.MathGame.main(MathGame.java:16) Command execution times exceed limit: 2, so command will exit. You can set it with -n option. ``` ### 据执行时间来过滤 ```bash $ stack demo.MathGame primeFactors '#cost>5' Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 35 ms. ts=2018-12-04 01:35:58;thread_name=main;id=1;is_daemon=false;priority=5;TCCL=sun.misc.Launcher$AppClassLoader@3d4eac69 @demo.MathGame.run() at demo.MathGame.main(MathGame.java:16) ```