Examples of DexReader


Examples of org.jf.dexlib2.dexbacked.DexReader

        };

        @Nonnull
        @Override
        public Iterator<DebugItem> iterator() {
            DexReader reader = dexFile.readerAt(debugInfoOffset);
            final int lineNumberStart = reader.readBigUleb128();
            int registerCount = methodImpl.getRegisterCount();

            //TODO: does dalvik allow references to invalid registers?
            final LocalInfo[] locals = new LocalInfo[registerCount];
            Arrays.fill(locals, EMPTY_LOCAL_INFO);

            DexBackedMethod method = methodImpl.method;

            // Create a MethodParameter iterator that uses our DexReader instance to read the parameter names.
            // After we have finished iterating over the parameters, reader will "point to" the beginning of the
            // debug instructions
            final Iterator<? extends MethodParameter> parameterIterator =
                    new ParameterIterator(method.getParameterTypes(),
                            method.getParameterAnnotations(),
                            getParameterNames(reader));

            // first, we grab all the parameters and temporarily store them at the beginning of locals,
            // disregarding any wide types
            int parameterIndex = 0;
            if (!AccessFlags.STATIC.isSet(methodImpl.method.getAccessFlags())) {
                // add the local info for the "this" parameter
                locals[parameterIndex++] = new LocalInfo() {
                    @Override public String getName() { return "this"; }
                    @Override public String getType() { return methodImpl.method.getDefiningClass(); }
                    @Override public String getSignature() { return null; }
                };
            }
            while (parameterIterator.hasNext()) {
                locals[parameterIndex++] = parameterIterator.next();
            }

            if (parameterIndex < registerCount) {
                // now, we push the parameter locals back to their appropriate register, starting from the end
                int localIndex = registerCount-1;
                while(--parameterIndex > -1) {
                    LocalInfo currentLocal = locals[parameterIndex];
                    String type = currentLocal.getType();
                    if (type != null && (type.equals("J") || type.equals("D"))) {
                        localIndex--;
                        if (localIndex == parameterIndex) {
                            // there's no more room to push, the remaining registers are already in the correct place
                            break;
                        }
                    }
                    locals[localIndex] = currentLocal;
                    locals[parameterIndex] = EMPTY_LOCAL_INFO;
                    localIndex--;
                }
            }

            return new VariableSizeLookaheadIterator<DebugItem>(dexFile, reader.getOffset()) {
                private int codeAddress = 0;
                private int lineNumber = lineNumberStart;

                @Nullable
                protected DebugItem readNextItem(@Nonnull DexReader reader) {
                    while (true) {
                        int next = reader.readUbyte();
                        switch (next) {
                            case DebugItemType.END_SEQUENCE: {
                                return null;
                            }
                            case DebugItemType.ADVANCE_PC: {
                                int addressDiff = reader.readSmallUleb128();
                                codeAddress += addressDiff;
                                continue;
                            }
                            case DebugItemType.ADVANCE_LINE: {
                                int lineDiff = reader.readSleb128();
                                lineNumber += lineDiff;
                                continue;
                            }
                            case DebugItemType.START_LOCAL: {
                                int register = reader.readSmallUleb128();
                                String name = dexFile.getOptionalString(reader.readSmallUleb128() - 1);
                                String type = dexFile.getOptionalType(reader.readSmallUleb128() - 1);
                                ImmutableStartLocal startLocal =
                                        new ImmutableStartLocal(codeAddress, register, name, type, null);
                                locals[register] = startLocal;
                                return startLocal;
                            }
                            case DebugItemType.START_LOCAL_EXTENDED: {
                                int register = reader.readSmallUleb128();
                                String name = dexFile.getOptionalString(reader.readSmallUleb128() - 1);
                                String type = dexFile.getOptionalType(reader.readSmallUleb128() - 1);
                                String signature = dexFile.getOptionalString(reader.readSmallUleb128() - 1);
                                ImmutableStartLocal startLocal =
                                        new ImmutableStartLocal(codeAddress, register, name, type, signature);
                                locals[register] = startLocal;
                                return startLocal;
                            }
                            case DebugItemType.END_LOCAL: {
                                int register = reader.readSmallUleb128();
                                LocalInfo localInfo = locals[register];
                                boolean replaceLocalInTable = true;
                                if (localInfo instanceof EndLocal) {
                                    localInfo = EMPTY_LOCAL_INFO;
                                    // don't replace the local info in locals. The new EndLocal won't have any info at all,
                                    // and we dont want to wipe out what's there, so that it is available for a subsequent
                                    // RestartLocal
                                    replaceLocalInTable = false;
                                }
                                ImmutableEndLocal endLocal =
                                        new ImmutableEndLocal(codeAddress, register, localInfo.getName(),
                                                localInfo.getType(), localInfo.getSignature());
                                if (replaceLocalInTable) {
                                    locals[register] = endLocal;
                                }
                                return endLocal;
                            }
                            case DebugItemType.RESTART_LOCAL: {
                                int register = reader.readSmallUleb128();
                                LocalInfo localInfo = locals[register];
                                ImmutableRestartLocal restartLocal =
                                        new ImmutableRestartLocal(codeAddress, register, localInfo.getName(),
                                                localInfo.getType(), localInfo.getSignature());
                                locals[register] = restartLocal;
                                return restartLocal;
                            }
                            case DebugItemType.PROLOGUE_END: {
                                return new ImmutablePrologueEnd(codeAddress);
                            }
                            case DebugItemType.EPILOGUE_BEGIN: {
                                return new ImmutableEpilogueBegin(codeAddress);
                            }
                            case DebugItemType.SET_SOURCE_FILE: {
                                String sourceFile = dexFile.getOptionalString(reader.readSmallUleb128() - 1);
                                return new ImmutableSetSourceFile(codeAddress, sourceFile);
                            }
                            default: {
                                int adjusted = next - 0x0A;
                                codeAddress += adjusted / 15;
View Full Code Here

Examples of org.jf.dexlib2.dexbacked.DexReader

            }

            @Override
            public void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
                try {
                    DexReader reader = dexFile.readerAt(out.getCursor());

                    int registers = reader.readUshort();
                    out.annotate(2, "registers_size = %d", registers);

                    int inSize = reader.readUshort();
                    out.annotate(2, "ins_size = %d", inSize);

                    int outSize = reader.readUshort();
                    out.annotate(2, "outs_size = %d", outSize);

                    int triesCount = reader.readUshort();
                    out.annotate(2, "tries_size = %d", triesCount);

                    int debugInfoOffset = reader.readSmallUint();
                    out.annotate(4, "debug_info_off = 0x%x", debugInfoOffset);

                    if (debugInfoOffset != 0) {
                        addDebugInfoIdentity(debugInfoOffset, itemIdentity);
                    }

                    int instructionSize = reader.readSmallUint();
                    out.annotate(4, "insns_size = 0x%x", instructionSize);

                    out.annotate(0, "instructions:");
                    out.indent();

                    out.setLimit(out.getCursor(), out.getCursor() + instructionSize * 2);

                    int end = reader.getOffset() + instructionSize*2;
                    try {
                        while (reader.getOffset() < end) {
                            Instruction instruction = DexBackedInstruction.readFrom(reader);

                            // if we read past the end of the instruction list
                            if (reader.getOffset() > end) {
                                out.annotateTo(end, "truncated instruction");
                                reader.setOffset(end);
                            } else {
                                switch (instruction.getOpcode().format) {
                                    case Format10x:
                                        annotateInstruction10x(out, instruction);
                                        break;
                                    case Format35c:
                                        annotateInstruction35c(out, (Instruction35c)instruction);
                                        break;
                                    case Format3rc:
                                        annotateInstruction3rc(out, (Instruction3rc)instruction);
                                        break;
                                    case ArrayPayload:
                                        annotateArrayPayload(out, (ArrayPayload)instruction);
                                        break;
                                    case PackedSwitchPayload:
                                        annotatePackedSwitchPayload(out, (PackedSwitchPayload)instruction);
                                        break;
                                    case SparseSwitchPayload:
                                        annotateSparseSwitchPayload(out, (SparseSwitchPayload)instruction);
                                        break;
                                    default:
                                        annotateDefaultInstruction(out, instruction);
                                        break;
                                }
                            }

                            assert reader.getOffset() == out.getCursor();
                        }
                    } catch (ExceptionWithContext ex) {
                        ex.printStackTrace(System.err);
                        out.annotate(0, "annotation error: %s", ex.getMessage());
                        out.moveTo(end);
                        reader.setOffset(end);
                    } finally {
                        out.clearLimit();
                        out.deindent();
                    }

                    if (triesCount > 0) {
                        if ((reader.getOffset() % 4) != 0) {
                            reader.readUshort();
                            out.annotate(2, "padding");
                        }

                        out.annotate(0, "try_items:");
                        out.indent();
                        try {
                            for (int i=0; i<triesCount; i++) {
                                out.annotate(0, "try_item[%d]:", i);
                                out.indent();
                                try {
                                    int startAddr = reader.readSmallUint();
                                    out.annotate(4, "start_addr = 0x%x", startAddr);

                                    int instructionCount = reader.readUshort();
                                    out.annotate(2, "insn_count = 0x%x", instructionCount);

                                    int handlerOffset = reader.readUshort();
                                    out.annotate(2, "handler_off = 0x%x", handlerOffset);
                                } finally {
                                    out.deindent();
                                }
                            }
                        } finally {
                            out.deindent();
                        }

                        int handlerListCount = reader.readSmallUleb128();
                        out.annotate(0, "encoded_catch_handler_list:");
                        out.annotateTo(reader.getOffset(), "size = %d", handlerListCount);
                        out.indent();
                        try {
                            for (int i=0; i<handlerListCount; i++) {
                                out.annotate(0, "encoded_catch_handler[%d]", i);
                                out.indent();
                                try {
                                    int handlerCount = reader.readSleb128();
                                    out.annotateTo(reader.getOffset(), "size = %d", handlerCount);
                                    boolean hasCatchAll = handlerCount <= 0;
                                    handlerCount = Math.abs(handlerCount);
                                    if (handlerCount != 0) {
                                        out.annotate(0, "handlers:");
                                        out.indent();
                                        try {
                                            for (int j=0; j<handlerCount; j++) {
                                                out.annotate(0, "encoded_type_addr_pair[%d]", i);
                                                out.indent();
                                                try {
                                                    int typeIndex = reader.readSmallUleb128();
                                                    out.annotateTo(reader.getOffset(), TypeIdItem.getReferenceAnnotation(dexFile, typeIndex));

                                                    int handlerAddress = reader.readSmallUleb128();
                                                    out.annotateTo(reader.getOffset(), "addr = 0x%x", handlerAddress);
                                                } finally {
                                                    out.deindent();
                                                }
                                            }
                                        } finally {
                                            out.deindent();
                                        }
                                    }
                                    if (hasCatchAll) {
                                        int catchAllAddress = reader.readSmallUleb128();
                                        out.annotateTo(reader.getOffset(), "catch_all_addr = 0x%x", catchAllAddress);
                                    }
                                } finally {
                                    out.deindent();
                                }
                            }
View Full Code Here

Examples of org.jf.dexlib2.dexbacked.DexReader

                return "debug_info_item";
            }

            @Override
            public void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
                DexReader reader = dexFile.readerAt(out.getCursor());

                int lineStart = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "line_start = %d", lineStart);

                int parametersSize = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "parameters_size = %d", parametersSize);

                if (parametersSize > 0) {
                    out.annotate(0, "parameters:");
                    out.indent();
                    for (int i=0; i<parametersSize; i++) {
                        int paramaterIndex = reader.readSmallUleb128() - 1;
                        out.annotateTo(reader.getOffset(), "%s",
                                StringIdItem.getOptionalReferenceAnnotation(dexFile, paramaterIndex, true));
                    }
                    out.deindent();
                }

                out.annotate(0, "debug opcodes:");
                out.indent();

                int codeAddress = 0;
                int lineNumber = lineStart;

                loop: while (true) {
                    int opcode = reader.readUbyte();
                    switch (opcode) {
                        case DebugItemType.END_SEQUENCE: {
                            out.annotateTo(reader.getOffset(), "DBG_END_SEQUENCE");
                            break loop;
                        }
                        case DebugItemType.ADVANCE_PC: {
                            out.annotateTo(reader.getOffset(), "DBG_ADVANCE_PC");
                            out.indent();
                            int addressDiff = reader.readSmallUleb128();
                            codeAddress += addressDiff;
                            out.annotateTo(reader.getOffset(), "addr_diff = +0x%x: 0x%x", addressDiff,
                                    codeAddress);
                            out.deindent();
                            break;
                        }
                        case DebugItemType.ADVANCE_LINE: {
                            out.annotateTo(reader.getOffset(), "DBG_ADVANCE_LINE");
                            out.indent();
                            int lineDiff = reader.readSleb128();
                            lineNumber += lineDiff;
                            out.annotateTo(reader.getOffset(), "line_diff = +%d: %d", Math.abs(lineDiff), lineNumber);
                            out.deindent();
                            break;
                        }
                        case DebugItemType.START_LOCAL: {
                            out.annotateTo(reader.getOffset(), "DBG_START_LOCAL");
                            out.indent();
                            int registerNum = reader.readSmallUleb128();
                            out.annotateTo(reader.getOffset(), "register_num = v%d", registerNum);
                            int nameIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "name_idx = %s",
                                    StringIdItem.getOptionalReferenceAnnotation(dexFile, nameIndex, true));
                            int typeIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "type_idx = %s",
                                    TypeIdItem.getOptionalReferenceAnnotation(dexFile, typeIndex));
                            out.deindent();
                            break;
                        }
                        case DebugItemType.START_LOCAL_EXTENDED: {
                            out.annotateTo(reader.getOffset(), "DBG_START_LOCAL_EXTENDED");
                            out.indent();
                            int registerNum = reader.readSmallUleb128();
                            out.annotateTo(reader.getOffset(), "register_num = v%d", registerNum);
                            int nameIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "name_idx = %s",
                                    StringIdItem.getOptionalReferenceAnnotation(dexFile, nameIndex, true));
                            int typeIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "type_idx = %s",
                                    TypeIdItem.getOptionalReferenceAnnotation(dexFile, typeIndex));
                            int sigIndex = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "sig_idx = %s",
                                    StringIdItem.getOptionalReferenceAnnotation(dexFile, sigIndex, true));
                            out.deindent();
                            break;
                        }
                        case DebugItemType.END_LOCAL: {
                            out.annotateTo(reader.getOffset(), "DBG_END_LOCAL");
                            out.indent();
                            int registerNum = reader.readSmallUleb128();
                            out.annotateTo(reader.getOffset(), "register_num = v%d", registerNum);
                            out.deindent();
                            break;
                        }
                        case DebugItemType.RESTART_LOCAL: {
                            out.annotateTo(reader.getOffset(), "DBG_RESTART_LOCAL");
                            out.indent();
                            int registerNum = reader.readSmallUleb128();
                            out.annotateTo(reader.getOffset(), "register_num = v%d", registerNum);
                            out.deindent();
                            break;
                        }
                        case DebugItemType.PROLOGUE_END: {
                            out.annotateTo(reader.getOffset(), "DBG_SET_PROLOGUE_END");
                            break;
                        }
                        case DebugItemType.EPILOGUE_BEGIN: {
                            out.annotateTo(reader.getOffset(), "DBG_SET_EPILOGUE_BEGIN");
                            break;
                        }
                        case DebugItemType.SET_SOURCE_FILE: {
                            out.annotateTo(reader.getOffset(), "DBG_SET_FILE");
                            out.indent();
                            int nameIdx = reader.readSmallUleb128() - 1;
                            out.annotateTo(reader.getOffset(), "name_idx = %s",
                                    StringIdItem.getOptionalReferenceAnnotation(dexFile, nameIdx));
                            out.deindent();
                            break;
                        }
                        default:
                            int adjusted = opcode - 0x0A;
                            int addressDiff = adjusted / 15;
                            int lineDiff = (adjusted % 15) - 4;
                            codeAddress += addressDiff;
                            lineNumber += lineDiff;
                            out.annotateTo(reader.getOffset(), "address_diff = +0x%x:0x%x, line_diff = +%d:%d, ",
                                    addressDiff, codeAddress, lineDiff, lineNumber);
                            break;
                    }
                }
                out.deindent();
