Files

6.3 KiB

Monitor methods in data aspect including return values{{}} , exceptions{{}} and parameters{{}} .

With the help of OGNL, you can easily check the details of variables when methods being invoked.

Parameters & Options

There are four different scenarios for watch command, which makes it rather complicated.

Name Specification
class-pattern pattern for the class name
method-pattern pattern for the method name
expression expression to watch, default value {params, target, returnObj}{{}}
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
[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);
  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. In the result of the watch command, the location{{}} information will be printed. There are three possible values for location{{}} : AtEnter{{}} , AtExit{{}} , and AtExceptionExit{{}} . Corresponding to the method entry, the method returns normally, and the method throws an exception.

Advanced:

Usage

Check the out parameters{{}} , this{{}} and return value{{}}

The expression to watch, default value {params, target, returnObj}{{}}

  • In the above result, the method is executed twice, the first result is location=AtExceptionExit{{}} , indicating that the method throws an exception, so returnObj{{}} is null
  • In the second result is location=AtExit{{}} , indicating that the method returns normally, so you can see that the result of returnObj{{}} is an ArrayList

Check in parameters{{}}

watch demo.MathGame primeFactors "{params,returnObj}" -x 2 -b{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

Compared to the previous check:

  • return value{{}} is null{{}} since it's -b{{}} .

Check before and after at the same time

watch demo.MathGame primeFactors "{params,target,returnObj}" -x 2 -b -s -n 2{{execute T2}}

F.Y.I

  • -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{{}} ).

Use -x{{}} to check more details

watch demo.MathGame primeFactors "{params,target}" -x 3{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

  • -x{{}} : Expand level of object (1 by default)

Use condition expressions to locate specific call

watch demo.MathGame primeFactors "{params[0],target}" "params[0]<0"{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

  • Only calls that meet the conditions will respond.

  • Watch Express{{}} single value can not be added '{}', and multiple values need to be added '{a, B, C}'.

  • condition Express{{}} cannot add '{}', you can use commas to separate subexpressions and take the last value of the expression to judge.

If there are other overloaded methods with the same name in the watch method, you can filter them by the following methods:

  • Filter according to parameter type

watch demo.MathGame primeFactors '{params, params[0].class.name}' 'params[0].class.name == "java.lang.Integer"'{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

  • Filter according to the number of parameters

watch demo.MathGame primeFactors '{params, params.length}' 'params.length==1'{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

Check exceptions{{}}

watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

  • -e{{}} : Trigger when an exception is thrown
  • throwExp{{}} : the exception object

Filter according to exception type or message:

watch demo.MathGame primeFactors '{params, throwExp}' '#msg=throwExp.toString(), #msg.contains("IllegalArgumentException")' -e -x 2{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

Filter by time cost

watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

  • #cost>200{{}} (ms{{}} ) filter out all invokings that take less than 200ms{{}} .

Check the field of the target object

  • target{{}} is the this{{}} object in java.

watch demo.MathGame primeFactors 'target'{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort

  • target.field_name{{}} : the field of the current object.

watch demo.MathGame primeFactors 'target.illegalArgumentCount'{{execute T2}}

Press Q{{exec interrupt}} or Ctrl+C{{exec interrupt}} to abort