preface

Arthas is an open source Java diagnostic tool for Alibaba that developers love. The official pitch is to help us solve problems online. Due to different environments, problems often occur when users actually use our test environment cannot reproduce. Our local debugging runs normally, but problems occur in the test environment. Arthas can help us quickly locate method calls, quickly reproduce problems, and resolve them in a timely manner. Arthas’s fire map and Trace commands also help us quickly detect the parts of the method that take the most time, which helps us optimize our code efficiency. Hot compilation helps us update our code faster while debugging, reducing compile wait times.

The document

​ GitHub

The official documentation

Basic tutorial

The installation

  1. Download path

    Arthas.aliyun.com/arthas-boot…

  2. download

    # Recommended method -> download the JAR package to the local direct execution
    curl -O https://arthas.aliyun.com/arthas-boot.jar
    
    # Linux/Unix/MAC can be installed with one click and then started with./as.sh
    curl -L https://arthas.aliyun.com/install.sh | sh
    Copy the code
  3. Printing Help Information

    java -jar arthas-boot.jar -h
    Copy the code
  4. Start the

    java -jar arthas-boot.jar
    You can add parameters to modify the tenlet and HTTP ports when the port is occupied
    java -jar arthas-boot.jar --telnet-port 9998 --http-port -1
    Copy the code

