Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of Java online Diagnostic tool Arthas

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use Arthas, a Java online diagnosis tool. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

It is really useful to investigate some production environmental problems.

Function

Arthas can help you solve problems like the following when you are at a loss as to what to do:

From which jar package was this class loaded? Why are all kinds of related Exception reported?

Why didn't the code I changed be executed? Is it because I don't have commit? Is the branch wrong?

If you can't debug online if you encounter a problem, can you only republish it by adding a log?

Online encounter a user's data processing problems, but online also can not debug, offline can not be reproduced!

Is there a global perspective to view the health of the system?

Is there any way to monitor the real-time running status of JVM?

Install wget https://alibaba.github.io/arthas/arthas-boot.jarjava-jar arthas-boot.jar using the sample environment

The above startup mode can only be accessed locally. The default ip is 127.0.0.1, in which case only command line interaction or http://127.0.0.1:8563/ access is used. If you want to access web pages on other machines, you can start the command with-- target-ip native ip, so that you can access the arthas client through web pages on other machines (as long as the machines can access each other and the port is open). As follows:

Root@iZuf63uqehzec73094965jZ:/data/arthas# java-jar arthas-boot.jar-- target-ip 139.room.room.112 [INFO] arthas-boot version: 3.1.4 [INFO] Found existing java process Please choose one and hit RETURN.* [1]: 1701 stock.war [2]: 15658 / data/secret/web/secret-web.jar2 [INFO] arthas home: / root/.arthas/lib/3.1.4/arthas [INFO] Try to attach process 15658 [INFO] Attach process 15658 success. [INFO] arthas-client connect 139.room.room.112 3658, -. . . ,. ,. ,. / O\ |. .--'|'|'-'| / O\'. -'|. | |'-. |. -. | |.. | `. | `-. |\ |. -'|`--'`- -'- -`-- 'wiki https://alibaba.github.io/arthastutorials https://alibaba.github.io/arthas/arthas-tutorialsversion 3.1.4pid 15658time 2019-10-31 00:16:59

Finally, it can be accessed in the following way, which is the same as typing the command line before the machine.

Http://139...112:8563/

Trace command (tracking the execution time of a method, supporting multiple methods, time filtering)

This command is used to track the execution performance of the method, support filtering by time, refer to the official documentation trace command in more detail

Normal monolayer tracking call link

Command:

Trace com.zero.secret.web.controller.AlbumController home

Effect:

In the result, the first column is the time consuming of the method, the second column is the specific method, and the third column is the line number where the code is located.

Screening time

Sometimes there are too many methods in the method, which can be filtered according to the time and the method can be filtered out at a certain time. The command is as follows:

Trace com.zero.secret.web.controller.AlbumController home'# cost > 10'

It doesn't work with the time filter test, try again next time.

Deep tracking call link

The trace command can only trace one layer, and if you want to trace deeper methods, you can use multiple methods.

Trace-E com.zero.secret.web.controller.AlbumController | com.zero.secret.dal.mapper.AlbumMapper home | selectByPrimaryKey

Note:

Trace itself is time-consuming, and the time shown here does not subtract the time spent by trace itself.

Watch command (allows you to view the values of variables online)

A detailed watch command to view the official documentation. Watch command

Command:

Watch com.zero.secret.service.AlbumService getAlbum "{params,returnObj}"-x 2

Here-x 2 indicates that the parameter depth displays 2 layers, and only one layer is displayed by default. The following is the return result of the first layer. Compare it with each other.

The value of the input parameter here is not shown.

Sometimes it is necessary to compare the value of the parameter before entering the method with that after the execution of the method, and to see the change in the value of the parameter, you can add the parameter-b-s. The command is as follows:

Watch com.zero.secret.service.AlbumService getAlbum "{params,returnObj}"-x 2-b-s

The results are as follows:

There are two sets of values returned here. The first set of values are the input parameters and return values when entering the method, and the second group are the input parameters and return values at the end of method execution.

Stack (View method invocation links)

For detailed stack commands, refer to the official documentation, the stack command.

In some complex services, if you don't know the calling link of a method, you can use this command to view the calling link. Or when the calling link is different in different business scenarios, you can also use this command to determine the calling link of a business data.

The command is as follows:

Stack com.zero.secret.service.AlbumService getAlbum

The effect is as follows:

Monitor (monitor the number of times a method is executed, the number of successes, the number of failures, and the average time taken over a period of time)

The official document of the command monitor

The command is as follows:

Monitor-c 10 com.zero.secret.web.controller.AlbumController home

The above is a count of 10s.

The results are as follows:

Dashboard (View JVM resource occupancy and garbage collection) thread (you can view the details of several threads in the top CPU resource occupancy, blocking threads)

For more information, please refer to the thread command. You can view the details of several threads in front of the current CPU resource occupancy, and you can directly see what several threads that consume more resources are doing, which is convenient for troubleshooting. Do not need dump off the stack, you can see it directly, using more lightweight.

Check out the first few threads occupied by CPU

The command is as follows:

Thread-n 3

The results are as follows:

Find the thread that is currently blocking other threads

This command currently only supports finding threads blocked by the synchronized keyword, but it is not supported if it is java.util.concurrent.Lock. It's not practical enough. The command is as follows:

Thread-b

As a result, let's take a look at the official example.

Logger (the level of logger can be dynamically modified, and the level of package can be specified)

This command is also more practical.

Sc sm (the way sc looks at jvm-loaded class,sm to view jvm-loaded classes)

Sc sm sc is officially ordered to view the class class, and sm to find the list of methods of the class class.

[arthas@15658] $sc * Albumcom.zero.secret.dal.dto.AlbumAffect (row-cnt:1) cost in 30 ms. [arthas@15658] $sm com.zero.secret.dal.dto.Albumcom.zero.secret.dal.dto.Album () Vcom.zero.secret.dal.dto.Album getTag () Ljava/lang/String;com.zero.secret.dal.dto.Album getDesc () Ljava/lang/String;com.zero.secret.dal.dto.Album getStatus () Ljava/lang/Integer Com.zero.secret.dal.dto.Album setStatus (Ljava/lang/Integer;) Vcom.zero.secret.dal.dto.Album getImgs () Ljava/util/List;com.zero.secret.dal.dto.Album getGirlId () Ljava/lang/Long;com.zero.secret.dal.dto.Album setDesc (Ljava/lang/String;) Vcom.zero.secret.dal.dto.Album setTitle (Ljava/lang/String;) Vcom.zero.secret.dal.dto.Album setCreateTime (Ljava/util/Date ) Vcom.zero.secret.dal.dto.Album setUpdateTime (Ljava/util/Date;) Vcom.zero.secret.dal.dto.Album setGirlId (Ljava/lang/Long;) Vheapdump print stack

Official command heapdump this command is simple, print the stack.

[arthas@15658] $heapdump / data/dump.hprofDumping heap to / data/dump.hprof...Heap dump file createdgetstatic gets the value of the static member variable of the class

Official command document: getstatic

Gets the value of a static member variable within the class.

[arthas@15658] $getstatic com.zero.secret.web.controller.AlbumController PAGE_SIZEfield: PAGE_SIZE@Integer [25] Affect (row-cnt:1) cost in 8 ms.jad, mc, redefine (decompilation, compilation, hot deployment)

For more information on the official documentation of the above commands, please see jad mc redefine

Decompiled AlbumController

[arthas@15658] $jad com.zero.secret.web.controller.AlbumControllerClassLoader:+-org.springframework.boot.loader.LaunchedURLClassLoader@5d099f62 +-sun.misc.Launcher$AppClassLoader@55f96302 +-sun.misc.Launcherexamples ExtClassLoaderholders 5dbd8ca1LocationLocationPartition fileWhetDataGen to use websecretMub web.jarbrands

The above information is important to remember the classloader number of the current class, 5d099f62. Next, output the decompiled source code to a directory that is convenient for editing, as follows:

Jad-- source-only com.zero.secret.web.controller.AlbumController > / data/artas/AlbumController.java

Edit the AlbumController.java source file and add a line of log:

Compile the edited source file AlbumController.java to the target location, and mc specifies that a loader is required, otherwise many dependencies can not be found. Here is to specify the original class loader to compile, and the default class file is in the original class file location. All you need to do is hot load.

[arthas@15658] $mc-c 5d099f62 / data/artas/AlbumController.javaMemory compiler output:/data/secret/web/com/zero/secret/web/controller/AlbumController.classAffect (row-cnt:1) cost in 5279 ms.

Then hot load, again, specify the original classloader.

[arthas@15658] $redefine-c 5d099f62 / data/secret/web/com/zero/secret/web/controller/AlbumController.classredefine success, size: 1

Finally, take a look at the results of the implementation.

In this way, you can test whether some features can solve the problem without republishing the application.

This is the end of the use of Arthas, a Java online diagnosis tool. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report