BTrace latest realese:
release-1.2.5.1
BTrace guide(1.2-20101020):
http://kenai.com/projects/btrace/pages/UserGuide
例子:在压缩包内有。
现网碰到问题,目前需要拦截框架请求,并判断req中storeId字段为11008时,输出其他参数,方便进一步查问题。
需要拦截的类结构如图:
需要拦截match方法,match方法这里有好几个重载方法,目前只需要拦截第一个。
btrace代码:
(补充一点:在btrace代码中,没法去调用非BTraceUtils之外的方法,也就是说,要输出类各字段信息是没法通过toString()方法,因为调用instance.toString()方法是不安全,编译fail,需要自己去解析每个字段值)
package btrace; import com.sun.btrace.AnyType; import com.sun.btrace.annotations.*; import java.lang.reflect.Field; import static com.sun.btrace.BTraceUtils.*; /** * Created by vernonzheng on 14-8-15. */ @BTrace public class FrameFilterTrace { @OnMethod( clazz="com.skymobi.market.applist.service.imp.CustomizedFrameMatcher", method="match", type = "com.skymobi.market.applist.entity.FrameStorageMetadata (com.skymobi.market.applist.bean.common.TlvCommonHeaderReq)" ) public static void onMatch(AnyType request){ Class frameRequestCl = classOf(request); String storeId = getVal("storeId",frameRequestCl, request); Class tlvCommentHeaderReqCl = getSuperclass(frameRequestCl); Class networkTrackCl = getSuperclass(tlvCommentHeaderReqCl); Class terminalRequestCl = getSuperclass(networkTrackCl); if(storeId!=null){ if(compare(storeId,"10118")){ //asHsman,asHstype,bizSource,sessionId,channelNo,storeId,clientVer,hsman,hstype,mnc,imsi, //networkType,capability println(strcat("vernon----store_id=10118--输出参数 : ", str(request))); println(strcat("storeId:",storeId)); println(strcat("channelNo:",getVal("channelNo",frameRequestCl,request))); println(strcat("asHsman:",getVal("asHsman",frameRequestCl,request))); println(strcat("asHstype:",getVal("asHstype",frameRequestCl,request))); println(strcat("bizSource:",getVal("bizSource",tlvCommentHeaderReqCl,request))); println(strcat("sessionId:",getVal("sessionId",tlvCommentHeaderReqCl,request))); println(strcat("clientVer:",getVal("clientVer",tlvCommentHeaderReqCl,request))); println(strcat("hsman:",getVal("hsman",terminalRequestCl,request))); println(strcat("hstype:",getVal("hstype",terminalRequestCl,request))); println(strcat("mnc:",getVal("mnc",terminalRequestCl,request))); println(strcat("imsi:",getVal("imsi",terminalRequestCl,request))); println(strcat("networkType:",getVal("networkType",terminalRequestCl, request))); println("----------------------------------------------------------"); } } } private static String getVal(String filedName,Class cl,AnyType instance){ Field field = field(classOf(cl), filedName, false); if(field!=null) { return str(get(field, instance)); }else{ return "is null"; } } }
把FrameFilterTrace.java 和下载好的btrace-bin .tar.gz丢到现网上,解压btrace-bin .tar.gz,
把FrameFilterTrace.java拷贝到bin目录,
vi btrace脚本,在头上加上(export BTRACE_HOME=解压路径),如果JAVA_HOME没有设置,也export一下。
然后jps,找到jvm PID,
执行btrace PID FrameFilterTrace.java > catch.log
完成!
输出如图:
参考:
https://kenai.com/projects/btrace/pages/UserGuide
http://linmingren2003.blog.163.com/blog/static/56751003201121871725139/
http://www.stacktrace.cn/?p=28