当前位置:网站首页>Online debugging tool Arthas Foundation
Online debugging tool Arthas Foundation
2022-06-13 09:10:00 【Q z1997】
summary
Arthas( Alsace ) What can I do for you ?

Arthas yes Alibaba Open source Java Diagnostic tools , Loved by developers .
When you encounter the following similar problems and are at a loss ,Arthas Can help you solve :
- Where does this class come from jar Package loaded ? Why do you report all kinds of related Exception?
- Why didn't I change the code to ? I didn't commit? The branch is wrong ?
- I can't get online when I have a problem debug, Can't we just add logs and redistribute them ?
- There is a problem with the data processing of a user online , But online also can't debug, It can't be reproduced offline !
- Is there a global perspective to see the health of the system ?
- Is there any way to monitor JVM The real-time running state of ?
- How to quickly locate the hot spot of application , Generate flame chart ?
Operating environment requirements
Arthas Support JDK 6+, Support Linux/Mac/Windows, Use command line interaction mode , At the same time, it provides rich Tab Automatic completion function , Further facilitate problem location and diagnosis .
Fast installation
download arthas-boot.jar, And then use java -jar Way to start :
command
curl -O https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
notes : In operation section 2 Before the command , Run one first java The process is in memory , Otherwise, there will be no java Process error .
Print help
java -jar arthas-boot.jar -h
If the download speed is slow , have access to aliyun Mirror image :
java -jar arthas-boot.jar --repo-mirror aliyun --use-http
Windows Lower installation
- stay c:\ Create directory under arthas, stay windows In the command window , Use curl Command to download... On the Alibaba server jar package , size 108k

- Use java start-up arthas-boot.jar, To install arthas, The size is about 10M. Running this command will find java process , Input 1 Press enter . Automatically download from the remote host arthas Go to the local directory

- Check the installed directory
C:\Users\Administrator\.arthas\lib\3.1.7\arthas\
Quick start :attach A process
The goal is : Get started with cases
- Execute one jar package
- adopt arthas Come on attach adhere
step
1. Prepare the code
Here's a simple one Java Program , Generate a random number every second , Then perform the quality factor decomposition , And print out the decomposition results . Ignore the content of the code. This is not the focus now .
package demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class MathGame {
private static Random random = new Random();
// Used to count the number of illegal variables generated
public int illegalArgumentCount = 0;
public static void main(String[] args) throws InterruptedException {
MathGame game = new MathGame();
// Dead cycle , every 1 Second call 1 The following method ( Instead of starting a thread )
while (true) {
game.run();
TimeUnit.SECONDS.sleep(1);
}
}
// Decomposing prime factor
public void run() throws InterruptedException {
try {
// Randomly generate an integer , It's possible that , It may be negative
int number = random.nextInt()/10000;
// Call method to decompose prime factor
List<Integer> primeFactors = primeFactors(number);
// Print the results
print(number, primeFactors);
} catch (Exception e) {
System.out.println(String.format("illegalArgumentCount:%3d, ", illegalArgumentCount) + e.getMessage());
}
}
// Print the result of prime factor decomposition
public static void print(int number, List<Integer> primeFactors) {
StringBuffer sb = new StringBuffer(number + "=");
for (int factor : primeFactors) {
sb.append(factor).append('*');
}
if (sb.charAt(sb.length() - 1) == '*') {
sb.deleteCharAt(sb.length() - 1);
}
System.out.println(sb);
}
// Calculation number The prime factorization of
public List<Integer> primeFactors(int number) {
// If it is less than 2, Throw an exception , And count plus 1
if (number < 2) {
illegalArgumentCount++;
throw new IllegalArgumentException("number is: " + number + ", need >= 2");
}
// Used to save each prime number
List<Integer> result = new ArrayList<Integer>();
// The decomposition process , from 2 Start to see if you can divide
int i = 2;
while (i <= number) {
// If i Greater than number Just exit the loop
// aliquot , be i Is a factor ,number Continue from... For the result of the integral division 2 Begin to divide
if (number % i == 0) {
result.add(i);
number = number / i;
i = 2;
} else {
i++; // otherwise i++
}
}
return result;
}
}
2. start-up Demo
command
Download the packaged arthas-demo.jar
curl -O https://alibaba.github.io/arthas/arthas-demo.jar
Execute... On the command line
java -jar arthas-demo.jar
effect

3. start-up arthas
- because arthas-demo.jar The process opens a window , So open another command window to execute arthas-boot.jar
- Select the process to adhere :arthas-demo.jar

- If adhesion is successful , stay arthas-demo.jar The logged information will appear in that window , Recorded in the c:\Users\Administrator\logs Under the table of contents
- If the port number is occupied , You can also use the following command to change to another port number
java -jar arthas-boot.jar --telnet-port 9998 --http-port -1
4. Connect through a browser arthas
Arthas At present, we support Web Console, The user is in attach After success , You can directly access :http://127.0.0.1:3658/.
It can be filled in IP, Remote connection to... On other machines arthas.
Quick start : Common command contacts
The goal is
- dashboard instrument panel
- adopt thread Command to get
arthas-demoProcess Main Class - adopt jad Decompile Main Class
- watch
Command Introduction
1. dashboard instrument panel
Input dashboard( instrument panel ), Press enter /enter, Will show information about the current process , Press ctrl+c Execution can be interrupted .
notes : Enter the first part of the letter , Press tab Can automatically complete the command
- The first part is to show JVM All threads running in : Thread group , priority , State of thread ,CPU The occupancy rate of , Whether it is a background process, etc
- The second part shows JVM Memory usage
- The third part is some information about the operating system Java Version number

2. adopt thread Command to get arthas-demo Process Main Class
Get arthas-demo Process Main Class
thread 1 Will print threads ID 1 The stack , Usually main Thread of function .
3. adopt jad Decompile Main Class
jad demo.MathGame

4. watch monitor
adopt watch Order to see demo.MathGame#primeFactors The return value of the function :
$ watch demo.MathGame primeFactors returnObj
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-lcaaTdRE-1653812649643)(assets/image-20200305194740589.png)]](/img/23/7347056dae04e52e502a163e1675de.png)
5. sign out arthas
If you just exit the current connection , It can be used quit perhaps exit command .Attach To the target process arthas It will continue to run , The port will remain open , The next time you connect, you can connect directly .
If you want to quit completely arthas, It can be executed stop command .
Basic commands
help
effect
View command help
effect

cat
effect
Print file contents , and linux Inside cat Command similar
If there is no write path , The files in the current directory will be displayed
effect

grep
effect
Match lookup , and linux Inside grep Command similar , But it can only be used for pipeline commands
grammar
| parameter list | effect |
|---|---|
| -n | According to the line Numbers |
| -i | Ignore case lookup |
| -m Row number | Maximum display lines , To use with query strings |
| -e “ Regular expressions ” | Use regular expressions to find |
give an example
Show only include java Line system attribute of string
sysprop | grep java

Display contains java System properties of the line and line number of the string
sysprop | grep java -n

Display contains system A string of 10 Line information
thread | grep system -m 10

Using regular expressions , Display contains 2 individual o Thread information of characters
thread | grep -e "o+"
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-RyMrUuVS-1653812790772)(assets/image-20200310120512042.png)]](/img/70/8552005b0a6819e78a5f29b909d424.png)
pwd
effect
Return to the current working directory , and linux Command similar
pwd: Print Work Directory Print current working directory
cls
effect
Clear the current screen area
session
effect
View information about the current session
reset
effect
Reset enhancement class , Will be Arthas All the enhanced classes are restored ,Arthas All enhanced classes will be reset when the server is shut down
grammar
Restore specified class
reset Test
Restore all to List Ending class
reset *List
Restore all classes
reset
effect

version
effect
Output current target Java Process loaded Arthas Version number
history
effect
Print command history
quit
effect
Quit current Arthas client , other Arthas The client is not affected
stop
effect
close Arthas Server side , all Arthas All clients exit
keymap
effect
Arthas List of shortcuts and custom shortcuts
Arthas Command line shortcuts
| Shortcut key description | Command specification |
|---|---|
| ctrl + a | Jump to the beginning of the line |
| ctrl + e | Jump to the end of the line |
| ctrl + f | Move a word forward |
| ctrl + b | Move a word back |
| Keyboard left direction key | Move the cursor forward one character |
| Right direction key on the keyboard | Move the cursor back one character |
| Direction keys under the keyboard | Scroll down to display the next command |
| Direction keys on the keyboard | Flip up to display the last command |
| ctrl + h | Delete one character back |
| ctrl + shift + / | Delete one character back |
| ctrl + u | Undo the last order , This is equivalent to clearing the current line |
| ctrl + d | Delete the current cursor character |
| ctrl + k | Delete all characters from the current cursor to the end of the line |
| ctrl + i | Automatic completion , Equivalent to knocking TAB |
| ctrl + j | End the current line , It's equivalent to knocking back |
| ctrl + m | End the current line , It's equivalent to knocking back |
- anytime
tabkey , The prompt will be given according to the current input - After the command, knock
-or--, Then presstabkey , You can show the specific options of this command
Shortcut keys related to background asynchronous commands
- ctrl + c: Terminate current order
- ctrl + z: Suspend current command , The follow-up can be bg/fg Re support this command , or kill fall
- ctrl + a: Back to the beginning of the line
- ctrl + e: Back to the end of the line
jvm One of the related commands
dashboard
effect
Display the real-time data panel of the current system , Press q or ctrl+c sign out
effect

Data description
- ID: Java Level thread ID, Pay attention to this ID Can't follow jstack Medium nativeID One-to-one correspondence
- NAME: The thread of
- GROUP: Thread group name
- PRIORITY: Thread priority , 1~10 Number between , The bigger the priority is
- STATE: State of thread
- CPU%: Thread consumption cpu Proportion , sampling 100ms, Put all threads here 100ms Internal cpu Sum of usage , Then calculate the of each thread cpu Use proportion .
- TIME: Total thread running time , The data format is
branch : second - INTERRUPTED: The current interrupt bit state of the thread
- DAEMON: Whether it is daemon Threads
thread Thread related
effect
View the current JVM Thread stack information for
Parameter description
| Parameter name | Parameter description |
|---|---|
| Numbers | Threads id |
| [n:] | Before you designate the busiest N Thread and print stack |
| [b] | Find out which threads are currently blocking other threads |
| [i <value>] | Appoint cpu Sampling interval of percentage Statistics , The unit is millisecond |
give an example
Show the current busiest front 3 Thread and print stack
thread -n 3

When there are no parameters , Show information about all threads
thread
Show 1 The running stack of thread No
thread 1

Find out which threads are currently blocking other threads , Sometimes we find the app stuck , Usually because a thread holds a lock , And other threads are waiting for the lock to cause . In order to check such problems , arthas Provides thread -b, One click to find the culprit .
thread -b
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-SveRcVeR-1653812944251)(assets/image-20200310155534864.png)]](/img/db/ee017f09720f807d108ed2326d21d2.png)
Specify the sampling interval , every 1000 Millisecond sampling , Displays the most time consuming 3 Threads
thread -i 1000 -n 3

View the threads waiting
thread --state WAITING

jvm
effect
View the current JVM Information about
effect

