拦截时机
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未执行到