用arthas的watch方法观察执行方法的输入输出
watch 的参数比较多,主要是因为它能在 4 个不同的场景观察对象
| 参数名称 | 参数说明 |
|---|---|
| class-pattern | 类名表达式匹配 |
| method-pattern | 方法名表达式匹配 |
| express | 观察表达式 |
| condition-express | 条件表达式 |
| [b] | 在方法调用之前观察 |
| [e] | 在方法异常之后观察 |
| [s] | 在方法返回之后观察 |
| [f] | 在方法结束之后(正常返回和异常返回)观察 |
| [E] | 开启正则表达式匹配,默认为通配符匹配 |
| [x:] | 指定输出结果的属性遍历深度,默认为 1 |
这里重点要说明的是观察表达式,观察表达式的构成主要由 ognl 表达式组成,所以你可以这样写"{params,returnObj}",只要是一个合法的 ognl 表达式,都能被正常支持。
观察的维度也比较多,主要体现在参数 advice 的数据结构上。Advice 参数最主要是封装了通知节点的所有信息。请参考表达式核心变量中关于该节点的描述。
- 特殊用法请参考:https://github.com/alibaba/arthas/issues/71
- OGNL表达式官网:https://commons.apache.org/proper/commons-ognl/language-guide.html
特别说明:
- watch 命令定义了4个观察事件点,即
-b方法调用前,-e方法异常后,-s方法返回后,-f方法结束后 - 4个观察事件点
-b、-e、-s默认关闭,-f默认打开,当指定观察点被打开后,在相应事件点会对观察表达式进行求值并输出 - 这里要注意
方法入参和方法出参的区别,有可能在中间被修改导致前后不一致,除了-b事件点params代表方法入参外,其余事件都代表方法出参 - 当使用
-b时,由于观察事件点是在方法调用前,此时返回值或异常均不存在
使用参考
观察方法出参和返回值
|
$ watch demo.MathGame primeFactors "{params,returnObj}" -x 2
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 44 ms.
|
|
|
ts=2018-12-03 19:16:51; [cost=1.280502ms] result=@ArrayList[
|
|
|
@Object[][
|
|
|
@Integer[535629513],
|
|
|
],
|
|
|
@ArrayList[
|
|
|
@Integer[3],
|
|
|
@Integer[19],
|
|
|
@Integer[191],
|
|
|
@Integer[49199],
|
|
|
],
|
|
|
]
|
观察方法入参
|
$ watch demo.MathGame primeFactors "{params,returnObj}" -x 2 -b
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 50 ms.
|
|
|
ts=2018-12-03 19:23:23; [cost=0.0353ms] result=@ArrayList[
|
|
|
@Object[][
|
|
|
@Integer[-1077465243],
|
|
|
],
|
|
|
null,
|
|
|
]
|
- 对比前一个例子,返回值为空(事件点为方法执行前,因此获取不到返回值)
同时观察方法调用前和方法返回后
|
$ watch demo.MathGame primeFactors "{params,target,returnObj}" -x 2 -b -s -n 2
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 46 ms.
|
|
|
ts=2018-12-03 19:29:54; [cost=0.01696ms] result=@ArrayList[
|
|
|
@Object[][
|
|
|
@Integer[1544665400],
|
|
|
],
|
|
|
@MathGame[
|
|
|
random=@Random[java.util.Random@522b408a],
|
|
|
illegalArgumentCount=@Integer[13038],
|
|
|
],
|
|
|
null,
|
|
|
]
|
|
|
ts=2018-12-03 19:29:54; [cost=4.277392ms] result=@ArrayList[
|
|
|
@Object[][
|
|
|
@Integer[1544665400],
|
|
|
],
|
|
|
@MathGame[
|
|
|
random=@Random[java.util.Random@522b408a],
|
|
|
illegalArgumentCount=@Integer[13038],
|
|
|
],
|
|
|
@ArrayList[
|
|
|
@Integer[2],
|
|
|
@Integer[2],
|
|
|
@Integer[2],
|
|
|
@Integer[5],
|
|
|
@Integer[5],
|
|
|
@Integer[73],
|
|
|
@Integer[241],
|
|
|
@Integer[439],
|
|
|
],
|
|
|
]
|
- 参数里
-n 2,表示只执行两次 - 这里输出结果中,第一次输出的是方法调用前的观察表达式的结果,第二次输出的是方法返回后的表达式的结果
- 结果的输出顺序和事件发生的先后顺序一致,和命令中
-s -b的顺序无关
调整-x的值,观察具体的方法参数值
|
$ watch demo.MathGame primeFactors "{params,target}" -x 3
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 58 ms.
|
|
|
ts=2018-12-03 19:34:19; [cost=0.587833ms] result=@ArrayList[
|
|
|
@Object[][
|
|
|
@Integer[47816758],
|
|
|
],
|
|
|
@MathGame[
|
|
|
random=@Random[
|
|
|
serialVersionUID=@Long[3905348978240129619],
|
|
|
seed=@AtomicLong[3133719055989],
|
|
|
multiplier=@Long[25214903917],
|
|
|
addend=@Long[11],
|
|
|
mask=@Long[281474976710655],
|
|
|
DOUBLE_UNIT=@Double[1.1102230246251565E-16],
|
|
|
BadBound=@String[bound must be positive],
|
|
|
BadRange=@String[bound must be greater than origin],
|
|
|
BadSize=@String[size must be non-negative],
|
|
|
seedUniquifier=@AtomicLong[-3282039941672302964],
|
|
|
nextNextGaussian=@Double[0.0],
|
|
|
haveNextNextGaussian=@Boolean[false],
|
|
|
serialPersistentFields=@ObjectStreamField[][isEmpty=false;size=3],
|
|
|
unsafe=@Unsafe[sun.misc.Unsafe@2eaa1027],
|
|
|
seedOffset=@Long[24],
|
|
|
],
|
|
|
illegalArgumentCount=@Integer[13159],
|
|
|
],
|
|
|
]
|
-x表示遍历深度,可以调整来打印具体的参数和结果内容,默认值是1。
条件表达式的例子
|
$ watch demo.MathGame primeFactors "{params[0],target}" "params[0]<0"
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 68 ms.
|
|
|
ts=2018-12-03 19:36:04; [cost=0.530255ms] result=@ArrayList[
|
|
|
@Integer[-18178089],
|
|
|
@MathGame[demo.MathGame@41cf53f9],
|
|
|
]
|
- 只有满足条件的调用,才会有响应。
观察异常信息的例子
|
$ watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 62 ms.
|
|
|
ts=2018-12-03 19:38:00; [cost=1.414993ms] result=@ArrayList[
|
|
|
@Integer[-1120397038],
|
|
|
java.lang.IllegalArgumentException: number is: -1120397038, need >= 2
|
|
|
at demo.MathGame.primeFactors(MathGame.java:46)
|
|
|
at demo.MathGame.run(MathGame.java:24)
|
|
|
at demo.MathGame.main(MathGame.java:16)
|
|
|
,
|
|
|
]
|
-e表示抛出异常时才触发- express中,表示异常信息的变量是
throwExp
按照耗时进行过滤
|
$ watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 66 ms.
|
|
|
ts=2018-12-03 19:40:28; [cost=2112.168897ms] result=@ArrayList[
|
|
|
@Object[][
|
|
|
@Integer[2141897465],
|
|
|
],
|
|
|
@ArrayList[
|
|
|
@Integer[5],
|
|
|
@Integer[428379493],
|
|
|
],
|
|
|
]
|
#cost>200(单位是ms)表示只有当耗时大于200ms时才会输出,过滤掉执行时间小于200ms的调用
观察当前对象中的属性
如果想查看方法运行前后,当前对象中的属性,可以使用target关键字,代表当前对象
|
$ watch demo.MathGame primeFactors 'target'
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 52 ms.
|
|
|
ts=2018-12-03 19:41:52; [cost=0.477882ms] result=@MathGame[
|
|
|
random=@Random[java.util.Random@522b408a],
|
|
|
illegalArgumentCount=@Integer[13355],
|
|
|
]
|
然后使用target.field_name访问当前对象的某个属性
|
$ watch demo.MathGame primeFactors 'target.illegalArgumentCount'
|
|
|
Press Ctrl+C to abort.
|
|
|
Affect(class-cnt:1 , method-cnt:1) cost in 67 ms.
|
|
|
ts=2018-12-03 20:04:34; [cost=131.303498ms] result=@Integer[8]
|
|
|
ts=2018-12-03 20:04:35; [cost=0.961441ms] result=@Integer[8]
|
用arthas的watch方法观察执行方法的输入输出的更多相关文章
- java反射获取类的类名、属性名、属性类型、方法、执行方法、构造函数
public class Demo02 { @SuppressWarnings("all") public static void main(String[] args) thro ...
- Java 多态方法构造器执行方法
我们参考下面这个例子: 读者可以提前考虑一下,这段程序的输出会是什么. public class Polymorphism { /** * 创建一个类A * 该类中有一个方法draw,以及一个构造方法 ...
- Spring AOP执行方法
execution(* springinaction.springidol.Instrument.play(..)) * 代表返回为任意类型 springinaction.springidol.I ...
- 事件之onTouch方法的执行过程 及和 onClick执行发生冲突的解决办法
转载:http://blog.csdn.net/jiangwei0910410003/article/details/17504315#quote 博主推荐: 风萧兮兮易水寒,“天真”一去兮不复还.如 ...
- Android中onTouch方法的执行过程以及和onClick执行发生冲突的解决办法
$*********************************************************************************************$ 博主推荐 ...
- 比较一下以“反射”和“表达式”执行方法的性能差异
由于频繁地使用反射会影响性能,所以ASP.NET MVC采用了表达式树的方式来执行目标Action方法.具体来说,ASP.NET MVC会构建一个表达式来体现针对目标Action方法的执行,并且将该表 ...
- 浅谈iOS开发中方法延迟执行的几种方式
Method1. performSelector方法 Method2. NSTimer定时器 Method3. NSThread线程的sleep Method4. GCD 公用延迟执行方法 - (vo ...
- struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
struts2 action配置时 method 省略不写 默认执行方法是父类ActionSuppot中的execute()方法
- 从源码角度看finish()方法的执行流程
1. finish()方法概览 首先我们来看一下finish方法的无参版本的定义: /** * Call this when your activity is done and should be c ...
随机推荐
- vue-preview vue图片预览插件+缩略图样式
一.安装 npm i vue-preview -S 二.main.js中 导入组件 //vue-preview 开始 import VuePreview from 'vue-preview'; // ...
- Linux环境下搭建JDK环境
yum安装 傻瓜式安装,记录几条命令 1.查看可安装的jdk版本(需要安装yum): yum -y list java* 2.安装jdk yum install -y java-1.8.0-openj ...
- 1、nio说明 和 对比bio
nio和bio的区别 bio: 面向流的. 单向的. 阻塞的,这也是b这个的由来. nio: 面向块的.(buffer) 双向的. 非阻塞的.同步的编程方式.是一种select模型 nio编程的常规步 ...
- Redhat Linx使用Centos yum源
一.故障现象: 在安装了Read linux后,使用yum安装软件,出现如下提示:[root@localhost~]# yum install xxxLoaded plugins: product-i ...
- [PHP] socket客户端时的超时问题
连接socket分为连接超时和读取超时 $sock=stream_socket_client("www.google.com:80", $errno,$errstr,2); ...
- mongodb基本安装
这次搞搞NOSQL, 但最简单的MONGODB安装,还是要作点配置的. 一,安装网址: https://www.mongodb.com/download-center/community?jmp=na ...
- javascript---简介的切换图片效果。
<!--切换图片--> <img src="img/9.gif" alt="" id="img"> <butt ...
- Django中的sql注入
Django中防止SQL注入的方法 方案一总是使用Django自带的数据库API.它会根据你所使用的数据库服务器(例如PostSQL或者MySQL)的转换规则,自动转义特殊的SQL参数.这被运用到了整 ...
- Swoole如何处理高并发
有需要学习交流的友人请加入swoole交流群的咱们一起,有问题一起交流,一起进步!前提是你是学技术的.感谢阅读! 点此加入该群 swoole如何处理高并发 ①Reactor模型介绍 IO复用异步非阻塞 ...
- Spring Boot MVC api返回的String无法关联到视图页面
1:问题 使用 @Restcontroller 返回值定义为String 时 无法返回具体的页面 @RestController public class HelloController { @Get ...