arthas-watch
Watch method execution result, parameters, and exceptions
TLDR
Observe the first parameter and return value of method, and expand the nested attributes of the object to 4 levels
When the value of the first parameter of the method is 5, the second parameter and return value are output, and the object is expanded 4 layers
When the method returns or an exception occurs, observe the count property of the second parameter
SYNOPSIS
watch [<class-pattern>] [<method-pattern>] <express> [<condition-express>] [options]
PARAMETERS
class-pattern
Regular expression for target class name, e.g., com.example.*
method-pattern
Regular expression for method name, e.g., add* or *
express
OGNL expression to evaluate, e.g., '{params,returnObj,exceptionMsg}', #this, #ret
condition-express
Optional OGNL condition to filter, e.g., '#cost>100' or '@java.lang.String@isEmpty(params[0])'
-b
Watch before method invocation (default: after return)
-e
Watch on exception throw
-x <max>
Max string length for params/return/exception (default: 512)
-l
Include source line number
-t
Print timestamp
-n <times>
Watch specified times then quit (default: infinite)
-s
Watch successful calls only (ignore exceptions)
-z
Enable regex for class/method patterns
-i <ms>
Watch interval in milliseconds
-v
Verbose mode for detailed output
-c <classloader>
Specify classloader hash or name
DESCRIPTION
arthas-watch, commonly known as the watch command in Arthas, is a dynamic monitoring tool for Java applications. It observes method invocations by evaluating OGNL expressions at key points: before method calls (params), after successful returns (returnObj), or after exceptions (exceptionMsg). Users specify class and method patterns with regex support, optional conditions like execution time (#cost > 100ms), and customizable output.
This command aids in debugging live services without restarts, profiling performance, inspecting arguments/returns, and tracing issues. Attach Arthas to a PID via as.sh PID, then run watch in the shell. Output includes thread name, cost, parameters, and results, truncated by max length. Ideal for microservices, Spring Boot apps, and high-load systems.
Key strengths: real-time, non-intrusive, condition-based filtering, timestamp/line number options. Combine with trace for entry/exit or monitor for stats.
CAVEATS
Not a standalone Linux binary; requires Arthas shell attached to Java PID. Supports JDK 6+, but regex/OGNL may impact performance on hot methods. Use stopWatch to halt.
BASIC EXAMPLE
watch demo.MathGame add '{params,returnObj}' '#ret>0' -x 2 -n 5
Watches add method 5 times if return >0, limits output.
COST MONITORING
watch com.example.* * '{@User@currentTimeMillis() - #this.joinPoint.start}'
Measures precise execution cost using joinPoint.
HISTORY
Introduced in Arthas 3.1.0 (2018) by Alibaba. Evolved with OGNL support, classloader targeting; now at v3.7+ with WebSocket enhancements for cloud diagnostics.


