BTrace:是一个VisualVM插件,本身也是可以独立运行的程序。作用是:不停止目标程序运行的前提下,通过HotSpot虚拟机的HotSwap技术动态加入原本并不存在的调试代码。
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public int add(int a,int b) {
return a + b;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Test test = new Test();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<10;i++) {
reader.readLine();
int a = (int) Math.round(Math.random() * 1000);
int b = (int) Math.round(Math.random() * 1000);
System.out.println(test.add(a, b));
}
}
}
打开jvisualvm.exe。运行上面程序后,选中红色框进程,右键“Trace Application...”
输入调试代码后,点击“start”按钮后稍等片刻,编译完成后,在程序运行时,在OUTPUT会输出调试信息。
/* put your code here */
@OnMethod(clazz="test.Test",method="add",location=@Location(Kind.RETURN))
public static void func(@Self test.Test instance,int a,int b,@Return int result){
println("调用堆栈:");
jstack();
println(strcat("方法参数A:", str(a)));
println(strcat("方法参数B:", str(b)));
println(strcat("方法结果:", str(result)));
}