当前位置:网站首页>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 ?

 Insert picture description here

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 :

  1. Where does this class come from jar Package loaded ? Why do you report all kinds of related Exception?
  2. Why didn't I change the code to ? I didn't commit? The branch is wrong ?
  3. I can't get online when I have a problem debug, Can't we just add logs and redistribute them ?
  4. There is a problem with the data processing of a user online , But online also can't debug, It can't be reproduced offline !
  5. Is there a global perspective to see the health of the system ?
  6. Is there any way to monitor JVM The real-time running state of ?
  7. 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

  1. 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
     Insert picture description here
  2. 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
     Insert picture description here
  3. 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

  1. Execute one jar package
  2. 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

 Insert picture description here

3. start-up arthas

  1. because arthas-demo.jar The process opens a window , So open another command window to execute arthas-boot.jar
  2. Select the process to adhere :arthas-demo.jar
     Insert picture description here
  3. 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
  4. 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.
 Insert picture description here

Quick start : Common command contacts

The goal is

  1. dashboard instrument panel
  2. adopt thread Command to get arthas-demo Process Main Class
  3. adopt jad Decompile Main Class
  4. 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

  1. 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
  2. The second part shows JVM Memory usage
  3. The third part is some information about the operating system Java Version number

 Insert picture description here

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 .
 Insert picture description here

3. adopt jad Decompile Main Class

jad demo.MathGame

 Insert picture description here

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)]

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

 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here
Display contains system A string of 10 Line information
thread | grep system -m 10

 Insert picture description here

 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)]

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

 Insert picture description here

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 tab key , The prompt will be given according to the current input
  • After the command, knock - or -- , Then press tab key , 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

 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here

 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)]

 Specify the sampling interval , every 1000 Millisecond sampling , Displays the most time consuming 3 Threads 
thread -i 1000 -n 3

 Insert picture description here

 View the threads waiting 
thread --state WAITING

 Insert picture description here

jvm

effect

View the current JVM Information about

effect

 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here

class/classloader

The goal is

  1. sc: Search Class
  2. 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

jad The 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

 Insert picture description here

 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).

reset Command to redefine Invalid class . If you want to reset , need redefine Original bytecode .

redefine Command and jad/watch/trace/monitor/tt Waiting for orders to conflict . After execution redefine after , If you execute the above command again , It will put redefine Bytecode 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, Only run() 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

 Insert picture description here

原网站

版权声明
本文为[Q z1997]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130859554240.html