文章目錄
- 啟動 math-game
- 啟動 arthas
- 查看 dashboard
- 通過 thread 命令來獲取到math-game進程的 Main Class
- 通過 jad 來反編譯 Main Class
- 退出 arthas
- 界面
- linux服務器掛不上進程怎么辦?
- 核心表達式變量
- loader 本次調用類所在的 ClassLoader
- clazz 本次調用類的 Class 引用
- method 本次調用方法反射引用
- target 本次調用類的實例
- params 本次調用參數列表,這是一個數組,如果方法是無參方法則為空數組
- returnObj 本次調用返回的對象。當且僅當 isReturn==true 成立時候有效,表明方法調用是以正常返回的方式結束。如果當前方法無返回值 void,則值為 null
- throwExp 本次調用拋出的異常。當且僅當 isThrow==true 成立時有效,表明方法調用是以拋出異常的方式結束。
- isBefore 輔助判斷標記,當前的通知節點有可能是在方法一開始就通知,此時 isBefore==true 成立,同時 #### isThrow==false 和 isReturn==false,因為在方法剛開始時,還無法確定方法調用將會如何結束。
- isThrow 輔助判斷標記,當前的方法調用以拋異常的形式結束。
- isReturn 輔助判斷標記,當前的方法調用以正常返回的形式結束。
- 命令列表
- jvm 相關。
- dashboard - 當前系統的實時數據面板
- getstatic - 查看類的靜態屬性
- heapdump - dump java heap, 類似 jmap 命令的 heap dump 功能
- jvm - 查看當前 JVM 的信息
- logger - 查看和修改 logger
- mbean - 查看 Mbean 的信息
- memory - 查看 JVM 的內存信息
- ognl - 執行 ognl 表達式
- perfcounter - 查看當前 JVM 的 Perf Counter 信息
- sysenv - 查看 JVM 的環境變量
- sysprop - 查看和修改 JVM 的系統屬性
- thread - 查看當前 JVM 的線程堆棧信息
- vmoption - 查看和修改 JVM 里診斷相關的 option
- vmtool - 從 jvm 里查詢對象,執行 forceGc
- class/classloader 相關
- classloader - 查看 classloader 的繼承樹,urls,類加載信息,使用 classloader 去 getResource
- dump - dump 已加載類的 byte code 到特定目錄
- jad - 反編譯指定已加載類的源碼
- mc - 內存編譯器,內存編譯.java文件為.class文件
- mc報錯亂碼
- redefine - 加載外部的.class文件,redefine 到 JVM 里
- retransform - 加載外部的.class文件,retransform 到 JVM 里
- sc - 查看 JVM 已加載的類信息
- sc文檔(界面)
- sc文檔(命令行)
- sm - 查看已加載類的方法信息
- options
- options unsafe <boolean>
- options strict <boolean>
- options文檔(界面)
- options文檔(命令行)
- 當前目錄是哪里
- 其他
- 官網地址
arthas是一款監控工具,內存、進程等。還是阿里的,阿里出品,必定精品,來一起看一下吧。
其實官網文檔已經寫的非常好了,這里再寫一遍主要為了加深記憶 。
由于arthas內容太多,博客容易出bug,分為幾個文章吧。
啟動 math-game
curl -O https://arthas.aliyun.com/math-game.jar
java -jar math-game.jar
math-game是一個簡單的程序,每隔一秒生成一個隨機數,再執行質因數分解,并打印出分解結果。
剛看到這,我也蒙了,是不是進錯網站了?后來才發現原來這是個進程,用來測試的。
啟動 arthas
在命令行下面執行(使用和目標進程一致的用戶啟動,否則可能 attach 失敗):
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar
執行該程序的用戶需要和目標進程具有相同的權限。比如以admin用戶來執行:sudo su admin && java -jar arthas-boot.jar 或 sudo -u admin -EH java -jar arthas-boot.jar。
如果 attach 不上目標進程,可以查看~/logs/arthas/ 目錄下的日志。
如果下載速度比較慢,可以使用 aliyun 的鏡像:java -jar arthas-boot.jar --repo-mirror aliyun --use-http
java -jar arthas-boot.jar -h
打印更多參數信息。
選擇應用 java 進程:
$ $ java -jar arthas-boot.jar
* [1]: 35542[2]: 71560 math-game.jar
math-game進程是第 2 個,則輸入 2,再輸入回車/enter。Arthas 會 attach 到目標進程上,并輸出日志:
[INFO] Try to attach process 71560
[INFO] Attach process 71560 success.
[INFO] arthas-client connect 127.0.0.1 3658,---. ,------. ,--------.,--. ,--. ,---. ,---./ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'wiki: https://arthas.aliyun.com/doc
version: 3.0.5.20181127201536
pid: 71560
time: 2018-11-28 19:16:24$
出現這個$
符號,說明掛上了。(實測在自己電腦上成功,在linux服務器上掛不上,估計是哪里有問題)
查看 dashboard
輸入dashboard,按回車/enter,會展示當前進程的信息,按ctrl+c可以中斷執行。
通過 thread 命令來獲取到math-game進程的 Main Class
thread 1會打印線程 ID 1 的棧,通常是 main 函數的線程。
$ thread 1 | grep 'main('at demo.MathGame.main(MathGame.java:17)
通過 jad 來反編譯 Main Class
$ jad demo.MathGame
牛,居然有這個功能。而且反編譯出來和源代碼一模一樣。。。
退出 arthas
如果只是退出當前的連接,可以用quit或者exit命令。Attach 到目標進程上的 arthas 還會繼續運行,端口會保持開放,下次連接時可以直接連接上。
如果想完全退出 arthas,可以執行stop命令。
界面
arthas是支持界面的,默認端口是3658。
linux服務器掛不上進程怎么辦?
服務可以啟動,輸入序號按回車,發現沒有反應。
核心表達式變量
這里列一個表格來說明不同變量的含義
變量名 變量解釋
loader 本次調用類所在的 ClassLoader
clazz 本次調用類的 Class 引用
method 本次調用方法反射引用
target 本次調用類的實例
params 本次調用參數列表,這是一個數組,如果方法是無參方法則為空數組
returnObj 本次調用返回的對象。當且僅當 isReturn==true 成立時候有效,表明方法調用是以正常返回的方式結束。如果當前方法無返回值 void,則值為 null
throwExp 本次調用拋出的異常。當且僅當 isThrow==true 成立時有效,表明方法調用是以拋出異常的方式結束。
isBefore 輔助判斷標記,當前的通知節點有可能是在方法一開始就通知,此時 isBeforetrue 成立,同時 #### isThrowfalse 和 isReturn==false,因為在方法剛開始時,還無法確定方法調用將會如何結束。
isThrow 輔助判斷標記,當前的方法調用以拋異常的形式結束。
isReturn 輔助判斷標記,當前的方法調用以正常返回的形式結束。
所有變量都可以在表達式中直接使用,如果在表達式中編寫了不符合 OGNL 腳本語法或者引入了不在表格中的變量,則退出命令的執行;用戶可以根據當前的異常信息修正條件表達式或觀察表達式
命令列表
命令列表和核心表達式應該是互通的,這個我試驗下。
在arthas命令窗口輸入help也可以查看。
jvm 相關。
dashboard - 當前系統的實時數據面板
getstatic - 查看類的靜態屬性
查看類的靜態屬性。
要點:
1、必須是屬性(不能看整個類)。
2、必須是靜態。
如果輸入非靜態方法,肯定找不到。提示:
getstatic: no matched static field was found
例1:
getstatic java.math.BigDecimal ZERO
# 這個對象有點復雜,因為返回的不是一個值,而是一個BigDecimal.ZERO實例。
例2:
getstatic java.lang.String serialVersionUID
[arthas@23052]$ getstatic java.lang.String serialVersionUID
輸出:
field: serialVersionUID
@Long[-6849794470754667710]
Affect(row-cnt:1) cost in 4 ms.
當然getstatic也可以用ognl替代。
如:
$ ognl @java.lang.String@serialVersionUID
輸出:
@Long[-6849794470754667710]
對比下寫法吧。
getstatic寫法:
getstatic java.lang.String serialVersionUID
ognl寫法:
ognl @java.lang.String@serialVersionUID
heapdump - dump java heap, 類似 jmap 命令的 heap dump 功能
jvm - 查看當前 JVM 的信息
logger - 查看和修改 logger
mbean - 查看 Mbean 的信息
memory - 查看 JVM 的內存信息
ognl - 執行 ognl 表達式
perfcounter - 查看當前 JVM 的 Perf Counter 信息
sysenv - 查看 JVM 的環境變量
sysprop - 查看和修改 JVM 的系統屬性
thread - 查看當前 JVM 的線程堆棧信息
vmoption - 查看和修改 JVM 里診斷相關的 option
vmtool - 從 jvm 里查詢對象,執行 forceGc
class/classloader 相關
classloader - 查看 classloader 的繼承樹,urls,類加載信息,使用 classloader 去 getResource
dump - dump 已加載類的 byte code 到特定目錄
jad - 反編譯指定已加載類的源碼
mc - 內存編譯器,內存編譯.java文件為.class文件
mc例子:
mc c:/Users/PC/Desktop/jad-demo/StartupApplication.Java
mc報錯亂碼
redefine - 加載外部的.class文件,redefine 到 JVM 里
retransform - 加載外部的.class文件,retransform 到 JVM 里
sc - 查看 JVM 已加載的類信息
sc 不加任何參數
sc sc com.example.controller.RedisController
# 那么只會顯示幾條結果
sc -d 顯示詳細信息
-d --detail
[arthas@18344]$ sc -d com.example.controller.RedisController
輸出:class-info com.example.controller.RedisControllercode-source /D:/longhu/code4/BW_LongHu-list/apollo_improve_etax6/BW_LongHu/custom-web/target/classes/name com.example.controller.RedisControllerisInterface falseisAnnotation falseisEnum falseisAnonymousClass falseisArray falseisLocalClass falseisMemberClass falseisPrimitive falseisSynthetic falsesimple-name RedisControllermodifier publicannotation org.springframework.web.bind.annotation.RestController,org.springframework.web.bind.annotation.RequestMappinginterfacessuper-class +-java.lang.Objectclass-loader +-sun.misc.Launcher$AppClassLoader@18b4aac2+-sun.misc.Launcher$ExtClassLoader@54bff557classLoaderHash 18b4aac2Affect(row-cnt:1) cost in 11 ms.
sc -f 顯示變量信息
sc -d -f com.example.controller.RedisController
-f --field 顯示所有變量信息,注:需要和-d一起使用,單獨使用-f沒有效果
報文這里略了。
變量都會展示,如:
- 成員變量
- logger
- 注入的對象等
sc -cs 查找類加載器
sc文檔(界面)
參數名稱 | 參數說明 |
---|---|
class-pattern | 類名表達式匹配 |
method-pattern | 方法名表達式匹配 |
[d] | 輸出當前類的詳細信息,包括這個類所加載的原始文件來源、類的聲明、加載的 ClassLoader 等詳細信息。 如果一個類被多個 ClassLoader 所加載,則會出現多次 |
[E] | 開啟正則表達式匹配,默認為通配符匹配 |
[f] | 輸出當前類的成員變量信息(需要配合參數-d 一起使用) |
[x:] | 指定輸出靜態變量時屬性的遍歷深度,默認為 0,即直接使用 toString 輸出 |
[c:] | 指定 class 的 ClassLoader 的 hashcode |
[classLoaderClass:] | 指定執行表達式的 ClassLoader 的 class name |
[n:] | 具有詳細信息的匹配類的最大數量(默認為 100) |
[cs <arg>] | 指定 class 的 ClassLoader#toString() 返回值。長格式[classLoaderStr <arg>] |
提示:
class-pattern 支持全限定名,如 com.taobao.test.AAA,也支持 com/taobao/test/AAA 這樣的格式,這樣,我們從異常堆棧里面把類名拷貝過來的時候,不需要在手動把/替換為.啦。
提示:
sc 默認開啟了子類匹配功能,也就是說所有當前類的子類也會被搜索出來,想要精確的匹配,請打開options disable-sub-class true開關
sc文檔(命令行)
[arthas@18344]$ help scUSAGE:sc [-c <value>] [--classLoaderClass <value>] [-cs <value>] [-d] [-x <value>] [-f] [-h] [-n <value>] [-E] class-patternSUMMARY:Search all the classes loaded by JVMEXAMPLES:sc -d org.apache.commons.lang.StringUtilssc -d org/apache/commons/lang/StringUtilssc -d *StringUtilssc -d -f org.apache.commons.lang.StringUtilssc -E org\\.apache\\.commons\\.lang\\.StringUtilsWIKI:https://arthas.aliyun.com/doc/scOPTIONS:-c, --classloader <value> The hash code of the special class's classLoader--classLoaderClass <value> The class name of the special class's classLoader.-cs, --classLoaderStr <value> The return value of the special class's ClassLoader#toString().-d, --details Display the details of class-x, --expand <value> Expand level of object (0 by default)-f, --field Display all the member variables-h, --help this help-n, --limits <value> Maximum number of matching classes with details (100 by default)-E, --regex Enable regular expression to match (wildcard matching by default)<class-pattern> Class name pattern, use either '.' or '/' as separator
sm - 查看已加載類的方法信息
monitor/watch/trace
options
options是設置選項用的,有很多有用的功能。
options unsafe
unsafe開關。
options unsafe true # 打開(慎用,會允許一些不安全的操作)
options unsafe false # 關閉(默認)
這個居然把值改了,厲害:
ognl '@com.example.controller.RedisController@setAge(2)' # 設置值(前提有這個方法)
ognl '@com.example.controller.RedisController@age' # 查看值
注:這個值應該是靜態變量,而且要加上get、set方法。非靜態變量不能直接獲取。(這難道就是為什么叫做get方法的原因嗎?)
options strict
options文檔(界面)
直接把官網界面復制過來(勿動)
名稱 | 默認值 | 描述 |
---|---|---|
unsafe | false | 是否支持對系統級別的類進行增強,打開該開關可能導致把 JVM 搞掛,請慎重選擇! |
dump | false | 是否支持被增強了的類 dump 到外部文件中,如果打開開關,class 文件會被 dump 到/${application working dir}/arthas-class-dump/ 目錄下,具體位置詳見控制臺輸出 |
batch-re-transform | true | 是否支持批量對匹配到的類執行 retransform 操作 |
json-format | false | 是否支持 json 化的輸出 |
disable-sub-class | false | 是否禁用子類匹配,默認在匹配目標類的時候會默認匹配到其子類,如果想精確匹配,可以關閉此開關 |
support-default-method | true | 是否支持匹配到 default method, 默認會查找 interface,匹配里面的 default method。參考 #1105 |
save-result | false | 是否打開執行結果存日志功能,打開之后所有命令的運行結果都將保存到~/logs/arthas-cache/result.log 中 |
job-timeout | 1d | 異步后臺任務的默認超時時間,超過這個時間,任務自動停止;比如設置 1d, 2h, 3m, 25s,分別代表天、小時、分、秒 |
print-parent-fields | true | 是否打印在 parent class 里的 filed |
verbose | false | 是否打印更多詳細信息 |
strict | true | 是否啟用 strict 模式 |
options文檔(命令行)
[arthas@22560]$ options
輸出:LEVEL TYPE NAME VALUE SUMMARY DESCRIPTION
------------------------------------------------------------------------------------------------------------------------------------0 boolean unsafe false Option to support system-l This option enables to proxy functionality of JVM classevel class es. Due to serious security risk a JVM crash is possibly be introduced. Do not activate it unless you are ableto manage.1 boolean dump false Option to dump the enhance This option enables the enhanced classes to be dumped td classes o external file for further de-compilation and analysis.1 boolean batch-re-transfor true Option to support batch re This options enables to reTransform classes with batchm Transform Class mode.2 boolean json-format false Option to support JSON for This option enables to format object output with JSON wmat of object output hen -x option selected.1 boolean disable-sub-class false Option to control include This option disable to include sub class when matchingsub class when class match class.ing1 boolean support-default-m true Option to control include This option disable to include default method in interfethod default method in interfac ace when matching class.e when class matching1 boolean save-result false Option to print command's This option enables to save each command's result to loresult to log file g file, which path is ${user.home}/logs/arthas-cache/result.log.2 String job-timeout 1d Option to job timeout This option setting job timeout,The unit can be d, h, m, s for day, hour, minute, second. 1d is one day in default1 boolean print-parent-fiel true Option to print all fileds This option enables print files in parent class, defaulds in parent class t value true.1 boolean verbose false Option to print verbose in This option enables print verbose information, defaultformation value false.1 boolean strict true Option to strict mode By default, strict mode is true, not allowed to set object properties. Want to set object properties, execute`options strict false`
當前目錄是哪里
有時候會比較迷惑。
實際比較簡單,打開cmd路徑是什么,就是當前的home目錄。
例如在桌面打開cmd:
C:\Users\PC>
# C:\Users\PC 就是home目錄。
其他
官網地址
https://arthas.aliyun.com/doc/
官網信息已經很全了,從用法到例子一應俱全。