Arthas 是 阿里巴巴最近开源出来的一个针对 java 的工具,主要是针对 java 的问题进行诊断!
一、概述
这个工具可以协助你做下面这些事情:
- 这个类是从哪个 jar 包加载而来的?
- 为什么会报各种类相关的 Exception?
- 线上遇到问题无法 debug 好蛋疼,难道只能反复通过增加 System.out 或通过加日志再重新发布吗?
- 线上的代码为什么没有执行到这里?是由于代码没有 commit?还是搞错了分支?
- 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
- 是否有一个全局视角来查看系统的运行状况?
- 有什么办法可以监控到JVM的实时运行状态?
二、安装方式
1.1 window 安装方式
下载地址:http://search.maven.org/classic/#search%7Cga%7C1%7Cg%3A%22com.taobao.arthas%22%20AND%20a%3A%22arthas-packaging%22
下载完成后,解压缩,如下图所示::
在Download
栏下载最新的 bin.zip
包,解压后在bin目录有 as.bat
。此脚本暂时只接受一个参数 pid,即只能诊断本机上的 Java 进程。
启动命令为:
as.bat <pid>
注:我在 window 10 上面启动的时候遇到如下问题,
D:\download\arthas-packaging-3.0.4-bin>telnet
'telnet' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
解决办法为:“控制面板” ——> “启动或关闭Windows功能” ——> 勾选 “Telnet 功能”
1.2 Linux 安装方式
安装Arthas:
curl -L https://alibaba.github.io/arthas/install.sh | sh
启动Arthas:
./as.sh
成功启动后,会看到如下界面。
三、常用命令
首先,在窗口中,输入 help 查看一下所有提供的可用命令(他的通信本质是通过 telnet 协议来通信的),如下图:
Attach success.
Connecting to arthas server... current timestamp is 1537266148
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki: https://alibaba.github.io/arthas
version: 3.0.4
pid: 25206
timestamp: 1537266148841
$ help
NAME DESCRIPTION
help Display Arthas Help
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
dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info.
dump Dump class byte array from JVM
options View and change various Arthas options
cls Clear the screen
reset Reset all the enhanced classes
version Display Arthas version
shutdown Shut down Arthas server and exit the console
session Display current session information
sysprop Display, and change the system properties.
redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...)
$
这里主要说一下 watch ,这个命令对变量进行数据监测。
==========================================================================
首先贴上我的测试代码:
package com.oct.tail;
import java.util.UUID;
/**
* @Author Ryan
* @Date 2018/9/18 9:58
* @desc
*/
public class OtherTestCase {
/**
*
* @return
*/
public static String uuid(){
return UUID.randomUUID().toString().replaceAll("-", "");
}
public static void main(String[] args) {
while(true){
System.out.println("uuid = " + uuid());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
如下我做一个示例 ( 本人是基于 Window 10 , JDK 8 环境, Linux 雷同),(对于 watch 命令我假装不知道如何使用,立即输入 watch help 来看看会有什么玩意儿):
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki: https://alibaba.github.io/arthas
version: 3.0.4
pid: 11924
timestamp: 1537326702039
$ watch -help
USAGE:
watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-
pattern method-pattern express [condition-express]
SUMMARY:
Display the input/output parameter, return object, and thrown exception of specif
ied method invocation
The express may be one of the following expression (evaluated dynamically):
target : the object
clazz : the object's class
method : the constructor or method
params[0..n] : the parameters of method
returnObj : the returned object of method
throwExp : the throw exception of method
isReturn : the method ended by return
isThrow : the method ended by throwing exception
#cost : the execution time in ms of method invocation
Examples:
watch -Eb org\.apache\.commons\.lang\.StringUtils isBlank params[0]
watch -b org.apache.commons.lang.StringUtils isBlank params[0]
watch -f org.apache.commons.lang.StringUtils isBlank returnObj
watch -bf *StringUtils isBlank params[0]
watch *StringUtils isBlank params[0]
watch *StringUtils isBlank params[0] params[0].length==1
watch *StringUtils isBlank '#cost>100'
WIKI:
https://alibaba.github.io/arthas/watch
OPTIONS:
-b, --before Watch before invocation
-e, --exception Watch after throw exception
-x, --expand <value> Expand level of object (1 by default)
-f, --finish Watch after invocation, enable by default
-h, --help this help
-n, --limits <value> Threshold of execution times
-E, --regex Enable regular expression to match (wildcard matching b
y default)
-M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1
024 by default)
-s, --success Watch after successful invocation
<class-pattern> The full qualified class name you want to watch
<method-pattern> The method name you want to watch
<express> the content you want to watch, written by ognl.
Examples:
params[0]
'params[0]+params[1]'
returnObj
throwExp
target
clazz
method
<condition-express> Conditional expression in ognl style, for example:
TRUE : 1==1
TRUE : true
FALSE : false
TRUE : 'params.length>=0'
FALSE : 1==2
$
在这里,我们针对方法 uuid() 返回值进行监测。监测结果如下:
$
$
$ watch -f com.oct.tail.OtherTestCase uuid returnObj
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 18 ms.
ts=2018-09-19 11:13:48;result=@String[26c80eb505664dbcb14f8d810fb4811c]
ts=2018-09-19 11:13:49;result=@String[fc03c43864f94372b646ce6253d90646]
ts=2018-09-19 11:13:50;result=@String[55ff41e0d66347c2bc75ab8ff4ffda4e]
ts=2018-09-19 11:13:51;result=@String[c504388c0aa74458a41a1b3a77c3d536]
ts=2018-09-19 11:13:52;result=@String[18d59c09ffde4c7aab15feb88b3e433f]
ts=2018-09-19 11:13:53;result=@String[c19dd8c1e5f8442696c8f886e81e74d5]
ts=2018-09-19 11:13:54;result=@String[d37a74aa502f4897aa1ed84dc69b83d8]
ts=2018-09-19 11:13:55;result=@String[cc11753b6f424c1e9a6a1ab36f334349]
ts=2018-09-19 11:13:56;result=@String[75a9b3c0bed4426d9363168912f16d74]
ts=2018-09-19 11:13:57;result=@String[f13022118e5a4115800a6eacc480e6a8]
简直方便的死去活来!太感动了。
总结,这个工具不止这里介绍的功能,还有其他的用法及命令,详细信息, 请移步官网(https://alibaba.github.io/arthas/sm.html)