目錄
背景
參數(shù)說明
參考
背景
watch能方便的觀察到指定函數(shù)的調(diào)用情況。能觀察到的范圍為:返回值
、拋出異常
、入?yún)?/code>,通過編寫 OGNL 表達式進行對應(yīng)變量的查看。ognl學(xué)習(xí),可以參考:https://xiaopanjia.blog.csdn.net/article/details/130429470
參數(shù)說明
watch 的參數(shù)比較多,主要是因為它能在 4 個不同的場景觀察對象
參數(shù)名稱 | 參數(shù)說明 |
---|---|
class-pattern | 類名表達式匹配 |
method-pattern | 函數(shù)名表達式匹配 |
express | 觀察表達式,默認值:{params, target, returnObj} |
condition-express | 條件表達式 |
[b] | 在函數(shù)調(diào)用之前觀察 |
[e] | 在函數(shù)異常之后觀察 |
[s] | 在函數(shù)返回之后觀察 |
[f] | 在函數(shù)結(jié)束之后(正常返回和異常返回)觀察 |
[E] | 開啟正則表達式匹配,默認為通配符匹配 |
[x:] | 指定輸出結(jié)果的屬性遍歷深度,默認為 1,最大值是 4 |
[m <arg>] |
指定 Class 最大匹配數(shù)量,默認值為 50。長格式為[maxMatch <arg>] 。 |
[n] | 設(shè)置執(zhí)行的次數(shù) |
特別說明:
1、注意函數(shù)入?yún)?/code>和
函數(shù)出參
的區(qū)別,有可能在中間被修改導(dǎo)致前后不一致,除了?-b
?事件點?params
?代表函數(shù)入?yún)⑼?,其余事件都代表函?shù)出參
2、當(dāng)使用?-b
?時,由于觀察事件點是在函數(shù)調(diào)用前,此時返回值或異常均不存在
3、4 個觀察事件點?-b
、-e
、-s
?默認關(guān)閉,-f
?默認打開,當(dāng)指定觀察點被打開后,在相應(yīng)事件點會對觀察表達式進行求值并輸出
例如:
1、觀察函數(shù)調(diào)用返回時的參數(shù)、this 對象和返回值
$ watch demo.MathGame primeFactors -x 2
method=demo.MathGame.primeFactors location=AtExit
ts=2021-08-31 15:22:58; [cost=1.020982ms] result=@ArrayList[
@Object[][
@Integer[1],
],
@MathGame[
random=@Random[java.util.Random@31cefde0],
illegalArgumentCount=@Integer[44],
],
@ArrayList[
@Integer[2],
@Integer[2],
@Integer[26947],
],
]
2、觀察函數(shù)調(diào)用入口的參數(shù)和返回值
$ watch demo.MathGame primeFactors "{params,returnObj}" -x 2 -b
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 50 ms.
ts=2018-12-03 19:23:23; [cost=0.0353ms] result=@ArrayList[
@Object[][
@Integer[-1077465243],
],
null,
]
事件點為函數(shù)執(zhí)行前,因此獲取不到返回值。
3、同時觀察函數(shù)調(diào)用前和函數(shù)返回后
$ watch demo.MathGame primeFactors "{params,target,returnObj}" -x 2 -b -s -n 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 46 ms.
ts=2018-12-03 19:29:54; [cost=0.01696ms] result=@ArrayList[
@Object[][
@Integer[1],
],
@MathGame[
random=@Random[java.util.Random@522b408a],
illegalArgumentCount=@Integer[13038],
],
null,
]
ts=2018-12-03 19:29:54; [cost=4.277392ms] result=@ArrayList[
@Object[][
@Integer[1],
],
@MathGame[
random=@Random[java.util.Random@522b408a],
illegalArgumentCount=@Integer[13038],
],
@ArrayList[
@Integer[2],
@Integer[2],
@Integer[2],
@Integer[5],
@Integer[5],
@Integer[73],
@Integer[241],
@Integer[439],
],
]
特殊說明:
-
參數(shù)里
-n 2
,表示只執(zhí)行兩次 -
這里輸出結(jié)果中,第一次輸出的是函數(shù)調(diào)用前的觀察表達式的結(jié)果,第二次輸出的是函數(shù)返回后的表達式的結(jié)果
-
結(jié)果的輸出順序和事件發(fā)生的先后順序一致,和命令中?
-s -b
?的順序無關(guān)
4、條件表達式的例子
只有滿足條件的調(diào)用,才會有響應(yīng)
$ watch demo.MathGame primeFactors "{params[0],target}" "params[0]<0"
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 68 ms.
ts=2018-12-03 19:36:04; [cost=0.530255ms] result=@ArrayList[
@Integer[-18178089],
@MathGame[demo.MathGame@41cf53f9],
]
5、觀察異常信息的例子
$ watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 62 ms.
ts=2018-12-03 19:38:00; [cost=1.414993ms] result=@ArrayList[
@Integer[-1120397038],
java.lang.IllegalArgumentException: number is: -1120397038, need >= 2
at demo.MathGame.primeFactors(MathGame.java:46)
at demo.MathGame.run(MathGame.java:24)
at demo.MathGame.main(MathGame.java:16)
,
]
特別說明:
-
-e
表示拋出異常時才觸發(fā) - express 中,表示異常信息的變量是
throwExp
6、按照耗時進行過濾
$ watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 66 ms.
ts=2018-12-03 19:40:28; [cost=2112.168897ms] result=@ArrayList[
@Object[][
@Integer[1],
],
@ArrayList[
@Integer[5],
@Integer[428379493],
],
]
-
#cost>200
(單位是ms
)表示只有當(dāng)耗時大于 200ms 時才會輸出,過濾掉執(zhí)行時間小于 200ms 的調(diào)用
7、觀察當(dāng)前對象中的屬性
查看函數(shù)運行前后,當(dāng)前對象中的屬性,可以使用target
關(guān)鍵字,代表當(dāng)前對象。然后使用target.field_name
訪問當(dāng)前對象的某個屬性。
$ watch demo.MathGame primeFactors 'target'
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 52 ms.
ts=2018-12-03 19:41:52; [cost=0.477882ms] result=@MathGame[
random=@Random[java.util.Random@522b408a],
illegalArgumentCount=@Integer[13355],
]
$ watch demo.MathGame primeFactors 'target.illegalArgumentCount'
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 67 ms.
ts=2018-12-03 20:04:34; [cost=131.303498ms] result=@Integer[8]
ts=2018-12-03 20:04:35; [cost=0.961441ms] result=@Integer[8]
8、排除掉指定的類
使用?--exclude-class-pattern
?參數(shù)可以排除掉指定的類,比如:
watch javax.servlet.Filter * --exclude-class-pattern com.demo.TestFilter
9、投影(Across)
簡單的說,就是查看集合中元素的屬性或者方法返回值
watch com.example.httpclientdemo.HttpclientDemoApplication doSend 'params[0].{#this.username}' -b -x 2
Press Q or Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:1) cost in 28 ms.
ts=2019-09-26 15:19:49; [cost=0.006187ms] result=@ArrayList[
@String[u0],
@String[u1],
@String[u2],
@String[u3],
@String[u4],
@String[u5],
@String[u6],
@String[u7],
@String[u8],
@String[u9],
]
watch com.example.httpclientdemo.HttpclientDemoApplication doSend 'params[0].{? #this.id > 8}' -b -x 2
Press Q or Ctrl+C to abort.
Affect(class-cnt:2 , method-cnt:1) cost in 33 ms.
ts=2019-09-26 15:23:10; [cost=0.006812ms] result=@ArrayList[
@User[
username=@String[u9],
id=@Long[9],
],
]
在投影中過濾后計數(shù)
watch com.example.HttpclientDemoApplication doSend 'params[0].{? #this.username.endsWith("9")}.size()'
選擇第一個匹配項
watch com.example.HttpclientDemoApplication doSend 'params[0].{^#this.username.startsWith("u")}' -b -x 2
選擇最后一個匹配項
watch com.example.HttpclientDemoApplication doSend 'params[0].{$#this.username.startsWith("u")}' -b -x 2
?10、條件表達式和觀察表達式
ognl有兩種表達式,條件表達式和觀察表達式,條件表達式關(guān)注于這次請求能不能攔截到,觀察表達式關(guān)注于這次請求打印出什么東西,兩者區(qū)別如下:
條件表達式過濾的是一次調(diào)用,判斷該次調(diào)用能否返回
觀察表達式里的過濾,過濾的是該次調(diào)用的數(shù)據(jù),不管怎么寫,該次調(diào)用一定返回
案例如下:
watch com.example.HttpclientDemoApplication doSend
'params[0].{#this.username}' 'params[0].{? #this.id>7}.size()>0'
其中'params[0].{#this.username}’是觀察表達式,'params[0].{? #this.id>7}.size()>0’是條件表達式。
觀察表達式和條件表達式都可以用ognl。文章來源:http://www.zghlxwxcb.cn/news/detail-431635.html
參考
watch | arthas文章來源地址http://www.zghlxwxcb.cn/news/detail-431635.html
到了這里,關(guān)于arthas--watch函數(shù)執(zhí)行數(shù)據(jù)觀測的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!