我需要探索一种静态方法。但是无法探测到方法调用。有人能提供帮助吗?
我的java代码:
// CaseObject.java
package test;
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;
}
public static boolean execute2(int sleepTime) throws Exception{
System.out.println("sleep: "+sleepTime);
sleepTotalTime+=sleepTime;
Thread.sleep(sleepTime);
return true;
}
}
// Case2.java
package test;
import java.util.Random;
public class Case2 {
public static void main(String[] args) throws Exception {
Random random = new Random();
boolean result = true;
while (result) {
result = CaseObject.execute2(random.nextInt(1000));
Thread.sleep(1000);
}
}
}我的
// TraceMethodArgsAndReturn2.java
import static com.sun.btrace.BTraceUtils.*;
import com.sun.btrace.annotations.*;
@BTrace public class TraceMethodArgsAndReturn2{
@OnMethod(
clazz="test.CaseObject",
method="execute2",
location=@Location(Kind.RETURN)
)
public static void traceExecute(@Self test.CaseObject instance,int sleepTime,@Return boolean result){
println("call CaseObject.execute2");
println(strcat("sleepTime is:",str(sleepTime)));
println(strcat("sleepTotalTime is:",str(get(field("test.CaseObject","sleepTotalTime"),instance))));
println(strcat("return value is:",str(result)));
}
}我运行它的方式如下:./btrace/bin/btrace -cp /tmp/a/目标/classes/ 8477 ./btrace/TraceMethodArgsAndRetur2.java
发布于 2014-07-23 05:55:27
好吧,我发现了问题.@Self不应使用。
发布于 2016-09-13 10:42:43
通常,您可以探测这样的静态方法:
@OnMethod(clazz = "java.lang.Integer", method = "valueOf")
public static void onValueOf(int name) {
println("***********************");
jstack();
}https://stackoverflow.com/questions/24902387
复制相似问题