THREAD relevant
- COUNT: JVM Number of threads currently active
- DAEMON-COUNT: JVM Number of currently active daemons
- PEAK-COUNT: from JVM The maximum number of threads that have been alive since startup
- STARTED-COUNT: from JVM Total number of threads started
- DEADLOCK-COUNT: JVM The number of threads currently deadlocked
File descriptors are related
- MAX-FILE-DESCRIPTOR-COUNT:JVM The maximum number of file descriptors that a process can open
- OPEN-FILE-DESCRIPTOR-COUNT:JVM Number of file descriptors currently open
sysprop
effect
View and modify JVM System properties of
give an example
See all the properties
sysprop
View individual properties , Supported by tab completion
sysprop java.version
Modify individual properties
sysprop user.country
user.country=US
sysprop user.country CN
Successfully changed the system property.
user.country=CN
sysenv
effect
View the current JVM Environmental attributes of (System Environment Variables)
give an example
View all environment variables
sysenv
Look at a single environment variable
sysenv USER
vmoption
effect
see , to update VM Diagnosis related parameters
give an example
View all options
vmoption
View the specified options
vmoption PrintGCDetails
Update the specified options
vmoption PrintGCDetails true
getstatic
effect
adopt getstatic Command can easily view the static properties of a class
grammar
getstatic Class name Property name
give an example
Show demo.MathGame Static properties in class random
getstatic demo.MathGame random
ognl
effect
perform ognl expression , This is from 3.0.5 New features in version
OGNL grammar
http://commons.apache.org/proper/commons-ognl/language-guide.html

Parameter description
| Parameter name | Parameter description |
|---|---|
| express | The expression to execute |
[c:] | Execute the expression ClassLoader Of hashcode, The default value is SystemClassLoader |
| [x] | The expanded hierarchy of the result object , The default value is 1 |
give an example
Call static functions
ognl '@[email protected]("hello")'
Get the static field of the static class
ognl '@[email protected]'
Execute multiline expressions , Assign to a temporary variable , Return to one List
ognl '#[email protected]@getProperty("java.home"), #[email protected]@getProperty("java.runtime.name"), {#value1, #value2}'
effect

class/classloader
The goal is
- sc: Search Class
- sm: Search Method
sc
effect
see JVM Loaded class information ,“Search-Class” Abbreviation , This command can search out all the loaded data JVM Medium Class Information
sc Subclass matching is enabled by default , That is to say, all subclasses of the current class will also be searched , Want an exact match , Please open the options disable-sub-class true switch
Parameter description
| Parameter name | Parameter description |
|---|---|
| class-pattern | Class name expression matching , Support full naming , Such as com.taobao.test.AAA, Also support com/taobao/test/AAA This format , such , When we copy the class name from the exception stack , There's no need to put / Replace with . La . |
| method-pattern | Method name expression matching |
| [d] | Output details of the current class , Include the source of the original file loaded by this class 、 Declaration of a class 、 Loaded ClassLoader Wait for details . If a class is divided into multiple ClassLoader Loaded , There will be many times |
| [E] | Turn on regular expression matching , The default is wildcard matching |
| [f] | Output the member variable information of the current class ( Need to match parameters -d Use it together ) |
give an example
Fuzzy search ,demo Package all classes
sc demo.*
Print class details
sc -d demo.MathGame
Print out the class Field Information
sc -df demo.MathGame
sm
effect
View method information for loaded classes
“Search-Method” Abbreviation , This command can search out all loaded Class Information method information .
sm The command can only see what is declared by the current class (declaring) Methods , The parent class cannot see .
Parameter description
| Parameter name | Parameter description |
|---|---|
| class-pattern | Class name expression matching |
| method-pattern | Method name expression matching |
| [d] | Show the details of each method |
| [E] | Turn on regular expression matching , The default is wildcard matching |
give an example
Show String Class loading methods
sm java.lang.String
Show String Medium toString Method details
sm -d java.lang.String toString
jad
effect
Decompile the specified loaded class source code
jadThe order will JVM In the actual operation of class Of byte code Decompile to java Code , It's easy for you to understand the business logic ;stay Arthas Console On , Decompiled source code is highlighted with syntax , Reading is more convenient
Of course , Decompiled java Code may have syntax errors , But it doesn't affect your reading comprehension
Parameter description
| Parameter name | Parameter description |
|---|---|
| class-pattern | Class name expression matching |
| [E] | Turn on regular expression matching , The default is wildcard matching |
give an example
compile java.lang.String
jad java.lang.String
Only the source code is displayed when decompiling , By default , The decompilation result will contain ClassLoader Information , adopt --source-only Options , You can print only the source code . Convenience and mc/redefine Command in combination with .
jad --source-only demo.MathGame

