btrace使用

时间:2024-01-12 20:14:50

btrace使用

目录btracee是btrace的解压目录

btrace/btrace是btrace的项目工程

root@ubuntu:/usr/local/bogon/btrace# tree btrace
btrace
├── bin
├── btrace-agent.jar
├── btrace-boot.jar
├── btrace-client.jar
└── src
├── btrace
├── HelloWorld.class
├── HelloWorld.java
└── TraceHelloWorld.java 3 directories, 6 files
root@ubuntu:/usr/local/bogon/btrace# cd -
/usr/local/bogon
root@ubuntu:/usr/local/bogon# tree btrace
btrace
├── bin
│   ├── btrace
│   ├── btrace.bat
│   ├── btracec
│   ├── btracec.bat
│   ├── btracer
│   └── btracer.bat
├── btrace
│   ├── bin
│   ├── btrace-agent.jar
│   ├── btrace-boot.jar
│   ├── btrace-client.jar
│   └── src
│   ├── btrace
│   ├── HelloWorld.class
│   ├── HelloWorld.java
│   └── TraceHelloWorld.java
├── btrace-bin.tar.gz
├── btrace.zip
├── build
│   ├── btrace-agent.jar
│   ├── btrace-boot.jar
│   └── btrace-client.jar
├── COPYRIGHT
├── docs

在src目录直接执行btrace pid TraceHelloWorld.java

# ../../bin/btrace 9221 TraceHelloWorld.java
the class name=>HelloWorld
the class method=>execute
the class method params=>1498
the class method execute time=>1499
-------------------------------------------
the class name=>HelloWorld
the class method=>execute
the class method params=>1903
the class method execute time=>1903
-------------------------------------------

如:TraceHelloWorld

# cat TraceHelloWorld.java 

import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.str;
import static com.sun.btrace.BTraceUtils.strcat;
import static com.sun.btrace.BTraceUtils.timeMillis; import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.ProbeClassName;
import com.sun.btrace.annotations.ProbeMethodName;
import com.sun.btrace.annotations.TLS;
@BTrace
public class TraceHelloWorld { @TLS
private static long startTime = 0; @OnMethod(clazz = "HelloWorld", method = "execute")
public static void startMethod(){
startTime = timeMillis();
} @OnMethod(clazz = "HelloWorld", method = "execute", location = @Location(Kind.RETURN))
public static void endMethod(){
println(strcat("the class method execute time=>", str(timeMillis()-startTime)));
println("-------------------------------------------");
} @OnMethod(clazz = "HelloWorld", method = "execute", location = @Location(Kind.RETURN))
public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method,int sleepTime){
println(strcat("the class name=>", name));
println(strcat("the class method=>", method));
println(strcat("the class method params=>", str(sleepTime))); }
}

比如我这里跟踪Jedis的连接ConnectionTrace

import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.timeMillis; import com.sun.btrace.BTraceUtils.Threads;
import com.sun.btrace.annotations.Export;
import com.sun.btrace.annotations.OnExit;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Return;
import com.sun.btrace.annotations.Self;
import com.sun.btrace.annotations.TLS; public class ConnectionTrace {
@TLS
private static long startTime = 0; @Export private static long openedCount;
@Export private static long penedCount;
@Export private static long closedCount; @OnMethod(clazz = "/*.jedis.Connection/", method = "makeObject")
public static void startMethod(@Return Object obj){
startTime = timeMillis();
penedCount++;
println("One connection is opened!");
println(obj);
Threads.jstack();
} @OnMethod(clazz = "/*.jedis.Connection/", method = "destroyObject")
public static void endMethod(@Self Object obj){
closedCount++;
println("One connection is closed!");
println(obj);
Threads.jstack();
} @OnExit
public static void f(){
println("Total opened connection:");
println(openedCount);
println("Total closed connection:");
println(closedCount);
}
}