Home >Java >javaTutorial >Detailed explanation of BTrace for Java online troubleshooting
What is BTrace
BTrace is a killer tool for checking and solving online problems. BTrace can obtain all the information during the execution of the program by writing scripts, and, please note, No need to restart the service, yes, no need to restart the service. After writing the script, you can execute it directly with the command without touching the original program code.
Principle
Installation and configuration
##
export JAVA_HOME=/home/fengzheng/soft/jdk1.8.0_111 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH export BTRACE_HOME=/home/fengzheng/soft/btrace export PATH=$PATH:$BTRACE_HOME/bin
Then execute the command
Simple test casebtrace The simplest syntax is
package kite.lab.utils; /** * NumberUtil * * @author fengzheng * @date 2017/2/15 */ public class NumberUtil { public int sum(){ int result = 0; for(int i = 0; i< 100; i++){ result += i * i; } return result; } public static void main(String[] args){ while (true) { Thread.currentThread().setName("计算"); NumberUtil util = new NumberUtil(); int result = util.sum(); System.out.println(result); try { Thread.sleep(5000); }catch (InterruptedException e){ } } } }
By the way, the process of compiling and running Java from the command line:
2. After executing the above program, you can use the
jps command to check the pid (generally, which account is used to start the program will depend on which account is used to execute jps, except for the root account). Execute the jps command to see the following results:
root@ubuntu:/home/fengzheng/codes/btrace# jps 10906 Jps 10860 NumberUtil3. You can see that the java process just executed is 10860. 4. Write a btrace script. The content of the script is as follows:
package kite; import com.sun.btrace.annotations.*; import static com.sun.btrace.BTraceUtils.Strings.strcat; import static com.sun.btrace.BTraceUtils.jstack; import static com.sun.btrace.BTraceUtils.println; import static com.sun.btrace.BTraceUtils.str; /** * NumberUtilBTrace * * @author fengzheng * @date 2017/6/20 */ @BTrace public class NumberUtilBTrace { @OnMethod( clazz="kite.lab.utils.NumberUtil", method="sum", location=@Location(Kind.RETURN) ) public static void func(@Return int result) { println("trace: ======================="); println(strcat("result:", str(result))); jstack(); } }means that after the execution ends (location=@Location(Kind.RETURN) indicates the end of execution), the results and stack information are output
5. Preparation Compilation: You can use the precompilation command to check the correctness of the script before execution. The precompilation command is btracec, which is a javac-like command, btracec NumberUtilBTrace.java
# #6. Call the command line to execute, btrace 10860 NumberUtilBTrace.java, (if you want to save it to a local file, you can use the redirection command btrace 10860 NumberUtilBTrace.java > mylog.log). The printed information is as follows
trace: ======================= result:328350 kite.lab.utils.NumberUtil.sum(NumberUtil.java:16) kite.lab.utils.NumberUtil.main(NumberUtil.java:27)
Press ctrl + c, the exit prompt will be given, then press 1 to exitUse Scenes
比如哪些方法执行太慢,例如监控执行时间超过1s的方法
查看哪些方法调用了 System.gc() ,调用栈是怎样的
查看方法参数或对象属性
哪些方法发生了异常
多说一点,为了更好解决问题,最好还要配合事前准备和进行中监控,事前准备就是埋点嘛,在一些可能出现问题的方法中进行日志输出,进行中监控就是利用一些实时监控工具,例如 VisualVM 、jmc 这些带界面的工具或者 jdk 提供的命令行工具等,再高级一点的就是利用 Graphite 这样的Metrics 工具配合 web 界面展示出来。
使用限制
BTrace class不能新建类, 新建数组, 抛异常, 捕获异常,
不能调用实例方法以及静态方法(com.sun.btrace.BTraceUtils除外)
不能将目标程序和对象赋值给BTrace的实例和静态field
不能定义外部, 内部, 匿名, 本地类
不能有同步块和方法
不能有循环
不能实现接口, 不能扩展类
不能使用assert语句, 不能使用class字面值
1. 精准定位
直接定位到一个类下的一个方法,上面测试用的例子就是
2. 正则表达式定位
正则表达式在两个"/" 之间,例如下面的例子,监控 javax.swing 包下的所有方法,注意正式环境中,范围尽可能小一点,太大了性能会有影响。
@OnMethod(clazz="/javax\\.swing\\..*/", method="/.*/") public static void swingMethods( @ProbeClassName String probeClass, @ProbeMethodName String probeMethod) { print("entered " + probeClass + "." + probeMethod); }
通过在拦截函数的定义里注入@ProbeClassName String probeClass, @ProbeMethodName String probeMethod 参数,告诉脚本实际匹配到的类和方法名。
3. 按接口或继承类定位
@OnMethod(clazz="+com.kite.base", method="doSome")
4. 按注解定位
在前面加上 @ 即可,例如@OnMethod(clazz="@javax.jws.WebService", method="@javax.jws.WebMethod")
拦截时机由 location 决定,当然也可为同一个定位加入多个拦截时机,即可以在进入方法时拦截、方法返回时拦截、抛出异常时拦截
1. Kind.Entry与Kind.Return
2. Kind.Error, Kind.Throw和 Kind.Catch
表示异常被 throw 、异常被捕获还有异常发生但是没有被捕获的情况,在拦截函数的参数定义里注入一个Throwable的参数,代表异常
@OnMethod(clazz = "com.kite.demo", location = @Location(value = Kind.LINE, line = 20)) public static void onBind() { println("执行到第20行"); }
@OnMethod(clazz = "java.net.ServerSocket", method = "bind", location =@Location(Kind.ERROR)) public static void onBind(Throwable exception, @Duration long duration){ }
3. Kind.Call 和 Kind.Line
Kind.Call 表示被监控的方法调用了哪些其他方法,例如:
@OnMethod(clazz = "com.kite", method = "login", location = @Location(value = Kind.CALL, clazz = "/.*/", method = "/.*/", where = Where.AFTER)) public static void onBind(@Self Object self, @TargetInstance Object instance, @TargetMethodOrField String method, @Duration long duration){ println(strcat("self: ", str(self))); println(strcat("instance: ", str(instance))); println(strcat("method: ", str(method))); println(strcat("duration(ms): ", str(duration / 1000000))); }
Kind.Line 监测类是否执行到了设置的行数,例如:
@OnMethod(clazz = "com.kite.demo", location = @Location(value = Kind.LINE, line = 20)) public static void onBind() { println("执行到第20行"); }
@OnMethod(clazz = "java.lang.System", method = "gc") public static void onSystemGC() { println("entered System.gc()"); jstack(); }
@OnMethod(clazz = "/com\\.kite\\.controller\\..*/",method = "/.*/",location = @Location(Kind.RETURN)) public static void slowQuery(@ProbeClassName String pcn,@ProbeMethodName String probeMethod, @Duration long duration){ if(duration > 1000000 * 100){ println(strcat("类:", pcn)); println(strcat("方法:", probeMethod)); println(strcat("时长:", str(duration / 1000000))); } }
The above is the detailed content of Detailed explanation of BTrace for Java online troubleshooting. For more information, please follow other related articles on the PHP Chinese website!