Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support and fix #484

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion matrix/matrix-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ allprojects {

ext {
// For android sub-projects
minSdkVersion = 19
minSdkVersion = 16
targetSdkVersion = 23
compileSdkVersion = 28
buildToolsVersion = '28.0.3'
Expand Down
2 changes: 1 addition & 1 deletion matrix/matrix-android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryErro
#Tue Jun 20 10:24:33 CST 2017


VERSION_NAME_PREFIX=0.6.5
VERSION_NAME_PREFIX=0.6.6-SNAPSHOT
VERSION_NAME_SUFFIX=
## two options: Internal (for wechat ), External (for outsize person)
PUBLISH_CHANNEL=External
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.tencent.matrix.trace;

import com.tencent.matrix.javalib.util.FileUtil;
import com.tencent.matrix.javalib.util.Log;
import com.tencent.matrix.javalib.util.Util;
import com.tencent.matrix.trace.retrace.MappingCollector;
import com.tencent.matrix.trace.retrace.MethodInfo;

import java.util.HashSet;

public class Configuration {
public static final String TAG = "Matrix.Configuration";

public String packageName;
public String mappingDir;
Expand All @@ -16,6 +19,7 @@ public class Configuration {
public String blackListFilePath;
public String traceClassOut;
public HashSet<String> blackSet = new HashSet<>();
public HashSet<MethodInfo> blackMethodSet = new HashSet<>();

Configuration(String packageName, String mappingDir, String baseMethodMapPath, String methodMapFilePath,
String ignoreMethodMapFilePath, String blackListFilePath, String traceClassOut) {
Expand All @@ -30,8 +34,7 @@ public class Configuration {

public int parseBlackFile(MappingCollector processor) {
String blackStr = TraceBuildConstants.DEFAULT_BLACK_TRACE + FileUtil.readFileAsString(blackListFilePath);

String[] blackArray = blackStr.trim().replace("/", ".").split("\n");
String[] blackArray = blackStr.trim().replace("/", ".").split("\r\n");

if (blackArray != null) {
for (String black : blackArray) {
Expand All @@ -47,14 +50,28 @@ public int parseBlackFile(MappingCollector processor) {

if (black.startsWith("-keepclass ")) {
black = black.replace("-keepclass ", "");
Log.i(TAG, "keepclass [" + black + " ] ");
blackSet.add(processor.proguardClassName(black, black));
} else if (black.startsWith("-keeppackage ")) {
black = black.replace("-keeppackage ", "");
Log.i(TAG, "keeppackage [" + black + " ] ");
blackSet.add(processor.proguardPackageName(black, black));
} else if (black.startsWith("-keepmethod")) {
Log.i(TAG, black);
black = black.replace("-keepmethod ", "");
String[] keepMethod = black.split(" ");
String originalClass = keepMethod[0];
String originalMethod = keepMethod[1];
String originalMethodDesc = keepMethod[2];
Log.i(TAG, "keepmethod [" + originalClass + "#" + originalMethod + " . desc = " + originalMethodDesc + " ] ");
MethodInfo methodInfo = processor.proguardMethodName(originalClass, originalMethod, originalMethodDesc);
if (methodInfo != null) {
blackMethodSet.add(methodInfo);
}
}
}
}
return blackSet.size();
return blackSet.size() + blackMethodSet.size();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tencent.matrix.javalib.util.Log;
import com.tencent.matrix.trace.item.TraceMethod;
import com.tencent.matrix.trace.retrace.MappingCollector;
import com.tencent.matrix.trace.retrace.MethodInfo;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
Expand All @@ -23,6 +24,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
Expand Down Expand Up @@ -81,12 +83,12 @@ public void collect(Set<File> srcFolderList, Set<File> dependencyJarList) throws
}

for (File classFile : classFileList) {
futures.add(executor.submit(new CollectSrcTask(classFile)));
futures.add(executor.submit(new CollectSrcTask(classFile, mappingCollector)));
}
}

for (File jarFile : dependencyJarList) {
futures.add(executor.submit(new CollectJarTask(jarFile)));
futures.add(executor.submit(new CollectJarTask(jarFile, mappingCollector)));
}

for (Future future : futures) {
Expand Down Expand Up @@ -119,9 +121,11 @@ public void run() {
class CollectSrcTask implements Runnable {

File classFile;
MappingCollector mappingCollector;

CollectSrcTask(File classFile) {
CollectSrcTask(File classFile, MappingCollector mappingCollector) {
this.classFile = classFile;
this.mappingCollector = mappingCollector;
}

@Override
Expand All @@ -131,7 +135,7 @@ public void run() {
is = new FileInputStream(classFile);
ClassReader classReader = new ClassReader(is);
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor visitor = new TraceClassAdapter(Opcodes.ASM5, classWriter);
ClassVisitor visitor = new TraceClassAdapter(Opcodes.ASM5, classWriter, mappingCollector);
classReader.accept(visitor, 0);

} catch (Exception e) {
Expand All @@ -148,9 +152,11 @@ public void run() {
class CollectJarTask implements Runnable {

File fromJar;
MappingCollector mappingCollector;

CollectJarTask(File jarFile) {
CollectJarTask(File jarFile, MappingCollector mappingCollector) {
this.fromJar = jarFile;
this.mappingCollector = mappingCollector;
}

@Override
Expand All @@ -167,7 +173,7 @@ public void run() {
InputStream inputStream = zipFile.getInputStream(zipEntry);
ClassReader classReader = new ClassReader(inputStream);
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor visitor = new TraceClassAdapter(Opcodes.ASM5, classWriter);
ClassVisitor visitor = new TraceClassAdapter(Opcodes.ASM5, classWriter, mappingCollector);
classReader.accept(visitor, 0);
}
}
Expand Down Expand Up @@ -269,9 +275,11 @@ private class TraceClassAdapter extends ClassVisitor {
private String className;
private boolean isABSClass = false;
private boolean hasWindowFocusMethod = false;
private MappingCollector mappingCollector;

TraceClassAdapter(int i, ClassVisitor classVisitor) {
TraceClassAdapter(int i, ClassVisitor classVisitor, MappingCollector mappingCollector) {
super(i, classVisitor);
this.mappingCollector = mappingCollector;
}

@Override
Expand All @@ -293,20 +301,22 @@ public MethodVisitor visitMethod(int access, String name, String desc,
if (!hasWindowFocusMethod) {
hasWindowFocusMethod = isWindowFocusChangeMethod(name, desc);
}
return new CollectMethodNode(className, access, name, desc, signature, exceptions);
return new CollectMethodNode(className, access, name, desc, signature, exceptions, mappingCollector);
}
}
}

private class CollectMethodNode extends MethodNode {
private String className;
private boolean isConstructor;
private MappingCollector mappingCollector;


CollectMethodNode(String className, int access, String name, String desc,
String signature, String[] exceptions) {
String signature, String[] exceptions, MappingCollector mappingCollector) {
super(Opcodes.ASM5, access, name, desc, signature, exceptions);
this.className = className;
this.mappingCollector = mappingCollector;
}

@Override
Expand All @@ -318,24 +328,32 @@ public void visitEnd() {
isConstructor = true;
}

boolean isNeedTrace = isNeedTrace(configuration, traceMethod.className, mappingCollector);
boolean isMethodNeedTrace = isMethodNeedTrace(configuration, mappingCollector, traceMethod.className,
traceMethod.methodName, traceMethod.desc);
if (!isMethodNeedTrace) {
Log.i(TAG, "keep method not trace [" + traceMethod.className + "#" + traceMethod.methodName + " . desc = " + traceMethod.desc + " ] ");
ignoreCount.incrementAndGet();
collectedIgnoreMethodMap.put(traceMethod.getMethodName(), traceMethod);
return;
}

boolean isClassNeedTrace = isClassNeedTrace(configuration, traceMethod.className, mappingCollector);
// filter simple methods
if ((isEmptyMethod() || isGetSetMethod() || isSingleMethod())
&& isNeedTrace) {
&& isClassNeedTrace) {
ignoreCount.incrementAndGet();
collectedIgnoreMethodMap.put(traceMethod.getMethodName(), traceMethod);
return;
}

if (isNeedTrace && !collectedMethodMap.containsKey(traceMethod.getMethodName())) {
if (isClassNeedTrace && !collectedMethodMap.containsKey(traceMethod.getMethodName())) {
traceMethod.id = methodId.incrementAndGet();
collectedMethodMap.put(traceMethod.getMethodName(), traceMethod);
incrementCount.incrementAndGet();
} else if (!isNeedTrace && !collectedIgnoreMethodMap.containsKey(traceMethod.className)) {
} else if (!isClassNeedTrace && !collectedIgnoreMethodMap.containsKey(traceMethod.className)) {
ignoreCount.incrementAndGet();
collectedIgnoreMethodMap.put(traceMethod.getMethodName(), traceMethod);
}

}

private boolean isGetSetMethod() {
Expand Down Expand Up @@ -408,11 +426,27 @@ private boolean isEmptyMethod() {

}

public static boolean isMethodNeedTrace(Configuration configuration,
MappingCollector mappingCollector, String className, String methodName, String desc) {
Iterator<MethodInfo> iterator = configuration.blackMethodSet.iterator();
while (iterator.hasNext()) {
MethodInfo methodInfo = iterator.next();
if (methodInfo.getOriginalClassName().equals(className)
&& methodInfo.getOriginalName().equals(methodName)) {
MappingCollector.DescInfo descInfo = mappingCollector.parseMethodDesc(desc, true);
if (methodInfo.matches(descInfo.getReturnType(), descInfo.getArguments())) {
return false;
}
}
}
return true;
}

public static boolean isWindowFocusChangeMethod(String name, String desc) {
return null != name && null != desc && name.equals(TraceBuildConstants.MATRIX_TRACE_ON_WINDOW_FOCUS_METHOD) && desc.equals(TraceBuildConstants.MATRIX_TRACE_ON_WINDOW_FOCUS_METHOD_ARGS);
}

public static boolean isNeedTrace(Configuration configuration, String clsName, MappingCollector mappingCollector) {
public static boolean isClassNeedTrace(Configuration configuration, String clsName, MappingCollector mappingCollector) {
boolean isNeed = true;
if (configuration.blackSet.contains(clsName)) {
isNeed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void visit(int version, int access, String name, String signature, String
super.visit(version, access, name, signature, superName, interfaces);
this.className = name;
this.isActivityOrSubClass = isActivityOrSubClass(className, collectedClassExtendMap);
this.isNeedTrace = MethodCollector.isNeedTrace(configuration, className, mappingCollector);
this.isNeedTrace = MethodCollector.isClassNeedTrace(configuration, className, mappingCollector);
if ((access & Opcodes.ACC_ABSTRACT) > 0 || (access & Opcodes.ACC_INTERFACE) > 0) {
this.isABSClass = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class TraceBuildConstants {
public final static String MATRIX_TRACE_ON_WINDOW_FOCUS_METHOD_ARGS = "(Z)V";
public static final String[] UN_TRACE_CLASS = {"R.class", "R$", "Manifest", "BuildConfig"};
public final static String DEFAULT_BLACK_TRACE =
"[package]\n"
+ "-keeppackage android/\n"
+ "-keeppackage com/tencent/matrix/\n";
"[package]\r\n"
+ "-keeppackage android/\r\n"
+ "-keeppackage com/tencent/matrix/\r\n";

private static final int METHOD_ID_MAX = 0xFFFFF;
public static final int METHOD_ID_DISPATCH = METHOD_ID_MAX - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ public String proguardPackageName(String originalPackage, String defaultPackage)
}
}

public MethodInfo proguardMethodName(String originalClassName, String originalMethodName, String originalMethodDesc) {
MethodInfo findMethod = null;
DescInfo descInfo = parseMethodDesc(originalMethodDesc, true);
// Log.i(TAG, "proguardMethodName desc parse " + descInfo);
// Class name -> obfuscated method names.
Map<String, Set<MethodInfo>> methodMap = mOriginalClassMethodMap.get(originalClassName);
if (methodMap != null) {
Set<MethodInfo> methodSet = methodMap.get(originalMethodName);
if (null != methodSet) {
// Find all matching methods.
Iterator<MethodInfo> methodInfoIterator = methodSet.iterator();
while (methodInfoIterator.hasNext()) {
MethodInfo methodInfo = methodInfoIterator.next();
if (methodInfo.matches(descInfo.returnType, descInfo.arguments)) {
findMethod = new MethodInfo(methodInfo);
findMethod.setDesc(methodInfo.desc);
break;
}
}
}
}
return findMethod;
}

/**
* get original method info
*
Expand Down Expand Up @@ -200,7 +224,7 @@ private void obfuscatedMethodInfo(MethodInfo methodInfo) {
* @param isRawToObfuscated
* @return
*/
private DescInfo parseMethodDesc(String desc, boolean isRawToObfuscated) {
public DescInfo parseMethodDesc(String desc, boolean isRawToObfuscated) {
DescInfo descInfo = new DescInfo();
Type[] argsObj = Type.getArgumentTypes(desc);
StringBuffer argumentsBuffer = new StringBuffer();
Expand Down Expand Up @@ -268,7 +292,7 @@ private DescInfo parseMethodDesc(String desc, boolean isRawToObfuscated) {
/**
* about method desc info
*/
private static class DescInfo {
public static class DescInfo {
private String desc;
private String arguments;
private String returnType;
Expand All @@ -284,6 +308,19 @@ public void setReturnType(String returnType) {
public void setDesc(String desc) {
this.desc = desc;
}

public String getArguments() {
return arguments;
}

public String getReturnType() {
return returnType;
}

@Override
public String toString() {
return "DescInfo{" + "desc='" + desc + '\'' + ", arguments='" + arguments + '\'' + ", returnType='" + returnType + '\'' + '}';
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,11 @@ public void setOriginalArguments(String originalArguments) {
public void setOriginalType(String originalType) {
this.originalType = originalType;
}

@Override
public String toString() {
return "MethodInfo{" + "originalClassName='" + originalClassName + '\'' + ", originalType='"
+ originalType + '\'' + ", originalArguments='" + originalArguments
+ '\'' + ", originalName='" + originalName + '\'' + '}';
}
}
Loading