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
成功啓動後,會看到如下界面。
三、常用命令
3.1 基礎命令
help——查看命令幫助信息
cls——清空當前屏幕區域
session——查看當前會話的信息
reset——重置增強類,將被 Arthas 增強過的類全部還原,Arthas 服務端關閉時會重置所有增強過的類
version——輸出當前目標 Java 進程所加載的 Arthas 版本號
quit——退出當前 Arthas 客戶端,其他 Arthas 客戶端不受影響
shutdown——關閉 Arthas 服務端,所有 Arthas 客戶端全部退出
keymap——Arthas快捷鍵列表及自定義快捷鍵
jvm相關
dashboard——當前系統的實時數據面板
thread——查看當前 JVM 的線程堆棧信息
jvm——查看當前 JVM 的信息
sysprop——查看和修改JVM的系統屬性
New! getstatic——查看類的靜態屬性
class/classloader相關
sc——查看JVM已加載的類信息
sm——查看已加載類的方法信息
dump——dump 已加載類的 byte code 到特定目錄
redefine——加載外部的.class文件,redefine到JVM裏
jad——反編譯指定已加載類的源碼
classloader——查看classloader的繼承樹,urls,類加載信息,使用classloader去getResource
monitor/watch/trace相關
請注意,這些命令,都通過字節碼增強技術來實現的,會在指定類的方法中插入一些切面來實現數據統計和觀測,因此在線上、預發使用時,請儘量明確需要觀測的類、方法以及條件,診斷結束要執行 shutdown 或將增強過的類執行 reset 命令。
monitor——方法執行監控
watch——方法執行數據觀測
trace——方法內部調用路徑,並輸出方法路徑上的每個節點上耗時
stack——輸出當前方法被調用的調用路徑
tt——方法執行數據的時空隧道,記錄下指定方法每次調用的入參和返回信息,並能對這些不同的時間下調用進行觀測
options
options——查看或設置Arthas全局開關
管道
Arthas支持使用管道對上述命令的結果進行進一步的處理,如sm org.apache.log4j.Logger | grep
grep——搜索滿足條件的結果
plaintext——將命令的結果去除顏色
wc——按行統計輸出結果
Web Console
通過websocket連接Arthas。
Web Console
其他特性
-
異步命令支持
-
執行結果存日誌
-
批處理的支持
-
ognl表達式的用法說明
3.2 使用示例
首先,在窗口中,輸入 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]
簡直方便的死去活來!太感動了。