用btrace分析线上服务

部署

使用方式

  • 基本使用方式

      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/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,347评论 19 139
  • /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home...
    光剑书架上的书阅读 9,386评论 2 8
  • 1.在C/C++中实现本地方法 生成C/C++头文件之后,你就需要写头文件对应的本地方法。注意:所有的本地方法的第...
    JayQiu阅读 7,024评论 0 3
  • 1、圆,原理:四个角都是圆角:四个角的取值为50%或为宽和高一样的值 2、扇形,原理:左上角是圆角,其余三个角都是...
    张延伟阅读 2,750评论 0 1