如何使用btrace probe静态方法?

时间:2014-07-23 05:16:38

标签: java btrace

我需要探测静态方法。但无法探测方法调用。任何人都能提供一些帮助吗?

我的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);
        }
    }

}

我的btrace脚本 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// 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 / target / classes / 8477 ./btrace/TraceMethodArgsAndReturn2.java

2 个答案:

答案 0 :(得分:1)

好吧,我发现了问题... @Self不应该被使用。

答案 1 :(得分:1)

通常,您可以探测如下的静态方法:

@OnMethod(clazz = "java.lang.Integer", method = "valueOf")
public static void onValueOf(int name) {
  println("***********************");
  jstack();
}