Btrace拦截返回值异常、行号

一、拦截时机
分类:

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

二 实例:
1.返回值测试:
控制层测试代码
/**
* 返回值拦截
*/
@RequestMapping("/return")
public User return1(User user){
return user;
}
}

btrace脚本代码:
package com.baoge_springboot.springboot_core.common.btrace;

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

@BTrace
public class ReturnBtrace {

@OnMethod(
        clazz = "com.baoge_springboot.springboot_core.web.controller.CahController",
        method = "return1",
        location =@Location(Kind.RETURN)
)
public static void anyRead(@ProbeClassName String pcm, @ProbeMethodName String pmn, @Return AnyType result){
    //    BTraceUtils.printArray(args);
    BTraceUtils.println(pcm+","+pmn+","+result);
    BTraceUtils.println();
}

}
运行:
启动程序
cd 进入脚本目录
jps -l 查看进程
命令:btrace 进程 脚本
在这里插入图片描述
在这里插入图片描述
2.异常拦截

目标代码:
/**
* 异常拦截
*/
@RequestMapping("/exception")
public String ex(){
try {
int i=1/0;
} catch (Exception e) {
e.printStackTrace();
}
return “success”;
}
脚本;
package com.baoge_springboot.springboot_core.common.btrace;

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

@BTrace
public class ExceptionBtrace {
@TLS
static Throwable currentException;

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

@OnMethod(
        clazz = "java.lang.Throwable",
        method = "<init>"
)
public static void onthrow1(@Self Throwable self,String s){//new Throwable(String msg)
    currentException=self;
}
@OnMethod(
        clazz = "java.lang.Throwable",
        method = "<init>"
)
public static void onthrow2(@Self Throwable self,String s,Throwable cause){//new Throwable(String msg,Throwable cause)
    currentException=self;
}

@OnMethod(
        clazz = "java.lang.Throwable",
        method = "<init>"
)
public static void onthrow3(@Self Throwable self,Throwable cause){//new Throwable(Throwable cause)
    currentException=self;
}
@OnMethod(
        clazz = "java.lang.Throwable",
        method = "<init>",
        location = @Location(Kind.RETURN)
)
public static void onthrowReturn(){
    if(currentException!=null){
        BTraceUtils.Threads.jstack(currentException);
        BTraceUtils.println("======================");
        currentException=null;
    }
}

}
//这里通过拦截异常构造函数 来打印异常信息
执行结果
在这里插入图片描述

3.拦截行号 确认某行是否执行 哪个方法哪行 如果执行了则会返回行号
目标代码:
/**
* 拦截某行是否执行
*/
@RequestMapping("/line")
public String ln(){
int i=8;
int k=i+9;//66行
return “success”;
}
脚本代码:
package com.baoge_springboot.springboot_core.common.btrace;

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

@BTrace
public class LineBtrace {
@OnMethod(
clazz = “com.baoge_springboot.springboot_core.web.controller.CahController”,
method = “ln”,
location =@Location(value = Kind.LINE,line = 66)
)
public static void anyRead(@ProbeClassName String pcm, @ProbeMethodName String pmn,int line){
// BTraceUtils.printArray(args);
BTraceUtils.println(pcm+","+pmn+","+line);
BTraceUtils.println();
}
}
执行结果:
在这里插入图片描述


版权声明:本文为weixin_42106184原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>