This is the third day of my participation in Gwen Challenge

1. What is it

Have you ever had a problem in production, but you couldn’t locate the problem line and had to find a way to reproduce the problem in development?

Arthas is an open source Java diagnostic tool for Alibaba.

2. What can be solved?

(1) From which JAR is this class loaded? Why are various class-related exceptions reported?

(2) Why is the changed code not executed?

(3) Problems that cannot be debugged online

(4) There is a problem with some data online, but it cannot be debugged online or reproduced offline.

(5) Is there a global perspective to view the operating status of the system?

(6) Is there any way to monitor the real-time health of the JVM?

3. Basic use

(1) Arthas-boot is enabled

Download arthas-boot.jar and start with java-jar

wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar 
Copy the code

-h: displays the help information

Arthas-boot. jar is the arthas launcher that lists all Java processes once it is started.

If you want to follow a Java process, you just type in the number and Arthas attaches to the target process and prints a log.

(2) Check the dashboard

When you enter Dashboard, information about the current process is displayed

(3) Run the sysenv command to obtain the Main class of the process

$ sysenv | grep MAIN
 JAVA_MAIN_CLASS_71560              demo.MathGame
Copy the code

(4) Decompile a class using jad

The fully qualified name of the jad classCopy the code

(5) watch

Use the watch command to view the return value of a method on a class

The fully qualified name of the Watch class is called returnObjCopy the code

Such as:

watch demo.MathGame primeFactors returnObj
Copy the code

4, advanced use

Basic commands

  • Help — View the command help information
  • CLS — Clears the current screen area
  • Session — View information about the current session
  • Reset — Resets enhanced classes. This will restore all Arthas enhanced classes and reset all enhanced classes when Arthas server is closed
  • Version – Prints the Arthas version number loaded by the current target Java process
  • History – Prints command history
  • Quit — Exit the current Arthas client, leaving other Arthas clients unaffected
  • Shutdown — shutdown the Arthas server and exit all Arthas clients
  • Keymap — Arthas list of shortcuts and custom shortcuts

The JVM related

  • Dashboard – Real-time data panel for the current system
  • Thread – Views the thread stack information for the current JVM
  • JVM – View information about the current JVM
  • Sysprop – View and modify system properties of the JVM
  • Sysenv – View environment variables for the JVM
  • New! Getstatic — View the static properties of the class

The class/this correlation

  • Sc – View information about classes loaded by the JVM
  • Sm — View method information for loaded classes
  • Dump — Dumps loaded class byte code to a specific directory
  • Re-define — Load an external. Class file, re-define it into the JVM
  • Jad – Decompiles the source code of a loaded class
  • Classloader – view the classloader inheritance tree, urls, class loading information, and use classloader to getResource

Monitor/watch/trace

Please note that these commands, through the bytecode enhancement technology to realize, will insert some aspects in the specified class methods to implement the data statistics and observation, so online, use pretest, please try to clear need classes, methods and conditions for observation, diagnosis end to perform shutdown or class that will enhance the reset command.

  • Monitor — method performs monitoring
  • Watch – method performs data observations
  • Trace — calls to the method internally and prints the time spent on each node on the method path
  • Stack – Outputs the call path where the current method is called
  • Tt – time tunnel of method execution data, recording input and return information for each call to the specified method, and observing these different time calls

options

Options — View or set the Arthas global switch

The pipe

Arthas supports the use of pipes for further processing, the results of the above command such as sm org.. Apache log4j. Logger | grep

  • Grep — Searches for results that meet the criteria
  • Plaintext — Removes color from the result of a command
  • Wc — Count the output by row

Background Asynchronous Task

Asynchronous background tasks come in handy when there is an occasional problem online, such as requiring a watch condition that may occur only once a day. See here for details

Use > to rewrite the result to the log file, and use & to specify that the command is run in the background and the session interruption does not affect the execution of tasks (the default life cycle is 1 day) JOBS — lists all job kills — forcibly terminates the task fg — pulls the suspended task to the foreground for execution bg — puts the suspended task to the background for execution

Web Console

Arthas is connected via websocket.

Web Console

Other features

  • Asynchronous command support
  • Execution results are saved in logs
  • Batch support
  • Description of ogNL expression usage

5, detailed command

(1) the jad

Decompile the source of the loaded class

