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设置环境变量: - export BTRACE_HOME=/home/workspace/btrace/
- 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
}