日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

BTrace使用小结

發(fā)布時(shí)間:2024/1/17 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 BTrace使用小结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

簡(jiǎn)介

BTrace是一個(gè)安全的JVM動(dòng)態(tài)追蹤工具,最初為原Sun公司Kenai項(xiàng)目下面的一個(gè)子項(xiàng)目。

典型的使用場(chǎng)景是,“我要查個(gè)問(wèn)題,可那個(gè)方法沒(méi)有打印入口參數(shù)和返回結(jié)果日志”,“我想看某個(gè)方法的執(zhí)行耗時(shí)”,“我想查看某方法如System.GC()的調(diào)用棧”等等,這些都是BTrace可以小試牛刀的地方。它的優(yōu)勢(shì)是,直接attach應(yīng)用JVM,不用重啟應(yīng)用進(jìn)程,可比較快速方便地定位問(wèn)題。

不錯(cuò)的教程

如果想簡(jiǎn)單學(xué)習(xí)一下BTrace,推薦幾個(gè)不錯(cuò)的教程,建議先看看下面幾篇文章:

  • Btrace入門(mén)到熟練小工完全指南 by 江南白衣,強(qiáng)烈建議先讀它!

  • 如何在生產(chǎn)環(huán)境使用Btrace進(jìn)行調(diào)試 by 占小狼,點(diǎn)評(píng)同事,強(qiáng)烈建議讀

  • BTrace Github Wiki

  • BTrace User's Guide,原官方文檔

  • BTrace原理分析,進(jìn)階文章,日期比較早了,想深入了解的可以一看。

使用

下面是我學(xué)習(xí)BTrace的一點(diǎn)筆記和小結(jié)。不過(guò)還是先來(lái)個(gè)例子比較直觀。

一個(gè)例子

一個(gè)簡(jiǎn)單的例子,我想查看某工程下這個(gè)方法的入?yún)⒓胺祷刂?#xff0c;但代碼中沒(méi)有打印方法返回結(jié)果,這個(gè)時(shí)候可以用BTrace試一下。

DemoView com.package.name.Demo.getDemoView(long id)

編寫(xiě)跟蹤腳本MethodReturnTracing.java如下:

import static com.sun.btrace.BTraceUtils.println; import static com.sun.btrace.BTraceUtils.str;import com.sun.btrace.AnyType; import com.sun.btrace.BTraceUtils; import com.sun.btrace.annotations.BTrace; import com.sun.btrace.annotations.Kind; import com.sun.btrace.annotations.Location; import com.sun.btrace.annotations.OnMethod; import com.sun.btrace.annotations.Return; import com.sun.btrace.annotations.Self;/*** 打印方法入?yún)⒓胺祷刂? * Created by zhouwei on 2017-6-21.*/@BTrace(unsafe = true) // 表示這是一個(gè)BTrace跟蹤腳本,并啟用unsafe模式(因?yàn)槭褂昧薆TraceUtils以外的方法,即String.valueOf(obj)) public class MethodReturnTracing {@OnMethod(clazz = "com.package.name.Demo", // 類(lèi)的全限定名method = "getDemoView", // 方法名location = @Location(Kind.RETURN)) // 表示跟蹤某個(gè)類(lèi)的某個(gè)方法,位置為方法返回處public static void onMethodReturn(@Self Object self, long id, @Return AnyType result) { // @Return注解將上面被跟蹤方法的返回值綁定到此探查方法的參數(shù)上println(BTraceUtils.Time.timestamp("yyyy-MM-dd HH:mm:ss")); // 打印時(shí)間println("method self: " + str(self));println("method params: " + id); // 打印入?yún)rintln("method return: " + String.valueOf(result)); // 打印結(jié)果對(duì)象,因String.valueOf(obj)為外部方法,故需使用unsafe模式println("=========================="); // 強(qiáng)烈建議加上,否則會(huì)因?yàn)檩敵鼍彌_看不到最新打印結(jié)果}}