Decompile the specified function
jad demo.MathGame main
mc
effect
Memory Compiler/ Memory compiler , compile .java File generation .class
give an example
Compile in memory Hello.java by Hello.class
mc /root/Hello.java
Can pass -d Command to specify the output directory
mc -d /root/bbb /root/Hello.java
redefine
effect
Load external .class file ,redefine To JVM in
Be careful , redefine The original class after cannot be restored ,redefine It's possible to fail ( For example, new field).
resetCommand toredefineInvalid class . If you want to reset , needredefineOriginal bytecode .
redefineCommand andjad/watch/trace/monitor/ttWaiting for orders to conflict . After executionredefineafter , If you execute the above command again , It will putredefineBytecode reset for .
redefine The limitation of
- No new additions are allowed field/method
- Running function , No exit can take effect , For example, the following new
System.out.println, Onlyrun()What's in the function works
public class MathGame {
public static void main(String[] args) throws InterruptedException {
MathGame game = new MathGame();
while (true) {
game.run();
TimeUnit.SECONDS.sleep(1);
// This doesn't work , Because the code is running all the time while in
System.out.println("in loop");
}
}
public void run() throws InterruptedException {
// This works , because run() The function can end completely every time
System.out.println("call run()");
try {
int number = random.nextInt();
List<Integer> primeFactors = primeFactors(number);
print(number, primeFactors);
} catch (Exception e) {
System.out.println(String.format("illegalArgumentCount:%3d, ", illegalArgumentCount) + e.getMessage());
}
}
}
Case study : combination jad/mc Command to use
step
1. Use jad Decompile demo.MathGame Output to /root/MathGame.java
jad --source-only demo.MathGame > /root/MathGame.java
2. After editing the code above , Use mc Compile new code in memory
mc /root/MathGame.java -d /root
3. Use redefine Command to load a new bytecode
redefine /root/demo/MathGame.class
result

边栏推荐
- QObject::connect: Cannot queue arguments of type ‘QTextCursor‘ (Make sure ‘QTextCursor‘ is registere
- JUC Unsafe
- Tutorial (5.0) 02 Management * fortiedr * Fortinet network security expert NSE 5
- Knowledge points related to system architecture 3
- Library management system based on wechat applet Rar (thesis + source code)
- Margin:0 reason why auto does not take effect
- What exactly is Huawei cloud desktop saying when it says "smooth"?
- Subspace of 20211004 matrix
- 20220524 如何把CoppeliaSim安装到D盘
- 类的加载概述
猜你喜欢
随机推荐
Spectre record
基于微信小程序的图书馆管理系统.rar(论文+源码)
output. Interpretation of topk() function
20211028 Stabilizability
BGP 联邦+Community
Collection of garbled code problems in idea development environment
Summary of the first retrospective meeting
15. copy constructor
JUC 原子累加器
strcpy_ S precautions for use. (do not use strcpy_s where memcpy_s can be used)
Bash: kill: (74001) - operation not allowed
【QNX Hypervisor 2.2 用户手册】4.5 构建Guest
Is it safe to open an account online? Can a novice open an account?
A very detailed blog about the implementation of bilinear interpolation by opencv
20211115 any n-order square matrix is similar to triangular matrix (upper triangle or lower triangle)
JUC原子引用与ABA问题
【 sécurité 】 comment devenir ingénieur de sécurité de 0 à 1 contre - attaque pour la Fondation zéro
QML(06)——qml. Add a new folder under QRC
20211028 Stabilizability
C/S模型与P2P模型
![[network security] webshell empowerment of new thinking of SQL injection](/img/03/b4a7ca0664675119b06e7246a49eb0.png)





