Add additional context information to exceptions when building a MethodDefinition object

git-svn-id: https://smali.googlecode.com/svn/trunk@672 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com 2010-03-06 06:12:09 +00:00
parent cbc21d5ece
commit 10a9518111

View File

@ -42,6 +42,7 @@ import org.jf.dexlib.Debug.DebugInstructionIterator;
import org.jf.dexlib.Util.AccessFlags; import org.jf.dexlib.Util.AccessFlags;
import org.antlr.stringtemplate.StringTemplateGroup; import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.StringTemplate; import org.antlr.stringtemplate.StringTemplate;
import org.jf.dexlib.Util.ExceptionWithContext;
import org.jf.dexlib.Util.Hex; import org.jf.dexlib.Util.Hex;
import org.jf.dexlib.Util.SparseIntArray; import org.jf.dexlib.Util.SparseIntArray;
@ -61,42 +62,49 @@ public class MethodDefinition {
private final int registerCount; private final int registerCount;
public MethodDefinition(StringTemplateGroup stg, ClassDataItem.EncodedMethod encodedMethod) { public MethodDefinition(StringTemplateGroup stg, ClassDataItem.EncodedMethod encodedMethod) {
this.stg = stg;
this.encodedMethod = encodedMethod;
//TODO: what about try/catch blocks inside the dead code? those will need to be commented out too. ugh.
if (encodedMethod.codeItem != null) { try {
methodAnalyzer = new MethodAnalyzer(encodedMethod, baksmali.deodex); this.stg = stg;
List<AnalyzedInstruction> instructions = methodAnalyzer.getInstructions(); this.encodedMethod = encodedMethod;
packedSwitchMap = new SparseIntArray(1); //TODO: what about try/catch blocks inside the dead code? those will need to be commented out too. ugh.
sparseSwitchMap = new SparseIntArray(1);
instructionMap = new SparseIntArray(instructions.size());
registerCount = encodedMethod.codeItem.getRegisterCount(); if (encodedMethod.codeItem != null) {
methodAnalyzer = new MethodAnalyzer(encodedMethod, baksmali.deodex);
List<AnalyzedInstruction> instructions = methodAnalyzer.getInstructions();
int currentCodeAddress = 0; packedSwitchMap = new SparseIntArray(1);
for (int i=0; i<instructions.size(); i++) { sparseSwitchMap = new SparseIntArray(1);
AnalyzedInstruction instruction = instructions.get(i); instructionMap = new SparseIntArray(instructions.size());
if (instruction.getInstruction().opcode == Opcode.PACKED_SWITCH) {
packedSwitchMap.append( registerCount = encodedMethod.codeItem.getRegisterCount();
currentCodeAddress + ((OffsetInstruction)instruction.getInstruction()).getTargetAddressOffset(),
currentCodeAddress); int currentCodeAddress = 0;
} else if (instruction.getInstruction().opcode == Opcode.SPARSE_SWITCH) { for (int i=0; i<instructions.size(); i++) {
sparseSwitchMap.append( AnalyzedInstruction instruction = instructions.get(i);
currentCodeAddress + ((OffsetInstruction)instruction.getInstruction()).getTargetAddressOffset(), if (instruction.getInstruction().opcode == Opcode.PACKED_SWITCH) {
currentCodeAddress); packedSwitchMap.append(
currentCodeAddress + ((OffsetInstruction)instruction.getInstruction()).getTargetAddressOffset(),
currentCodeAddress);
} else if (instruction.getInstruction().opcode == Opcode.SPARSE_SWITCH) {
sparseSwitchMap.append(
currentCodeAddress + ((OffsetInstruction)instruction.getInstruction()).getTargetAddressOffset(),
currentCodeAddress);
}
instructionMap.append(currentCodeAddress, i);
currentCodeAddress += instruction.getInstruction().getSize(currentCodeAddress);
} }
instructionMap.append(currentCodeAddress, i); } else {
currentCodeAddress += instruction.getInstruction().getSize(currentCodeAddress); packedSwitchMap = null;
sparseSwitchMap = null;
instructionMap = null;
methodAnalyzer = null;
registerCount = 0;
} }
} else { }catch (Exception ex) {
packedSwitchMap = null; throw ExceptionWithContext.withContext(ex, String.format("Error while processing method %s",
sparseSwitchMap = null; encodedMethod.method.getMethodString()));
instructionMap = null;
methodAnalyzer = null;
registerCount = 0;
} }
} }