將上面的跟蹤腳本拷貝到測(cè)試服務(wù)器上,執(zhí)行:

$ btrace -u 24801 MethodReturnTracing.java 2017-12-03 14:20:22 method self: com.package.name.Demo@6ae7d3b4 method params: 19261 method return: DemoView(id:19261, contactName:測(cè)試聯(lián)系人, contactEmail:email, address:測(cè)試地址, ctime:1511871027, utime:1511871027, valid:1) ==========================

其中,-u表示使用BTrace的unsafe模式,24801為Java進(jìn)程ID,MethodReturnTracing.java為BTrace跟蹤腳本。下面是其跟蹤日志,打印出了當(dāng)前時(shí)間、方法入?yún)⒑头祷貙?duì)象。

除腳本中的注釋外,其它需要注意的點(diǎn)會(huì)在下面一一指出。

命令行啟動(dòng)

常用的三個(gè)命令:btrace用于將腳本attach應(yīng)用Java進(jìn)程,btracec用于編譯腳本,btracer用于帶著腳本啟動(dòng)Java進(jìn)程并同時(shí)attach。

$ btrace <PID> <trace_script> It will attach to the java application with the given PID and compile and submit the trace script. $ btracec <trace_script> It will compile the provided trace script. $ btracer <compiled_script> <args to launch a java app> It will start the specified java application with the btrace agent running and the script previously compiled by btracec loaded.

<trace_script>: Xxx.java,表示BTrace跟蹤腳本。

<compiled_script>: Xxx.class,表示編譯后的腳本。

常用注解介紹

下面是一些常用的注解,基本是從官網(wǎng)上或API文檔上摘抄下來(lái)的,未作翻譯。主要分兩類(lèi):

  • 用于注解探查方法(Action/probe Method),上面例子MethodReturnTracing.java中的onMethodReturn即稱(chēng)為探查方法,作用通常是打印跟蹤結(jié)果。

  • 用于注解探查方法的參數(shù)。例如上面例子MethodReturnTracing.java中的@Return AnyType result,用于將被跟蹤方法的返回值綁定到該探查方法的參數(shù)上。

注解探查方法(Action/probe Method Annotations)

@OnMethod(clazz=<cname_spec>[, method=<mname_spec>]? [, type=<signature>]? [, location=<location_spec>]?)

An action method annotated by this annotation is called when the matching method(s) reaches the specified location.

cname_spec = | + | /regex/

class_name is a fully qualified class name.

+class_name is a fully qualified class name prepended with +; means all subclasses and implementors of the prepended class name.

/regex/ is a standard regular expression used to identify the class names.

mname_spec = | /regex/

method_name is a simple method name (no signature or return type).

There is another way to abstractly specify traced class(es) and method(s). Traced classes and methods may be specified by annotation. For example, if the "clazz" attribute is specified as @javax.jws.WebService BTrace will instrument all classes that are annotated by the WebService annotation. Similarly, method level annotations may be used to specify methods abstractly.

@OnTimer

used to specify tracing actions that have to run periodically once every N milliseconds.

@OnError

used to specify actions that are run whenever any exception is thrown by tracing actions of some other probe.
BTrace method annotated by this annotation is called when any exception is thrown by any of the other BTrace action methods in the same BTrace class.

@OnExit

used to specify actions that are run when BTrace code calls "exit(int)" built-in function to finish the tracing "session".

@OnEvent

used to associate tracing methods with "external" events send by BTrace client.

@OnLowMemory

used to trace memory threshold exceed event.

@OnProbe

used to specify to avoid using implementation internal classes in BTrace scripts.

@Sampled

enables sampling for the annotated handler. To be used in conjunction with @OnMethod annotation.

