Package org.jf.dexlib2.dexbacked

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


                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

            @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

        }
    }

    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

                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

                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

    @Nonnull
    public Iterable<? extends DexBackedField> getStaticFields(final boolean skipDuplicates) {
        if (staticFieldCount > 0) {
            DexReader reader = dexFile.readerAt(staticFieldsOffset);

            final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
            final int staticInitialValuesOffset =
                    dexFile.readSmallUint(classDefOffset + ClassDefItem.STATIC_VALUES_OFFSET);
            final int fieldsStartOffset = reader.getOffset();

            return new Iterable<DexBackedField>() {
                @Nonnull
                @Override
                public Iterator<DexBackedField> iterator() {
                    final AnnotationsDirectory.AnnotationIterator annotationIterator =
                            annotationsDirectory.getFieldAnnotationIterator();
                    final StaticInitialValueIterator staticInitialValueIterator =
                            StaticInitialValueIterator.newOrEmpty(dexFile, staticInitialValuesOffset);

                    return new VariableSizeLookaheadIterator<DexBackedField>(dexFile, fieldsStartOffset) {
                        private int count;
View Full Code Here

    @Nonnull
    public Iterable<? extends DexBackedField> getInstanceFields(final boolean skipDuplicates) {
        if (instanceFieldCount > 0) {
            DexReader reader = dexFile.readerAt(getInstanceFieldsOffset());

            final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
            final int fieldsStartOffset = reader.getOffset();

            return new Iterable<DexBackedField>() {
                @Nonnull
                @Override
                public Iterator<DexBackedField> iterator() {
                    final AnnotationsDirectory.AnnotationIterator annotationIterator =
                            annotationsDirectory.getFieldAnnotationIterator();

                    return new VariableSizeLookaheadIterator<DexBackedField>(dexFile, fieldsStartOffset) {
                        private int count;
                        @Nullable private FieldReference previousField;
                        private int previousIndex;
View Full Code Here

    @Nonnull
    public Iterable<? extends DexBackedMethod> getDirectMethods(final boolean skipDuplicates) {
        if (directMethodCount > 0) {
            DexReader reader = dexFile.readerAt(getDirectMethodsOffset());

            final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
            final int methodsStartOffset = reader.getOffset();

            return new Iterable<DexBackedMethod>() {
                @Nonnull
                @Override
                public Iterator<DexBackedMethod> iterator() {
                    final AnnotationsDirectory.AnnotationIterator methodAnnotationIterator =
                            annotationsDirectory.getMethodAnnotationIterator();
                    final AnnotationsDirectory.AnnotationIterator parameterAnnotationIterator =
                            annotationsDirectory.getParameterAnnotationIterator();

                    return new VariableSizeLookaheadIterator<DexBackedMethod>(dexFile, methodsStartOffset) {
                        private int count;
                        @Nullable private MethodReference previousMethod;
                        private int previousIndex;
View Full Code Here

    @Nonnull
    public Iterable<? extends DexBackedMethod> getVirtualMethods(final boolean skipDuplicates) {
        if (virtualMethodCount > 0) {
            DexReader reader = dexFile.readerAt(getVirtualMethodsOffset());

            final AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
            final int methodsStartOffset = reader.getOffset();

            return new Iterable<DexBackedMethod>() {
                final AnnotationsDirectory.AnnotationIterator methodAnnotationIterator =
                        annotationsDirectory.getMethodAnnotationIterator();
                final AnnotationsDirectory.AnnotationIterator parameterAnnotationIterator =
                        annotationsDirectory.getParameterAnnotationIterator();

                @Nonnull
                @Override
                public Iterator<DexBackedMethod> iterator() {
                    return new VariableSizeLookaheadIterator<DexBackedMethod>(dexFile, methodsStartOffset) {
View Full Code Here

TOP

Related Classes of org.jf.dexlib2.dexbacked.DexReader

Copyright © 2018 www.massapicom. 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.