Skip to content

Instantly share code, notes, and snippets.

@charleehu
Created May 5, 2012 11:06
Show Gist options
  • Save charleehu/2601585 to your computer and use it in GitHub Desktop.
Save charleehu/2601585 to your computer and use it in GitHub Desktop.
BTrace sample
import java.util.Random;
public class Case1{
public static void main(String[] args) throws Exception{
Random random=new Random();
CaseObject object=new CaseObject();
boolean result=true;
while(result){
result=object.execute(random.nextInt(1000));
Thread.sleep(1000);
}
}
}
public class CaseObject{
private static int sleepTotalTime=0;
public boolean execute(int sleepTime) throws Exception{
System.out.println("sleep: "+sleepTime);
sleepTotalTime+=sleepTime;
Thread.sleep(sleepTime);
return true;
}
}
btrace -cp build\;. pid TraceMethodArgsAndReturn.java
1、调用此方法时传入的是什么参数,返回的是什么值,当时sleepTotalTime是多少?
2、execute方法执行耗时是多久?
3、谁调用了execute方法?
4、有没有人调用CaseObject中的哪一行代码?
http://kenai.com/projects/btrace/pages/UserGuide
import static com.sun.btrace.BTraceUtils.*;
import com.sun.btrace.annotations.*;
@BTrace public class TraceMethodArgsAndReturn{
@OnMethod(
clazz="CaseObject",
method="execute",
location=@Location(Kind.RETURN)
)
public static void traceExecute(@Self CaseObject instance,int sleepTime,@Return boolean result){
println("call CaseObject.execute");
println(strcat("sleepTime is:",str(sleepTime)));
println(strcat("sleepTotalTime is:",str(get(field("CaseObject","sleepTotalTime"),instance))));
println(strcat("return value is:",str(result)));
}
}
import static com.sun.btrace.BTraceUtils.*;
import com.sun.btrace.annotations.*;
@BTrace public class TraceMethodCallee{
@OnMethod(
clazz="CaseObject",
method="execute"
)
public static void traceExecute(){
println("who call CaseObject.execute :");
jstack();
}
}
import static com.sun.btrace.BTraceUtils.*;
import com.sun.btrace.annotations.*;
@BTrace public class TraceMethodExecuteTime{
@TLS static long beginTime;
@OnMethod(
clazz="CaseObject",
method="execute"
)
public static void traceExecuteBegin(){
beginTime=timeMillis();
}
@OnMethod(
clazz="CaseObject",
method="execute",
location=@Location(Kind.RETURN)
)
public static void traceExecute(int sleepTime,@Return boolean result){
println(strcat(strcat("CaseObject.execute time is:",str(timeMillis()-beginTime)),"ms"));
}
}
import static com.sun.btrace.BTraceUtils.*;
import com.sun.btrace.annotations.*;
@BTrace public class TraceMethodLine{
@OnMethod(
clazz="CaseObject",
location=@Location(value=Kind.LINE,line=5)
)
public static void traceExecute(@ProbeClassName String pcn,@ProbeMethodName String pmn,int line){
println(strcat(strcat(strcat("call ",pcn),"."),pmn));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment