Readded smali debugging feature.

This commit is contained in:
Ryszard Wiśniewski 2013-05-02 01:42:44 +02:00
parent 70deba0c5d
commit 59da5db05f
3 changed files with 59 additions and 12 deletions

View File

@ -193,7 +193,7 @@ public class DebugInjector {
} }
private String next() { private String next() {
return mIt.next().trim(); return mIt.next().split("//", 2)[1].trim();
} }
private String nextAndAppend() { private String nextAndAppend() {

View File

@ -76,17 +76,16 @@ public class SmaliBuilder {
if (!mFlags.containsKey("debug")) { if (!mFlags.containsKey("debug")) {
final String[] linesArray = lines.toArray(new String[0]); final String[] linesArray = lines.toArray(new String[0]);
for (int i = 2; i < linesArray.length - 2; i++) { for (int i = 1; i < linesArray.length - 1; i++) {
out.append(linesArray[i]).append('\n'); out.append(linesArray[i].split("//", 2)[1]).append('\n');
} }
} else { } else {
lines.remove(lines.size() - 1); lines.remove(lines.size() - 1);
lines.remove(lines.size() - 1); ListIterator<String> it = lines.listIterator(1);
ListIterator<String> it = lines.listIterator(2);
out.append(".source \"").append(inFile.getName()).append("\"\n"); out.append(".source \"").append(inFile.getName()).append("\"\n");
while (it.hasNext()) { while (it.hasNext()) {
String line = it.next().trim(); String line = it.next().split("//", 2)[1].trim();
if (line.isEmpty() || line.charAt(0) == '#' if (line.isEmpty() || line.charAt(0) == '#'
|| line.startsWith(".source")) { || line.startsWith(".source")) {
continue; continue;

View File

@ -17,10 +17,20 @@
package brut.androlib.src; package brut.androlib.src;
import brut.androlib.AndrolibException; import brut.androlib.AndrolibException;
import org.jf.baksmali.baksmali;
import org.jf.dexlib.Code.Analysis.ClassPath;
import org.jf.dexlib.DexFile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.jf.baksmali.baksmali; import java.nio.charset.Charset;
import org.jf.dexlib.DexFile; import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
/** /**
* @author Ryszard Wiśniewski <brut.alll@gmail.com> * @author Ryszard Wiśniewski <brut.alll@gmail.com>
@ -35,24 +45,62 @@ public class SmaliDecoder {
private SmaliDecoder(File apkFile, File outDir, boolean debug, private SmaliDecoder(File apkFile, File outDir, boolean debug,
boolean bakdeb) { boolean bakdeb) {
mApkFile = apkFile; mApkFile = apkFile;
mOutDir = outDir; mOutDir = outDir.toPath();
mDebug = debug; mDebug = debug;
mBakDeb = bakdeb; mBakDeb = bakdeb;
} }
private void decode() throws AndrolibException { private void decode() throws AndrolibException {
try { try {
ClassPath.dontLoadClassPath = mDebug;
baksmali.disassembleDexFile(mApkFile.getAbsolutePath(), baksmali.disassembleDexFile(mApkFile.getAbsolutePath(),
new DexFile(mApkFile), false, mOutDir.getAbsolutePath(), new DexFile(mApkFile), false, mOutDir.toAbsolutePath().toString(),
null, null, null, false, true, true, mBakDeb, false, false, null, null, null, false, true, true, mBakDeb, false, false,
0, false, false, null, false); mDebug ? org.jf.baksmali.main.DIFFPRE : 0, false, false, null, false);
if (mDebug) {
Files.walkFileTree(mOutDir, new SmaliFileVisitor());
}
} catch (IOException ex) { } catch (IOException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} }
} }
private final File mApkFile; private final File mApkFile;
private final File mOutDir; private final Path mOutDir;
private final boolean mDebug; private final boolean mDebug;
private final boolean mBakDeb; private final boolean mBakDeb;
private class SmaliFileVisitor extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String fileName = file.getFileName().toString();
if (! fileName.endsWith(".smali")) {
return FileVisitResult.CONTINUE;
}
fileName = fileName.substring(0, fileName.length() - 6);
try (
BufferedReader in = Files.newBufferedReader(file, Charset.defaultCharset());
BufferedWriter out = Files.newBufferedWriter(
file.resolveSibling(fileName + ".java"), Charset.defaultCharset())
) {
TypeName type = TypeName.fromPath(mOutDir.relativize(file.resolveSibling(fileName)));
out.write("package " + type.package_ + "; class " + type.getName(true, true) + " {");
out.newLine();
String line;
while ((line = in.readLine()) != null) {
out.write("// ");
out.write(line);
out.newLine();
}
out.write("}");
out.newLine();
}
Files.delete(file);
return FileVisitResult.CONTINUE;
}
}
} }