操作步驟
-
下載arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
-
啓動arthas
leiting@leitingdeMBP test % java -jar arthas-boot.jar [INFO] arthas-boot version: 3.5.4 [INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER. * [1]: 68801 /Applications/美事.app/Contents/Resources/YCSDK/sdk.jar [2]: 67301 org.jetbrains.jps.cmdline.Launcher [3]: 68772 arthas-boot.jar [4]: 66724 com.ting.log4j.Log4jBug2Application [5]: 66152 org.jetbrains.jps.cmdline.Launcher
-
選擇需要調試的進程
4 [INFO] local lastest version: 3.5.1, remote lastest version: 3.5.4, try to download from remote. [INFO] Start download arthas from remote server: https://arthas.aliyun.com/download/3.5.4?mirror=aliyun [INFO] File size: 12.72 MB, downloaded size: 2.49 MB, downloading ... [INFO] File size: 12.72 MB, downloaded size: 5.22 MB, downloading ... [INFO] File size: 12.72 MB, downloaded size: 7.91 MB, downloading ... [INFO] File size: 12.72 MB, downloaded size: 10.60 MB, downloading ... [INFO] Download arthas success. [INFO] arthas home: /Users/leiting/.arthas/lib/3.5.4/arthas [INFO] Try to attach process 66724 [INFO] Attach process 66724 success. [INFO] arthas-client connect 127.0.0.1 3658 ,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki https://arthas.aliyun.com/doc tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html version 3.5.4 main_class com.ting.log4j.Log4jBug2Application pid 66724 time 2021-12-21 10:12:55 [arthas@66724]$
-
查看命令
[arthas@66724]$ help NAME DESCRIPTION help Display Arthas Help auth Authenticates the current session keymap Display all the available keymap for the specified connection. sc Search all the classes loaded by JVM sm Search the method of classes loaded by JVM classloader Show classloader info jad Decompile class getstatic Show the static field of a class monitor Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. stack Display the stack trace for the specified class and method thread Display thread info, thread stack trace Trace the execution time of specified method invocation. watch Display the input/output parameter, return object, and thrown exception of specified method invocation tt Time Tunnel jvm Display the target JVM information perfcounter Display the perf counter information. ognl Execute ognl expression. mc Memory compiler, compiles java files into bytecode and class files in memory. redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...) retransform Retransform classes. @see Instrumentation#retransformClasses(Class...) dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info. dump Dump class byte array from JVM heapdump Heap dump options View and change various Arthas options cls Clear the screen reset Reset all the enhanced classes version Display Arthas version session Display current session information sysprop Display, and change the system properties. sysenv Display the system env. vmoption Display, and update the vm diagnostic options. logger Print logger info, and update the logger level history Display command history cat Concatenate and print files base64 Encode and decode using Base64 representation echo write arguments to the standard output pwd Return working directory name mbean Display the mbean information grep grep command for pipes. tee tee command for pipes. profiler Async Profiler. https://github.com/jvm-profiling-tools/async-profiler vmtool jvm tool stop Stop
-
退出
[arthas@66724]$ stop Resetting all enhanced classes ... Affect(class count: 0 , method count: 0) cost in 1 ms, listenerId: 0 Arthas Server is going to shutdown... [arthas@66724]$ session (dcf4629c-5414-42a1-9de3-b9bc49acd06d) is closed because server is going to shutdown. leiting@leitingdeMBP test %
小技巧
- arthas的默認端口是3658,假設arthas進程已經存在,需要重新進入命令端,可以使用telnet命令
[work(leiting04)@tjtx167-79-50 ~]$ telnet 127.0.0.1 3658
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version 3.5.3
main_class
pid 1321
time 2021-12-21 10:43:46
[arthas@1321]$
- 默認情況下在啓動arthas的過程中是需要選擇進程的,但如果已經知道進程名,則在啓動時可以加啓動參數,指定進程,具體流程如下
比如,通過JPS命令,查看進程
[work(leiting04)@tjtx167-79-50 ~]$ jps
415746 Jps
1321 Main
1021 magici-agent-1.0-SNAPSHOT.jar
[work(leiting04)@tjtx167-79-50 ~]$
本次需要調試的進程是 1321 Main,則可以在啓動參數中直接指定
[work(leiting04)@tjtx167-79-50 arthas-bin]$ ./as.sh --select Main
Arthas script version: 3.5.3
[INFO] JAVA_HOME: /opt/soft/jdk/jdk1.8.0_231
Arthas home: /home/work/arthas-bin
Calculating attach execution time...
Attaching to 1321 using version /home/work/arthas-bin...
real 0m3.118s
user 0m0.286s
sys 0m0.035s
Attach success.
telnet connecting to arthas server... current timestamp is 1640067233
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki https://arthas.aliyun.com/doc
tutorials https://arthas.aliyun.com/doc/arthas-tutorials.html
version 3.5.3
main_class
pid 1321
time 2021-12-21 14:13:52
[arthas@1321]$