add instrument process debug log

This commit is contained in:
金戟 2020-11-16 14:40:24 +08:00
parent 36e260688e
commit f7dba2d33d
4 changed files with 43 additions and 0 deletions

View File

@ -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();
}
}
}
}

View File

@ -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);

View File

@ -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<String>(className));
List<MethodInfo> 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<String>(className));
return new TestClassHandler().getBytes(classFileBuffer);
}

View File

@ -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;
}
}