阿里開源的 java 診斷工具—— Arthas
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;[email protected][26c80eb505664dbcb14f8d810fb4811c]
ts=2018-09-19 11:13:49;[email protected][fc03c43864f94372b646ce6253d90646]
ts=2018-09-19 11:13:50;[email protected][55ff41e0d66347c2bc75ab8ff4ffda4e]
ts=2018-09-19 11:13:51;[email protected][c504388c0aa74458a41a1b3a77c3d536]
ts=2018-09-19 11:13:52;[email protected][18d59c09ffde4c7aab15feb88b3e433f]
ts=2018-09-19 11:13:53;[email protected][c19dd8c1e5f8442696c8f886e81e74d5]
ts=2018-09-19 11:13:54;[email protected][d37a74aa502f4897aa1ed84dc69b83d8]
ts=2018-09-19 11:13:55;[email protected][cc11753b6f424c1e9a6a1ab36f334349]
ts=2018-09-19 11:13:56;[email protected][75a9b3c0bed4426d9363168912f16d74]
ts=2018-09-19 11:13:57;[email protected][f13022118e5a4115800a6eacc480e6a8]
簡直方便的死去活來!太感動了。