BTrace二三事之二:OnMethod子类匹配BUG(怀疑)

BTRace1.2.2版本
@OnMethod(clazz = "+my.Command", method="execute")
对子类的匹配(怀疑)存在BUG。

即:加载Agent之前的已载入的Class被拦截了,但Agent加载之后的Class没有拦截。

从源代码看应该是这样,涉及到子类匹配检查的主要两个变量:

hasSubclassChecks :是否需要检查子类匹配,如果需要的话,
所有通过ClassLoader.defineClass()加载的类,会被注入一段静态初始化代码:
{
BTraceRuntime.retransform(Ljava/lang/String;Ljava/lang/Class;)
}

作用是,在类被初始化后,立即触发类的重转换。

第二个变量是:skipRetransforms:是否跳过类的重转换,
当Agent加载后,如果已经存在部分类发生了转换,那么这个变量会被设置为true,
从而导致后续类没有被拦截。

BTraceHost:
public interface Command {
void execute();
}

public class Foo1Command implements Command {
public void execute() {
System.out.println("foo1 command execute...");
}
}

public class Foo2Command implements Command {
public void execute() {
System.out.println("foo2 command execute...");
}
}

public class HostMain {
public static void main(String[] args) {
System.out.println("pid = " + getSelfPID());
new Thread(new Spy()).start();
}
static String getSelfPID() {
String pname = ManagementFactory.getRuntimeMXBean().getName();
int index = pname.indexOf('@');
if (index == -1) {
throw new RuntimeException("获取自身进程PID失败!进程名称为:" + pname);
}
String pid = pname.substring(0, index);
return pid;
}
}

public class Spy implements Runnable {
static Map<String, String> commands = new HashMap<String, String>();
static {
commands.put("1", "my.Foo1Command");
commands.put("2", "my.Foo2Command");
}

public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter Command: ");
System.out.println("commands : " + commands);
System.out.println("请验证下面两个场景:");
System.out.println("场景一:加载Foo1;加载Agent;加载Foo2---结果:Foo1被拦截;Foo2未拦截");
System.out.println("场景二:加载Agent; 加载Foo2---结果:Foo1被拦截;Foo2被拦截");
for (String line; (line = in.readLine()) != null;) {
doAction(line);
}
}
catch (Exception e) {
e.printStackTrace();
}
}

void doAction(String action) throws Exception {
String commandName = commands.get(action);
if (commandName != null) {
Command command = (Command) Class.forName(commandName).newInstance();
command.execute();
}
else {
System.out.println("invalid command..");
System.out.println("commands : " + commands);
}
}
}


BTraceClient:
public class BTraceStarter {
public static void main(String[] args) throws Exception {
String classpath = System.getProperty("java.class.path");
if (!classpath.contains("tools.jar")) {
throw new RuntimeException("请在类路径中设置tools.jar!");
}
System.setProperty("com.sun.btrace.probeDescPath", ".");
System.setProperty("com.sun.btrace.dumpClasses", "true");
System.setProperty("com.sun.btrace.dumpDir", "./dump");
System.setProperty("com.sun.btrace.debug", "false");
System.setProperty("com.sun.btrace.unsafe", "true");

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入目标进程PID: ");
String pid = in.readLine();
String script = new File(BTraceStarter.class.getResource("SampleScript.class").getFile()).getCanonicalPath();
com.sun.btrace.client.Main.main(new String[] { pid, script });
}
}

@BTrace(unsafe = true)
public class SampleScript {
@OnMethod(clazz = "+my.Command", method = "execute", location = @Location(Kind.RETURN))
public static void onExecuteReturn(@ProbeClassName String className, @Duration long duration) {
BTraceUtils.println(Strings.concat("execute return : className = ", className));
}
}


附件一:BTrace1.2.2 执行库下载;
附件二:BTrace1.2.2 源代码下载;
附件三:验证的例子;(直接运行例子中的BTraceHost和BTraceClient即可。)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值