You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
arthas/en/_sources/watch.md.txt

235 lines
6.8 KiB
Plaintext

6 years ago
watch
=====
6 years ago
Monitor methods in data aspect including `return values`, `exceptions` and `parameters`.
6 years ago
6 years ago
With the help of [OGNL](https://commons.apache.org/proper/commons-ognl/index.html), you can easily check the details of variables when methods being invoked.
6 years ago
### Parameters & Options
6 years ago
There are four different scenarios for `watch` command, which makes it rather complicated.
6 years ago
|Name|Specification|
|---:|:---|
|*class-pattern*|pattern for the class name|
|*method-pattern*|pattern for the method name|
6 years ago
|*expression*|expression to monitor|
|*condition-expression*|condition expression to filter|
|[b]|before method being invoked|
|[e]|when method encountering exceptions|
|[s]|when method exits normally|
|[f]|when method exits (either succeed or fail with exceptions)|
|[E]|turn on regex matching while the default is wildcard matching|
6 years ago
|[x:]|the depth to print the specified property with default value: 1|
F.Y.I
1. any valid OGNL expression as `"{params,returnObj}"` supported
2. there are four *watching* points: `-b`, `-e`, `-s` and `-f` (the first three are off in default while `-f` on);
6 years ago
3. at the *watching* point, Arthas will use the *expression* to evaluate the variables and print them out;
4. `in parameters` and `out parameters` are different since they can be modified within the invoked methods; `params` stands for `in parameters` in `-b`while `out parameters` in other *watching* points;
5. there are no `return values` and `exceptions` when using `-b`.
6 years ago
Advanced:
6 years ago
* [Critical fields in *expression*](advice-class.md)
* [Special usages](https://github.com/alibaba/arthas/issues/71)
6 years ago
* [OGNL official guide](https://commons.apache.org/proper/commons-ognl/language-guide.html)
### Usage
6 years ago
#### Start Demo
6 years ago
Start `arthas-demo` in [Quick Start](quick-start.md).
6 years ago
6 years ago
#### Check the `out parameters` and `return value`
6 years ago
6 years ago
```bash
$ watch demo.MathGame primeFactors "{params,returnObj}" -x 2
6 years ago
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 44 ms.
6 years ago
ts=2018-12-03 19:16:51; [cost=1.280502ms] result=@ArrayList[
6 years ago
@Object[][
6 years ago
@Integer[535629513],
],
@ArrayList[
@Integer[3],
@Integer[19],
@Integer[191],
@Integer[49199],
6 years ago
],
]
```
6 years ago
#### Check `in parameters`
6 years ago
6 years ago
```bash
$ watch demo.MathGame primeFactors "{params,returnObj}" -x 2 -b
6 years ago
Press Ctrl+C to abort.
6 years ago
Affect(class-cnt:1 , method-cnt:1) cost in 50 ms.
ts=2018-12-03 19:23:23; [cost=0.0353ms] result=@ArrayList[
6 years ago
@Object[][
6 years ago
@Integer[-1077465243],
6 years ago
],
null,
]
```
6 years ago
Compared to the previous *check*:
* `return value` is `null` since it's `-b`.
6 years ago
#### Check *before* and *after* at the same time
6 years ago
```bash
$ watch demo.MathGame primeFactors "{params,target,returnObj}" -x 2 -b -s -n 2
6 years ago
Press Ctrl+C to abort.
6 years ago
Affect(class-cnt:1 , method-cnt:1) cost in 46 ms.
ts=2018-12-03 19:29:54; [cost=0.01696ms] result=@ArrayList[
6 years ago
@Object[][
6 years ago
@Integer[1544665400],
],
@MathGame[
random=@Random[java.util.Random@522b408a],
illegalArgumentCount=@Integer[13038],
6 years ago
],
null,
]
6 years ago
ts=2018-12-03 19:29:54; [cost=4.277392ms] result=@ArrayList[
6 years ago
@Object[][
6 years ago
@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],
6 years ago
],
]
```
F.Y.I
6 years ago
* `-n 2`: threshold of execution times is 2.
* the first block of output is the *before watching* point;
* *the order of the output determined by the *watching* order itself (nothing to do with the order of the options `-b -s`).
6 years ago
6 years ago
#### Use `-x` to check more details
6 years ago
6 years ago
```bash
$ watch demo.MathGame primeFactors "{params,target}" -x 3
6 years ago
Press Ctrl+C to abort.
6 years ago
Affect(class-cnt:1 , method-cnt:1) cost in 58 ms.
ts=2018-12-03 19:34:19; [cost=0.587833ms] result=@ArrayList[
6 years ago
@Object[][
6 years ago
@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],
6 years ago
],
6 years ago
illegalArgumentCount=@Integer[13159],
6 years ago
],
]
```
6 years ago
* `-x`: Expand level of object (1 by default)
6 years ago
#### Use condition expressions to locate specific call
6 years ago
6 years ago
```bash
$ watch demo.MathGame primeFactors "{params[0],target}" "params[0]<0"
6 years ago
Press Ctrl+C to abort.
6 years ago
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],
6 years ago
]
```
#### Check `exceptions`
6 years ago
```bash
$ watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2
6 years ago
Press Ctrl+C to abort.
6 years ago
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)
,
6 years ago
]
```
6 years ago
* `-e`: Trigger when an exception is thrown
* `throwExp`: the exception object
6 years ago
6 years ago
#### Filter by time cost
```bash
$ watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2
6 years ago
Press Ctrl+C to abort.
6 years ago
Affect(class-cnt:1 , method-cnt:1) cost in 66 ms.
ts=2018-12-03 19:40:28; [cost=2112.168897ms] result=@ArrayList[
6 years ago
@Object[][
6 years ago
@Integer[2141897465],
],
@ArrayList[
@Integer[5],
@Integer[428379493],
6 years ago
],
]
```
6 years ago
* `#cost>200` (`ms`) filter out all invokings that take less than `200ms`.
6 years ago
6 years ago
#### Check the field of the target object
6 years ago
6 years ago
* `target` is the `this` object in java.
6 years ago
6 years ago
```bash
$ watch demo.MathGame primeFactors 'target'
6 years ago
Press Ctrl+C to abort.
6 years ago
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],
6 years ago
]
```
6 years ago
* `target.field_name`: the field of the current object.
6 years ago
6 years ago
```bash
6 years ago
$ watch demo.MathGame primeFactors 'target.illegalArgumentCount'
6 years ago
Press Ctrl+C to abort.
6 years ago
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]
6 years ago
```