1. Introduction to Arthas Quick Start
-
Alibaba's open source Java diagnostic tool, which can dynamically diagnose and debug Java applications at runtime
-
When you encounter the following similar problems and feel at a loss,
Arthas
we can help you solve them- Which jar package is this class loaded from? Why are various types of related Exception reported?
- Why is the code I changed not executed? Could it be that I didn't commit? Branching wrong?
- If you encounter a problem and cannot debug online, can you only republish it by adding a log?
- I encountered a problem with the data processing of a certain user online, but it also cannot be debugged online, and cannot be reproduced offline!
- Is there a global view into the health of the system?
- Is there any way to monitor the real-time running status of the JVM?
- How to quickly locate application hotspots and generate flame graphs?
- How to find an instance of a class directly from within the JVM?
-
github address: https://github.com/alibaba/arthas
-
Official website: https://arthas.aliyun.com/
-
Version: Arthas-3.6.7
-
Environmental description
Arthas
Support JDK 6+, support Linux/Mac/Windows, use command line interactive mode,- Provides rich
Tab
auto- completion functions to further facilitate problem location and diagnosis - It also supports the browser to directly access the corresponding ip+port, fixed port
8563
- By default, arthas only listens to 127.0.0.1, so if you want to connect remotely, use
--target-ip
the parameter to specify the IP of listen
-
quick use
- start up
java -jar arthas-boot.jar
- run log path
~/logs/arthas/arthas.log
- start up
-
quit arthas
- If you just exit the current connection and other clients are not affected, you can use the quit or exit command
- The arthas on the target process will continue to run, the port will remain open, and the next time you connect,
java -jar arthas-boot.jar
you can directly connect to it - If you want to completely exit arthas, you can execute the stop command
2. Arthas commonly used basic commands in actual combat
-
version - Outputs the version number of Arthas currently loaded by the target Java process
-
base64 - base64 encoding conversion, similar to the base64 command in linux
-
cat - print the contents of the file, similar to the cat command in linux
-
cls - clear the current screen area
-
echo - print parameters, similar to the echo command in linux
-
grep - match search, similar to the grep command in linux
-
help - view command help information
-
history - print command history
-
keymap - Arthas shortcut key list and custom shortcut keys
-
pwd - returns the current working directory, similar to the linux command
-
quit - exit the current Arthas client, other Arthas clients are not affected
-
reset - Reset the enhanced class, restore all the classes enhanced by Arthas, all enhanced classes will be reset when the Arthas server is closed
-
session - View information about the current session
-
stop - closes the Arthas server, all Arthas clients exit
-
tee - copy standard input to standard output and the specified file, similar to the tee command in linux
3. Arthas common JVM command case combat
(1) JVM related commands
- dashboard - real-time data panel of the current system
- heapdump - dump java heap, heap dump function similar to jmap command
- jvm - view information about the current JVM
- memory - view JVM memory information
- ognl - execute ognl expressions
- perfcounter - View the Perf Counter information of the current JVM
- sysenv - View JVM environment variables
- sysprop - View and modify system properties of the JVM
- thread - view the thread stack information of the current JVM
- vmoption - View and modify diagnostic-related options in the JVM
(2) dashboard - the real-time data panel of the current system
- Overall large panel
- Top - basic information about the thread
field | illustrate |
---|---|
id | Java-level thread ID |
name | thread name |
group | thread group name |
priority | Thread priority, a number between 1 and 10, the larger the priority, the higher the priority |
state | state of the thread |
cpu | thread cpu usage |
delta_time | Incremental CPU time of the thread running since the last sampling, the data format is seconds |
time | The total CPU time of thread running, the data format is分:秒 |
interupted | Whether the current thread is interrupted |
daemon | Is it a daemon daemon thread |
- Central - heap memory usage
field | illustrate |
---|---|
used | How much memory is currently used |
total | How much memory is allocated in total |
max | How much is the maximum used |
usage | Use ratio |
gc | garbage collector |
- Bottom - OS info, JDK version
(3) thead - view the thread stack information of the current JVM
-
Common parameters
- –all : Display all matching threads, the default is the first page of thread information
- -i: Set the sampling interval for cpu statistics, in milliseconds
thread -i 2000
- [ id ]: view the thread stack of the specified ID
thread 54
- -n : View the TopN threads with the highest CPU usage, if the value is -1, it means display all threads
thread -n 3
- -b : show blocked threads
thread -b
- --state : filter threads based on thread state
thread --state TIMED_WAITING
- Status type: NEW, RUNNABLE, TIMED_WAITING, WAITING, BLOCKED, TERMINATED
(4) heapdump - similar to the heap dump function of the jmap command
- Take a stack snapshot
heapdump /Users/mac/Desktop/heapdump.hprof
(5) jvm - view the information of the current JVM
(6) sysenv - view the environment variables of the JVM
(7) sysprop - view and modify the system properties of the JVM
- View all properties: sysprop
- View individual properties: sysprop java.version
- Modify a property: sysprop user.country CN
(8) sc - View the class information loaded by the JVM
- -d details, -f class attribute output
sc -d -f com.lixiang.controller.SpringTestController
(9) sm - view the method information of the loaded class
sm -d com.lixiang.controller.SpringTestController
(10) jad - decompile the source code of the specified loaded class
- decompile the entire class
jad com.lixiang.SpringTestApplication
- With
--source-only
options, it is possible to print only the source code
- Application Scenario
- View the business logic and method logic of a class
- Check whether the locally modified code takes effect online successfully
4. Arthas method diagnosis command case actual combat
(1) monitor - method execution monitoring
- For non-real-time response, the corresponding method needs to be called, so a web interface request needs to be triggered
- Monitor the execution times, success times, failure times, time-consuming and other information of the specified method in a period of time
monitor -c 2 com.lixiang.controller.SpringTestController query
(2) stack - output the call path where the current method is called. There are many paths for a method to be executed. If you don’t know where this method is executed, you can use
- the case
stack com.lixiang.controller.SpringTestController query
(3) trace - method internal call, output time-consuming on each node on the method path, and locate performance problems caused by high RT
-
Only the call link of the first-level method can be traced each time
-
Case output all methods
trace com.lixiang.controller.SpringTestController *
ts
: Timestamp, indicating the log record time, the value of this field is 5:48:46 PM on March 26, 2023.thread_name
: Thread name, indicating the thread name currently executing the log record, the value of this field is http-nio-8080-exec-10.id
: Thread ID, indicating the thread ID currently executing the log record, the value of this field is 20.is_daemon
: Whether it is a daemon thread, the value of this field is true, indicating that the thread is a daemon thread.priority
: Thread priority, the value of this field is 5, indicating that the thread priority is 5.TCCL
: Thread context class loader, indicating that the context class loader of the current thread is TomcatEmbeddedWebappClassLoader.
- By default, trace will not include function calls in jdk. If you want to trace functions in jdk, you need to explicitly set --skipJDKMethod false
- Right now
trace --skipJDKMethod false com.lixiang.controller.SpringTestController *
(4) watch - method execution data observation
- Application scenario: view method call stack, parameter entry, return value, etc. debugging
- The default watch expression, the default value is
{params, target, returnObj}
- You can also specify the observed return value
watch com.lixiang.controller.SpringTestController * {params,returnObj}
-
Expand the specific value in the -x parameter to indicate the traversal depth, which can be adjusted to print specific parameters and result content. The default value is 1, and the maximum is 4.
-
watch com.lixiang.controller.SpringTestController * {params,returnObj} -x 4
5. Arthas Online Debugging Case Actual Combat
- The java program is running in the production environment, and online debugging is required, without restarting the JVM program, dynamically adjusting, printing parameters or modifying the internal logic of the method.
- Steps and environment preparation
- The first step: jad decompiles the bytecode file into source code
- Step 2: mc compiles the source code into a bytecode file in memory
- Step 3: redefine executes the newly generated bytecode file in memory