Btrace

入门

拦截方法

* 普通方法 @OnMethod(clazz="", method="")
* 构造函数 @OnMethod(clazz="", method="")
* 拦截同名函数,用参数区分

拦截时机

* Kind.ENTRY: 入口,默认值
* Kind.RETURN: 返回
* Kind.THROW: 异常
* Kind.Line: 行

拦截this,参数,返回值

* this: @Self
* 入参: 可以用AnyType,也可以用真实类型,同名的用真实的
* 返回: @Return

获取对象的值

* 简单类型: 直接获取
* 复杂类型: 反射,类名+属性名

其他

* 打印行号: Kind.LINE
* 打印堆栈: Threads.jstack()
* 打印环境变量
/**
 * @author zhoucong04
 * @date 2018/12/23.
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/v1")
    @ResponseBody
    public String index(@RequestParam("name") String username) {
        return "hello," + username;
    }

    @GetMapping("/user")
    @ResponseBody
    public User get(User user) {
        return user;
    }

    @GetMapping("/hi1")
    @ResponseBody
    public String welcome(@RequestParam("name") String name, @RequestParam("id") int id) {
        return name + "," + id;
    }

    @GetMapping("/hi2")
    @ResponseBody
    public String welcome(@RequestParam("name") String name) {
        return name;
    }

    @GetMapping("/exception")
    @ResponseBody
    public String save() {
        try {
            System.out.println("start....");
            System.out.println(1 / 0);
            System.out.println("end....");
        } catch (Exception e) {
        }
        return "success";
    }
}
@BTrace
public class Arg {

    @OnMethod(
            clazz = "com.le.controller.HelloController",
            method = "index",
            location = @Location(Kind.ENTRY)
    )
    public static void anyRead(@ProbeClassName String className, @ProbeMethodName String methodName, AnyType[] args) {
        BTraceUtils.println(className);
        BTraceUtils.println(methodName);
        BTraceUtils.printArray(args);
    }
}
@BTrace
public class Constructor {

    @OnMethod(
            clazz = "com.le.model.User",
            method = "",
            location = @Location(Kind.ENTRY)
    )
    public static void construct(@ProbeClassName String className, @ProbeMethodName String methodName, AnyType[] args) {
        BTraceUtils.println(className);
        BTraceUtils.println(methodName);
        BTraceUtils.printArray(args);
    }
}
@BTrace
public class Same {

    @OnMethod(
            clazz = "com.le.controller.HelloController",
            method = "welcome",
            location = @Location(Kind.ENTRY)
    )
    public static void hihihihi(@ProbeClassName String className, @ProbeMethodName String methodName, String name, int id) {
        BTraceUtils.println(className);
        BTraceUtils.println(methodName);
        BTraceUtils.println(name + " ~ " + id);
    }
}
@BTrace
public class Return {

    @OnMethod(
            clazz = "com.le.controller.HelloController",
            method = "index",
            location = @Location(Kind.RETURN)
    )
    public static void hi(@ProbeClassName String className, @ProbeMethodName String methodName, @com.sun.btrace.annotations.Return AnyType arg) {
        BTraceUtils.println(className);
        BTraceUtils.println(methodName);
        BTraceUtils.println(arg);

    }
}
@BTrace
public class Line {

    @OnMethod(
            clazz = "com.le.controller.HelloController",
            method = "save",
            location = @Location(value = Kind.LINE, line = 42)
    )
    public static void printLine(@ProbeClassName String className, @ProbeMethodName String methodName, int line) {
        BTraceUtils.println(className);
        BTraceUtils.println(methodName);
        BTraceUtils.println(line);
    }
}
@BTrace
public class OnThrow {

    @TLS
    static Throwable currentException;

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = ""
    )
    public static void onthrow(@Self Throwable self) {
        currentException = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = ""
    )
    public static void onthrow1(@Self Throwable self, String s) {
        currentException = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = ""
    )
    public static void onthrow1(@Self Throwable self, String s, Throwable cause) {
        currentException = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = ""
    )
    public static void onthrow2(@Self Throwable self, Throwable cause) {
        currentException = self;
    }

    @OnMethod(
            clazz = "java.lang.Throwable",
            method = "",
            location = @Location(Kind.RETURN)
    )
    public static void onthrowreturn() {
        if (currentException != null) {
            BTraceUtils.Threads.jstack(currentException);
            BTraceUtils.println("=====================");
            currentException = null;
        }
    }
}
@BTrace
public class ArgComplex {

    @OnMethod(
            clazz = "com.zc.controller.HelloController",
            method = "get",
            location = @Location(Kind.ENTRY)
    )
    public static void anyRead(@ProbeClassName String className, @ProbeMethodName String methodName, User user) {
        BTraceUtils.printFields(user);
        Field name = BTraceUtils.field("com.le.model.User", "name");
        BTraceUtils.println(BTraceUtils.get(name, user));
        BTraceUtils.println(className);
        BTraceUtils.println(methodName);
    }
}
@BTrace
public class JInfo {
    static {
        BTraceUtils.println("System.Properties:");
        BTraceUtils.printProperties();
        BTraceUtils.println("VM Flags:");
        BTraceUtils.printVmArguments();
        BTraceUtils.println("OS Enviroment:");
        BTraceUtils.printEnv();
        BTraceUtils.exit(0);
    }
}

参考

https://github.com/btraceio/btrace/tree/master/samples
http://calvin1978.blogcn.com/articles/btrace1.html
https://www.jianshu.com/p/cff037edb750
https://blog.csdn.net/ZYC88888/article/details/81662671
http://blog.51cto.com/zero01/2143096
http://www.cnblogs.com/laoxia/p/9773319.html

你可能感兴趣的:(Btrace)