java 在线找bug 神器 btrace



示例地址   :  http://kenai.com/projects/btrace/sources/hg/show/samples





1. 示例代码
示例代码定义了Counter计数器,有一个add()方法,每次增加随机值,总数保存在totalCount属性中。

Btracetest.java代码   收藏代码
  1. package com.learnworld;  
  2. import java.util.Random;  
  3.   
  4. public class BTraceTest {  
  5.   
  6.     public static void main(String[] args) throws Exception {  
  7.         Random random = new Random();  
  8.   
  9.         // 计数器  
  10.         Counter counter = new Counter();  
  11.         while (true) {  
  12.             // 每次增加随机值  
  13.             counter.add(random.nextInt(10));  
  14.             Thread.sleep(1000);  
  15.         }  
  16.     }  
  17. }  

Counter.java代码   收藏代码
  1. package com.learnworld;  
  2. public class Counter {  
  3.     // 总数  
  4.     private static int totalCount = 0;  
  5.   
  6.     public int add(int num) throws Exception {  
  7.         totalCount += num;  
  8.         sleep();  
  9.         return totalCount;  
  10.     }  
  11.       
  12.     public void sleep() throws Exception {  
  13.         Thread.sleep(1000);  
  14.     }  
  15.   
  16. }  



2. 常见使用场景
下面通过几个常见使用场景演示如何使用BTrace脚本。

1) 获取add()方法参数值和返回值。
Java代码   收藏代码
  1. import com.sun.btrace.annotations.*;  
  2. import static com.sun.btrace.BTraceUtils.*;  
  3.   
  4. @BTrace  
  5. public class TracingScript {  
  6.    @OnMethod(  
  7.       clazz="com.learnworld.Counter",  
  8.       method="add",  
  9.       location=@Location(Kind.RETURN)  
  10.    )  
  11.    public static void traceExecute(int num,@Return int result){  
  12.      println("====== ");  
  13.      println(strcat("parameter num: ",str(num)));  
  14.      println(strcat("return value:",str(result)));  
  15.    }  
  16. }  


2) 定时获取Counter类的属性值totalCount。
Java代码   收藏代码
  1. import com.sun.btrace.annotations.*;  
  2. import static com.sun.btrace.BTraceUtils.*;  
  3.   
  4. @BTrace  
  5. public class TracingScript {  
  6.    private static Object totalCount = 0;  
  7.      
  8.    @OnMethod(  
  9.       clazz="com.learnworld.Counter",  
  10.       method="add",  
  11.       location=@Location(Kind.RETURN)  
  12.    )   
  13.    public static void traceExecute(@Self com.learnworld.Counter counter){  
  14.      totalCount = get(field("com.learnworld.Counter","totalCount"), counter);  
  15.    }   
  16.       
  17.    @OnTimer(1000)  
  18.    public static void print(){  
  19.      println("====== ");  
  20.      println(strcat("totalCount: ",str(totalCount)));  
  21.    }  
  22. }  



3) 获取add方法执行时间。
Java代码   收藏代码
  1. import com.sun.btrace.annotations.*;  
  2. import static com.sun.btrace.BTraceUtils.*;  
  3.   
  4. @BTrace  
  5. public class TracingScript {  
  6.    @TLS private static long startTime = 0;  
  7.      
  8.    @OnMethod(  
  9.       clazz="com.learnworld.Counter",  
  10.       method="add"  
  11.    )   
  12.    public static void startExecute(){  
  13.      startTime = timeNanos();  
  14.    }   
  15.       
  16.    @OnMethod(  
  17.       clazz="com.learnworld.Counter",  
  18.       method="add",  
  19.       location=@Location(Kind.RETURN)  
  20.    )   
  21.    public static void endExecute(@Duration long duration){  
  22.      long time = timeNanos() - startTime;  
  23.      println(strcat("execute time(nanos): ", str(time)));  
  24.      println(strcat("duration(nanos): ", str(duration)));  
  25.    }   
  26. }  


4) 获取add()方法调用方法sleep()次数。
Java代码   收藏代码
  1. import com.sun.btrace.annotations.*;  
  2. import static com.sun.btrace.BTraceUtils.*;  
  3.   
  4. @BTrace  
  5. public class TracingScript {  
  6.    private static long count;   
  7.        
  8.    @OnMethod(  
  9.       clazz="/.*/",  
  10.       method="add",  
  11.       location=@Location(value=Kind.CALL, clazz="/.*/", method="sleep")  
  12.    )  
  13.    public static void traceExecute(@ProbeClassName String pcm, @ProbeMethodName String pmn,  
  14.    @TargetInstance Object instance,  @TargetMethodOrField String method){  
  15.      println("====== ");  
  16.      println(strcat("ProbeClassName: ",pcm));  
  17.      println(strcat("ProbeMethodName: ",pmn));  
  18.      println(strcat("TargetInstance: ",str(classOf(instance))));  
  19.      println(strcat("TargetMethodOrField : ",str(method)));  
  20.      count++;  
  21.    }  
  22.      
  23.    @OnEvent  
  24.    public static void getCount(){  
  25.        println(strcat("count: ", str(count)));  
  26.    }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值