0

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

Brian HU
  • 187
  • 2
  • 10

2 Answers2

1

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

Brian HU
  • 187
  • 2
  • 10
  • 1
    This answer could use more explanation. – Dave Cousineau Jul 23 '14 at 06:18
  • hum, sorry for that... i just removed "@Self test.CaseObject instance" and got what i want. – Brian HU Jul 23 '14 at 07:54
  • Indeed, using @Self will make the probe match only non-static methods. And it is possible to probe a method invocation using "location=@Location(Kind.CALL)" – JB- Jul 23 '14 at 10:21
  • @J.B I am having pretty much no luck running any of the samples on Java 8, Windows 7 or Mac OSX 10. Is there any documentation that explain the ins and outs, or do we need to just be working out the examples? – Victor Grazi Sep 08 '15 at 21:18
1

In general you can probe static methods like this:

@OnMethod(clazz = "java.lang.Integer", method = "valueOf")
public static void onValueOf(int name) {
  println("***********************");
  jstack();
}
Dan
  • 9,681
  • 14
  • 55
  • 70