The JAD command decompiles the byte code of the class actually running in the JVM into Java code so you can understand the business logic.

  • On Arthas Console, decomcompiled source code is syntactically highlighted to make it easier to read

  • Of course, decompilated Java code may have syntax errors, but that doesn’t affect your reading comprehension

    Class-pattern Class name expression matching [C :] HashCode [E] Of the ClassLoader to which the class belongs Enables regular expression matching. The default value is wildcard matching

1. Decompile the specified method

A fully qualified named method of a jad classCopy the code

Such as:

  • Decompile a class

  • Decompile a method of a class

(2) watch

Method to perform data monitoring

Allows you to easily observe the invocation of the specified method. The range that can be observed is: return value, throw exception, input parameter, by writing OGNL expression to view the corresponding variable.

Parameters:

Class-pattern class name expression matching method-pattern method name expression matching express observation expression conditional-Express condition expression [b] observation before method call [e] observation after method exception [s] Observe after method return [f] Observe after method end (normal and abnormal returns) [E] Turn on regular expression matching, default wildcard matching [x:] Specify the depth of property traversal for output results, default 1Copy the code

Note that observation expressions are composed mainly of OGNL expressions, so you can write “{params,returnObj}” that any valid OGNL expression will be acceptable.

“1. Observe the method parameters and return values”

$ watch com.liusy.arthas_demo.controller.ArthasController test "{params,returnObj}" -x 2
Copy the code

“2. Observation Method input”

$ watch com.liusy.arthas_demo.controller.ArthasController test "{params,returnObj}" -x 2 -b
Copy the code

“3. Observe both before method call and after method return”

$ watch com.liusy.arthas_demo.controller.ArthasController test "{params,returnObj}" -x 2 -b -s 
Copy the code

** “4, adjust the value of -x, observe the specific method parameter value” **x indicates the number of levels

$ watch com.liusy.arthas_demo.controller.ArthasController test "{params,returnObj}" -x 3 
Copy the code

5. Examples of conditional expressions

$  watch com.liusy.arthas_demo.controller.ArthasController test "{params, returnObj}" "params[0].id == 1" -x 2
Copy the code

Only calls that meet the criteria will have a response

“6. Observe abnormal Information”

$ watch com.liusy.arthas_demo.controller.ArthasController test "{params, throwExp}"  -e -x 2 
Copy the code

“7. Filter by time”

$ watch com.liusy.arthas_demo.controller.ArthasController test "{params,returnObj}" #cost>200 -x 3 
Copy the code

#cost>200(in ms) indicates that the output is generated only when the execution time is greater than 200ms. Calls whose execution time is less than 200ms are filtered out

8. Observe global properties in the current object

$ watch com.liusy.arthas_demo.controller.ArthasController test 'target'
Copy the code

If you want to see global properties in the current object before and after a method is executed, you can use the target keyword, which represents the current object

Then use target.field_name to access a global property of the current object

$ watch com.liusy.arthas_demo.controller.ArthasController test 'target.service'
Copy the code

(3) the trace

Method internally calls the path and prints the time spent on each node on the method path

The trace command can proactively search the method invocation path corresponding to class-pattern/method-pattern, render and count all performance costs along the entire invocation link, and trace the invocation link.

Class-pattern Class name expression matching method-pattern Method name expression matching condition- Express condition expression [E] Regular expression matching is enabled. The default value is wildcard matching. [n:] Number of times the command is executed #cost Method execution timeCopy the code

Such as:

trace com.liusy.arthas_demo.controller.ArthasController test
Copy the code

Filter by time:

trace com.liusy.arthas_demo.controller.ArthasController test #cost>1
Copy the code

(4) stack

Prints the call path to which the current method is called

A lot of times you know a method is being executed, but there are too many paths to execute it, or you don’t know where the method is being executed from, and you need stack commands.

Class-pattern Class name expression matching method-pattern Method name expression matching condition- Express Conditional expression [E] Enables regular expression matching. The default value is wildcard matching [n:]Copy the code

Such as:

Based on the time query, only the stack whose time is less than 30ms is displayed

stack com.liusy.arthas_demo.controller.ArthasController test #cost<30
Copy the code

(5) the sc

View information about classes loaded by the JVM

Short for “search-class”, this command will Search for all classes that have been loaded into the JVM. The supported arguments are [d], [E], [f], and [x:].

Parameter Description:

[d] Displays detailed information about the current class, including the source of the original file loaded by this class, the declaration of the class, and the ClassLoader loaded. If a class is loaded by multiple classLoaders, there will be multiple occurrences. [E] Enable regular expression matching, which defaults to wildcard matching. [f] Output the member variables of the current class (with -d). That is, output directly using toStringCopy the code

