java instraction_Java Instruction.getCodeUnits方法代码示例

这篇博客深入探讨了Dalvik虚拟机的方法实现,通过DexFile和MethodImplementation解析代码块,并构建DexBody。内容涉及指令集、参数类型、局部变量、静态方法判断、代码单元计数等关键概念,还提到了与Android Dalvik调试相关的代码检查。
摘要由CSDN通过智能技术生成

import org.jf.dexlib2.iface.instruction.Instruction; //导入方法依赖的package包/类

/**

* @param code the codeitem that is contained in this body

* @param method the method that is associated with this body

*/

public DexBody(DexFile dexFile, Method method, RefType declaringClassType) {

MethodImplementation code = method.getImplementation();

if (code == null)

throw new RuntimeException("error: no code for method "+ method.getName());

this.declaringClassType = declaringClassType;

tries = code.getTryBlocks();

methodSignature = method.getDefiningClass() +": "+ method.getReturnType() +" "+ method.getName() +"(";

for (MethodParameter mp: method.getParameters())

methodSignature += mp.getType() +",";

List extends CharSequence> paramTypes = method.getParameterTypes();

if (paramTypes != null) {

parameterTypes = new ArrayList();

for (CharSequence type : paramTypes)

parameterTypes.add(DexType.toSoot(type.toString()));

} else {

parameterTypes = Collections.emptyList();

}

isStatic = Modifier.isStatic(method.getAccessFlags());

numRegisters = code.getRegisterCount();

numParameterRegisters = MethodUtil.getParameterRegisterCount(method);

if (!isStatic)

numParameterRegisters--;

instructions = new ArrayList();

instructionAtAddress = new HashMap();

registerLocals = new Local[numRegisters];

int address = 0;

for (Instruction instruction : code.getInstructions()) {

DexlibAbstractInstruction dexInstruction = fromInstruction(instruction, address);

instructions.add(dexInstruction);

instructionAtAddress.put(address, dexInstruction);

Debug.printDbg(" put instruction '", dexInstruction ,"' at 0x", Integer.toHexString(address));

address += instruction.getCodeUnits();

}

// Check taken from Android's dalvik/libdex/DexSwapVerify.cpp

if (numParameterRegisters > numRegisters)

throw new RuntimeException("Malformed dex file: insSize (" + numParameterRegisters

+ ") > registersSize (" + numRegisters + ")");

for (DebugItem di: code.getDebugItems()) {

if (di instanceof ImmutableLineNumber) {

ImmutableLineNumber ln = (ImmutableLineNumber)di;

DexlibAbstractInstruction ins = instructionAtAddress(ln.getCodeAddress());

if (ins == null) {

Debug.printDbg("Line number tag pointing to invalid offset: " + ln.getCodeAddress());

continue;

}

ins.setLineNumber(ln.getLineNumber());

Debug.printDbg("Add line number tag " + ln.getLineNumber() + " for instruction: "

+ instructionAtAddress(ln.getCodeAddress()));

}

}

this.dexFile = dexFile;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值