This is an alibaba open source Java tool for real-time diagnosis of running applications. I used it briefly, and it turned to powder

Installation is simple

Windows platform, download this JAR package

Alibaba. Making. IO/arthas/arth…

One-click installation for non-Windows platforms

$ curl -L https://alibaba.github.io/arthas/install.sh | sh
Copy the code

To test Arthas, download the test program

Alibaba. Making. IO/arthas/arth…

Run it

$ java -jar arthas-demo.jar
Copy the code

Then run Arthas

$ java -jar arthas-boot.jar
Copy the code

Automatically outputs the currently running Java Process to the console

$ java -jar arthas-boot.jar
[INFO] arthas-boot version: 3.1.7
[INFO] Found existing java process, please choose one and hit RETURN.
* [1]: 67445 org.jetbrains.jps.cmdline.Launcher
  [2]: 79685 arthas-demo.jar
  [3]: 19911
  [4]: 50879 org.jetbrains.jps.cmdline.Launcher
Copy the code

If you want to attach to a process, type the number and press Enter (some dependency packages will be downloaded the first time). If successful, the following information will be displayed

[INFO] Try to attach process 79685
[INFO] Attach process 79685 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ' '-.. -'|'--' | / O \ '. -'|. -. | |'--'. '. | | | -. | | - | `. ` - | | | | | | \ \ | | | | | | | | | |. -'| ` --' `--'`--' The '-'   `--'` -'  `--'`--' `--'` -- -- -- -- --'https://alibaba.github.io/arthas wiki tutorials https://alibaba.github.io/arthas/arthas-tutorials version 3.1.7 pid 79685 time 2020-03-31 22:12:05 [arthas@79685]$Copy the code

This process can then be mapped using the commands Arthas provides

Thread command

Prints thread information for this process

To see what a thread is doing at the moment, for example, main, enter the ID of the first column, for example, 1, thread 1, and output the call stack of main

[arthas@79685]$ thread 1
"main" Id=1 TIMED_WAITING
    at java.lang.Thread.sleep(Native Method)
    at java.lang.Thread.sleep(Thread.java:340)
    at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
    at demo.MathGame.main(MathGame.java:17)
Copy the code

Jad command

This is awesome. Decompile the output directly. Follow the command with the name of the fully qualified class in the program that you want to decompile

[arthas@79685]$ jad demo.MathGame
Copy the code

Watch command

Real-time monitoring of specified method data, such as return values, incoming arguments, and exceptions thrown

Look here for the return value of the method primeFactors

[arthas@79685]$ watch demo.MathGame primeFactors returnObj
Copy the code

Monitor method parameters and return values before and after the call:

watch demo.MathGame primeFactors "{params,returnObj}" -x 2 -b -s -n 2
Copy the code

-n indicates the number of times to monitor. Fill in 2 here to end the monitoring before and after one call. [] (p1-jj.byteimg.com/tos-cn-i-t2… 07a4d8? w=1676&h=742&f=png&s=396972)

Monitor calls that take too long (the following example outputs only methods that take more than 100ms) :

[arthas@79685]$ watch demo.MathGame primeFactors '{params, returnObj}' '#cost>100' -x 2
Copy the code

Hot update code

This goes even further, updating code in real time to a running application

For the convenience of the experiment, I will first test the code in the program

[arthas@79685]$ jad --source-only demo.MathGame  > /Users/mac/MathGame.java
Copy the code

System.out.println(“print method in”);

  1. The MC command compiles Java to class

The -d command specifies the directory for storing the class file

$[arthas@79685] mc /Users/mac/MathGame.java -d /Users/mac/
Copy the code

Success message

Memory compiler output:
/Users/mac/demo/MathGame.class
Affect(row-cnt:1) cost in 110 ms.
Copy the code
  1. Hot update
[arthas@79685]$ redefine /Users/mac/demo/MathGame.class
redefine success, size: 1
Copy the code

The test program outputs the added information

But there are two limitations

  • New fields or methods are not allowed
  • The running method does not take effect without exit

Stop command

Disconnect the process normally

conclusion

First contact with this tool, I found a lot of very useful functions, check thread status, decomcompile real-time source code, monitoring method parameters and return values, exceptions, hot update code, daily debugging Java program to bring considerable help, here is just a few of its functions. More accessible to alibaba. Making. IO/arthas