Search in sources :

Example 1 with ILocalVar

use of jadx.api.plugins.input.data.ILocalVar in project jadx by skylot.

the class Smali method formatMthParamInfo.

private boolean formatMthParamInfo(IMethodData mth, SmaliWriter smali, ICodeReader codeReader, LineInfo line) {
    List<String> types = mth.getMethodRef().getArgTypes();
    if (types.isEmpty()) {
        return false;
    }
    int paramCount = 0;
    int paramStart = 0;
    int regNum = line.smaliMthNode.getParamRegStart();
    if (!hasStaticFlag(mth.getAccessFlags())) {
        line.addRegName(regNum, "p0");
        line.smaliMthNode.setParamReg(regNum, "p0");
        regNum += 1;
        paramStart = 1;
    }
    IDebugInfo dbgInfo = codeReader.getDebugInfo();
    if (dbgInfo != null) {
        for (ILocalVar var : dbgInfo.getLocalVars()) {
            if (var.getStartOffset() == -1) {
                int i = writeParamInfo(smali, line, regNum, paramStart, var.getName(), var.getType());
                regNum += i;
                paramStart += i;
                paramCount++;
            }
        }
    }
    for (; paramCount < types.size(); paramCount++) {
        int i = writeParamInfo(smali, line, regNum, paramStart, "", types.get(paramCount));
        regNum += i;
        paramStart += i;
    }
    return true;
}
Also used : IDebugInfo(jadx.api.plugins.input.data.IDebugInfo) ILocalVar(jadx.api.plugins.input.data.ILocalVar)

Example 2 with ILocalVar

use of jadx.api.plugins.input.data.ILocalVar in project jadx by skylot.

the class DebugInfoApplyVisitor method searchDebugInfoByOffset.

private static void searchDebugInfoByOffset(MethodNode mth, SSAVar ssaVar) {
    LocalVarsDebugInfoAttr debugInfoAttr = mth.get(AType.LOCAL_VARS_DEBUG_INFO);
    if (debugInfoAttr == null) {
        return;
    }
    OptionalInt max = ssaVar.getUseList().stream().mapToInt(DebugInfoApplyVisitor::getInsnOffsetByArg).max();
    if (!max.isPresent()) {
        return;
    }
    int startOffset = getInsnOffsetByArg(ssaVar.getAssign());
    int endOffset = max.getAsInt();
    int regNum = ssaVar.getRegNum();
    for (ILocalVar localVar : debugInfoAttr.getLocalVars()) {
        if (localVar.getRegNum() == regNum) {
            int startAddr = localVar.getStartOffset();
            int endAddr = localVar.getEndOffset();
            if (isInside(startOffset, startAddr, endAddr) || isInside(endOffset, startAddr, endAddr)) {
                if (Consts.DEBUG_TYPE_INFERENCE) {
                    LOG.debug("Apply debug info by offset for: {} to {}", ssaVar, localVar);
                }
                ArgType type = DebugInfoAttachVisitor.getVarType(mth, localVar);
                applyDebugInfo(mth, ssaVar, type, localVar.getName());
                break;
            }
        }
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) LocalVarsDebugInfoAttr(jadx.core.dex.attributes.nodes.LocalVarsDebugInfoAttr) OptionalInt(java.util.OptionalInt) ILocalVar(jadx.api.plugins.input.data.ILocalVar)

Example 3 with ILocalVar

use of jadx.api.plugins.input.data.ILocalVar in project jadx by skylot.

the class DebugInfoAttachVisitor method attachDebugInfo.

private void attachDebugInfo(MethodNode mth, List<ILocalVar> localVars, InsnNode[] insnArr) {
    if (localVars.isEmpty()) {
        return;
    }
    for (ILocalVar var : localVars) {
        int regNum = var.getRegNum();
        int start = var.getStartOffset();
        int end = var.getEndOffset();
        ArgType type = getVarType(mth, var);
        RegDebugInfoAttr debugInfoAttr = new RegDebugInfoAttr(type, var.getName());
        if (start <= 0) {
            // attach to method arguments
            RegisterArg thisArg = mth.getThisArg();
            if (thisArg != null) {
                attachDebugInfo(thisArg, debugInfoAttr, regNum);
            }
            for (RegisterArg arg : mth.getArgRegs()) {
                attachDebugInfo(arg, debugInfoAttr, regNum);
            }
            start = 0;
        }
        for (int i = start; i <= end; i++) {
            InsnNode insn = insnArr[i];
            if (insn == null) {
                continue;
            }
            int count = 0;
            for (InsnArg arg : insn.getArguments()) {
                count += attachDebugInfo(arg, debugInfoAttr, regNum);
            }
            if (count != 0) {
                // don't apply same info for result if applied to args
                continue;
            }
            attachDebugInfo(insn.getResult(), debugInfoAttr, regNum);
        }
    }
    mth.addAttr(new LocalVarsDebugInfoAttr(localVars));
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) InsnNode(jadx.core.dex.nodes.InsnNode) RegDebugInfoAttr(jadx.core.dex.attributes.nodes.RegDebugInfoAttr) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) LocalVarsDebugInfoAttr(jadx.core.dex.attributes.nodes.LocalVarsDebugInfoAttr) InsnArg(jadx.core.dex.instructions.args.InsnArg) ILocalVar(jadx.api.plugins.input.data.ILocalVar)

