Skip to content

Instantly share code, notes, and snippets.

@melin
Created November 21, 2014 01:06
Show Gist options
  • Save melin/1f1b2f605aa21d74408d to your computer and use it in GitHub Desktop.
Save melin/1f1b2f605aa21d74408d to your computer and use it in GitHub Desktop.
btrace script
/* BTrace Script Template */
import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import java.lang.reflect.Field;
@BTrace
public class TracingScript {
/*
* 获取方参数、返回值信息;获取方法调用时间
*/
@OnMethod(clazz="test.BtraceTest", method="add", location=@Location(Kind.RETURN))
public static void func(@Self 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)));
}
/*
* 正则表达式匹配获取调用方法名称以及类名称
*/
@OnMethod(clazz="/test\\..*/", method="/.*/")
public static void m(@ProbeClassName String probeClass, @ProbeMethodName String probeMethod) {
print(Strings.strcat("entered ", probeClass));
println(Strings.strcat(".", probeMethod));
}
/*
* 获取类实例变量值
*/
@OnMethod(clazz = "test.BtraceTest", method = "/.*add/", location = @Location(value = Kind.ENTRY))
public static void bufferMonitor(@Self Object self) {
Field sumField = field("test.BtraceTest", "sum");
int sum = (Integer) get(sumField, self);
println(strcat("总和:", str(sum)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment