Skip to content

Instantly share code, notes, and snippets.

@yongchun
Forked from melin/btrace.txt
Created October 9, 2015 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongchun/598ea15de0f04c830815 to your computer and use it in GitHub Desktop.
Save yongchun/598ea15de0f04c830815 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