Energy of Love  
日历
<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
统计
  • 随笔 - 70
  • 文章 - 0
  • 评论 - 80
  • 引用 - 0

导航

常用链接

留言簿

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
BTrace的技术分析,本人暂没有这个技术能力,大家可以看 http://www.iteye.com/topic/1005918,
http://mgoann.iteye.com/blog/1409667 
下面是个人写的一些简单实例,不过我一直没办法通过BTrace拿到局部变量的值,不知道哪位牛人帮帮解答下
Linux下:
在http://kenai.com/projects/btrace下载btrace-bin.tar.gz,并解压,在/etc/profile设置环境变量: 
  1. export BTRACE_HOME=/home/workspace/btrace/
  2. export PATH=$BTRACE_HOME/bin:$PATH  
设置完成后 source /etc/profile
给执行文件赋权限 chmod +x btrace

/* BTrace Script Template */
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
@BTrace
public class TracingScript {
/* put your code here */
//打印实例属性
@OnMethod(clazz = "com.gameplus.action.siteLobby.LobbyAction", method = "/.*bankPage/", location = @Location(value = Kind.ENTRY))
//clazz = "com.gameplus.action.siteLobby.LobbyAction" 表示监控的类,method = "/.*bankPage/"表示监控的方法 ,这两个参数都可以用正则匹配
//如果是接口用+号clazz = "+com.gameplus.action.siteLobby.LobbyAction" 
public static void bufferMonitor(@Self Object self ){ // @Self 表示监控点实例 
   print(strcat(strcat(name(probeClass()), "."), probeMethod())); //probeClass()监控的类,probeMethod()监控的方法
        println(self);
        println(get(field(classOf(self), "money"))); //只能取值static变量
println(get(field(classOf(self), "money"),self)); //可以取值当前实例变量,static也可以取到
Field moneyField = field("com.gameplus.action.siteLobby.LobbyAction", "money"); //知道class的名称也可以取值
get(moneyField,self);
get(field("com.gameplus.action.siteLobby.LobbyAction", "money"), self); 
        Object montmp =get(field(getSuperclass(classOf(self)), "user"), self); //获取父类变量的方法
        println(str(montmp));
long userId = (Long)get(field(classOf(montmp),"userId"),montmp); //获取superClass.Object.变量值
        println(userId);
    }
//打印运行时lineNumber
    @OnMethod(clazz = "com.gameplus.action.siteLobby.LobbyAction", location=@Location(value=Kind.LINE, line=-1))
    public static void online(@ProbeClassName String pcn, @ProbeMethodName String pmn, int line) {
print(Strings.strcat(pcn, ".")); //className
print(Strings.strcat(pmn, ":")); //methodName
println(line); //lineNumber
//结果为:com.gameplus.action.siteLobby.LobbyAction.bankPage:161
}
//打印传递的参数值
import com.sun.btrace.AnyType;
@OnMethod(clazz="com.gameplus.service.operateBankService.OperateBankService",method="/.*/")
public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, AnyType[] args) {
println(pcn);
println(pmn);
printArray(args);
}
//打印所有属性
@OnMethod(clazz = "com.gameplus.action.siteLobby.LobbyAction", method = "/.*bankPage/", location = @Location(value = Kind.RETURN))
    public static void bufferMonitor(@Self Object self,@Return Object command ,@Duration long time){ 
        printFields(self);
        Object montmp =get(field(getSuperclass(classOf(self)), "user"), self);
        printFields(montmp);
//{password=null, newPassword=null, rePassword=, bankPassword=, newBankPassword=, reBankPassword=, operateType=0, operateBankType=0, money=111, integral=0, dateStartQuery=, dateEndQuery=, isDefaultPasswd=0, }
//{userId=10918, username=titanaly11, realname=方法1, expTime=1146510, bankMoney=1317886229, bankPasswd=243b6503f2e3e83faccc89830aca1d91, ifAvailable=1, password=1bbd886460827015e5d605ed44252251, money=100000, }
    }
//初始化时的变量参数
//public User(long userId,String playServerId) {
// this.userId = userId;
// this.playServerId = playServerId;
//}
@OnMethod(clazz = "com.gameplus.core.model.oracle.user.User", method="<init>")
     public static void bufferMonitor(@Self Object self,long userId,String gameId){ 
        printFields(self);
        println(userId);
        println(gameId);
//结果
//{userId=0, username=null, realname=null, expTime=0, bankMoney=0, bankPasswd=null, ifAvailable=0, password=null, money=0, nickName=null, iconNum=0, tim=null, infullCount=0, userToken=null, onlineTime=0,}
//10918
//222
    }
//打印系统参数
static {
println("System Properties:");
printProperties();
println("VM Flags:");
printVmArguments();
println("OS Enviroment:");
printEnv();
exit(0);
}
//打印程序执行关系
//LobbyAction中所有方法执行的执行顺序
@OnMethod(clazz="com.gameplus.action.siteLobby.LobbyAction", method="/.*/",
              location=@Location(value=Kind.CALL, clazz="/.*/", method="/.*/"))
    public static void n(@Self Object self, @ProbeClassName String pcm, @ProbeMethodName String pmn,
                         @TargetInstance Object instance, @TargetMethodOrField String method){ // all calls to the methods with signature "(String)"
        println(Strings.strcat("Context: ", Strings.strcat(pcm, Strings.strcat("#", pmn))));
        println(instance); //被调用目标对象
        println(Strings.strcat("",method)); //被调用方法
//Context: com/gameplus/action/siteLobby/LobbyAction#bankPage
//$Proxy18@807c31
//getBankPage
//Context: com/gameplus/action/siteLobby/LobbyAction#bankPage
//{userEx=10, leverRestriction=6, isDefaultPasswd=0, bankPassEqGamePass=0, moneyTransferMsg=S, isSimplePasswd=0, mobile=null}
//get
//Context: com/gameplus/action/siteLobby/LobbyAction#bankPage
//6
//intValue
//Context: com/gameplus/action/siteLobby/LobbyAction#bankPage
//{userEx=10, leverRestriction=6, isDefaultPasswd=0, bankPassEqGamePass=0, moneyTransferMsg=S, isSimplePasswd=0, mobile=null}
//get
}
posted on 2012-02-02 17:38 不高兴 阅读(2334) 评论(3)  编辑  收藏 所属分类: Java
评论:
  • # re: btrace使用实例加注释  http://www.salomon-mgabard.fr Posted @ 2015-09-15 16:32
    import com.sun.btrace.annotations.*;
    import static com.sun.btrace.BTraceUtils.*;
    import java.lang.reflect.Field;  回复  更多评论   

  • # re: btrace使用实例加注释  http://www.maladies-chroniques.fr Posted @ 2015-10-15 11:10
    而’08’,’09’按8进制解析会得到’0’,因为’8’、’9’在8进制中是非法字符,不会被解析。由此导致上述的bug。
    找到问题根源,修复就变得很简单了,显示指定radix为10.
    parseInt("08",10);  回复  更多评论   

  • # re: btrace使用实例加注释  http://www.monte-escalier-info.fr Posted @ 2016-01-20 16:18
    import com.sun.btrace.annotations.*;  回复  更多评论   

 
Copyright © 不高兴 Powered by: 博客园 模板提供:沪江博客