From f7dba2d33daca8d61ce9169c773efed61faaa15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=91=E6=88=9F?= Date: Mon, 16 Nov 2020 14:40:24 +0800 Subject: [PATCH] add instrument process debug log --- .../com/alibaba/testable/agent/PreMain.java | 16 ++++++++++++++ .../agent/handler/SourceClassHandler.java | 3 +++ .../transformer/TestableClassTransformer.java | 3 +++ .../alibaba/testable/agent/util/LogUtil.java | 21 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 testable-agent/src/main/java/com/alibaba/testable/agent/util/LogUtil.java diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java index 35f74e5..17257b4 100755 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/PreMain.java @@ -1,6 +1,7 @@ package com.alibaba.testable.agent; import com.alibaba.testable.agent.transformer.TestableClassTransformer; +import com.alibaba.testable.agent.util.LogUtil; import java.lang.instrument.Instrumentation; @@ -10,8 +11,23 @@ import java.lang.instrument.Instrumentation; */ public class PreMain { + private static final String AND = "&"; + private static final String DEBUG = "debug"; + public static void premain(String agentArgs, Instrumentation inst) { + parseArgs(agentArgs); inst.addTransformer(new TestableClassTransformer()); } + private static void parseArgs(String args) { + if (args == null) { + return; + } + for (String a : args.split(AND)) { + if (a.equals(DEBUG)) { + LogUtil.enableDebugLog(); + } + } + } + } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java index b00119e..665cdeb 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/handler/SourceClassHandler.java @@ -4,6 +4,7 @@ import com.alibaba.testable.agent.constant.ConstPool; import com.alibaba.testable.agent.model.MethodInfo; import com.alibaba.testable.agent.util.BytecodeUtil; import com.alibaba.testable.agent.util.ClassUtil; +import com.alibaba.testable.agent.util.LogUtil; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.*; @@ -142,6 +143,7 @@ public class SourceClassHandler extends BaseClassHandler { private AbstractInsnNode[] replaceNewOps(ClassNode cn, MethodNode mn, String newOperatorInjectMethodName, AbstractInsnNode[] instructions, int start, int end) { + LogUtil.debug(" Using %s mock new operation in %s", newOperatorInjectMethodName, mn.name); String classType = ((TypeInsnNode)instructions[start]).desc; String constructorDesc = ((MethodInsnNode)instructions[end]).desc; String testClassName = ClassUtil.getTestClassName(cn.name); @@ -163,6 +165,7 @@ public class SourceClassHandler extends BaseClassHandler { private AbstractInsnNode[] replaceMemberCallOps(ClassNode cn, MethodNode mn, String substitutionMethod, AbstractInsnNode[] instructions, String ownerClass, int opcode, int start, int end) { + LogUtil.debug(" Using %s mock method in %s", substitutionMethod, mn.name); mn.maxStack++; MethodInsnNode method = (MethodInsnNode)instructions[end]; String testClassName = ClassUtil.getTestClassName(cn.name); diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java index 1b8296d..a1169db 100644 --- a/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/transformer/TestableClassTransformer.java @@ -8,6 +8,7 @@ import com.alibaba.testable.agent.model.MethodInfo; import com.alibaba.testable.agent.tool.ComparableWeakRef; import com.alibaba.testable.agent.util.AnnotationUtil; import com.alibaba.testable.agent.util.ClassUtil; +import com.alibaba.testable.agent.util.LogUtil; import org.objectweb.asm.ClassReader; import org.objectweb.asm.tree.AnnotationNode; import org.objectweb.asm.tree.ClassNode; @@ -40,11 +41,13 @@ public class TestableClassTransformer implements ClassFileTransformer { try { if (shouldTransformAsSourceClass(className)) { // it's a source class with testable enabled + LogUtil.debug("Handling source class %s", className); loadedClassNames.add(new ComparableWeakRef(className)); List injectMethods = getTestableMockMethods(ClassUtil.getTestClassName(className)); return new SourceClassHandler(injectMethods).getBytes(classFileBuffer); } else if (shouldTransformAsTestClass(className)) { // it's a test class with testable enabled + LogUtil.debug("Handling test class %s", className); loadedClassNames.add(new ComparableWeakRef(className)); return new TestClassHandler().getBytes(classFileBuffer); } diff --git a/testable-agent/src/main/java/com/alibaba/testable/agent/util/LogUtil.java b/testable-agent/src/main/java/com/alibaba/testable/agent/util/LogUtil.java new file mode 100644 index 0000000..22b6b8c --- /dev/null +++ b/testable-agent/src/main/java/com/alibaba/testable/agent/util/LogUtil.java @@ -0,0 +1,21 @@ +package com.alibaba.testable.agent.util; + +/** + * @author flin + */ +public class LogUtil { + + private static final int LEVEL_INFO = 0; + private static final int LEVEL_DEBUG = 2; + private static int level = LEVEL_INFO; + + public static void debug(String msg, Object... args) { + if (level >= LEVEL_DEBUG) { + System.err.println(String.format("[DEBUG] " + msg, args)); + } + } + + public static void enableDebugLog() { + level = LEVEL_DEBUG; + } +}