VisualVM Btrace使用

来源:互联网 发布:c语言的思考图 编辑:程序博客网 时间:2024/06/05 07:05
  1.    int a = (int) (Math.random() * 1000);  
  2.         int b = (int) (Math.random() * 1000);  
  3.         System.out.println(add(a, b));  
  4.     }  
  5.     public static void main(String[] args) throws IOException {  
  6.         BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));  
  7.         BTraceTest bTraceTest=new BTraceTest();  
  8.         bReader.readLine();  
  9.         for (int i = 0; i < 10; i++) {  
  10.             bTraceTest.run();  
  11.         }  
  12.     }  
  13. }  
运行Main函数,阻塞等待控制台输入。

3. 在VisualVM中选择该监控服务右键点击trace application.

TracingScript类中输入以下代码

/* BTrace Script Template */
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
@BTrace
public class TracingScript {
/* put your code here */
    @OnMethod(
        clazz="com.lss.test.BTraceTest",
        method="add",
        location=@Location(Kind.RETURN)
    )
    public static void func(@Self com.lss.test.BTraceTest instance ,int a,int b,@Return int result){
        println("调用堆栈");    
        jstack();
        println(strcat("方法参数A:",str(a)));
        println(strcat("方法参数B:",str(b)));
        println(strcat("方法结果:",str(result)));
        
    }
}
点击开始,控制台输出以下表示编译通过:

** Compiling the BTrace script ...
*** Compiled
** Instrumenting 1 classes ...
*** Done
** BTrace up&running

*** Done
** BTrace up&running

4.回到Eclipse 在控制台回车,让程序继续执行,完成后回到VisualVM 则看到控制台输出如下信息



1 0
原创粉丝点击