Arthas 是 阿里巴巴最近開源出來的一個針對 java 的工具,主要是針對 java 的問題進行診斷!
一、概述
這個工具可以協助你做下面這些事情:
- 這個類是從哪個 jar 包加載而來的?
- 爲什麼會報各種類相關的 Exception?
- 線上遇到問題無法 debug 好蛋疼,難道只能反覆通過增加 System.out 或通過加日誌再重新發布嗎?
- 線上的代碼爲什麼沒有執行到這裏?是由於代碼沒有 commit?還是搞錯了分支?
- 線上遇到某個用戶的數據處理有問題,但線上同樣無法 debug,線下無法重現!
- 是否有一個全局視角來查看系統的運行狀況?
- 有什麼辦法可以監控到JVM的實時運行狀態?
二、安裝方式
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...)
$