BTrace功能

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

BTrace功能  

一、背景
       在生产环境中可能经常遇到各种问题,定位问题需要获取程序运行时的数据信息,如方法参数、返回值、全局变量、堆栈信息等。为了获取这些数据信息,我们可以 通过改写代码,增加日志信息的打印,再发布到生产环境。通过这种方式,一方面将增大定位问题的成本和周期,对于紧急问题无法做到及时响应;另一方面重新部 署后环境可能已被破坏,很难重新问题的场景。

二、BTrace功能
       BTrace天生就为解决这类问题而来,它可以动态地跟踪java运行程序。通过hotswap技术,动态将跟踪字节码注入到运行类中,对运行代码侵入较小,对性能上的影响可以忽略不计。
       BTrace在使用上有很多限制条件,如不能创建对象、数组、抛出和捕获异常、循环等,具体限制条件参考用户文档中的BTrace Restrictions。用户文档地址: http://kenai.com/projects/btrace/pages/UserGuide。
       根据官方声明,不当地使用btrace可能导致jvm崩溃,如BTrace使用错误的.class文件,Hotspot JVM自身存在的hotswap bug等。可以先在本地验证BTrace脚本的正确性,再传到生产环境中定位问题。

   
三、安装步骤
1. 下载安装压缩包,最新版本的是1.2.1,下载地址: http://kenai.com/projects/btrace/downloads/directory/releases。
2. 解压缩,命令脚本放在bin目录中。
3. 设置脚本环境变量。
4. 增加脚本可执行权限。

四、使用方法
BTrace主要包含btracec和btrace两个命令编译和启动BTrace脚本:
1. btrace
功能: 用于运行BTrace跟踪程序。
命令格式:
btrace [-I <include-path>] [-p <port>] [-cp <classpath>] <pid> <btrace-script> [<args>]
示例:
btrace -cp build/  1200 AllCalls1.java
参数含义:
include-path指定头文件的路径,用于脚本预处理功能,可选;
port指定BTrace agent的服务端监听端口号,用来监听clients,默认为2020,可选;
classpath用来指定类加载路径,默认为当前路径,可选;
pid表示进程号,可通过jps命令获取;
btrace-script即为BTrace脚本;btrace脚本如果以.java结尾,会先编译再提交执行。可使用btracec命令对脚本进行预编译。
args是BTrace脚本可选参数,在脚本中可通过"$"和"$length"获取参数信息。

2. btracec
功能: 用于预编译BTrace脚本,用于在编译时期验证脚本正确性。
btracec [-I <include-path>] [-cp <classpath>] [-d <directory>] <one-or-more-BTrace-.java-files>
参数意义同btrace命令一致,directory表示编译结果输出目录。

3. btracer
功能: btracer命令同时启动应用程序和BTrace脚本,即在应用程序启动过程中使用BTrace脚本。而btrace命令针对已运行程序执行BTrace脚本。
命令格式:
btracer <pre-compiled-btrace.class> <application-main-class> <application-args>
参数说明:
pre-compiled-btrace.class表示经过btracec编译后的BTrace脚本。
application-main-class表示应用程序代码;
application-args表示应用程序参数。
该命令的等价写法为:
java -javaagent:btrace-agent.jar=script=<pre-compiled-btrace-script1>[,<pre-compiled-btrace-script1>]* <MainClass> <AppArguments>

4. jvisualvm插件
BTrace提供了jvisualvm插件,强烈推荐在jvisualvm中编写和测试BTrace脚本,启动、关闭、发送事件、增加classpath都非常方便。


五、BTrace实战
1. 示例代码
示例代码定义了Counter计数器,有一个add()方法,每次增加随机值,总数保存在totalCount属性中。

Btracetest.java代码   收藏代码
  1. package com.learnworld;  
  2. import java.util.Random;  
  3.   
  4. public class BTraceTest {  
  5.   
  6.     public static void main(String[] args) throws Exception {  
  7.         Random random = new Random();  
  8.   
  9.         // 计数器  
  10.         Counter counter = new Counter();  
  11.         while (true) {  
  12.             // 每次增加随机值  
  13.             counter.add(random.nextInt(10));  
  14.             Thread.sleep(1000);  
  15.         }  
  16.     }  
  17. }  

 

