Btrace拦截返回值、异常、行号

拦截时机

Kind.ENTRY:入口,默认值

Kind.RETURN:返回

Kind.THROW:异常

Kind.Line:行

Kind.RETURN

执行程序

public class Btrace {

    public static void main(String[] args) throws InterruptedException {

        // TODO Auto-generated method stub

        System.out.println("start");

        while(true){

            read(args[0]);

            Thread.sleep(10000);           

        }

    }

    public static String read(String arg){       

        System.out.println("hello,"+arg);

        return arg;

    }

}

Btrace脚本


import com.sun.btrace.AnyType;

import com.sun.btrace.BTraceUtils;

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.Return;

@BTrace

public class PrintReturn {   

    @OnMethod(

            clazz="com.pcitc.Btrace",

            method="read",

            location=@Location(Kind.RETURN)

    )

    public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, @Return AnyType result) {

        BTraceUtils.println(pcn+","+pmn + "," + result);

        BTraceUtils.println();

    }

}

运行效果


e:\>java -jar Btrace.jar 123

start

hello,123

hello,123

##

C:\Program Files\Java\jdk1.8.0_74\bin>jps

9552 jar

C:\Program Files\Java\jdk1.8.0_74\bin>btrace 9552 E:\PrintReturn.java

com.pcitc.Btrace,read,123



com.pcitc.Btrace,read,123

Kind.THROW

执行程序

public class exception {

    public static void main(String[] args) throws InterruptedException {

        // TODO Auto-generated method stub

        while(true){

            findexception();

            Thread.sleep(10000);           

        }

    }

    public static String findexception() {

        try {

            System.out.println("start...");

            System.out.println(1/0);

            System.out.println("end...");

        }catch(Exception e) {

            //

        }

        return "success";
    }

}

出现异常未做处理

Btrace脚本


import com.sun.btrace.BTraceUtils;

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.Self;

import com.sun.btrace.annotations.TLS;

@BTrace

public class PrintOnThrow {   

    // store current exception in a thread local

    // variable (@TLS annotation). Note that we can't

    // store it in a global variable!

    @TLS

    static Throwable currentException;

    // introduce probe into every constructor of java.lang.Throwable

    // class and store "this" in the thread local variable.

    @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 onthrow1(@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 onthrow2(@Self Throwable self, Throwable cause) {//new Throwable(Throwable cause)

        currentException = self;

    }

    // when any constructor of java.lang.Throwable returns

    // print the currentException's stack trace.

    @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;

        }

    }

}

无论哪种异常的构造函数都能拦截到

运行效果


e:\>java -jar exception.jar

start...

start...

##



C:\Program Files\Java\jdk1.8.0_74\bin>jps

9080 jar

e:\>btrace 9080 E:\PrintOnThrow.java

java.lang.ArithmeticException: / by zero

        com.pcitc.exception.findexception(exception.java:22)

        com.pcitc.exception.main(exception.java:12)

=====================

Kind.Line

可以看到某一行代码是否执行

显示行号的设置

 

执行程序


public class exception {

    public static void main(String[] args) throws InterruptedException {

        // TODO Auto-generated method stub

        while(true){

            findexception();

            Thread.sleep(10000);

           

        }

    }

    public static String findexception() {

        try {

            System.out.println("start...");      //ROW21

            System.out.println(1/0);              //ROW22

            System.out.println("end...");        //ROW23

        }catch(Exception e) {

            //

        }

        return "success";

    }

}

Btrace脚本


import com.sun.btrace.BTraceUtils;

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;

@BTrace

public class PrintLine {

    @OnMethod(

            clazz="com.pcitc.exception",

            method="findexception",

            location=@Location(value=Kind.LINE, line=21)

    )

    public static void anyRead(@ProbeClassName String pcn, @ProbeMethodName String pmn, int line) {

        BTraceUtils.println(pcn+","+pmn + "," +line);

        BTraceUtils.println();

    }

}

运行效果


e:\>java -jar exception.jar

start...

start...

##

e:\>btrace 9080 E:\PrintLine.java

com.pcitc.exception,findexception,21



com.pcitc.exception,findexception,21

如果是拦截23行,将不会打印出相关的内容,因为23未执行到

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值