其中,重點(diǎn)關(guān)注@OnMethod注解,最常用,用于跟蹤某個(gè)方法。

  • clazz 指明要被跟蹤的類(lèi)的全限制名,支持正則表達(dá)式和繼承,語(yǔ)法見(jiàn)說(shuō)明。

  • method 指明要被跟蹤的方法名,支持正則表達(dá)式。

  • type 指明要被跟蹤的方法的簽名。一般可以不聲明,絕大部分情況下依靠clazz和method即可確定要跟蹤的方法。

  • location 指明要跟蹤的方法的位置。具體可見(jiàn)@Location注解的說(shuō)明,例如@Location(Kind.RETURN)表示方法返回處,@Location(Kind.ENTRY)表示方法入口處。

注解探查方法的參數(shù)

這類(lèi)注解的作用是將被跟蹤方法的相關(guān)屬性(關(guān)注點(diǎn),如類(lèi)名、方法名、方法入?yún)ⅰ⒎祷刂怠?zhí)行時(shí)間、拋出的異常等等)綁定到探查方法的參數(shù)上,然后在探查方法內(nèi)作處理,如打印出來(lái)等等。例如@Duration用來(lái)捕獲方法執(zhí)行時(shí)間,@Return用于捕獲方法返回值(它倆都只能用于@Location(Kind.RETURN)的location下),等等。分別摘錄介紹如下。

@ProbeClassName

It is used to mark a probe method argument as the receiver of the probe target class name. Applicable only for OnMethod annotation. (對(duì)應(yīng)@OnMethod的clazz的名字)

@ProbeMethodName

It is used to mark a probe method argument as the receiver of the probe target method name. Applicable only for OnMethod annotation. (對(duì)應(yīng)@OnMethod的method的名字)

@Duration

It is used to mark a probe method argument as the receiver of the duration value. Applicable only for OnMethod annotation with Location value of Kind.RETURN or Kind.ERROR. (long, 納秒;或用在where.AFTER)

@Return

Marks a method parameter as the one that should contain the return value. (applicable only for Kind.RETURN)

@Self

Marks a method parameter as the one that should contain this instance. (對(duì)應(yīng)@OnMethod的clazz的對(duì)象)

@TargetInstance

It is used to mark a probe method argument as the receiver of called instance in Location = Kind.CALL. (對(duì)應(yīng)@Location的clazz的對(duì)象,如果是靜態(tài)方法,則返回null)

@TargetMethodOrField

It is used to mark a probe method argument as the receiver of called method name in Location = Kind.CALL. (對(duì)應(yīng)@Location的method的名字)

一點(diǎn)經(jīng)驗(yàn)