Class-pattern supports fully qualified names, such as com.liusy.demo, as well as com/liusy/demo.

Sc subclass matching is enabled by default, that is, all subclasses of the current class will be searched. For an exact match, please turn on options disable-sub-class True

1. View the static variable information of the class

Fully qualified name of the SC-df classCopy the code

(6) sm

View method information for loaded classes

Short for “search-method”, this command will Search for all methods that have Class information loaded.

The sm command sees only the method declared by the current class, not the parent class.

Class-pattern Class name expression matching Method-pattern Method name expression matching [D] Displays detailed information about each method [E] Enables regular expression matching. The default is wildcard matchingCopy the code

(7) dashboard

Current process real-time data panel (including memory, thread, etc.), press CTRL + C to exit.

Data description:

NAME: thread NAME GROUP: thread GROUP NAME PRIORITY: thread PRIORITY. The number ranges from 1 to 10. A larger number indicates a higher PRIORITY. Thread status CPU%: CPU usage of threads. Sample 100ms, sum up the CPU usage of all threads in 100ms, and then calculate the CPU usage of each thread. TIME: indicates the total running TIME of a thread. The data format is minute: second. INTERRUPTED: indicates the current interrupt bit status of a threadCopy the code

(8) Thread

View the current thread information, view the thread stack.

Parameter Description:

Id Thread ID [n:] specifies the first n busiest threads and prints the stack [b] finds the threads currently blocking other threads [I <value>] Specifies the sampling interval for CPU usage statistics, in millisecondsCopy the code

1. One-click display of the current top N busiest threads and print the stack.

thread -n 3
Copy the code

2. If no parameter is specified, information about all threads is displayed

Displays the run stack for the specified thread

therad id
Copy the code

Find out which thread is currently blocking another thread

thread -b
Copy the code

Currently supports only to find out the synchronized keyword blocked threads, if is a Java util. Concurrent. The Lock, it is not support

5. Specify the sampling interval

Thread - I timeCopy the code

For example, thread -n 3 -i 1000

(9) the JVM

View current JVM information

Information description:

Daemon-count: indicates the number of active daemons on the JVM. Peak-count: indicates the maximum number of threads that have been alive since the JVM STARTED. Total number of threads that have been started since the JVM started Number of threads currently deadlocked by the JVM FILE descriptors-count: indicates the maximum number of FILE descriptors that can be opened by a JVM process. Open-file-descriptors-count: indicates the number of FILE descriptors currently opened by the JVMCopy the code

Sysprop (10)

View system Properties for the current JVM

1. View all properties

sysprop
Copy the code

2. View a property

sysprop java.version
Copy the code

3. Modify an attribute value

sysprop key value
Copy the code

Sysenv (11)

View the current JVM’s system Environment variables

1. View all environment variables

sysenv
Copy the code

2. View a single environment variable

sysenv USER
Copy the code

Getstatic (12)

You can view the static properties of the class

getstatic class_name field_name
Copy the code

If the static property is a complex object, operations such as traversal, filtering, and access to the object’s internal properties can also be supported on the property via the OGNL representation.

For example, if n is a Map and the Map Key is an Enum, you can run the following command to filter out the values whose Key is an Enum in the Map

$ getstatic com.alibaba.arthas.Test n 'entrySet().iterator.{? #this.key.name()=="STOP"}'
field: n
@ArrayList[
    @Node[STOP=bbb],
]
Copy the code

Ognl (13)

Execute ogNL expression

Parameter Description:

Express executes the expression [C :] hashCode of the ClassLoader that executes the expression. The default value is SystemClassLoader [x] resulting in an expansion hierarchy of objects. The default value is 1Copy the code

1. Call static functions

ognl '@[email protected]("hello")'
Copy the code

2. Get the static field of the static class

ognl '@demo.MathGame@random'
Copy the code

Execute a multiline expression, assign to a temporary variable, and return a list

$ ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, [/opt/ Java /8.0.181-zulu/jre], @string [OpenJDK Runtime Environment],]Copy the code

(14) dump

Dump the loaded class’s Bytecode to a specific directory

Parameter Description:

Class-pattern Class name expression matching [C :] HashCode [E] Of the ClassLoader to which the class belongs Enables regular expression matching. The default value is wildcard matchingCopy the code

This (15)

View the classloader inheritance tree, urls, class loading information

The classloader command counts all classLoaders in the JVM and displays inheritance trees, urls, and so on.

You can tell the specified classloader to go to getResources and print out the urls of all resources found. For ResourceNotFoundException is useful.

Parameter Description:

Parameter Name Parameter Description [L] Statistics by class load instance [t] Print the inheritance tree of all classLoaders [a] List all classloaders loaded. Use [C :] ClassLoader hashCode [C :] with caution. R :] use ClassLoader to find resourceCopy the code

1. View statistics by class loading type

classloader
Copy the code

2. View statistics by class loading instance

classloader -l
Copy the code

3. View the ClassLoader inheritance tree

classloader -t
Copy the code

4. View the actual URLClassLoader urls

classloader -c 5ffe9775
Copy the code

5. Use ClassLoader to find resource

$ classloader -c 226b143b -r META-INF/MANIFEST.MF
Copy the code

Select * from class;

$ classloader -c 1b6d3586 -r java/lang/String.class
Copy the code

Redefine (16)

Re-define the JVM loaded classes.

[c:] ClassLoader hashCode [p:] Complete path to an external. Class file, for example, define -p/TMP/test.classCopy the code

(17) monitor

Method Execution monitoring

Monitor calls to classes and methods that match class-pattern/method-pattern.

The monitor command is a non-real-time return command.

A real-time return command returns immediately after typing, whereas a non-real-time return command waits for the target Java process to return information until the user presses Ctrl+C.

The server runs the task in the background as a task, and the embedded code is not executed as the task is aborted, so there is no significant performance impact after the task is shut down, and in principle any Arthas command does not cause any changes to the existing business logic.

Monitoring dimensions:

Timestamp timestamp class Java class method method (constructor or common method) total number of calls success number of successful methods fail number of failed methods rt average rt fail-rate failure rateCopy the code

Parameters:

Class-pattern Class name expression matching method-pattern Method name expression matching [E] Enables regular expression matching. The default value is wildcard matching. [C :] Statistical periodCopy the code

Such as:

$ monitor -c 5 com.alibaba.sample.petstore.web.store.module.screen.ItemList execute
Copy the code

Tt (18)

A space-time tunnel of method execution data, recording the input and return information of each call to the specified method, and observing these different time calls

Although watch is convenient and flexible, it needs to think clearly and observe the spelling of expressions in advance, which is too high for troubleshooting, because in many cases, we don’t know where the problem comes from and can only guess by clues.

At this time, if we can record all the input parameters and return values of the method call at that time, the exception thrown will be very helpful to the thinking and judgment of the whole problem.

Thus, the TimeTunnel command is born.

  • Command Parameter parsing

  • -t

The tt command has many main arguments. -t is one of them. This parameter indicates that you want to record each execution of the print method of class *Test.

  • -n 3

If you are executing a low-volume method you may still have enough time to interrupt the TT command recording process with CTRL+C, but if you are executing a high-volume method you can burst your JVM memory in an instant.

At this point you can specify the number of times you need to log with the -n parameter. Arthas will actively interrupt the TT command when this number of times is reached to prevent manual operations from being stopped.

INDEX Time segment record number. Each number represents a call. Subsequent TT commands specify record operations based on this number, which is very important. TIMESTAMP the local time at which the method was executed, COST(ms) method execution time is-RET method ends as a normal return is-exp method ends as an exception OBJECT executes the OBJECT's hashCode(), notice that, It has been mistaken for an object's memory address in the JVM, but unfortunately it is not. But it can help you simply mark the name of the CLASS entity that is currently executing the METHODCopy the code

(19) options

The global switch

Name Default Value Description Unsafefalse Specifies whether system-level classes can be enhanced. If this switch is enabled, the JVM may be suspended. Dumpfalse specifies whether to dump enhanced classes to external files. If enabled, class files are dumped to /${application dir}/arthas-class-dump/. See console output batch-Re-transformTrue whether batch-re-transformTrue supports batch retransform of matched classes json-formatFalse Whether json-formatted output is supported disable-sub-classFalse Whether subclass matching is disabled. By default, when a target class is matched, its subclass is matched by default. If you want to match the target class accurately, you can disable this function. Debug-for-asmfalse Displays ASM debugging information. Open after all commands run results will be saved to the/home/admin/logs/arthas, arthas. Log in the job – the default timeout timeout1d asynchronous background tasks, and more than this time, task automatically stop; For example, 1d, 2h, 3m, and 25s represent day, hour, minute, and second respectively

For example, to enable the log saving function, run the following command:

$ options save-result true
Copy the code