View Full Code Here

Examples of org.jf.dexlib2.dexbacked.DexReader

                return "encoded_array_item";
            }

            @Override
            protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
                DexReader reader = dexFile.readerAt(out.getCursor());
                EncodedValue.annotateEncodedArray(out, reader);
            }
        };
    }
View Full Code Here

Examples of org.jf.dexlib2.dexbacked.DexReader

            @Override
            protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
                int visibility = dexFile.readUbyte(out.getCursor());
                out.annotate(1, "visibility = %d: %s", visibility, getAnnotationVisibility(visibility));

                DexReader reader = dexFile.readerAt(out.getCursor());

                EncodedValue.annotateEncodedAnnotation(out, reader);
            }
        };
    }
View Full Code Here

Examples of org.jf.dexlib2.dexbacked.DexReader

        }
    }

    public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int annotationItemOffset) {
        try {
            DexReader reader = dexFile.readerAt(annotationItemOffset);
            reader.readUbyte();
            int typeIndex = reader.readSmallUleb128();
            String annotationType = dexFile.getType(typeIndex);
            return String.format("annotation_item[0x%x]: %s", annotationItemOffset, annotationType);
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
View Full Code Here

Examples of org.jf.dexlib2.dexbacked.DexReader

                return "string_data_item";
            }

            @Override
            protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
                DexReader reader = dexFile.readerAt(out.getCursor());
                int utf16Length = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "utf16_size = %d", utf16Length);

                String value = reader.readString(utf16Length);
                out.annotateTo(reader.getOffset() + 1, "data = \"%s\"", StringUtils.escapeString(value));
            }
        };
    }
View Full Code Here

Examples of org.jf.dexlib2.dexbacked.DexReader

                return "class_data_item";
            }

            @Override
            protected void annotateItem(@Nonnull AnnotatedBytes out, int itemIndex, @Nullable String itemIdentity) {
                DexReader reader = dexFile.readerAt(out.getCursor());

                int staticFieldsSize = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "static_fields_size = %d", staticFieldsSize);

                int instanceFieldsSize = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "instance_fields_size = %d", instanceFieldsSize);

                int directMethodsSize = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "direct_methods_size = %d", directMethodsSize);

                int virtualMethodsSize = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "virtual_methods_size = %d", virtualMethodsSize);

                int previousIndex = 0;
                if (staticFieldsSize > 0) {
                    out.annotate(0, "static_fields:");
                    out.indent();
                    for (int i=0; i<staticFieldsSize; i++) {
                        out.annotate(0, "static_field[%d]", i);
                        out.indent();
                        previousIndex = annotateEncodedField(out, dexFile, reader, previousIndex);
                        out.deindent();
                    }
                    out.deindent();
                }

                if (instanceFieldsSize > 0) {
                    out.annotate(0, "instance_fields:");
                    out.indent();
                    previousIndex = 0;
                    for (int i=0; i<instanceFieldsSize; i++) {
                        out.annotate(0, "instance_field[%d]", i);
                        out.indent();
                        previousIndex = annotateEncodedField(out, dexFile, reader, previousIndex);
                        out.deindent();
                    }
                    out.deindent();
                }

                if (directMethodsSize > 0) {
                    out.annotate(0, "direct_methods:");
                    out.indent();
                    previousIndex = 0;
                    for (int i=0; i<directMethodsSize; i++) {
                        out.annotate(0, "direct_method[%d]", i);
                        out.indent();
                        previousIndex = annotateEncodedMethod(out, dexFile, reader, previousIndex);
                        out.deindent();
                    }
                    out.deindent();
                }

                if (virtualMethodsSize > 0) {
                    out.annotate(0, "virtual_methods:");
                    out.indent();
                    previousIndex = 0;
                    for (int i=0; i<virtualMethodsSize; i++) {
                        out.annotate(0, "virtual_method[%d]", i);
                        out.indent();
                        previousIndex = annotateEncodedMethod(out, dexFile, reader, previousIndex);
                        out.deindent();
                    }
                    out.deindent();
                }
            }

            private int annotateEncodedField(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile,
                                             @Nonnull DexReader reader, int previousIndex) {
                // large values may be used for the index delta, which cause the cumulative index to overflow upon
                // addition, effectively allowing out of order entries.
                int indexDelta = reader.readLargeUleb128();
                int fieldIndex = previousIndex + indexDelta;
                out.annotateTo(reader.getOffset(), "field_idx_diff = %d: %s", indexDelta,
                        FieldIdItem.getReferenceAnnotation(dexFile, fieldIndex));

                int accessFlags = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "access_flags = 0x%x: %s", accessFlags,
                        Joiner.on('|').join(AccessFlags.getAccessFlagsForField(accessFlags)));

                return fieldIndex;
            }

            private int annotateEncodedMethod(@Nonnull AnnotatedBytes out, @Nonnull RawDexFile dexFile,
                                              @Nonnull DexReader reader, int previousIndex) {
                // large values may be used for the index delta, which cause the cumulative index to overflow upon
                // addition, effectively allowing out of order entries.
                int indexDelta = reader.readLargeUleb128();
                int methodIndex = previousIndex + indexDelta;
                out.annotateTo(reader.getOffset(), "method_idx_diff = %d: %s", indexDelta,
                        MethodIdItem.getReferenceAnnotation(dexFile, methodIndex));

                int accessFlags = reader.readSmallUleb128();
                out.annotateTo(reader.getOffset(), "access_flags = 0x%x: %s", accessFlags,
                        Joiner.on('|').join(AccessFlags.getAccessFlagsForMethod(accessFlags)));

                int codeOffset = reader.readSmallUleb128();
                if (codeOffset == 0) {
                    out.annotateTo(reader.getOffset(), "code_off = code_item[NO_OFFSET]");
                } else {
                    out.annotateTo(reader.getOffset(), "code_off = code_item[0x%x]", codeOffset);
                    addCodeItemIdentity(codeOffset, MethodIdItem.asString(dexFile, methodIndex));
                }

                return methodIndex;
            }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.