@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即可。)