下面是我在使用BTrace的過(guò)程中積累的一點(diǎn)經(jīng)驗(yàn),希望對(duì)大家有用。

  • 請(qǐng)?jiān)谝呀?jīng)搭好的添加過(guò)依賴(lài)的maven工程中編寫(xiě)跟蹤腳本!Git地址如下:btrace samples。其中com.sun.btrace.samples包中的代碼為官方示例腳本,強(qiáng)烈建議看看;me.kopite.test下面為部分其它簡(jiǎn)單示例。

  • 將btrace上傳到服務(wù)器上,并設(shè)置環(huán)境變量,將btrace等命令加入命令行PATH中:

    首先,在目標(biāo)服務(wù)器(server)上執(zhí)行(使用nc命令):

    $ mkdir -p ~/zhouwei/btrace-bin-1.3.9 ; cd ~/zhouwei/btrace-bin-1.3.9 ; nc -l 8080 > btrace-bin-1.3.9.tgz ; tar zxvf btrace-bin-1.3.9.tgz ; export JAVA_HOME="/usr/local/java" ; export BTRACE_HOME=~/zhouwei/btrace-bin-1.3.9 ; export PATH=$BTRACE_HOME/bin:$PATH ; cd ~/zhouwei ; ll ; which btrace

    如果服務(wù)器上已經(jīng)有btrace包,則只需要執(zhí)行上面后半部分的命令來(lái)設(shè)置環(huán)境變量即可:

    $ export JAVA_HOME="/usr/local/java" ; export BTRACE_HOME=~/zhouwei/btrace-bin-1.3.9 ; export PATH=$BTRACE_HOME/bin:$PATH ; cd ~/zhouwei ; ll ; which btrace

    然后,在本地機(jī)器上執(zhí)行(serverIP即為目標(biāo)服務(wù)器的IP地址):

    $ nc $serverIP 8080 < ~/Downloads/btrace-bin-1.3.9.tgz

    按自己的需要事先寫(xiě)好命令,即可在需要時(shí)快速上傳和使用,節(jié)省時(shí)間提高效率。

    由于服務(wù)器一般有端口訪問(wèn)限制,請(qǐng)使用8080附近的端口。

    下載btrace-bin-1.3.9.tgz。

  • 用于匹配方法入?yún)⒒蚍祷仡?lèi)型時(shí),因嫌麻煩不想引入外部依賴(lài)(一般也沒(méi)有必要),外部類(lèi)型請(qǐng)用AnyType代替,而不是Object!因?yàn)槟憧赡苡肙bject來(lái)準(zhǔn)確匹配方法返回參數(shù)或返回類(lèi)型。例如上面例子MethodReturnTracing.java中的@Return AnyType result。

  • 由于BTrace的安全和性能考慮,一般情況下不允許在探查方法中調(diào)用BTraceUtils以外的其它方法,但可使用unsafe模式。

    例如使用String.valueOf()方法來(lái)打印對(duì)象(注意不要使用obj.toString(),因?yàn)閷?duì)象可能是null!),這時(shí)需要使用非安全模式@BTrace(unsafe = true),并使用-u選項(xiàng)btrace -u <PID> <trace_script>來(lái)啟動(dòng)跟蹤。

    例如使用JSON.toJSONString()來(lái)打印對(duì)象(使用-cp選項(xiàng)來(lái)指明外部依賴(lài)的jar路徑):
    btrace -u -cp .:$(find /apps/xxx_service -type f -name "fastjson-*.jar" 2>/dev/null | head -1) <PID> <trace_script>

    具體參考BTraceUtils.str(Object)方法說(shuō)明,由非bootstrap class loader加載的對(duì)象不會(huì)調(diào)用對(duì)象的toString()方法,但List里面的對(duì)象可以打印出來(lái)(因?yàn)長(zhǎng)ist對(duì)象是標(biāo)準(zhǔn)庫(kù)里的類(lèi),由bootstrap class loader加載,且List的toString()方法調(diào)用了對(duì)象的toString()方法)。

  • 如何在thrift客戶端攔截thrift接口調(diào)用?因?yàn)锽Trace不支持?jǐn)r截接口方法。通過(guò)查看調(diào)用棧,發(fā)現(xiàn)可以這么寫(xiě):clazz = "xxxThriftIface$Client",即攔截的類(lèi)名clazz為接口名后面加$Client。

  • 打印輸出有緩沖區(qū)延遲,故需要在探查方法的最后一行打印:println("=================================");

  • 其它:

    • 啟動(dòng)跟蹤腳本時(shí),請(qǐng)使用和啟動(dòng)Java進(jìn)程相同的Linux賬號(hào),不然會(huì)因?yàn)闄?quán)限問(wèn)題而attach失敗。

    • BTrace也可以用來(lái)跟蹤匿名內(nèi)部類(lèi)的方法,只不過(guò)clazz對(duì)應(yīng)的類(lèi)名里面有個(gè)"$"符號(hào),只要寫(xiě)對(duì)其類(lèi)名即可。

    • 對(duì)象構(gòu)造函數(shù)的名字是<init>,類(lèi)構(gòu)造器的名字是<clinit>。

    • 另一個(gè)和BTrace類(lèi)似的Java診斷工具greys-anatomy,由阿里釋出,感興趣的也可以學(xué)習(xí)一下。

    • 若報(bào)錯(cuò)"Port 2020 unavailable.",則使用btrace -p 2021 ...來(lái)指定其它端口。

    • Linux下已經(jīng)有個(gè)命令也叫btrace,注意別用混了。


  • ?

    總結(jié)

    以上是生活随笔為你收集整理的BTrace使用小结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。