BTrace系列之二:BTrace简单示例

BTrace工具使用简介

“Hello World”

案例描述

使用test方法接收一个int类型参数,根据参数放入map中两个字符串。

现代码中没有打印出任何的日志,导致产生的int值无法确定,以及执行路径无法确定,这个时候可以使用BTrace来跟踪、分析程序路径。

 

import java.util.HashMap; import java.util.Map; import java.util.Random; public class TestHello { public static void main(String[] args) throws InterruptedException { Thread.sleep(2000 * 10); TestHello th = new TestHello(); Random random = new Random(47); th.test(random.nextInt(9) + 1); } private Map<String, String> model = new HashMap<String, String>(); public boolean test(int age) { String variable1 = "默认提示信息:你输入的参数是:" + age; String variable2 = "欢迎观临网上乐园"; model.put("variable1", variable1); model.put("variable2", variable2); if (age >= 1 && age <= 5) { variable1 = "你的输入参数介于1和5之间"; variable2 = "欢迎小朋友玩益智类游戏"; model.put("variable1", variable1); model.put("variable2", variable2); return true; } else if (age > 5 && age <= 10) { variable1 = "你的输入参数介于5和10之间"; variable2 = "欢迎小朋友进入英语游乐场"; model.put("variable1", variable1); model.put("variable2", variable2); return false; } else { return true; } } }

 

 

BTrace脚本:

 

import com.sun.btrace.annotations.*; import static com.sun.btrace.BTraceUtils.*; import java.util.HashMap; @BTrace public class BTraceField { @OnMethod(clazz="java.util.HashMap", method="put") public static void m(@Self HashMap map, Object Key, Object value) { // all calls to the methods with signature "()" println("===================="); print(Key); print(":"); println(value); } @OnMethod(clazz="TestHello", method="test") public static void d(@Self Object obj, int age){ print("test method is called, input age="); println(age); } }

 

首先运行TestHello

使用jps查找TestHello的PID,输入btrace <pid> BTraceField.java

 

输出:

test method is called, input age=3

====================

variable1:默认提示信息:你输入的参数是:3

====================

variable2:欢迎观临网上乐园

====================

variable1:你的输入参数介于1和5之间

====================

variable2:欢迎小朋友玩益智类游戏

 

其他相关博文

BTrace工具简介http://mgoann.iteye.com/blog/1409667 
BTrace实例应用http://mgoann.iteye.com/blog/1409676 
BTrace实际案例分析http://mgoann.iteye.com/blog/1409685

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值