https://apistore.top 开源电商支付系统,欢迎加入转发。
package com.test;
import org.openjdk.btrace.core.BTraceUtils;
import org.openjdk.btrace.core.annotations.*;
import static org.openjdk.btrace.core.BTraceUtils.*;
/**
https://www.jianshu.com/p/d51a065a39d0
https://blog.csdn.net/hellozhxy/article/details/95619877
* btrace -cp . BTraceChecker.java <pid>
*/
@BTrace
public class BTraceChecker {
static{
printSystem();
}
private static long startTime = 0;
@OnMethod(clazz="org.redisson.api.RedissonClient", method="getSet")
public static void test1() {
println("RedissonClient getSet method called");
}
@OnMethod(clazz = "org.redisson.api.RedissonClient", method = "getSet")
public static void test2() {
startTime = timeNanos();
}
@OnMethod(clazz = "org.redisson.api.RedissonClient", method = "getSet", location = @Location(Kind.RETURN))
public static void test3() {
long elapsedTime = (timeNanos() - startTime) / 1000000;
println("Method elapsed time: " + elapsedTime + "ms");
}
@OnMethod(clazz="@javax.jws.WebService", method="@javax.jws.WebMethod")
public static void testAnnotation() {
println("monitor annotation");
}
@OnMethod(clazz="com.vip.MyServer$MyInnerClass", method="hello")
public static void testStaticMethod() {
println("monitor annotation");
}
/**
* 构造方法
*/
@OnMethod(clazz="java.lang.ClassNotFoundException", method="<init>")
public static void testConstructor(){
jstack();
}
@OnMethod(clazz = "java.net.ServerSocket", method = "bind", location =@Location(value = Kind.ERROR))
public static void onThrow(Throwable exception, @Duration long duration){
println(exception);
}
@OnMethod(clazz = "com.kite.controller..*", method = ".*", location = @Location(Kind.RETURN))
public static void slowQuery(@ProbeClassName String pcn, @ProbeMethodName String probeMethod, @Duration long duration){
println(strcat("类:", pcn));
println(strcat("方法:", probeMethod));
println(strcat("时长:", str(duration / 1000000)));
}
@OnMethod(clazz = "com.kite.demo", location = @Location(value = Kind.LINE, line = 20))
public static void on20Line() {
println("执行到第20行");
}
@OnMethod(clazz="java.lang.Thread", method="start")
public static void test6(){
jstack();
}
public static void printSystem() {
println("java vm properties:===>");
printVmArguments();
println("System properties:===>");
printProperties();
println("OS properties:===>");
printEnv();
}
@OnMethod(clazz = "java.net.ServerSocket", method = "bind", location = @Location(value = Kind.CALL, clazz ="/.*/", method = "/.*/", where = Where.AFTER))
public static void onBind(@Self Object self, @TargetInstance Object instance, @Duration long duration) {
}
@OnMethod(clazz = "java.net.ServerSocket", method = "bind", location = @Location(value = Kind.RETURN))
public static void onBind(@Self Object self, String name, @Return Object result, @Duration long duration) {
BTraceUtils.println("方法调用栈: ");
BTraceUtils.println("入参1:" + name);
BTraceUtils.println("返回值: result=" + result);
BTraceUtils.println("时间(ns): " + duration);
}
/**
* 定时
*/
@OnTimer(4000)
public static void printMemory(){
println("heap:");
println(heapUsage());
println("no-heap:");
println(nonHeapUsage());
}
}