Skip to content

Instantly share code, notes, and snippets.

@r4um
Created November 3, 2016 06:11
Show Gist options
  • Save r4um/969bbea541a072f77bbe0909b3e3976a to your computer and use it in GitHub Desktop.
Save r4um/969bbea541a072f77bbe0909b3e3976a to your computer and use it in GitHub Desktop.
BTrace foo

Need java 7, jdk8 crashes pending fix see btraceio/btrace#240

⚡ java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

Assuming btrace is installed in ~/bin/btrace-1.3.8.3/

Example program.

⚡ cat CA.java 
public class CA {
    public static void printarray() {
            char[] a = {'a', 'b', 'c'};
            System.out.println(a);
            String s = "sasfasf";
            System.out.println(s);
    }
    public static void main(String[] args) throws InterruptedException {
        while(true) {
            System.out.println("sleeping...");
            Thread.sleep(1000);
            printarray();
        }
    }

}

Launch with btrace agent lib

⚡ java -javaagent:$HOME/bin/btrace-1.3.8.3/build/btrace-agent.jar CA
sleeping...
abc
sasfasf
sleeping...
abc
sasfasf
sleeping...
....

Attach to this a btrace program, example btrace program

⚡ cat CharArray.java 
// track char array allocations
package com.sun.btrace.samples;

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;

@BTrace public class CharArray {
    @OnMethod(
      clazz="/.*/",
      method="/.*/",
      location=@Location(value=Kind.NEWARRAY, clazz="char")
    )
    public static void onnew(@ProbeClassName String pcn, @ProbeMethodName String pmn, String arrType, int dim) {
        println("CharArray new from class " + pcn + " method " + pmn + " Type " + arrType + " Size " + dim);
    }
}

Attach to our process

⚡ jps -l
30490 CA
30526 sun.tools.jps.Jps
⚡ ./bin/btrace 30490 CharArray.java
CharArray new from class CA method printarray Type char Size 1
CharArray new from class CA method printarray Type char Size 1
CharArray new from class CA method printarray Type char Size 1
CharArray new from class CA method printarray Type char Size 1
^CPlease enter your option:
	1. exit
	2. send an event
	3. send a named event
1
CharArray new from class CA method printarray Type char Size 1

More examples/usage https://github.com/btraceio/btrace/tree/master/samples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment