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();
}
}
执行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值