美文网首页
用btrace分析线上服务

用btrace分析线上服务

作者: AGIHunt | 来源:发表于2015-01-26 14:06 被阅读360次

部署

使用方式

  • 基本使用方式

      sh /btrace-1.2.5.1/bin/btrace <pid>  <your .java btrace class>
    

实战

  • 动态观察指定进程中所有产生throwable(包括被吞掉的exception)的逻辑(打出stacktrace)

      sh ~/xjyin/btrace-1.2.5.1/bin/btrace <pid>  ~/xjyin/btrace-1.2.5.1/myClasses/OnThrow.java > exception.log
    
  • OnThrow.java类内容如下

      /*
       * Copyright 2008-2010 Sun Microsystems, Inc.  All Rights Reserved.
       * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       *
       * This code is free software; you can redistribute it and/or modify it
       * under the terms of the GNU General Public License version 2 only, as
       * published by the Free Software Foundation.  Sun designates this
       * particular file as subject to the "Classpath" exception as provided
       * by Sun in the LICENSE file that accompanied this code.
       *
       * This code is distributed in the hope that it will be useful, but WITHOUT
       * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       * version 2 for more details (a copy is included in the LICENSE file that
       * accompanied this code).
       *
       * You should have received a copy of the GNU General Public License version
       * 2 along with this work; if not, write to the Free Software Foundation,
       * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       *
       * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       * CA 95054 USA or visit www.sun.com if you need additional information or
       * have any questions.
       */
    
      package lisn;
    
      import com.sun.btrace.annotations.*;
      import static com.sun.btrace.BTraceUtils.*;
    
      /**
       * This example demonstrates printing stack trace
       * of an exception and thread local variables. This
       * trace script prints exception stack trace whenever
       * java.lang.Throwable's constructor returns. This way
       * you can trace all exceptions that may be caught and
       * "eaten" silently by the traced program. Note that the
       * assumption is that the exceptions are thrown soon after
       * creation [like in "throw new FooException();"] rather
       * that be stored and thrown later.
       */
      @BTrace public class OnThrow {
          // 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) {
              currentException = self;
          }
    
          @OnMethod(
              clazz="java.lang.Throwable",
              method="<init>"
          )
          public static void onthrow1(@Self Throwable self, String s) {
              currentException = self;
          }
    
          @OnMethod(
              clazz="java.lang.Throwable",
              method="<init>"
          )
          public static void onthrow1(@Self Throwable self, String s, Throwable cause) {
              currentException = self;
          }
    
          @OnMethod(
              clazz="java.lang.Throwable",
              method="<init>"
          )
          public static void onthrow2(@Self Throwable self, 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) {
                  Threads.jstack(currentException);
                  println("=====================");
                  currentException = null;
              }
          }
      }
    
  • 更多例子见 ~/xjyin/btrace-1.2.5.1/samples/

相关文章

  • 用btrace分析线上服务

    部署 btrace1.2.5.1已经部署到集群/home/xjyin/btrace-1.2.5.1 使用方式 基本...

  • BTrace:Java 线上问题排查神器

    BTrace 是什么 BTrace 是检查和解决线上的问题的杀器,BTrace 可以通过编写脚本的方式,获取程序执...

  • Gain Running State by Btrace Scr

    Btrace在github上对自己的介绍是: Btrace是一款利用了Java动态织入技术来追踪已经部署在线上的应...

  • btrace线上定位问题

    开发环境可以local debug,测试环境可以remote debug,线上环境只能看看log了, debug肯...

  • BTrace实现原理

    BTrace是每个Java程序员必备的瑞士军刀,可以实现线上服务器不重启增加调试信息。本文简单介绍一下其实现原理。...

  • 项目开发常用工具

    线上排查工具 Btrace Greys DevOps工具 teletraan gopub 容器相关工具 Jib 未完待续

  • Btrace学习笔记二

    Btrace拦截行号 被拦截对象(Btrace只能在本地运行) 拦截行号的btrace脚本 运行btrace脚本 ...

  • Btrace UserGuide翻译转载

    原文地址:BTrace用户手册<译> BTrace(https://btrace.dev.java.net/) 是...

  • BTrace:线上问题排查工具

    BTrace简介 GitHub地址:BTrace 下载地址:v1.3.11.3 官方使用教程:Btrace使用教程...

  • btrace-doc

    btrace on github demo 动态获取传入参数 源码 btrace脚本(PrintArgSimple...

网友评论

      本文标题:用btrace分析线上服务

      本文链接:https://www.haomeiwen.com/subject/foukxttx.html