2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * Implementation file of the dexdump utility.
18 * This is a re-implementation of the original dexdump utility that was
19 * based on Dalvik functions in libdex into a new dexdump that is now
20 * based on Art functions in libart instead. The output is very similar to
21 * to the original for correct DEX files. Error messages may differ, however.
22 * Also, ODEX files are no longer supported.
24 * The dexdump tool is intended to mimic objdump. When possible, use
25 * similar command-line arguments.
27 * Differences between XML output and the "current.xml" file:
28 * - classes in same package are not all grouped together; nothing is sorted
29 * - no "deprecated" on fields and methods
30 * - no parameter names
31 * - no generic signatures on parameters, e.g. type="java.lang.Class<?>"
32 * - class shows declared fields and methods; does not show inherited fields
45 #include "android-base/stringprintf.h"
47 #include "dexdump_cfg.h"
48 #include "dex_file-inl.h"
49 #include "dex_file_types.h"
50 #include "dex_instruction-inl.h"
55 * Options parsed in main driver.
57 struct Options gOptions;
60 * Output file. Defaults to stdout.
62 FILE* gOutFile = stdout;
65 * Data types that match the definitions in the VM specification.
77 * Basic information about a field or a method.
79 struct FieldMethodInfo {
80 const char* classDescriptor;
82 const char* signature;
86 * Flags for use with createAccessFlagStr().
89 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
91 const int kNumFlags = 18;
94 * Gets 2 little-endian bytes.
96 static inline u2 get2LE(unsigned char const* pSrc) {
97 return pSrc[0] | (pSrc[1] << 8);
101 * Converts a single-character primitive type into human-readable form.
103 static const char* primitiveTypeLabel(char typeChar) {
105 case 'B': return "byte";
106 case 'C': return "char";
107 case 'D': return "double";
108 case 'F': return "float";
109 case 'I': return "int";
110 case 'J': return "long";
111 case 'S': return "short";
112 case 'V': return "void";
113 case 'Z': return "boolean";
114 default: return "UNKNOWN";
119 * Converts a type descriptor to human-readable "dotted" form. For
120 * example, "Ljava/lang/String;" becomes "java.lang.String", and
121 * "[I" becomes "int[]". Also converts '$' to '.', which means this
122 * form can't be converted back to a descriptor.
124 static std::unique_ptr<char[]> descriptorToDot(const char* str) {
125 int targetLen = strlen(str);
128 // Strip leading [s; will be added to end.
129 while (targetLen > 1 && str[offset] == '[') {
134 const int arrayDepth = offset;
136 if (targetLen == 1) {
138 str = primitiveTypeLabel(str[offset]);
140 targetLen = strlen(str);
142 // Account for leading 'L' and trailing ';'.
143 if (targetLen >= 2 && str[offset] == 'L' &&
144 str[offset + targetLen - 1] == ';') {
150 // Copy class name over.
151 std::unique_ptr<char[]> newStr(new char[targetLen + arrayDepth * 2 + 1]);
153 for (; i < targetLen; i++) {
154 const char ch = str[offset + i];
155 newStr[i] = (ch == '/' || ch == '$') ? '.' : ch;
158 // Add the appropriate number of brackets for arrays.
159 for (int j = 0; j < arrayDepth; j++) {
169 * Converts the class name portion of a type descriptor to human-readable
170 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
172 static std::unique_ptr<char[]> descriptorClassToDot(const char* str) {
173 // Reduce to just the class name prefix.
174 const char* lastSlash = strrchr(str, '/');
175 if (lastSlash == nullptr) {
176 lastSlash = str + 1; // start past 'L'
178 lastSlash++; // start past '/'
181 // Copy class name over, trimming trailing ';'.
182 const int targetLen = strlen(lastSlash);
183 std::unique_ptr<char[]> newStr(new char[targetLen]);
184 for (int i = 0; i < targetLen - 1; i++) {
185 const char ch = lastSlash[i];
186 newStr[i] = ch == '$' ? '.' : ch;
188 newStr[targetLen - 1] = '\0';
193 * Returns string representing the boolean value.
195 static const char* strBool(bool val) {
196 return val ? "true" : "false";
200 * Returns a quoted string representing the boolean value.
202 static const char* quotedBool(bool val) {
203 return val ? "\"true\"" : "\"false\"";
207 * Returns a quoted string representing the access flags.
209 static const char* quotedVisibility(u4 accessFlags) {
210 if (accessFlags & kAccPublic) {
212 } else if (accessFlags & kAccProtected) {
213 return "\"protected\"";
214 } else if (accessFlags & kAccPrivate) {
215 return "\"private\"";
217 return "\"package\"";
222 * Counts the number of '1' bits in a word.
224 static int countOnes(u4 val) {
225 val = val - ((val >> 1) & 0x55555555);
226 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
227 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
231 * Creates a new string with human-readable access flags.
233 * In the base language the access_flags fields are type u2; in Dalvik
236 static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
237 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
239 "PUBLIC", /* 0x00001 */
240 "PRIVATE", /* 0x00002 */
241 "PROTECTED", /* 0x00004 */
242 "STATIC", /* 0x00008 */
243 "FINAL", /* 0x00010 */
248 "INTERFACE", /* 0x00200 */
249 "ABSTRACT", /* 0x00400 */
251 "SYNTHETIC", /* 0x01000 */
252 "ANNOTATION", /* 0x02000 */
253 "ENUM", /* 0x04000 */
255 "VERIFIED", /* 0x10000 */
256 "OPTIMIZED", /* 0x20000 */
258 "PUBLIC", /* 0x00001 */
259 "PRIVATE", /* 0x00002 */
260 "PROTECTED", /* 0x00004 */
261 "STATIC", /* 0x00008 */
262 "FINAL", /* 0x00010 */
263 "SYNCHRONIZED", /* 0x00020 */
264 "BRIDGE", /* 0x00040 */
265 "VARARGS", /* 0x00080 */
266 "NATIVE", /* 0x00100 */
268 "ABSTRACT", /* 0x00400 */
269 "STRICT", /* 0x00800 */
270 "SYNTHETIC", /* 0x01000 */
273 "MIRANDA", /* 0x08000 */
274 "CONSTRUCTOR", /* 0x10000 */
275 "DECLARED_SYNCHRONIZED", /* 0x20000 */
277 "PUBLIC", /* 0x00001 */
278 "PRIVATE", /* 0x00002 */
279 "PROTECTED", /* 0x00004 */
280 "STATIC", /* 0x00008 */
281 "FINAL", /* 0x00010 */
283 "VOLATILE", /* 0x00040 */
284 "TRANSIENT", /* 0x00080 */
289 "SYNTHETIC", /* 0x01000 */
291 "ENUM", /* 0x04000 */
298 // Allocate enough storage to hold the expected number of strings,
299 // plus a space between each. We over-allocate, using the longest
300 // string above as the base metric.
301 const int kLongest = 21; // The strlen of longest string above.
302 const int count = countOnes(flags);
305 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
307 for (int i = 0; i < kNumFlags; i++) {
309 const char* accessStr = kAccessStrings[forWhat][i];
310 const int len = strlen(accessStr);
314 memcpy(cp, accessStr, len);
325 * Copies character data from "data" to "out", converting non-ASCII values
326 * to fprintf format chars or an ASCII filler ('.' or '?').
328 * The output buffer must be able to hold (2*len)+1 bytes. The result is
331 static void asciify(char* out, const unsigned char* data, size_t len) {
334 // Could do more here, but we don't need them yet.
348 } else if (*data >= 0x80) {
359 * Dumps a string value with some escape characters.
361 static void dumpEscapedString(const char* p) {
362 fputs("\"", gOutFile);
366 fputs("\\\\", gOutFile);
369 fputs("\\\"", gOutFile);
372 fputs("\\t", gOutFile);
375 fputs("\\n", gOutFile);
378 fputs("\\r", gOutFile);
384 fputs("\"", gOutFile);
388 * Dumps a string as an XML attribute value.
390 static void dumpXmlAttribute(const char* p) {
394 fputs("&", gOutFile);
397 fputs("<", gOutFile);
400 fputs(">", gOutFile);
403 fputs(""", gOutFile);
406 fputs("	", gOutFile);
409 fputs("
", gOutFile);
412 fputs("
", gOutFile);
421 * Reads variable width value, possibly sign extended at the last defined byte.
423 static u8 readVarWidth(const u1** data, u1 arg, bool sign_extend) {
425 for (u4 i = 0; i <= arg; i++) {
426 value |= static_cast<u8>(*(*data)++) << (i * 8);
429 int shift = (7 - arg) * 8;
430 return (static_cast<s8>(value) << shift) >> shift;
436 * Dumps encoded value.
438 static void dumpEncodedValue(const DexFile* pDexFile, const u1** data); // forward
439 static void dumpEncodedValue(const DexFile* pDexFile, const u1** data, u1 type, u1 arg) {
441 case DexFile::kDexAnnotationByte:
442 fprintf(gOutFile, "%" PRId8, static_cast<s1>(readVarWidth(data, arg, false)));
444 case DexFile::kDexAnnotationShort:
445 fprintf(gOutFile, "%" PRId16, static_cast<s2>(readVarWidth(data, arg, true)));
447 case DexFile::kDexAnnotationChar:
448 fprintf(gOutFile, "%" PRIu16, static_cast<u2>(readVarWidth(data, arg, false)));
450 case DexFile::kDexAnnotationInt:
451 fprintf(gOutFile, "%" PRId32, static_cast<s4>(readVarWidth(data, arg, true)));
453 case DexFile::kDexAnnotationLong:
454 fprintf(gOutFile, "%" PRId64, static_cast<s8>(readVarWidth(data, arg, true)));
456 case DexFile::kDexAnnotationFloat: {
462 conv.data = static_cast<u4>(readVarWidth(data, arg, false)) << (3 - arg) * 8;
463 fprintf(gOutFile, "%g", conv.f);
466 case DexFile::kDexAnnotationDouble: {
472 conv.data = readVarWidth(data, arg, false) << (7 - arg) * 8;
473 fprintf(gOutFile, "%g", conv.d);
476 case DexFile::kDexAnnotationString: {
477 const u4 idx = static_cast<u4>(readVarWidth(data, arg, false));
478 if (gOptions.outputFormat == OUTPUT_PLAIN) {
479 dumpEscapedString(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
481 dumpXmlAttribute(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
485 case DexFile::kDexAnnotationType: {
486 const u4 str_idx = static_cast<u4>(readVarWidth(data, arg, false));
487 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(str_idx)), gOutFile);
490 case DexFile::kDexAnnotationField:
491 case DexFile::kDexAnnotationEnum: {
492 const u4 field_idx = static_cast<u4>(readVarWidth(data, arg, false));
493 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
494 fputs(pDexFile->StringDataByIdx(pFieldId.name_idx_), gOutFile);
497 case DexFile::kDexAnnotationMethod: {
498 const u4 method_idx = static_cast<u4>(readVarWidth(data, arg, false));
499 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
500 fputs(pDexFile->StringDataByIdx(pMethodId.name_idx_), gOutFile);
503 case DexFile::kDexAnnotationArray: {
504 fputc('{', gOutFile);
505 // Decode and display all elements.
506 const u4 size = DecodeUnsignedLeb128(data);
507 for (u4 i = 0; i < size; i++) {
508 fputc(' ', gOutFile);
509 dumpEncodedValue(pDexFile, data);
511 fputs(" }", gOutFile);
514 case DexFile::kDexAnnotationAnnotation: {
515 const u4 type_idx = DecodeUnsignedLeb128(data);
516 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(type_idx)), gOutFile);
517 // Decode and display all name=value pairs.
518 const u4 size = DecodeUnsignedLeb128(data);
519 for (u4 i = 0; i < size; i++) {
520 const u4 name_idx = DecodeUnsignedLeb128(data);
521 fputc(' ', gOutFile);
522 fputs(pDexFile->StringDataByIdx(dex::StringIndex(name_idx)), gOutFile);
523 fputc('=', gOutFile);
524 dumpEncodedValue(pDexFile, data);
528 case DexFile::kDexAnnotationNull:
529 fputs("null", gOutFile);
531 case DexFile::kDexAnnotationBoolean:
532 fputs(strBool(arg), gOutFile);
535 fputs("????", gOutFile);
541 * Dumps encoded value with prefix.
543 static void dumpEncodedValue(const DexFile* pDexFile, const u1** data) {
544 const u1 enc = *(*data)++;
545 dumpEncodedValue(pDexFile, data, enc & 0x1f, enc >> 5);
549 * Dumps the file header.
551 static void dumpFileHeader(const DexFile* pDexFile) {
552 const DexFile::Header& pHeader = pDexFile->GetHeader();
553 char sanitized[sizeof(pHeader.magic_) * 2 + 1];
554 fprintf(gOutFile, "DEX file header:\n");
555 asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
556 fprintf(gOutFile, "magic : '%s'\n", sanitized);
557 fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
558 fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
559 pHeader.signature_[0], pHeader.signature_[1],
560 pHeader.signature_[DexFile::kSha1DigestSize - 2],
561 pHeader.signature_[DexFile::kSha1DigestSize - 1]);
562 fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
563 fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
564 fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
565 fprintf(gOutFile, "link_off : %d (0x%06x)\n",
566 pHeader.link_off_, pHeader.link_off_);
567 fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
568 fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
569 pHeader.string_ids_off_, pHeader.string_ids_off_);
570 fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
571 fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
572 pHeader.type_ids_off_, pHeader.type_ids_off_);
573 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
574 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
575 pHeader.proto_ids_off_, pHeader.proto_ids_off_);
576 fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
577 fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
578 pHeader.field_ids_off_, pHeader.field_ids_off_);
579 fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
580 fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
581 pHeader.method_ids_off_, pHeader.method_ids_off_);
582 fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
583 fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
584 pHeader.class_defs_off_, pHeader.class_defs_off_);
585 fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
586 fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
587 pHeader.data_off_, pHeader.data_off_);
591 * Dumps a class_def_item.
593 static void dumpClassDef(const DexFile* pDexFile, int idx) {
594 // General class information.
595 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
596 fprintf(gOutFile, "Class #%d header:\n", idx);
597 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_.index_);
598 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
599 pClassDef.access_flags_, pClassDef.access_flags_);
600 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_.index_);
601 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
602 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
603 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_.index_);
604 fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
605 pClassDef.annotations_off_, pClassDef.annotations_off_);
606 fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
607 pClassDef.class_data_off_, pClassDef.class_data_off_);
609 // Fields and methods.
610 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
611 if (pEncodedData != nullptr) {
612 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
613 fprintf(gOutFile, "static_fields_size : %d\n", pClassData.NumStaticFields());
614 fprintf(gOutFile, "instance_fields_size: %d\n", pClassData.NumInstanceFields());
615 fprintf(gOutFile, "direct_methods_size : %d\n", pClassData.NumDirectMethods());
616 fprintf(gOutFile, "virtual_methods_size: %d\n", pClassData.NumVirtualMethods());
618 fprintf(gOutFile, "static_fields_size : 0\n");
619 fprintf(gOutFile, "instance_fields_size: 0\n");
620 fprintf(gOutFile, "direct_methods_size : 0\n");
621 fprintf(gOutFile, "virtual_methods_size: 0\n");
623 fprintf(gOutFile, "\n");
627 * Dumps an annotation set item.
629 static void dumpAnnotationSetItem(const DexFile* pDexFile, const DexFile::AnnotationSetItem* set_item) {
630 if (set_item == nullptr || set_item->size_ == 0) {
631 fputs(" empty-annotation-set\n", gOutFile);
634 for (u4 i = 0; i < set_item->size_; i++) {
635 const DexFile::AnnotationItem* annotation = pDexFile->GetAnnotationItem(set_item, i);
636 if (annotation == nullptr) {
639 fputs(" ", gOutFile);
640 switch (annotation->visibility_) {
641 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", gOutFile); break;
642 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", gOutFile); break;
643 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", gOutFile); break;
644 default: fputs("VISIBILITY_UNKNOWN ", gOutFile); break;
646 // Decode raw bytes in annotation.
647 const u1* rData = annotation->annotation_;
648 dumpEncodedValue(pDexFile, &rData, DexFile::kDexAnnotationAnnotation, 0);
649 fputc('\n', gOutFile);
654 * Dumps class annotations.
656 static void dumpClassAnnotations(const DexFile* pDexFile, int idx) {
657 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
658 const DexFile::AnnotationsDirectoryItem* dir = pDexFile->GetAnnotationsDirectory(pClassDef);
659 if (dir == nullptr) {
663 fprintf(gOutFile, "Class #%d annotations:\n", idx);
665 const DexFile::AnnotationSetItem* class_set_item = pDexFile->GetClassAnnotationSet(dir);
666 const DexFile::FieldAnnotationsItem* fields = pDexFile->GetFieldAnnotations(dir);
667 const DexFile::MethodAnnotationsItem* methods = pDexFile->GetMethodAnnotations(dir);
668 const DexFile::ParameterAnnotationsItem* pars = pDexFile->GetParameterAnnotations(dir);
670 // Annotations on the class itself.
671 if (class_set_item != nullptr) {
672 fprintf(gOutFile, "Annotations on class\n");
673 dumpAnnotationSetItem(pDexFile, class_set_item);
676 // Annotations on fields.
677 if (fields != nullptr) {
678 for (u4 i = 0; i < dir->fields_size_; i++) {
679 const u4 field_idx = fields[i].field_idx_;
680 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
681 const char* field_name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
682 fprintf(gOutFile, "Annotations on field #%u '%s'\n", field_idx, field_name);
683 dumpAnnotationSetItem(pDexFile, pDexFile->GetFieldAnnotationSetItem(fields[i]));
687 // Annotations on methods.
688 if (methods != nullptr) {
689 for (u4 i = 0; i < dir->methods_size_; i++) {
690 const u4 method_idx = methods[i].method_idx_;
691 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
692 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
693 fprintf(gOutFile, "Annotations on method #%u '%s'\n", method_idx, method_name);
694 dumpAnnotationSetItem(pDexFile, pDexFile->GetMethodAnnotationSetItem(methods[i]));
698 // Annotations on method parameters.
699 if (pars != nullptr) {
700 for (u4 i = 0; i < dir->parameters_size_; i++) {
701 const u4 method_idx = pars[i].method_idx_;
702 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
703 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
704 fprintf(gOutFile, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
705 const DexFile::AnnotationSetRefList*
706 list = pDexFile->GetParameterAnnotationSetRefList(&pars[i]);
707 if (list != nullptr) {
708 for (u4 j = 0; j < list->size_; j++) {
709 fprintf(gOutFile, "#%u\n", j);
710 dumpAnnotationSetItem(pDexFile, pDexFile->GetSetRefItemItem(&list->list_[j]));
716 fputc('\n', gOutFile);
720 * Dumps an interface that a class declares to implement.
722 static void dumpInterface(const DexFile* pDexFile, const DexFile::TypeItem& pTypeItem, int i) {
723 const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
724 if (gOptions.outputFormat == OUTPUT_PLAIN) {
725 fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
727 std::unique_ptr<char[]> dot(descriptorToDot(interfaceName));
728 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dot.get());
733 * Dumps the catches table associated with the code.
735 static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
736 const u4 triesSize = pCode->tries_size_;
739 if (triesSize == 0) {
740 fprintf(gOutFile, " catches : (none)\n");
744 // Dump all table entries.
745 fprintf(gOutFile, " catches : %d\n", triesSize);
746 for (u4 i = 0; i < triesSize; i++) {
747 const DexFile::TryItem* pTry = pDexFile->GetTryItems(*pCode, i);
748 const u4 start = pTry->start_addr_;
749 const u4 end = start + pTry->insn_count_;
750 fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
751 for (CatchHandlerIterator it(*pCode, *pTry); it.HasNext(); it.Next()) {
752 const dex::TypeIndex tidx = it.GetHandlerTypeIndex();
753 const char* descriptor = (!tidx.IsValid()) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
754 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
760 * Callback for dumping each positions table entry.
762 static bool dumpPositionsCb(void* /*context*/, const DexFile::PositionInfo& entry) {
763 fprintf(gOutFile, " 0x%04x line=%d\n", entry.address_, entry.line_);
768 * Callback for dumping locals table entry.
770 static void dumpLocalsCb(void* /*context*/, const DexFile::LocalInfo& entry) {
771 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
772 fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
773 entry.start_address_, entry.end_address_, entry.reg_,
774 entry.name_, entry.descriptor_, signature);
778 * Helper for dumpInstruction(), which builds the string
779 * representation for the index in the given instruction.
780 * Returns a pointer to a buffer of sufficient size.
782 static std::unique_ptr<char[]> indexString(const DexFile* pDexFile,
783 const Instruction* pDecInsn,
785 static const u4 kInvalidIndex = std::numeric_limits<u4>::max();
786 std::unique_ptr<char[]> buf(new char[bufSize]);
787 // Determine index and width of the string.
789 u4 secondary_index = kInvalidIndex;
791 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
792 // SOME NOT SUPPORTED:
793 // case Instruction::k20bc:
794 case Instruction::k21c:
795 case Instruction::k35c:
796 // case Instruction::k35ms:
797 case Instruction::k3rc:
798 // case Instruction::k3rms:
799 // case Instruction::k35mi:
800 // case Instruction::k3rmi:
801 index = pDecInsn->VRegB();
804 case Instruction::k31c:
805 index = pDecInsn->VRegB();
808 case Instruction::k22c:
809 // case Instruction::k22cs:
810 index = pDecInsn->VRegC();
813 case Instruction::k45cc:
814 case Instruction::k4rcc:
815 index = pDecInsn->VRegB();
816 secondary_index = pDecInsn->VRegH();
823 // Determine index type.
825 switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
826 case Instruction::kIndexUnknown:
827 // This function should never get called for this type, but do
828 // something sensible here, just to help with debugging.
829 outSize = snprintf(buf.get(), bufSize, "<unknown-index>");
831 case Instruction::kIndexNone:
832 // This function should never get called for this type, but do
833 // something sensible here, just to help with debugging.
834 outSize = snprintf(buf.get(), bufSize, "<no-index>");
836 case Instruction::kIndexTypeRef:
837 if (index < pDexFile->GetHeader().type_ids_size_) {
838 const char* tp = pDexFile->StringByTypeIdx(dex::TypeIndex(index));
839 outSize = snprintf(buf.get(), bufSize, "%s // type@%0*x", tp, width, index);
841 outSize = snprintf(buf.get(), bufSize, "<type?> // type@%0*x", width, index);
844 case Instruction::kIndexStringRef:
845 if (index < pDexFile->GetHeader().string_ids_size_) {
846 const char* st = pDexFile->StringDataByIdx(dex::StringIndex(index));
847 outSize = snprintf(buf.get(), bufSize, "\"%s\" // string@%0*x", st, width, index);
849 outSize = snprintf(buf.get(), bufSize, "<string?> // string@%0*x", width, index);
852 case Instruction::kIndexMethodRef:
853 if (index < pDexFile->GetHeader().method_ids_size_) {
854 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
855 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
856 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
857 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
858 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // method@%0*x",
859 backDescriptor, name, signature.ToString().c_str(), width, index);
861 outSize = snprintf(buf.get(), bufSize, "<method?> // method@%0*x", width, index);
864 case Instruction::kIndexFieldRef:
865 if (index < pDexFile->GetHeader().field_ids_size_) {
866 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index);
867 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
868 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
869 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
870 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // field@%0*x",
871 backDescriptor, name, typeDescriptor, width, index);
873 outSize = snprintf(buf.get(), bufSize, "<field?> // field@%0*x", width, index);
876 case Instruction::kIndexVtableOffset:
877 outSize = snprintf(buf.get(), bufSize, "[%0*x] // vtable #%0*x",
878 width, index, width, index);
880 case Instruction::kIndexFieldOffset:
881 outSize = snprintf(buf.get(), bufSize, "[obj+%0*x]", width, index);
883 case Instruction::kIndexMethodAndProtoRef: {
884 std::string method("<method?>");
885 std::string proto("<proto?>");
886 if (index < pDexFile->GetHeader().method_ids_size_) {
887 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
888 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
889 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
890 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
891 method = android::base::StringPrintf("%s.%s:%s",
894 signature.ToString().c_str());
896 if (secondary_index < pDexFile->GetHeader().proto_ids_size_) {
897 const DexFile::ProtoId& protoId = pDexFile->GetProtoId(secondary_index);
898 const Signature signature = pDexFile->GetProtoSignature(protoId);
899 proto = signature.ToString();
901 outSize = snprintf(buf.get(), bufSize, "%s, %s // method@%0*x, proto@%0*x",
902 method.c_str(), proto.c_str(), width, index, width, secondary_index);
905 case Instruction::kIndexCallSiteRef:
906 // Call site information is too large to detail in disassembly so just output the index.
907 outSize = snprintf(buf.get(), bufSize, "call_site@%0*x", width, index);
909 // SOME NOT SUPPORTED:
910 // case Instruction::kIndexVaries:
911 // case Instruction::kIndexInlineMethod:
913 outSize = snprintf(buf.get(), bufSize, "<?>");
917 // Determine success of string construction.
918 if (outSize >= bufSize) {
919 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
920 // doesn't count/ the '\0' as part of its returned size, so we add explicit
921 // space for it here.
922 return indexString(pDexFile, pDecInsn, outSize + 1);
928 * Dumps a single instruction.
930 static void dumpInstruction(const DexFile* pDexFile,
931 const DexFile::CodeItem* pCode,
932 u4 codeOffset, u4 insnIdx, u4 insnWidth,
933 const Instruction* pDecInsn) {
934 // Address of instruction (expressed as byte offset).
935 fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
937 // Dump (part of) raw bytes.
938 const u2* insns = pCode->insns_;
939 for (u4 i = 0; i < 8; i++) {
942 fprintf(gOutFile, " ... ");
944 // Print 16-bit value in little-endian order.
945 const u1* bytePtr = (const u1*) &insns[insnIdx + i];
946 fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
949 fputs(" ", gOutFile);
953 // Dump pseudo-instruction or opcode.
954 if (pDecInsn->Opcode() == Instruction::NOP) {
955 const u2 instr = get2LE((const u1*) &insns[insnIdx]);
956 if (instr == Instruction::kPackedSwitchSignature) {
957 fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
958 } else if (instr == Instruction::kSparseSwitchSignature) {
959 fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
960 } else if (instr == Instruction::kArrayDataSignature) {
961 fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
963 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
966 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
969 // Set up additional argument.
970 std::unique_ptr<char[]> indexBuf;
971 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
972 indexBuf = indexString(pDexFile, pDecInsn, 200);
975 // Dump the instruction.
977 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
979 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
980 case Instruction::k10x: // op
982 case Instruction::k12x: // op vA, vB
983 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
985 case Instruction::k11n: // op vA, #+B
986 fprintf(gOutFile, " v%d, #int %d // #%x",
987 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
989 case Instruction::k11x: // op vAA
990 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
992 case Instruction::k10t: // op +AA
993 case Instruction::k20t: { // op +AAAA
994 const s4 targ = (s4) pDecInsn->VRegA();
995 fprintf(gOutFile, " %04x // %c%04x",
997 (targ < 0) ? '-' : '+',
998 (targ < 0) ? -targ : targ);
1001 case Instruction::k22x: // op vAA, vBBBB
1002 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1004 case Instruction::k21t: { // op vAA, +BBBB
1005 const s4 targ = (s4) pDecInsn->VRegB();
1006 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
1008 (targ < 0) ? '-' : '+',
1009 (targ < 0) ? -targ : targ);
1012 case Instruction::k21s: // op vAA, #+BBBB
1013 fprintf(gOutFile, " v%d, #int %d // #%x",
1014 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
1016 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
1017 // The printed format varies a bit based on the actual opcode.
1018 if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) {
1019 const s4 value = pDecInsn->VRegB() << 16;
1020 fprintf(gOutFile, " v%d, #int %d // #%x",
1021 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1023 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
1024 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
1025 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1028 case Instruction::k21c: // op vAA, thing@BBBB
1029 case Instruction::k31c: // op vAA, thing@BBBBBBBB
1030 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf.get());
1032 case Instruction::k23x: // op vAA, vBB, vCC
1033 fprintf(gOutFile, " v%d, v%d, v%d",
1034 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
1036 case Instruction::k22b: // op vAA, vBB, #+CC
1037 fprintf(gOutFile, " v%d, v%d, #int %d // #%02x",
1038 pDecInsn->VRegA(), pDecInsn->VRegB(),
1039 (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC());
1041 case Instruction::k22t: { // op vA, vB, +CCCC
1042 const s4 targ = (s4) pDecInsn->VRegC();
1043 fprintf(gOutFile, " v%d, v%d, %04x // %c%04x",
1044 pDecInsn->VRegA(), pDecInsn->VRegB(),
1046 (targ < 0) ? '-' : '+',
1047 (targ < 0) ? -targ : targ);
1050 case Instruction::k22s: // op vA, vB, #+CCCC
1051 fprintf(gOutFile, " v%d, v%d, #int %d // #%04x",
1052 pDecInsn->VRegA(), pDecInsn->VRegB(),
1053 (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC());
1055 case Instruction::k22c: // op vA, vB, thing@CCCC
1057 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
1058 fprintf(gOutFile, " v%d, v%d, %s",
1059 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf.get());
1061 case Instruction::k30t:
1062 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
1064 case Instruction::k31i: { // op vAA, #+BBBBBBBB
1065 // This is often, but not always, a float.
1070 conv.i = pDecInsn->VRegB();
1071 fprintf(gOutFile, " v%d, #float %g // #%08x",
1072 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
1075 case Instruction::k31t: // op vAA, offset +BBBBBBBB
1076 fprintf(gOutFile, " v%d, %08x // +%08x",
1077 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
1079 case Instruction::k32x: // op vAAAA, vBBBB
1080 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1082 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
1083 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, method@BBBB, proto@HHHH
1085 // case Instruction::k35ms: // [opt] invoke-virtual+super
1086 // case Instruction::k35mi: // [opt] inline invoke
1087 u4 arg[Instruction::kMaxVarArgRegs];
1088 pDecInsn->GetVarArgs(arg);
1089 fputs(" {", gOutFile);
1090 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1092 fprintf(gOutFile, "v%d", arg[i]);
1094 fprintf(gOutFile, ", v%d", arg[i]);
1097 fprintf(gOutFile, "}, %s", indexBuf.get());
1100 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
1101 case Instruction::k4rcc: { // op {vCCCC .. v(CCCC+AA-1)}, method@BBBB, proto@HHHH
1103 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1104 // case Instruction::k3rmi: // [opt] execute-inline/range
1105 // This doesn't match the "dx" output when some of the args are
1106 // 64-bit values -- dx only shows the first register.
1107 fputs(" {", gOutFile);
1108 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1110 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
1112 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
1115 fprintf(gOutFile, "}, %s", indexBuf.get());
1118 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1119 // This is often, but not always, a double.
1124 conv.j = pDecInsn->WideVRegB();
1125 fprintf(gOutFile, " v%d, #double %g // #%016" PRIx64,
1126 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
1130 // case Instruction::k00x: // unknown op or breakpoint
1133 fprintf(gOutFile, " ???");
1137 fputc('\n', gOutFile);
1141 * Dumps a bytecode disassembly.
1143 static void dumpBytecodes(const DexFile* pDexFile, u4 idx,
1144 const DexFile::CodeItem* pCode, u4 codeOffset) {
1145 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1146 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1147 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1148 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1151 std::unique_ptr<char[]> dot(descriptorToDot(backDescriptor));
1152 fprintf(gOutFile, "%06x: |[%06x] %s.%s:%s\n",
1153 codeOffset, codeOffset, dot.get(), name, signature.ToString().c_str());
1155 // Iterate over all instructions.
1156 const u2* insns = pCode->insns_;
1157 for (u4 insnIdx = 0; insnIdx < pCode->insns_size_in_code_units_;) {
1158 const Instruction* instruction = Instruction::At(&insns[insnIdx]);
1159 const u4 insnWidth = instruction->SizeInCodeUnits();
1160 if (insnWidth == 0) {
1161 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx);
1164 dumpInstruction(pDexFile, pCode, codeOffset, insnIdx, insnWidth, instruction);
1165 insnIdx += insnWidth;
1170 * Dumps code of a method.
1172 static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags,
1173 const DexFile::CodeItem* pCode, u4 codeOffset) {
1174 fprintf(gOutFile, " registers : %d\n", pCode->registers_size_);
1175 fprintf(gOutFile, " ins : %d\n", pCode->ins_size_);
1176 fprintf(gOutFile, " outs : %d\n", pCode->outs_size_);
1177 fprintf(gOutFile, " insns size : %d 16-bit code units\n",
1178 pCode->insns_size_in_code_units_);
1180 // Bytecode disassembly, if requested.
1181 if (gOptions.disassemble) {
1182 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
1185 // Try-catch blocks.
1186 dumpCatches(pDexFile, pCode);
1188 // Positions and locals table in the debug info.
1189 bool is_static = (flags & kAccStatic) != 0;
1190 fprintf(gOutFile, " positions : \n");
1191 pDexFile->DecodeDebugPositionInfo(pCode, dumpPositionsCb, nullptr);
1192 fprintf(gOutFile, " locals : \n");
1193 pDexFile->DecodeDebugLocalInfo(pCode, is_static, idx, dumpLocalsCb, nullptr);
1199 static void dumpMethod(const DexFile* pDexFile, u4 idx, u4 flags,
1200 const DexFile::CodeItem* pCode, u4 codeOffset, int i) {
1201 // Bail for anything private if export only requested.
1202 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1206 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1207 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1208 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1209 char* typeDescriptor = strdup(signature.ToString().c_str());
1210 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1211 char* accessStr = createAccessFlagStr(flags, kAccessForMethod);
1213 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1214 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1215 fprintf(gOutFile, " name : '%s'\n", name);
1216 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1217 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
1218 if (pCode == nullptr) {
1219 fprintf(gOutFile, " code : (none)\n");
1221 fprintf(gOutFile, " code -\n");
1222 dumpCode(pDexFile, idx, flags, pCode, codeOffset);
1224 if (gOptions.disassemble) {
1225 fputc('\n', gOutFile);
1227 } else if (gOptions.outputFormat == OUTPUT_XML) {
1228 const bool constructor = (name[0] == '<');
1230 // Method name and prototype.
1232 std::unique_ptr<char[]> dot(descriptorClassToDot(backDescriptor));
1233 fprintf(gOutFile, "<constructor name=\"%s\"\n", dot.get());
1234 dot = descriptorToDot(backDescriptor);
1235 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
1237 fprintf(gOutFile, "<method name=\"%s\"\n", name);
1238 const char* returnType = strrchr(typeDescriptor, ')');
1239 if (returnType == nullptr) {
1240 fprintf(stderr, "bad method type descriptor '%s'\n", typeDescriptor);
1243 std::unique_ptr<char[]> dot(descriptorToDot(returnType + 1));
1244 fprintf(gOutFile, " return=\"%s\"\n", dot.get());
1245 fprintf(gOutFile, " abstract=%s\n", quotedBool((flags & kAccAbstract) != 0));
1246 fprintf(gOutFile, " native=%s\n", quotedBool((flags & kAccNative) != 0));
1247 fprintf(gOutFile, " synchronized=%s\n", quotedBool(
1248 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1251 // Additional method flags.
1252 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1253 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1254 // The "deprecated=" not knowable w/o parsing annotations.
1255 fprintf(gOutFile, " visibility=%s\n>\n", quotedVisibility(flags));
1258 if (typeDescriptor[0] != '(') {
1259 fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor);
1262 char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1));
1263 const char* base = typeDescriptor + 1;
1265 while (*base != ')') {
1267 while (*base == '[') {
1271 // Copy through ';'.
1274 } while (*cp++ != ';');
1276 // Primitive char, copy it.
1277 if (strchr("ZBCSIFJD", *base) == nullptr) {
1278 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
1283 // Null terminate and display.
1285 std::unique_ptr<char[]> dot(descriptorToDot(tmpBuf));
1286 fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n"
1287 "</parameter>\n", argNum++, dot.get());
1291 fprintf(gOutFile, "</constructor>\n");
1293 fprintf(gOutFile, "</method>\n");
1298 free(typeDescriptor);
1303 * Dumps a static (class) field.
1305 static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i, const u1** data) {
1306 // Bail for anything private if export only requested.
1307 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1311 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(idx);
1312 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
1313 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
1314 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
1315 char* accessStr = createAccessFlagStr(flags, kAccessForField);
1317 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1318 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1319 fprintf(gOutFile, " name : '%s'\n", name);
1320 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1321 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
1322 if (data != nullptr) {
1323 fputs(" value : ", gOutFile);
1324 dumpEncodedValue(pDexFile, data);
1325 fputs("\n", gOutFile);
1327 } else if (gOptions.outputFormat == OUTPUT_XML) {
1328 fprintf(gOutFile, "<field name=\"%s\"\n", name);
1329 std::unique_ptr<char[]> dot(descriptorToDot(typeDescriptor));
1330 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
1331 fprintf(gOutFile, " transient=%s\n", quotedBool((flags & kAccTransient) != 0));
1332 fprintf(gOutFile, " volatile=%s\n", quotedBool((flags & kAccVolatile) != 0));
1333 // The "value=" is not knowable w/o parsing annotations.
1334 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1335 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1336 // The "deprecated=" is not knowable w/o parsing annotations.
1337 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(flags));
1338 if (data != nullptr) {
1339 fputs(" value=\"", gOutFile);
1340 dumpEncodedValue(pDexFile, data);
1341 fputs("\"\n", gOutFile);
1343 fputs(">\n</field>\n", gOutFile);
1350 * Dumps an instance field.
1352 static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
1353 dumpSField(pDexFile, idx, flags, i, nullptr);
1357 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1358 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1359 * tool, so this is not performance-critical.
1362 static void dumpCfg(const DexFile* dex_file,
1364 const DexFile::CodeItem* code_item) {
1365 if (code_item != nullptr) {
1366 std::ostringstream oss;
1367 DumpMethodCFG(dex_file, dex_method_idx, oss);
1368 fputs(oss.str().c_str(), gOutFile);
1372 static void dumpCfg(const DexFile* dex_file, int idx) {
1373 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
1374 const u1* class_data = dex_file->GetClassData(class_def);
1375 if (class_data == nullptr) { // empty class such as a marker interface?
1378 ClassDataItemIterator it(*dex_file, class_data);
1379 while (it.HasNextStaticField()) {
1382 while (it.HasNextInstanceField()) {
1385 while (it.HasNextDirectMethod()) {
1387 it.GetMemberIndex(),
1388 it.GetMethodCodeItem());
1391 while (it.HasNextVirtualMethod()) {
1393 it.GetMemberIndex(),
1394 it.GetMethodCodeItem());
1402 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1404 * If "*pLastPackage" is nullptr or does not match the current class' package,
1405 * the value will be replaced with a newly-allocated string.
1407 static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1408 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1410 // Omitting non-public class.
1411 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1415 if (gOptions.showSectionHeaders) {
1416 dumpClassDef(pDexFile, idx);
1419 if (gOptions.showAnnotations) {
1420 dumpClassAnnotations(pDexFile, idx);
1423 if (gOptions.showCfg) {
1424 dumpCfg(pDexFile, idx);
1428 // For the XML output, show the package name. Ideally we'd gather
1429 // up the classes, sort them, and dump them alphabetically so the
1430 // package name wouldn't jump around, but that's not a great plan
1431 // for something that needs to run on the device.
1432 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1433 if (!(classDescriptor[0] == 'L' &&
1434 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1435 // Arrays and primitives should not be defined explicitly. Keep going?
1436 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1437 } else if (gOptions.outputFormat == OUTPUT_XML) {
1438 char* mangle = strdup(classDescriptor + 1);
1439 mangle[strlen(mangle)-1] = '\0';
1441 // Reduce to just the package name.
1442 char* lastSlash = strrchr(mangle, '/');
1443 if (lastSlash != nullptr) {
1449 for (char* cp = mangle; *cp != '\0'; cp++) {
1455 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1456 // Start of a new package.
1457 if (*pLastPackage != nullptr) {
1458 fprintf(gOutFile, "</package>\n");
1460 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1461 free(*pLastPackage);
1462 *pLastPackage = mangle;
1468 // General class information.
1469 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1470 const char* superclassDescriptor;
1471 if (!pClassDef.superclass_idx_.IsValid()) {
1472 superclassDescriptor = nullptr;
1474 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1476 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1477 fprintf(gOutFile, "Class #%d -\n", idx);
1478 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1479 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1480 if (superclassDescriptor != nullptr) {
1481 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1483 fprintf(gOutFile, " Interfaces -\n");
1485 std::unique_ptr<char[]> dot(descriptorClassToDot(classDescriptor));
1486 fprintf(gOutFile, "<class name=\"%s\"\n", dot.get());
1487 if (superclassDescriptor != nullptr) {
1488 dot = descriptorToDot(superclassDescriptor);
1489 fprintf(gOutFile, " extends=\"%s\"\n", dot.get());
1491 fprintf(gOutFile, " interface=%s\n",
1492 quotedBool((pClassDef.access_flags_ & kAccInterface) != 0));
1493 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1494 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1495 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1496 // The "deprecated=" not knowable w/o parsing annotations.
1497 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1498 fprintf(gOutFile, ">\n");
1502 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1503 if (pInterfaces != nullptr) {
1504 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1505 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1509 // Fields and methods.
1510 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
1511 if (pEncodedData == nullptr) {
1512 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1513 fprintf(gOutFile, " Static fields -\n");
1514 fprintf(gOutFile, " Instance fields -\n");
1515 fprintf(gOutFile, " Direct methods -\n");
1516 fprintf(gOutFile, " Virtual methods -\n");
1519 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
1521 // Prepare data for static fields.
1522 const u1* sData = pDexFile->GetEncodedStaticFieldValuesArray(pClassDef);
1523 const u4 sSize = sData != nullptr ? DecodeUnsignedLeb128(&sData) : 0;
1526 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1527 fprintf(gOutFile, " Static fields -\n");
1529 for (u4 i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) {
1530 dumpSField(pDexFile,
1531 pClassData.GetMemberIndex(),
1532 pClassData.GetRawMemberAccessFlags(),
1534 i < sSize ? &sData : nullptr);
1538 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1539 fprintf(gOutFile, " Instance fields -\n");
1541 for (u4 i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) {
1542 dumpIField(pDexFile,
1543 pClassData.GetMemberIndex(),
1544 pClassData.GetRawMemberAccessFlags(),
1549 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1550 fprintf(gOutFile, " Direct methods -\n");
1552 for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) {
1553 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1554 pClassData.GetRawMemberAccessFlags(),
1555 pClassData.GetMethodCodeItem(),
1556 pClassData.GetMethodCodeItemOffset(), i);
1560 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1561 fprintf(gOutFile, " Virtual methods -\n");
1563 for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) {
1564 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1565 pClassData.GetRawMemberAccessFlags(),
1566 pClassData.GetMethodCodeItem(),
1567 pClassData.GetMethodCodeItemOffset(), i);
1572 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1573 const char* fileName;
1574 if (pClassDef.source_file_idx_.IsValid()) {
1575 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1577 fileName = "unknown";
1579 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
1580 pClassDef.source_file_idx_.index_, fileName);
1581 } else if (gOptions.outputFormat == OUTPUT_XML) {
1582 fprintf(gOutFile, "</class>\n");
1588 static void dumpMethodHandle(const DexFile* pDexFile, u4 idx) {
1589 const DexFile::MethodHandleItem& mh = pDexFile->GetMethodHandle(idx);
1590 bool is_invoke = false;
1592 switch (static_cast<DexFile::MethodHandleType>(mh.method_handle_type_)) {
1593 case DexFile::MethodHandleType::kStaticPut:
1594 type = "put-static";
1596 case DexFile::MethodHandleType::kStaticGet:
1597 type = "get-static";
1599 case DexFile::MethodHandleType::kInstancePut:
1600 type = "put-instance";
1602 case DexFile::MethodHandleType::kInstanceGet:
1603 type = "get-instance";
1605 case DexFile::MethodHandleType::kInvokeStatic:
1606 type = "invoke-static";
1609 case DexFile::MethodHandleType::kInvokeInstance:
1610 type = "invoke-instance";
1613 case DexFile::MethodHandleType::kInvokeConstructor:
1614 type = "invoke-constructor";
1619 const char* declaring_class;
1621 std::string member_type;
1623 const DexFile::MethodId& method_id = pDexFile->GetMethodId(mh.field_or_method_idx_);
1624 declaring_class = pDexFile->GetMethodDeclaringClassDescriptor(method_id);
1625 member = pDexFile->GetMethodName(method_id);
1626 member_type = pDexFile->GetMethodSignature(method_id).ToString();
1628 const DexFile::FieldId& field_id = pDexFile->GetFieldId(mh.field_or_method_idx_);
1629 declaring_class = pDexFile->GetFieldDeclaringClassDescriptor(field_id);
1630 member = pDexFile->GetFieldName(field_id);
1631 member_type = pDexFile->GetFieldTypeDescriptor(field_id);
1634 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1635 fprintf(gOutFile, "Method handle #%u:\n", idx);
1636 fprintf(gOutFile, " type : %s\n", type);
1637 fprintf(gOutFile, " target : %s %s\n", declaring_class, member);
1638 fprintf(gOutFile, " target_type : %s\n", member_type.c_str());
1640 fprintf(gOutFile, "<method_handle index=\"%u\"\n", idx);
1641 fprintf(gOutFile, " type=\"%s\"\n", type);
1642 fprintf(gOutFile, " target_class=\"%s\"\n", declaring_class);
1643 fprintf(gOutFile, " target_member=\"%s\"\n", member);
1644 fprintf(gOutFile, " target_member_type=");
1645 dumpEscapedString(member_type.c_str());
1646 fprintf(gOutFile, "\n>\n</method_handle>\n");
1650 static void dumpCallSite(const DexFile* pDexFile, u4 idx) {
1651 const DexFile::CallSiteIdItem& call_site_id = pDexFile->GetCallSiteId(idx);
1652 CallSiteArrayValueIterator it(*pDexFile, call_site_id);
1653 if (it.Size() < 3) {
1654 fprintf(stderr, "ERROR: Call site %u has too few values.\n", idx);
1658 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1660 dex::StringIndex method_name_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1661 const char* method_name = pDexFile->StringDataByIdx(method_name_idx);
1663 uint32_t method_type_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1664 const DexFile::ProtoId& method_type_id = pDexFile->GetProtoId(method_type_idx);
1665 std::string method_type = pDexFile->GetProtoSignature(method_type_id).ToString();
1668 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1669 fprintf(gOutFile, "Call site #%u:\n", idx);
1670 fprintf(gOutFile, " link_argument[0] : %u (MethodHandle)\n", method_handle_idx);
1671 fprintf(gOutFile, " link_argument[1] : %s (String)\n", method_name);
1672 fprintf(gOutFile, " link_argument[2] : %s (MethodType)\n", method_type.c_str());
1674 fprintf(gOutFile, "<call_site index=\"%u\">\n", idx);
1676 "<link_argument index=\"0\" type=\"MethodHandle\" value=\"%u\"/>\n",
1679 "<link_argument index=\"1\" type=\"String\" values=\"%s\"/>\n",
1682 "<link_argument index=\"2\" type=\"MethodType\" value=\"%s\"/>\n",
1683 method_type.c_str());
1686 size_t argument = 3;
1687 while (it.HasNext()) {
1690 switch (it.GetValueType()) {
1691 case EncodedArrayValueIterator::ValueType::kByte:
1693 value = android::base::StringPrintf("%u", it.GetJavaValue().b);
1695 case EncodedArrayValueIterator::ValueType::kShort:
1697 value = android::base::StringPrintf("%d", it.GetJavaValue().s);
1699 case EncodedArrayValueIterator::ValueType::kChar:
1701 value = android::base::StringPrintf("%u", it.GetJavaValue().c);
1703 case EncodedArrayValueIterator::ValueType::kInt:
1705 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1707 case EncodedArrayValueIterator::ValueType::kLong:
1709 value = android::base::StringPrintf("%" PRId64, it.GetJavaValue().j);
1711 case EncodedArrayValueIterator::ValueType::kFloat:
1713 value = android::base::StringPrintf("%g", it.GetJavaValue().f);
1715 case EncodedArrayValueIterator::ValueType::kDouble:
1717 value = android::base::StringPrintf("%g", it.GetJavaValue().d);
1719 case EncodedArrayValueIterator::ValueType::kMethodType: {
1720 type = "MethodType";
1721 uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1722 const DexFile::ProtoId& proto_id = pDexFile->GetProtoId(proto_idx);
1723 value = pDexFile->GetProtoSignature(proto_id).ToString();
1726 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1727 type = "MethodHandle";
1728 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1730 case EncodedArrayValueIterator::ValueType::kString: {
1732 dex::StringIndex string_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1733 value = pDexFile->StringDataByIdx(string_idx);
1736 case EncodedArrayValueIterator::ValueType::kType: {
1738 dex::TypeIndex type_idx = static_cast<dex::TypeIndex>(it.GetJavaValue().i);
1739 const DexFile::ClassDef* class_def = pDexFile->FindClassDef(type_idx);
1740 value = pDexFile->GetClassDescriptor(*class_def);
1741 value = descriptorClassToDot(value.c_str()).get();
1744 case EncodedArrayValueIterator::ValueType::kField:
1745 case EncodedArrayValueIterator::ValueType::kMethod:
1746 case EncodedArrayValueIterator::ValueType::kEnum:
1747 case EncodedArrayValueIterator::ValueType::kArray:
1748 case EncodedArrayValueIterator::ValueType::kAnnotation:
1749 // Unreachable based on current EncodedArrayValueIterator::Next().
1750 UNIMPLEMENTED(FATAL) << " type " << type;
1753 case EncodedArrayValueIterator::ValueType::kNull:
1757 case EncodedArrayValueIterator::ValueType::kBoolean:
1759 value = it.GetJavaValue().z ? "true" : "false";
1763 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1764 fprintf(gOutFile, " link_argument[%zu] : %s (%s)\n", argument, value.c_str(), type);
1766 fprintf(gOutFile, "<link_argument index=\"%zu\" type=\"%s\" value=", argument, type);
1767 dumpEscapedString(value.c_str());
1768 fprintf(gOutFile, "/>\n");
1775 if (gOptions.outputFormat == OUTPUT_XML) {
1776 fprintf(gOutFile, "</call_site>\n");
1781 * Dumps the requested sections of the file.
1783 static void processDexFile(const char* fileName,
1784 const DexFile* pDexFile, size_t i, size_t n) {
1785 if (gOptions.verbose) {
1786 fputs("Opened '", gOutFile);
1787 fputs(fileName, gOutFile);
1789 fprintf(gOutFile, ":%s", DexFile::GetMultiDexClassesDexName(i).c_str());
1791 fprintf(gOutFile, "', DEX version '%.3s'\n", pDexFile->GetHeader().magic_ + 4);
1795 if (gOptions.showFileHeaders) {
1796 dumpFileHeader(pDexFile);
1799 // Open XML context.
1800 if (gOptions.outputFormat == OUTPUT_XML) {
1801 fprintf(gOutFile, "<api>\n");
1804 // Iterate over all classes.
1805 char* package = nullptr;
1806 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
1807 for (u4 i = 0; i < classDefsSize; i++) {
1808 dumpClass(pDexFile, i, &package);
1811 // Iterate over all method handles.
1812 for (u4 i = 0; i < pDexFile->NumMethodHandles(); ++i) {
1813 dumpMethodHandle(pDexFile, i);
1816 // Iterate over all call site ids.
1817 for (u4 i = 0; i < pDexFile->NumCallSiteIds(); ++i) {
1818 dumpCallSite(pDexFile, i);
1821 // Free the last package allocated.
1822 if (package != nullptr) {
1823 fprintf(gOutFile, "</package>\n");
1827 // Close XML context.
1828 if (gOptions.outputFormat == OUTPUT_XML) {
1829 fprintf(gOutFile, "</api>\n");
1834 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1836 int processFile(const char* fileName) {
1837 if (gOptions.verbose) {
1838 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1841 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1842 // all of which are Zip archives with "classes.dex" inside.
1843 const bool kVerifyChecksum = !gOptions.ignoreBadChecksum;
1844 std::string error_msg;
1845 std::vector<std::unique_ptr<const DexFile>> dex_files;
1846 if (!DexFile::Open(fileName, fileName, kVerifyChecksum, &error_msg, &dex_files)) {
1847 // Display returned error message to user. Note that this error behavior
1848 // differs from the error messages shown by the original Dalvik dexdump.
1849 fputs(error_msg.c_str(), stderr);
1850 fputc('\n', stderr);
1854 // Success. Either report checksum verification or process
1855 // all dex files found in given file.
1856 if (gOptions.checksumOnly) {
1857 fprintf(gOutFile, "Checksum verified\n");
1859 for (size_t i = 0, n = dex_files.size(); i < n; i++) {
1860 processDexFile(fileName, dex_files[i].get(), i, n);