`
laziobird
  • 浏览: 23236 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

btrace动态跟踪线上程序

阅读更多
  • 先贴代码,有时间再补充


  • 获取方法消耗时间和调用者

import static com.sun.btrace.BTraceUtils.println;
import static com.sun.btrace.BTraceUtils.str;
import static com.sun.btrace.BTraceUtils.strcat;
import static com.sun.btrace.BTraceUtils.timeMillis;

import static 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.TLS;
@BTrace
public class TraceHelloWorld {

	@TLS
	private static long startTime = 0;

	@OnMethod(clazz = "my.app.test.HelloWorld", method = "execute")
	public static void startMethod(){
		startTime = timeMillis();
	}

	@OnMethod(clazz = "my.app.test.HelloWorld", method = "execute", location = @Location(Kind.RETURN))
	public static void endMethod(){
		println(strcat("the class method execute time=>", str(timeMillis()-startTime)));
		println("-------------------------------------------");
	}

	@OnMethod(clazz = "my.app.test.HelloWorld", method = "execute", location = @Location(Kind.RETURN))
	public static void traceExecute(@ProbeClassName String name,@ProbeMethodName String method,int sleepTime){
		println(strcat("the class name=>", name));
		println(strcat("the class method=>", method));
		println(strcat("the class method params=>", str(sleepTime)));
		println("who call CaseObject.execute :");
		jstack();

	}
}





  • 跟踪出现异常的方法



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) {
            jstack(currentException);
            println("=====================");
            currentException = null;
        }
    }
}


  • 跟踪内存

public class PrintMemory {    
   
    /* 
      * 指定内存区域低于一定的界限的时候才内存使用打印数据<br> 也可以指定时间间隔打印内存使用 
      */   
    @OnLowMemory(pool = "Tenured Gen", threshold = 6000000)    
    public static void printMem(MemoryUsage mu) {    
         print("MemoryUsage : ");    
         println(mu);    
         print("FreeMem : ");    
         println(freeMemory());    
         print("Heap:");    
         println(heapUsage());    
         print("Non-Heap:");    
         println(nonHeapUsage());    
     }    
}   



  • 查看死锁情况

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;

/**
 * This BTrace program demonstrates deadlocks
 * built-in function. This example prints 
 * deadlocks (if any) once every 4 seconds.
 */ 
@BTrace public class Deadlock {
    @OnTimer(4000)
    public static void print() {
        deadlocks();        
    }
}
 



  • btrace使用可以看看这个教程
http://blog.csdn.net/qyongkang/article/details/6090497
分享到:
评论

相关推荐

    btrace java线上debug神器

    btrace java线上debug神器,对线上代码自动进行注入,打日志等

    btrace.jar

    btrace提供了线上检测java程序的功能,使得可以不重启程序的情况下,在线上对程序进行跟踪。不过在日常的调试维护中,还是建议尽量使用日志来帮忙跟踪调试,btrace只作为辅助性工具

    java btrace线上代码调试工具

    java btrace线上代码调试工具,可以用来在不关闭线上系统的情况下动态植入调试代码

    btrace1.3.11

    btrace1.3.11,用来动态跟踪线上java代码的隐藏bug、OOM、GC等问题。

    BTrace-一个用于Java平台的安全动态的跟踪工具

    BTrace可用于动态跟踪正在运行的Java程序(类似于DTrace for OpenSolaris应用程序)。 BTrace动态地测试目标应用程序的类以注入跟踪代码(“字节代码跟踪”)。

    java程序运行跟踪利器Btrace分享

    你还在为各种意想不到的状况烦恼吗?不知如何去及时了解jvm的运行情况吗?java线程、内存使用情况等都可以通过Btrace进行跟踪分析,了解系统运行情况,方便易用!

    btrace调试工具

    BTrace通过动态(字节码)检测类运行Java程序。BTrace插入跟踪行动运行Java程序的类和hotswaps跟踪程序类。 因此,也就成为我们线上跟踪生产代码的有力工具!

    一个用于演示btrace功能的demo程序

    这是一个用于演示btrace功能的demo程序, 其中TestJar4是源码,使用maven作为项目管理工具 TestJar4-0.0.1-SNAPSHOT.jar为maven构建之后的jar包 AllMethod.java是trace script

    BTrace安装包 btrace-bin-1.3.11.3.zip + btrace-src-1.3.11.3.zip

    linux和windows通用,1.3.11版本。btrace-bin直接解压缩配置环境变量后即可运行使用。 java监控调试工具虚拟机监控程序,利用BTrace...BTrace将跟踪操作插入到正在运行的Java程序的类中,并对跟踪的程序类进行热交换。

    btrace:BTrace-用于Java平台的安全,动态跟踪工具

    BTrace动态地检测目标应用程序的类以注入跟踪代码(“字节码跟踪”)。 学分 基于 由提供支持 由提供支持 使用优化 使用构建环境助手 建筑BTrace 建立 您将需要安装以下应用程序 JDK -JDK 8,Java 9和Java 11必须...

    bTrace跟踪线程堆栈

    NULL 博文链接:https://junefsh.iteye.com/blog/1770800

    btrace-bin-1.3.9

    BTrace作为线上问题定位神器,它在侵入、安全、资源占用等方面表现的都非常出色。

    btrace-bin-1.3.9.tgz

    Java的安全动态追踪工具 BTrace通过运行Java程序的动态(字节码)工具类来工作。 BTrace将追踪操作插入正在运行的Java程序的类中,并将被追踪的程序类热插拔。

    Btrace非侵入式调试Java程序神奇win版

    Btrace非侵入式调试Java程序神奇win版本,可以在线调试java应用不需要重新编译

    btrace release-1.2.5.1

    btrace btrace btrace btrace

    btrace:另一个btrace分支!

    BTrace动态地检测目标应用程序的类以注入跟踪代码(“字节码跟踪”)。 建筑BTrace 设置 您将需要安装以下应用程序 (最好是JDK8) 建造 cd &lt;btrace&gt; mvn --also-make --projects dist install verify 二进制dist...

    btrace api 1.2.chm

    btrace api 1.2 文档,从网上扒下来自己做的,其他地方貌似都没有...btrace是一个跟踪、监控java程序的小工具,能够在不改变源代码的情况下监控很多东西,比如:方法运行时间、输入输出参数、抛出的异常、调用的次数等

    btrace-bin.zip

    BTrace 是一款利用hotSpot虚拟机可以动态替换class的特点而完成的,可以对online的程序动态的改变类的行为(一般为加些打印日志),进而进行线上调试的一个工具。

    Btrace非侵入式调试Java程序神奇linux版

    Btrace非侵入式调试Java程序神奇linux版,可以在线调试java程序无需重新编译

    btrace扩展功能工具

    1.btrace扩展是在btrace已由功能上进行的扩展,原有功能和使用方式依然没变。目前版本扩展了两个功能:接口时间监控和接口时间调用树监控。扩展之后的btrace功能使用时都不需要写btrace脚本。 2.使用接口时间监控...

Global site tag (gtag.js) - Google Analytics