Common commands

  1. Dashboard Displays the real-time system data panel

    Display 5 times every 1000ms
    dashboard -i 1000 -n 5
    Copy the code
  2. Thread Displays information about the current thread and the thread stack

    # list the 3 busiest threads in 5000ms
    thread -i 5000 -n 3 
    # view the thread information on the first page
    thread
    # check all thread information
    thread -all
    # check for blocked threads (currently synchronized only)
    thread -b
    # view the specified thread status
    thread --state WAITING
    # view the specified thread
    thread 3
    Copy the code
  3. Jad decompiles the code for the target method or class

    # decompile the specified class
    jad --source-only com.example.demo.controller.TestController 
    Decompile a method in a specified class
    jad --source-only com.example.demo.controller.TestController a
    Copy the code
  4. mc

    Memory Compiler Java generates classes

    -d specifies the path to compile the Java file
    mc E:/Downloads/Ding/demo/src/main/java/com/example/demo/controller/TestController.java -d E:/Downloads/Ding/demo/src/main/java/
    Copy the code
  5. retransform

    Load an external.class file. Overrides classes loaded by the JVM

    Limitations:

    • New fields/methods are not allowed
    • A running function cannot take effect without exit
    Replace loaded classes in the JVM with compiled class files
    retransform  E:/Downloads/Ding/demo/src/main/java/com/example/demo/controller/TestController.class
    # Retransform Entry
    retransform -l
    # delete specified retransform entry
    retransform -d 1
    Delete all retransform entries
    retransform --deleteAll
    # show trigger reTransform (use the latest transform if there is one in entry, restore if there is none)
    retransform --classPattern com.example.demo.controller.TestController
    Copy the code

    Eliminate retransform effects:

    • Delete the corresponding Retransform entry of this class. (Generally, the class will be restored after the corresponding entry is deleted directly. If a class is retransformed multiple times, you may need to run the retransform command to eliminate this.)

    • Retransform is triggered

      If you do not clear all reTransform entries and retransform is triggered, arthas stops and the classes retransformed are still in effect.

  6. ognl

    The document

    Call a static function
    ognl '@com.example.demo.controller.TestController@staticVoid()'
    Call the static field of the static class
    ognl '@com.example.demo.controller.TestController@LOOP_COUNT'
    Execute a multiline expression, assign to a temporary variable, and return a List:
    ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #vaue2}'
    Copy the code
  7. monitor

    Monitoring items instructions
    timestamp The time stamp
    class Java classes
    method Methods (constructors, common methods)
    total Call the number
    success Number of successful
    fail Number of failures
    rt The average RT
    fail-rate Failure rate
    # monitoring specified method of execution record monitor - c 5 com. Example. Demo. Controller. TestController # monitoring according to the ognl expressions when adding a filter For example, the following said the first parameter to "1" to enter the monitor  monitor -c 5 com.example.demo.controller.TestController a 'params[0] == "1"'Copy the code
  8. ✨watch Views the return value of the specified function

    The parameter name Parameters that
    class-pattern Class name expression matches
    method-pattern Method name expression matches
    express Observation expression
    condition-express Conditional expression
    [b] inBefore method callTo observe the
    [e] inAfter method exceptionTo observe the
    [s] inAfter the method returnsTo observe the
    [f] inAfter the method is done(Normal return and abnormal return) observation
    [E] Enable regular expression matching. The default value is wildcard matching
    [x:] Specifies the depth of property traversal for the output, which defaults to 1
    Check the method input parameter
    watch com.example.demo.controller.TestController a  '{params}' -x 3 -b
    
    {method output parameter, return value, exception thrown}
    # -v indicates that the value and execution result of Condition Express will be printed for easy confirmation
    # -n 5 Indicates that the command is executed only for five times
    # -x 3 indicates that the depth of the parameter is 3. If this parameter is not set, the default depth is 1
    Params. Length > 0 specifies whether to enter watch if the pass parameter is not null
    watch com.example.demo.controller.TestController a '{params,returnObj,throwExp}' -v -n 5 -x 3 'params.length > 0'
    
    Enter watch when the ID of the second argument is 1
    watch com.example.demo.controller.TestController a '{params,returnObj,throwExp}' -v -n 5 -x 3 'params[1].id == 1'
    
    View the arguments before and after the method call
    # -b Observe before method call
    # -s Observe after method call
    watch com.example.demo.controller.TestController a '{params,returnObj,throwExp}' -v -n 5 -x 3 -b -s 'params[1].id == 1'
    
    Filter by time. Enter the watch when the time is greater than 6000ms
    watch com.example.demo.controller.TestController a '{params,returnObj,throwExp}' -v -n 5 -x 3 '#cost > 6000'
    
    View the current object properties
    watch com.example.demo.controller.TestController a  'target'
    
    Check the constant value of the current object
    watch com.example.demo.controller.TestController a  'target.LOOP_COUNT'
    Copy the code
  9. ✨ profiler

    Use async-Profiler to generate flame maps and find ways to take a long time

    # start statistics
    profiler start
    # check sample count (optional)
    profiler getSamples
    # End statistics
    profiler stop
    Copy the code
  10. ✨ trace

    Displays the execution time of each method

      # trace a single method
       trace com.example.demo.controller.TestController a -v -n 5 --skipJDKMethod false '1 = = 1'
       
       # trace multiple methods
       trace -E com.example.demo.controller.TestController a|b|c -n 5 -v --skipJDKMethod false '1 = = 1'
    Copy the code
  11. ✨tt TimeTunnel

    # record
    tt -t com.example.demo.controller.TestController a -n 1
    
    # Trigger again
    tt -p -i 1000
    
    Get the return value of the previously executed argument, etc
    tt  -w '{method.name,params,returnObj,throwExp}' -x 3 -i 1000
    
    Trigger three times periodically
    tt -p --replay-times 3 --replay-interval 2000 -i 1000
    
    # in the record, the springMVC RequestMappingHandlerAdapter# invokeHandlerMethod request (because this object can be through getApplicationContext ()) to get to the corresponding values
    tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod    
    
    Get the ApplicationContext of the Spring container based on the monitored object
    tt -i 1000 -w 'target.getApplicationContext()'
    
    Extract any Bean from the container!
    tt -i 1000 -w 'target.getApplicationContext().getBean("testController")'
    
    Execute the method in the Bean
    tt -i 1000 -w 'target.getApplicationContext().getBean("testController").updateUserInfo(null)'
    # execute method in Bean (pass parameter as object)
    tt -i 1000 -w 'target.getApplicationContext().getBean("testController").updateUserInfo((#user = new com.example.demo.pojo.User(), #user.setId(1), #user.setName("testName") , #user.setBook((#book = new com.example.demo.pojo.Book(), #book.setBookId(2), #book)), #user))'
    
    Add the class that gets the ApplicationContext
    ognl -x 3 '#[email protected]@context,#springContext.getBean("testController").print Mail()'
    
    Check the configuration item information
    ognl -x 3 '#[email protected]@context,#springContext.getEnvironment().getProperty("t est.value")'
    Copy the code

other

  1. IDEA Plug-in Installation

    Idea plugin address

    Plug-in usage

  2. Documents/Community

    User stories

    Some special uses