Example 4 with ILocalVar

use of jadx.api.plugins.input.data.ILocalVar in project jadx by skylot.

the class JavaCodeReader method getDebugInfo.

@Override
@Nullable
public IDebugInfo getDebugInfo() {
    int maxStack = readMaxStack();
    reader.skip(2);
    reader.skip(reader.readU4());
    reader.skip(reader.readU2() * 8);
    JavaAttrStorage attrs = clsData.getAttributesReader().load(reader);
    LineNumberTableAttr linesAttr = attrs.get(JavaAttrType.LINE_NUMBER_TABLE);
    LocalVarsAttr varsAttr = attrs.get(JavaAttrType.LOCAL_VAR_TABLE);
    if (linesAttr == null && varsAttr == null) {
        return null;
    }
    Map<Integer, Integer> linesMap = linesAttr != null ? linesAttr.getLineMap() : Collections.emptyMap();
    List<ILocalVar> vars;
    if (varsAttr == null) {
        vars = Collections.emptyList();
    } else {
        List<JavaLocalVar> javaVars = varsAttr.getVars();
        LocalVarTypesAttr typedVars = attrs.get(JavaAttrType.LOCAL_VAR_TYPE_TABLE);
        if (typedVars != null && !typedVars.getVars().isEmpty()) {
            // merge signature from typedVars into javaVars
            Map<JavaLocalVar, JavaLocalVar> varsMap = new HashMap<>(javaVars.size());
            javaVars.forEach(v -> varsMap.put(v, v));
            for (JavaLocalVar typedVar : typedVars.getVars()) {
                JavaLocalVar jv = varsMap.get(typedVar);
                if (jv != null) {
                    jv.setSignature(typedVar.getSignature());
                }
            }
        }
        javaVars.forEach(v -> v.shiftRegNum(maxStack));
        vars = Collections.unmodifiableList(javaVars);
    }
    return new DebugInfo(linesMap, vars);
}
Also used : LocalVarTypesAttr(jadx.plugins.input.java.data.attributes.debuginfo.LocalVarTypesAttr) HashMap(java.util.HashMap) LocalVarsAttr(jadx.plugins.input.java.data.attributes.debuginfo.LocalVarsAttr) LineNumberTableAttr(jadx.plugins.input.java.data.attributes.debuginfo.LineNumberTableAttr) ILocalVar(jadx.api.plugins.input.data.ILocalVar) JavaLocalVar(jadx.plugins.input.java.data.attributes.debuginfo.JavaLocalVar) JavaAttrStorage(jadx.plugins.input.java.data.attributes.JavaAttrStorage) DebugInfo(jadx.api.plugins.input.data.impl.DebugInfo) IDebugInfo(jadx.api.plugins.input.data.IDebugInfo) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ILocalVar (jadx.api.plugins.input.data.ILocalVar)4 IDebugInfo (jadx.api.plugins.input.data.IDebugInfo)2 LocalVarsDebugInfoAttr (jadx.core.dex.attributes.nodes.LocalVarsDebugInfoAttr)2 ArgType (jadx.core.dex.instructions.args.ArgType)2 DebugInfo (jadx.api.plugins.input.data.impl.DebugInfo)1 RegDebugInfoAttr (jadx.core.dex.attributes.nodes.RegDebugInfoAttr)1 InsnArg (jadx.core.dex.instructions.args.InsnArg)1 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)1 InsnNode (jadx.core.dex.nodes.InsnNode)1 JavaAttrStorage (jadx.plugins.input.java.data.attributes.JavaAttrStorage)1 JavaLocalVar (jadx.plugins.input.java.data.attributes.debuginfo.JavaLocalVar)1 LineNumberTableAttr (jadx.plugins.input.java.data.attributes.debuginfo.LineNumberTableAttr)1 LocalVarTypesAttr (jadx.plugins.input.java.data.attributes.debuginfo.LocalVarTypesAttr)1 LocalVarsAttr (jadx.plugins.input.java.data.attributes.debuginfo.LocalVarsAttr)1 HashMap (java.util.HashMap)1 OptionalInt (java.util.OptionalInt)1 Nullable (org.jetbrains.annotations.Nullable)1