首页  

btrace使用简介     所属分类 java 浏览量 1425
Java运行时追踪工具

BTrace is a safe, dynamic tracing tool for the Java platform. 
BTrace can be used to dynamically trace a running Java program 
(similar to DTrace for OpenSolaris applications and OS). 
BTrace dynamically instruments the classes of the target application to inject tracing code ("bytecode tracing")


https://github.com/btraceio/btrace


可以拦截方法 输入参数和返回值获取 方法执行时间


使用 btrace 1.3.11.3 版本

例子程序
带参数的静态方法
无参数的静态方法
实例方法



import java.util.Date;
import java.util.Random;

public class Hello {

	private static final Random rand = new Random();

	public static void main(String[] args) throws Exception {
		System.out.println("hello");
		Hello hello = new Hello();
		while (true) {

			method1(System.currentTimeMillis());
			method2();
			hello.method3();
		}
	}

	private static int method1(long time) throws Exception {
		int tmp = rand.nextInt(1000);
		Thread.sleep(tmp);
		System.out.println("method1," + tmp + "," + new Date(time));
		return tmp;
	}

	private static int method2() throws Exception {
		int tmp = rand.nextInt(1000);
		Thread.sleep(tmp);
		System.out.println("method2," + tmp + "," + new Date());
		return tmp;
	}

	private int method3() throws Exception {
		int tmp = rand.nextInt(1000);
		Thread.sleep(tmp);
		System.out.println("method3," + tmp + "," + new Date());
		return tmp;
	}

}


测试脚本 import com.sun.btrace.BTraceUtils; import com.sun.btrace.annotations.BTrace; import com.sun.btrace.annotations.Duration; import com.sun.btrace.annotations.Kind; import com.sun.btrace.annotations.Location; import com.sun.btrace.annotations.OnMethod; import com.sun.btrace.annotations.ProbeClassName; import com.sun.btrace.annotations.ProbeMethodName; import com.sun.btrace.annotations.Return; @BTrace public class Script1 { @OnMethod(clazz = "dyyx.Hello", method = "method1", location = @Location(Kind.RETURN) ) public static void method1(long param1,@ProbeClassName String probeClassName, @ProbeMethodName String probeMethod, @Duration long duration,@Return int result) { BTraceUtils.println(probeClassName + "." + probeMethod + ",cost time:" + duration / 1000000 + " ms"+",param1="+param1+",result="+result); } @OnMethod(clazz = "dyyx.Hello", method = "method2", location = @Location(Kind.RETURN) ) public static void method2(@ProbeClassName String probeClassName, @ProbeMethodName String probeMethod, @Duration long duration) { BTraceUtils.println(probeClassName + "." + probeMethod + ",cost time:" + duration / 1000000 + " ms"); } @OnMethod(clazz = "dyyx.Hello", method = "method3", location = @Location(Kind.RETURN) ) public static void method3(@ProbeClassName String probeClassName, @ProbeMethodName String probeMethod, @Duration long duration) { BTraceUtils.println(probeClassName + "." + probeMethod + ",cost time:" + duration / 1000000 + " ms"); } }
简单使用 /Users/dugang/soft/btrace/btrace-bin-1.3.11.3/bin 先编译下 ./btracec Script1.java 获取需要调试的Java进程id ps aux|grep Hello 执行脚本 ./btrace PID Script1.java -v 需要 加 -v ,否则控制台无输出

上一篇     下一篇
maven clean package install deploy区别

jar冲突处理

java对象访问方式

为什么开发一个操作系统那么难

AQS同步队列与条件队列

公平锁与非公平锁的关键区别