Counter.java代码   收藏代码
  1. package com.learnworld;  
  2. public class Counter {  
  3.     // 总数  
  4.     private static int totalCount = 0;  
  5.   
  6.     public int add(int num) throws Exception {  
  7.         totalCount += num;  
  8.         sleep();  
  9.         return totalCount;  
  10.     }  
  11.       
  12.     public void sleep() throws Exception {  
  13.         Thread.sleep(1000);  
  14.     }  
  15.   
  16. }  




2. 常见使用场景
下面通过几个常见使用场景演示如何使用BTrace脚本。

1) 获取add()方法参数值和返回值。

Java代码   收藏代码
  1. import com.sun.btrace.annotations.*;  
  2. import static com.sun.btrace.BTraceUtils.*;  
  3.   
  4. @BTrace  
  5. public class TracingScript {  
  6.    @OnMethod(  
  7.       clazz="com.learnworld.Counter",  
  8.       method="add",  
  9.       location=@Location (Kind.RETURN)  
  10.    )  
  11.    public static void traceExecute(int num,@Return  int result){  
  12.      println("====== ");  
  13.      println(strcat("parameter num: ",str(num)));  
  14.      println(strcat("return value:",str(result)));  
  15.    }  
  16. }  



2) 定时获取Counter类的属性值totalCount。

Java代码   收藏代码
  1. import com.sun.btrace.annotations.*;  
  2. import static com.sun.btrace.BTraceUtils.*;  
  3.   
  4. @BTrace  
  5. public class TracingScript {  
  6.    private static Object totalCount = 0;  
  7.      
  8.    @OnMethod(  
  9.       clazz="com.learnworld.Counter",  
  10.       method="add",  
  11.       location=@Location(Kind.RETURN)  
  12.    )   
  13.    public static void traceExecute(@Self com.learnworld.Counter counter){  
  14.      totalCount = get(field("com.learnworld.Counter","totalCount"), counter);  
  15.    }   
  16.       
  17.    @OnTimer(1000)  
  18.    public static void print(){  
  19.      println("====== ");  
  20.      println(strcat("totalCount: ",str(totalCount)));  
  21.    }  
  22. }  




3) 获取add方法执行时间。

Java代码   收藏代码
  1. import com.sun.btrace.annotations.*;  
  2. import static com.sun.btrace.BTraceUtils.*;  
  3.   
  4. @BTrace  
  5. public class TracingScript {  
  6.    @TLS private static long startTime = 0;  
  7.      
  8.    @OnMethod(  
  9.       clazz="com.learnworld.Counter",  
  10.       method="add"  
  11.    )   
  12.    public static void startExecute(){  
  13.      startTime = timeNanos();  
  14.    }   
  15.       
  16.    @OnMethod(  
  17.       clazz="com.learnworld.Counter",  
  18.       method="add",  
  19.       location=@Location(Kind.RETURN)  
  20.    )   
  21.    public static void endExecute(@Duration long duration){  
  22.      long time = timeNanos() - startTime;  
  23.      println(strcat("execute time(nanos): ", str(time)));  
  24.      println(strcat("duration(nanos): ", str(duration)));  
  25.    }   
  26. }  



4) 获取add()方法调用方法sleep()次数。

Java代码   收藏代码
  1. import com.sun.btrace.annotations.*;  
  2. import static com.sun.btrace.BTraceUtils.*;  
  3.   
  4. @BTrace  
  5. public class TracingScript {  
  6.    private static long count;   
  7.        
  8.    @OnMethod(  
  9.       clazz="/.*/",  
  10.       method="add",  
  11.       location=@Location(value=Kind.CALL, clazz="/.*/", method="sleep")  
  12.    )  
  13.    public static void traceExecute(@ProbeClassName String pcm, @ProbeMethodName String pmn,  
  14.    @TargetInstance Object instance,  @TargetMethodOrField String method){  
  15.      println("====== ");  
  16.      println(strcat("ProbeClassName: ",pcm));  
  17.      println(strcat("ProbeMethodName: ",pmn));  
  18.      println(strcat("TargetInstance: ",str(classOf(instance))));  
  19.      println(strcat("TargetMethodOrField : ",str(method)));  
  20.      count++;  
  21.    }  
  22.      
  23.    @OnEvent  
  24.    public static void getCount(){  
  25.        println(strcat("count: ", str(count)));  
  26.    }  
  27. }  



