Search code examples
javabtrace

how to use btrace probe static method?


i need to probe a static method. but the method invocation could not be probed. can anybody offer some help?

my java code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// 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);
        }
    }

}

my btrace script ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// 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)));
    }
}

and i run it like this ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./btrace/bin/btrace -cp /tmp/a/target/classes/ 8477 ./btrace/TraceMethodArgsAndReturn2.java


Solution

  • ok, i found the problem... @Self should not be used.