六、参考文档
1. userGuide: http://kenai.com/projects/btrace/pages/UserGuide
2. JAVA doc:  http://btrace.kenai.com/javadoc/1.2/index.html
3. BTrace用户手册<译>,http://macrochen.iteye.com/blog/838920
4. btrace使用简介,http://rdc.taobao.com/team/jm/archives/509
5. btrace记忆,http://agapple.iteye.com/blog/962119
6. btrace一些你不知道的事(源码入手),http://agapple.iteye.com/blog/1005918

 

http://learnworld.iteye.com/blog/1402763

转载于:https://my.oschina.net/xiaominmin/blog/1599641

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278711.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

.NET(c#) 移动APP开发平台 - Smobiler(1)

原文&#xff1a;https://www.cnblogs.com/oudi/p/8288617.html 如果说基于.net的移动开发平台&#xff0c;目前比较流行的可能是xamarin了&#xff0c;不过除了这个&#xff0c;还有一个比xamarin更好用的国内的.net移动开发平台&#xff0c;smobiler&#xff0c;不用学习另外…

如何在Vizio电视上禁用运动平滑

Vizio维齐奥New Vizio TVs use motion smoothing to make the content you watch appear smoother. This looks good for some content, like sports, but can ruin the feel of movies and TV shows. 新的Vizio电视使用运动平滑来使您观看的内容显得更平滑。 这对于某些内容(例…

无服务器架构 - 从使用场景分析其6大特性

2019独角兽企业重金招聘Python工程师标准>>> 无服务器架构 - 从使用场景分析其6大特性 博客分类&#xff1a; 架构 首先我应该提到&#xff0c;“无服务器”技术肯定有服务器涉及。 我只是使用这个术语来描述这种方法和技术&#xff0c;它将任务处理和调度抽象为与…

ES6实用方法Object.assign、defineProperty、Symbol

文章目录1.合并对象 - Object.assign()介绍进阶注意用途2.定义对象 - Object.defineProperty(obj, prop, descriptor)3.新数据类型- Symbol定义应用1.合并对象 - Object.assign() 介绍 assign方法可以将多个对象&#xff08;字典&#xff09;&#xff0c;语法&#xff1a;Obj…

Enable Authentication on MongoDB

1、Connect to the server using the mongo shell mongo mongodb://localhost:270172、Create the user administrator Change to the admin database: use admindb.createUser({user: "Admin",pwd: "Admin123",roles: [ { role: "userAdminAnyDataba…

windows驱动程序编写_如何在Windows中回滚驱动程序

windows驱动程序编写Updating a driver on your PC doesn’t always work out well. Sometimes, they introduce bugs or simply don’t run as well as the version they replaced. Luckily, Windows makes it easy to roll back to a previous driver in Windows 10. Here’s…

运行tomcat报Exception in thread ContainerBackgroundProcessor[StandardEngine[Catalina]]

解决方法1&#xff1a; 手动设置MaxPermSize大小&#xff0c;如果是linux系统&#xff0c;修改TOMCAT_HOME/bin/catalina.sh&#xff0c;如果是windows系统&#xff0c;修改TOMCAT_HOME/bin/catalina.bat&#xff0c; 在“echo "Using CATALINA_BASE: $CATALINA_BASE&q…

new子类会先运行父类的构造函数

发现子类构造函数运行时&#xff0c;先运行了父类的构造函数。为什么呢? 原因&#xff1a;子类的所有构造函数中的第一行&#xff0c;其实都有一条隐身的语句super(); super(): 表示父类的构造函数&#xff0c;并会调用于参数相对应的父类中的构造函数。而super():是在调用父类…

在Windows 7中的Windows Media Player 12中快速预览歌曲

Do you ever wish you could quickly preview a song without having to play it? Today we look at a quick and easy way to do that in Windows Media Player 12. 您是否曾经希望无需播放就可以快速预览歌曲&#xff1f; 今天&#xff0c;我们探讨一种在Windows Media Play…

Vue.js中的8种组件间的通信方式;3个组件实例是前6种通信的实例,组件直接复制粘贴即可看到运行结果

文章目录一、$children / $parent二、props / $emit三、eventBus四、ref五、provide / reject六、$attrs / $listeners七、localStorage / sessionStorage八、Vuex实例以element ui为例。例子从上往下逐渐变复杂&#xff08;后面例子没有删前面的无用代码&#xff0c;有时间重新…

不可思议的混合模式 background-blend-mode

本文接前文&#xff1a;不可思议的混合模式 mix-blend-mode 。由于 mix-blend-mode 这个属性的强大&#xff0c;很多应用场景和动效的制作不断完善和被发掘出来&#xff0c;遂另起一文继续介绍一些使用 mix-blend-mode 制作的酷炫动画。 CSS3 新增了一个很有意思的属性 -- mix-…

adb错误 - INSTALL_FAILED_NO_MATCHING_ABIS

#背景 换组啦&#xff0c;去了UC国际浏览器&#xff0c;被拥抱变化了。还在熟悉阶段&#xff0c;尝试了下adb&#xff0c;然后就碰到了这个INSTALL_FAILED_NO_MATCHING_ABIS的坑。。。 #解决方法 INSTALL_FAILED_NO_MATCHING_ABIS is when you are trying to install an app th…

如何更改从Outlook发送的电子邮件中的“答复”地址

If you’re sending an email on behalf of someone else, you might want people to reply to that person instead of you. Microsoft Outlook gives you the option to choose a different default Reply address to cover this situation. 如果您要代表其他人发送电子邮件&…

visio自定义图形填充

选中图形&#xff0c;最上面一栏&#xff1a;开发工具-操作&#xff08;-组合-连接&#xff09;-拆分

Ansible 详解2-Playbook使用

aaa转载于:https://www.cnblogs.com/Presley-lpc/p/10107491.html

Angular2官网项目 (4)--路由

行动计划 把AppComponent变成应用程序的“壳”&#xff0c;它只处理导航 把现在由AppComponent关注的英雄们移到一个独立的GeneralComponent中 添加路由 创建一个新的DashboardComponent组件 把仪表盘加入导航结构中 路由是导航的另一个名字。路由器就是从一个视图导航到另…

在Windows 7或Vista资源管理器中禁用缩略图预览

If you want to speed up browsing around in explorer, you might think about disabling thumbnail previews in folders. 如果要加快在资源管理器中的浏览速度&#xff0c;可以考虑禁用文件夹中的缩略图预览。 Note that this works in Windows 7 or Vista 请注意&#xf…

rimraf 秒删 node_modules

//第一次需要安装删除指令 npm install rimraf -g //进入对应目录后卸载node依赖库在这里插入代码片 rimraf node_modules

[Halcon] 算子学习_Calibration_Calibration Object

find_caltab find_marks_and_pose   输出参数StartPose是标定板的位姿  通过pose_to_hom_mat3d转化为Hom矩阵可得到下面的关系 3D_Point_Coord_in_Cam Hom_Mat(StartPose) * 3D_Point_Coord_in_Cam ; 转载于:https://www.cnblogs.com/LoveBuzz/p/10109202.html

mysql 表数据转储_在MySQL中仅将表结构转储到文件中

mysql 表数据转储For this exercise, we will use the mysqldump utility the same as if we were backing up the entire database. 在本练习中&#xff0c;我们将像备份整个数据库一样使用mysqldump实用程序。 Syntax: 句法&#xff1a; mysqldump -d -h localhost -u root -…