blob: 96c326749ebc825e4791122c68745a825ee0e18f [file] [log] [blame]
Aart Bik69ae54a2015-07-01 14:52:26 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 *
16 * Implementation file of the dexdump utility.
17 *
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
Aart Bikdce50862016-06-10 16:04:03 -070020 * 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.
Aart Bik69ae54a2015-07-01 14:52:26 -070022 * Also, ODEX files are no longer supported.
23 *
24 * The dexdump tool is intended to mimic objdump. When possible, use
25 * similar command-line arguments.
26 *
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
Aart Bik69ae54a2015-07-01 14:52:26 -070030 * - 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
33 */
34
35#include "dexdump.h"
36
37#include <inttypes.h>
38#include <stdio.h>
39
Andreas Gampe5073fed2015-08-10 11:40:25 -070040#include <iostream>
Aart Bik69ae54a2015-07-01 14:52:26 -070041#include <memory>
Andreas Gampe5073fed2015-08-10 11:40:25 -070042#include <sstream>
Aart Bik69ae54a2015-07-01 14:52:26 -070043#include <vector>
44
45#include "dex_file-inl.h"
46#include "dex_instruction-inl.h"
Andreas Gampe5073fed2015-08-10 11:40:25 -070047#include "utils.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070048
49namespace art {
50
51/*
52 * Options parsed in main driver.
53 */
54struct Options gOptions;
55
56/*
Aart Bik4e149602015-07-09 11:45:28 -070057 * Output file. Defaults to stdout.
Aart Bik69ae54a2015-07-01 14:52:26 -070058 */
59FILE* gOutFile = stdout;
60
61/*
62 * Data types that match the definitions in the VM specification.
63 */
64typedef uint8_t u1;
65typedef uint16_t u2;
66typedef uint32_t u4;
67typedef uint64_t u8;
Aart Bikdce50862016-06-10 16:04:03 -070068typedef int8_t s1;
69typedef int16_t s2;
Aart Bik69ae54a2015-07-01 14:52:26 -070070typedef int32_t s4;
71typedef int64_t s8;
72
73/*
74 * Basic information about a field or a method.
75 */
76struct FieldMethodInfo {
77 const char* classDescriptor;
78 const char* name;
79 const char* signature;
80};
81
82/*
83 * Flags for use with createAccessFlagStr().
84 */
85enum AccessFor {
86 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
87};
88const int kNumFlags = 18;
89
90/*
91 * Gets 2 little-endian bytes.
92 */
93static inline u2 get2LE(unsigned char const* pSrc) {
94 return pSrc[0] | (pSrc[1] << 8);
95}
96
97/*
98 * Converts a single-character primitive type into human-readable form.
99 */
100static const char* primitiveTypeLabel(char typeChar) {
101 switch (typeChar) {
102 case 'B': return "byte";
103 case 'C': return "char";
104 case 'D': return "double";
105 case 'F': return "float";
106 case 'I': return "int";
107 case 'J': return "long";
108 case 'S': return "short";
109 case 'V': return "void";
110 case 'Z': return "boolean";
111 default: return "UNKNOWN";
112 } // switch
113}
114
115/*
116 * Converts a type descriptor to human-readable "dotted" form. For
117 * example, "Ljava/lang/String;" becomes "java.lang.String", and
118 * "[I" becomes "int[]". Also converts '$' to '.', which means this
119 * form can't be converted back to a descriptor.
120 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700121static std::unique_ptr<char[]> descriptorToDot(const char* str) {
Aart Bik69ae54a2015-07-01 14:52:26 -0700122 int targetLen = strlen(str);
123 int offset = 0;
124
125 // Strip leading [s; will be added to end.
126 while (targetLen > 1 && str[offset] == '[') {
127 offset++;
128 targetLen--;
129 } // while
130
131 const int arrayDepth = offset;
132
133 if (targetLen == 1) {
134 // Primitive type.
135 str = primitiveTypeLabel(str[offset]);
136 offset = 0;
137 targetLen = strlen(str);
138 } else {
139 // Account for leading 'L' and trailing ';'.
140 if (targetLen >= 2 && str[offset] == 'L' &&
141 str[offset + targetLen - 1] == ';') {
142 targetLen -= 2;
143 offset++;
144 }
145 }
146
147 // Copy class name over.
Aart Bikc05e2f22016-07-12 15:53:13 -0700148 std::unique_ptr<char[]> newStr(new char[targetLen + arrayDepth * 2 + 1]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700149 int i = 0;
150 for (; i < targetLen; i++) {
151 const char ch = str[offset + i];
152 newStr[i] = (ch == '/' || ch == '$') ? '.' : ch;
153 } // for
154
155 // Add the appropriate number of brackets for arrays.
156 for (int j = 0; j < arrayDepth; j++) {
157 newStr[i++] = '[';
158 newStr[i++] = ']';
159 } // for
160
161 newStr[i] = '\0';
162 return newStr;
163}
164
165/*
166 * Converts the class name portion of a type descriptor to human-readable
Aart Bikc05e2f22016-07-12 15:53:13 -0700167 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
Aart Bik69ae54a2015-07-01 14:52:26 -0700168 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700169static std::unique_ptr<char[]> descriptorClassToDot(const char* str) {
170 // Reduce to just the class name prefix.
Aart Bik69ae54a2015-07-01 14:52:26 -0700171 const char* lastSlash = strrchr(str, '/');
172 if (lastSlash == nullptr) {
173 lastSlash = str + 1; // start past 'L'
174 } else {
175 lastSlash++; // start past '/'
176 }
177
Aart Bikc05e2f22016-07-12 15:53:13 -0700178 // Copy class name over, trimming trailing ';'.
179 const int targetLen = strlen(lastSlash);
180 std::unique_ptr<char[]> newStr(new char[targetLen]);
181 for (int i = 0; i < targetLen - 1; i++) {
182 const char ch = lastSlash[i];
183 newStr[i] = ch == '$' ? '.' : ch;
Aart Bik69ae54a2015-07-01 14:52:26 -0700184 } // for
Aart Bikc05e2f22016-07-12 15:53:13 -0700185 newStr[targetLen - 1] = '\0';
Aart Bik69ae54a2015-07-01 14:52:26 -0700186 return newStr;
187}
188
189/*
Aart Bikdce50862016-06-10 16:04:03 -0700190 * Returns string representing the boolean value.
191 */
192static const char* strBool(bool val) {
193 return val ? "true" : "false";
194}
195
196/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700197 * Returns a quoted string representing the boolean value.
198 */
199static const char* quotedBool(bool val) {
200 return val ? "\"true\"" : "\"false\"";
201}
202
203/*
204 * Returns a quoted string representing the access flags.
205 */
206static const char* quotedVisibility(u4 accessFlags) {
207 if (accessFlags & kAccPublic) {
208 return "\"public\"";
209 } else if (accessFlags & kAccProtected) {
210 return "\"protected\"";
211 } else if (accessFlags & kAccPrivate) {
212 return "\"private\"";
213 } else {
214 return "\"package\"";
215 }
216}
217
218/*
219 * Counts the number of '1' bits in a word.
220 */
221static int countOnes(u4 val) {
222 val = val - ((val >> 1) & 0x55555555);
223 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
224 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
225}
226
227/*
228 * Creates a new string with human-readable access flags.
229 *
230 * In the base language the access_flags fields are type u2; in Dalvik
231 * they're u4.
232 */
233static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
234 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
235 {
236 "PUBLIC", /* 0x00001 */
237 "PRIVATE", /* 0x00002 */
238 "PROTECTED", /* 0x00004 */
239 "STATIC", /* 0x00008 */
240 "FINAL", /* 0x00010 */
241 "?", /* 0x00020 */
242 "?", /* 0x00040 */
243 "?", /* 0x00080 */
244 "?", /* 0x00100 */
245 "INTERFACE", /* 0x00200 */
246 "ABSTRACT", /* 0x00400 */
247 "?", /* 0x00800 */
248 "SYNTHETIC", /* 0x01000 */
249 "ANNOTATION", /* 0x02000 */
250 "ENUM", /* 0x04000 */
251 "?", /* 0x08000 */
252 "VERIFIED", /* 0x10000 */
253 "OPTIMIZED", /* 0x20000 */
254 }, {
255 "PUBLIC", /* 0x00001 */
256 "PRIVATE", /* 0x00002 */
257 "PROTECTED", /* 0x00004 */
258 "STATIC", /* 0x00008 */
259 "FINAL", /* 0x00010 */
260 "SYNCHRONIZED", /* 0x00020 */
261 "BRIDGE", /* 0x00040 */
262 "VARARGS", /* 0x00080 */
263 "NATIVE", /* 0x00100 */
264 "?", /* 0x00200 */
265 "ABSTRACT", /* 0x00400 */
266 "STRICT", /* 0x00800 */
267 "SYNTHETIC", /* 0x01000 */
268 "?", /* 0x02000 */
269 "?", /* 0x04000 */
270 "MIRANDA", /* 0x08000 */
271 "CONSTRUCTOR", /* 0x10000 */
272 "DECLARED_SYNCHRONIZED", /* 0x20000 */
273 }, {
274 "PUBLIC", /* 0x00001 */
275 "PRIVATE", /* 0x00002 */
276 "PROTECTED", /* 0x00004 */
277 "STATIC", /* 0x00008 */
278 "FINAL", /* 0x00010 */
279 "?", /* 0x00020 */
280 "VOLATILE", /* 0x00040 */
281 "TRANSIENT", /* 0x00080 */
282 "?", /* 0x00100 */
283 "?", /* 0x00200 */
284 "?", /* 0x00400 */
285 "?", /* 0x00800 */
286 "SYNTHETIC", /* 0x01000 */
287 "?", /* 0x02000 */
288 "ENUM", /* 0x04000 */
289 "?", /* 0x08000 */
290 "?", /* 0x10000 */
291 "?", /* 0x20000 */
292 },
293 };
294
295 // Allocate enough storage to hold the expected number of strings,
296 // plus a space between each. We over-allocate, using the longest
297 // string above as the base metric.
298 const int kLongest = 21; // The strlen of longest string above.
299 const int count = countOnes(flags);
300 char* str;
301 char* cp;
302 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
303
304 for (int i = 0; i < kNumFlags; i++) {
305 if (flags & 0x01) {
306 const char* accessStr = kAccessStrings[forWhat][i];
307 const int len = strlen(accessStr);
308 if (cp != str) {
309 *cp++ = ' ';
310 }
311 memcpy(cp, accessStr, len);
312 cp += len;
313 }
314 flags >>= 1;
315 } // for
316
317 *cp = '\0';
318 return str;
319}
320
321/*
322 * Copies character data from "data" to "out", converting non-ASCII values
323 * to fprintf format chars or an ASCII filler ('.' or '?').
324 *
325 * The output buffer must be able to hold (2*len)+1 bytes. The result is
326 * NULL-terminated.
327 */
328static void asciify(char* out, const unsigned char* data, size_t len) {
329 while (len--) {
330 if (*data < 0x20) {
331 // Could do more here, but we don't need them yet.
332 switch (*data) {
333 case '\0':
334 *out++ = '\\';
335 *out++ = '0';
336 break;
337 case '\n':
338 *out++ = '\\';
339 *out++ = 'n';
340 break;
341 default:
342 *out++ = '.';
343 break;
344 } // switch
345 } else if (*data >= 0x80) {
346 *out++ = '?';
347 } else {
348 *out++ = *data;
349 }
350 data++;
351 } // while
352 *out = '\0';
353}
354
355/*
Aart Bikdce50862016-06-10 16:04:03 -0700356 * Dumps a string value with some escape characters.
357 */
358static void dumpEscapedString(const char* p) {
359 fputs("\"", gOutFile);
360 for (; *p; p++) {
361 switch (*p) {
362 case '\\':
363 fputs("\\\\", gOutFile);
364 break;
365 case '\"':
366 fputs("\\\"", gOutFile);
367 break;
368 case '\t':
369 fputs("\\t", gOutFile);
370 break;
371 case '\n':
372 fputs("\\n", gOutFile);
373 break;
374 case '\r':
375 fputs("\\r", gOutFile);
376 break;
377 default:
378 putc(*p, gOutFile);
379 } // switch
380 } // for
381 fputs("\"", gOutFile);
382}
383
384/*
385 * Dumps a string as an XML attribute value.
386 */
387static void dumpXmlAttribute(const char* p) {
388 for (; *p; p++) {
389 switch (*p) {
390 case '&':
391 fputs("&amp;", gOutFile);
392 break;
393 case '<':
394 fputs("&lt;", gOutFile);
395 break;
396 case '>':
397 fputs("&gt;", gOutFile);
398 break;
399 case '"':
400 fputs("&quot;", gOutFile);
401 break;
402 case '\t':
403 fputs("&#x9;", gOutFile);
404 break;
405 case '\n':
406 fputs("&#xA;", gOutFile);
407 break;
408 case '\r':
409 fputs("&#xD;", gOutFile);
410 break;
411 default:
412 putc(*p, gOutFile);
413 } // switch
414 } // for
415}
416
417/*
418 * Reads variable width value, possibly sign extended at the last defined byte.
419 */
420static u8 readVarWidth(const u1** data, u1 arg, bool sign_extend) {
421 u8 value = 0;
422 for (u4 i = 0; i <= arg; i++) {
423 value |= static_cast<u8>(*(*data)++) << (i * 8);
424 }
425 if (sign_extend) {
426 int shift = (7 - arg) * 8;
427 return (static_cast<s8>(value) << shift) >> shift;
428 }
429 return value;
430}
431
432/*
433 * Dumps encoded value.
434 */
435static void dumpEncodedValue(const DexFile* pDexFile, const u1** data); // forward
436static void dumpEncodedValue(const DexFile* pDexFile, const u1** data, u1 type, u1 arg) {
437 switch (type) {
438 case DexFile::kDexAnnotationByte:
439 fprintf(gOutFile, "%" PRId8, static_cast<s1>(readVarWidth(data, arg, false)));
440 break;
441 case DexFile::kDexAnnotationShort:
442 fprintf(gOutFile, "%" PRId16, static_cast<s2>(readVarWidth(data, arg, true)));
443 break;
444 case DexFile::kDexAnnotationChar:
445 fprintf(gOutFile, "%" PRIu16, static_cast<u2>(readVarWidth(data, arg, false)));
446 break;
447 case DexFile::kDexAnnotationInt:
448 fprintf(gOutFile, "%" PRId32, static_cast<s4>(readVarWidth(data, arg, true)));
449 break;
450 case DexFile::kDexAnnotationLong:
451 fprintf(gOutFile, "%" PRId64, static_cast<s8>(readVarWidth(data, arg, true)));
452 break;
453 case DexFile::kDexAnnotationFloat: {
454 // Fill on right.
455 union {
456 float f;
457 u4 data;
458 } conv;
459 conv.data = static_cast<u4>(readVarWidth(data, arg, false)) << (3 - arg) * 8;
460 fprintf(gOutFile, "%g", conv.f);
461 break;
462 }
463 case DexFile::kDexAnnotationDouble: {
464 // Fill on right.
465 union {
466 double d;
467 u8 data;
468 } conv;
469 conv.data = readVarWidth(data, arg, false) << (7 - arg) * 8;
470 fprintf(gOutFile, "%g", conv.d);
471 break;
472 }
473 case DexFile::kDexAnnotationString: {
474 const u4 idx = static_cast<u4>(readVarWidth(data, arg, false));
475 if (gOptions.outputFormat == OUTPUT_PLAIN) {
476 dumpEscapedString(pDexFile->StringDataByIdx(idx));
477 } else {
478 dumpXmlAttribute(pDexFile->StringDataByIdx(idx));
479 }
480 break;
481 }
482 case DexFile::kDexAnnotationType: {
483 const u4 str_idx = static_cast<u4>(readVarWidth(data, arg, false));
484 fputs(pDexFile->StringByTypeIdx(str_idx), gOutFile);
485 break;
486 }
487 case DexFile::kDexAnnotationField:
488 case DexFile::kDexAnnotationEnum: {
489 const u4 field_idx = static_cast<u4>(readVarWidth(data, arg, false));
490 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
491 fputs(pDexFile->StringDataByIdx(pFieldId.name_idx_), gOutFile);
492 break;
493 }
494 case DexFile::kDexAnnotationMethod: {
495 const u4 method_idx = static_cast<u4>(readVarWidth(data, arg, false));
496 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
497 fputs(pDexFile->StringDataByIdx(pMethodId.name_idx_), gOutFile);
498 break;
499 }
500 case DexFile::kDexAnnotationArray: {
501 fputc('{', gOutFile);
502 // Decode and display all elements.
503 const u4 size = DecodeUnsignedLeb128(data);
504 for (u4 i = 0; i < size; i++) {
505 fputc(' ', gOutFile);
506 dumpEncodedValue(pDexFile, data);
507 }
508 fputs(" }", gOutFile);
509 break;
510 }
511 case DexFile::kDexAnnotationAnnotation: {
512 const u4 type_idx = DecodeUnsignedLeb128(data);
513 fputs(pDexFile->StringByTypeIdx(type_idx), gOutFile);
514 // Decode and display all name=value pairs.
515 const u4 size = DecodeUnsignedLeb128(data);
516 for (u4 i = 0; i < size; i++) {
517 const u4 name_idx = DecodeUnsignedLeb128(data);
518 fputc(' ', gOutFile);
519 fputs(pDexFile->StringDataByIdx(name_idx), gOutFile);
520 fputc('=', gOutFile);
521 dumpEncodedValue(pDexFile, data);
522 }
523 break;
524 }
525 case DexFile::kDexAnnotationNull:
526 fputs("null", gOutFile);
527 break;
528 case DexFile::kDexAnnotationBoolean:
529 fputs(strBool(arg), gOutFile);
530 break;
531 default:
532 fputs("????", gOutFile);
533 break;
534 } // switch
535}
536
537/*
538 * Dumps encoded value with prefix.
539 */
540static void dumpEncodedValue(const DexFile* pDexFile, const u1** data) {
541 const u1 enc = *(*data)++;
542 dumpEncodedValue(pDexFile, data, enc & 0x1f, enc >> 5);
543}
544
545/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700546 * Dumps the file header.
Aart Bik69ae54a2015-07-01 14:52:26 -0700547 */
548static void dumpFileHeader(const DexFile* pDexFile) {
549 const DexFile::Header& pHeader = pDexFile->GetHeader();
550 char sanitized[sizeof(pHeader.magic_) * 2 + 1];
551 fprintf(gOutFile, "DEX file header:\n");
552 asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
553 fprintf(gOutFile, "magic : '%s'\n", sanitized);
554 fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
555 fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
556 pHeader.signature_[0], pHeader.signature_[1],
557 pHeader.signature_[DexFile::kSha1DigestSize - 2],
558 pHeader.signature_[DexFile::kSha1DigestSize - 1]);
559 fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
560 fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
561 fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
562 fprintf(gOutFile, "link_off : %d (0x%06x)\n",
563 pHeader.link_off_, pHeader.link_off_);
564 fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
565 fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
566 pHeader.string_ids_off_, pHeader.string_ids_off_);
567 fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
568 fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
569 pHeader.type_ids_off_, pHeader.type_ids_off_);
Aart Bikdce50862016-06-10 16:04:03 -0700570 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
571 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
Aart Bik69ae54a2015-07-01 14:52:26 -0700572 pHeader.proto_ids_off_, pHeader.proto_ids_off_);
573 fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
574 fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
575 pHeader.field_ids_off_, pHeader.field_ids_off_);
576 fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
577 fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
578 pHeader.method_ids_off_, pHeader.method_ids_off_);
579 fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
580 fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
581 pHeader.class_defs_off_, pHeader.class_defs_off_);
582 fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
583 fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
584 pHeader.data_off_, pHeader.data_off_);
585}
586
587/*
588 * Dumps a class_def_item.
589 */
590static void dumpClassDef(const DexFile* pDexFile, int idx) {
591 // General class information.
592 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
593 fprintf(gOutFile, "Class #%d header:\n", idx);
594 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_);
595 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
596 pClassDef.access_flags_, pClassDef.access_flags_);
597 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_);
598 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
599 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
600 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_);
601 fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
602 pClassDef.annotations_off_, pClassDef.annotations_off_);
603 fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
604 pClassDef.class_data_off_, pClassDef.class_data_off_);
605
606 // Fields and methods.
607 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
608 if (pEncodedData != nullptr) {
609 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
610 fprintf(gOutFile, "static_fields_size : %d\n", pClassData.NumStaticFields());
611 fprintf(gOutFile, "instance_fields_size: %d\n", pClassData.NumInstanceFields());
612 fprintf(gOutFile, "direct_methods_size : %d\n", pClassData.NumDirectMethods());
613 fprintf(gOutFile, "virtual_methods_size: %d\n", pClassData.NumVirtualMethods());
614 } else {
615 fprintf(gOutFile, "static_fields_size : 0\n");
616 fprintf(gOutFile, "instance_fields_size: 0\n");
617 fprintf(gOutFile, "direct_methods_size : 0\n");
618 fprintf(gOutFile, "virtual_methods_size: 0\n");
619 }
620 fprintf(gOutFile, "\n");
621}
622
Aart Bikdce50862016-06-10 16:04:03 -0700623/**
624 * Dumps an annotation set item.
625 */
626static void dumpAnnotationSetItem(const DexFile* pDexFile, const DexFile::AnnotationSetItem* set_item) {
627 if (set_item == nullptr || set_item->size_ == 0) {
628 fputs(" empty-annotation-set\n", gOutFile);
629 return;
630 }
631 for (u4 i = 0; i < set_item->size_; i++) {
632 const DexFile::AnnotationItem* annotation = pDexFile->GetAnnotationItem(set_item, i);
633 if (annotation == nullptr) {
634 continue;
635 }
636 fputs(" ", gOutFile);
637 switch (annotation->visibility_) {
638 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", gOutFile); break;
639 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", gOutFile); break;
640 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", gOutFile); break;
641 default: fputs("VISIBILITY_UNKNOWN ", gOutFile); break;
642 } // switch
643 // Decode raw bytes in annotation.
644 const u1* rData = annotation->annotation_;
645 dumpEncodedValue(pDexFile, &rData, DexFile::kDexAnnotationAnnotation, 0);
646 fputc('\n', gOutFile);
647 }
648}
649
650/*
651 * Dumps class annotations.
652 */
653static void dumpClassAnnotations(const DexFile* pDexFile, int idx) {
654 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
655 const DexFile::AnnotationsDirectoryItem* dir = pDexFile->GetAnnotationsDirectory(pClassDef);
656 if (dir == nullptr) {
657 return; // none
658 }
659
660 fprintf(gOutFile, "Class #%d annotations:\n", idx);
661
662 const DexFile::AnnotationSetItem* class_set_item = pDexFile->GetClassAnnotationSet(dir);
663 const DexFile::FieldAnnotationsItem* fields = pDexFile->GetFieldAnnotations(dir);
664 const DexFile::MethodAnnotationsItem* methods = pDexFile->GetMethodAnnotations(dir);
665 const DexFile::ParameterAnnotationsItem* pars = pDexFile->GetParameterAnnotations(dir);
666
667 // Annotations on the class itself.
668 if (class_set_item != nullptr) {
669 fprintf(gOutFile, "Annotations on class\n");
670 dumpAnnotationSetItem(pDexFile, class_set_item);
671 }
672
673 // Annotations on fields.
674 if (fields != nullptr) {
675 for (u4 i = 0; i < dir->fields_size_; i++) {
676 const u4 field_idx = fields[i].field_idx_;
677 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
678 const char* field_name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
679 fprintf(gOutFile, "Annotations on field #%u '%s'\n", field_idx, field_name);
680 dumpAnnotationSetItem(pDexFile, pDexFile->GetFieldAnnotationSetItem(fields[i]));
681 }
682 }
683
684 // Annotations on methods.
685 if (methods != nullptr) {
686 for (u4 i = 0; i < dir->methods_size_; i++) {
687 const u4 method_idx = methods[i].method_idx_;
688 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
689 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
690 fprintf(gOutFile, "Annotations on method #%u '%s'\n", method_idx, method_name);
691 dumpAnnotationSetItem(pDexFile, pDexFile->GetMethodAnnotationSetItem(methods[i]));
692 }
693 }
694
695 // Annotations on method parameters.
696 if (pars != nullptr) {
697 for (u4 i = 0; i < dir->parameters_size_; i++) {
698 const u4 method_idx = pars[i].method_idx_;
699 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
700 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
701 fprintf(gOutFile, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
702 const DexFile::AnnotationSetRefList*
703 list = pDexFile->GetParameterAnnotationSetRefList(&pars[i]);
704 if (list != nullptr) {
705 for (u4 j = 0; j < list->size_; j++) {
706 fprintf(gOutFile, "#%u\n", j);
707 dumpAnnotationSetItem(pDexFile, pDexFile->GetSetRefItemItem(&list->list_[j]));
708 }
709 }
710 }
711 }
712
713 fputc('\n', gOutFile);
714}
715
Aart Bik69ae54a2015-07-01 14:52:26 -0700716/*
717 * Dumps an interface that a class declares to implement.
718 */
719static void dumpInterface(const DexFile* pDexFile, const DexFile::TypeItem& pTypeItem, int i) {
720 const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
721 if (gOptions.outputFormat == OUTPUT_PLAIN) {
722 fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
723 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -0700724 std::unique_ptr<char[]> dot(descriptorToDot(interfaceName));
725 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -0700726 }
727}
728
729/*
730 * Dumps the catches table associated with the code.
731 */
732static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
733 const u4 triesSize = pCode->tries_size_;
734
735 // No catch table.
736 if (triesSize == 0) {
737 fprintf(gOutFile, " catches : (none)\n");
738 return;
739 }
740
741 // Dump all table entries.
742 fprintf(gOutFile, " catches : %d\n", triesSize);
743 for (u4 i = 0; i < triesSize; i++) {
744 const DexFile::TryItem* pTry = pDexFile->GetTryItems(*pCode, i);
745 const u4 start = pTry->start_addr_;
746 const u4 end = start + pTry->insn_count_;
747 fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
748 for (CatchHandlerIterator it(*pCode, *pTry); it.HasNext(); it.Next()) {
749 const u2 tidx = it.GetHandlerTypeIndex();
750 const char* descriptor =
751 (tidx == DexFile::kDexNoIndex16) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
752 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
753 } // for
754 } // for
755}
756
757/*
758 * Callback for dumping each positions table entry.
759 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000760static bool dumpPositionsCb(void* /*context*/, const DexFile::PositionInfo& entry) {
761 fprintf(gOutFile, " 0x%04x line=%d\n", entry.address_, entry.line_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700762 return false;
763}
764
765/*
766 * Callback for dumping locals table entry.
767 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000768static void dumpLocalsCb(void* /*context*/, const DexFile::LocalInfo& entry) {
769 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
Aart Bik69ae54a2015-07-01 14:52:26 -0700770 fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
David Srbeckyb06e28e2015-12-10 13:15:00 +0000771 entry.start_address_, entry.end_address_, entry.reg_,
772 entry.name_, entry.descriptor_, signature);
Aart Bik69ae54a2015-07-01 14:52:26 -0700773}
774
775/*
776 * Helper for dumpInstruction(), which builds the string
Aart Bika0e33fd2016-07-08 18:32:45 -0700777 * representation for the index in the given instruction.
778 * Returns a pointer to a buffer of sufficient size.
Aart Bik69ae54a2015-07-01 14:52:26 -0700779 */
Aart Bika0e33fd2016-07-08 18:32:45 -0700780static std::unique_ptr<char[]> indexString(const DexFile* pDexFile,
781 const Instruction* pDecInsn,
782 size_t bufSize) {
783 std::unique_ptr<char[]> buf(new char[bufSize]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700784 // Determine index and width of the string.
785 u4 index = 0;
786 u4 width = 4;
787 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
788 // SOME NOT SUPPORTED:
789 // case Instruction::k20bc:
790 case Instruction::k21c:
791 case Instruction::k35c:
792 // case Instruction::k35ms:
793 case Instruction::k3rc:
794 // case Instruction::k3rms:
795 // case Instruction::k35mi:
796 // case Instruction::k3rmi:
797 index = pDecInsn->VRegB();
798 width = 4;
799 break;
800 case Instruction::k31c:
801 index = pDecInsn->VRegB();
802 width = 8;
803 break;
804 case Instruction::k22c:
805 // case Instruction::k22cs:
806 index = pDecInsn->VRegC();
807 width = 4;
808 break;
809 default:
810 break;
811 } // switch
812
813 // Determine index type.
814 size_t outSize = 0;
815 switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
816 case Instruction::kIndexUnknown:
817 // This function should never get called for this type, but do
818 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700819 outSize = snprintf(buf.get(), bufSize, "<unknown-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700820 break;
821 case Instruction::kIndexNone:
822 // This function should never get called for this type, but do
823 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700824 outSize = snprintf(buf.get(), bufSize, "<no-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700825 break;
826 case Instruction::kIndexTypeRef:
827 if (index < pDexFile->GetHeader().type_ids_size_) {
828 const char* tp = pDexFile->StringByTypeIdx(index);
Aart Bika0e33fd2016-07-08 18:32:45 -0700829 outSize = snprintf(buf.get(), bufSize, "%s // type@%0*x", tp, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700830 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700831 outSize = snprintf(buf.get(), bufSize, "<type?> // type@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700832 }
833 break;
834 case Instruction::kIndexStringRef:
835 if (index < pDexFile->GetHeader().string_ids_size_) {
836 const char* st = pDexFile->StringDataByIdx(index);
Aart Bika0e33fd2016-07-08 18:32:45 -0700837 outSize = snprintf(buf.get(), bufSize, "\"%s\" // string@%0*x", st, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700838 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700839 outSize = snprintf(buf.get(), bufSize, "<string?> // string@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700840 }
841 break;
842 case Instruction::kIndexMethodRef:
843 if (index < pDexFile->GetHeader().method_ids_size_) {
844 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
845 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
846 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
847 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700848 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // method@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700849 backDescriptor, name, signature.ToString().c_str(), width, index);
850 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700851 outSize = snprintf(buf.get(), bufSize, "<method?> // method@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700852 }
853 break;
854 case Instruction::kIndexFieldRef:
855 if (index < pDexFile->GetHeader().field_ids_size_) {
856 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index);
857 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
858 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
859 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700860 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // field@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700861 backDescriptor, name, typeDescriptor, width, index);
862 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700863 outSize = snprintf(buf.get(), bufSize, "<field?> // field@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700864 }
865 break;
866 case Instruction::kIndexVtableOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700867 outSize = snprintf(buf.get(), bufSize, "[%0*x] // vtable #%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700868 width, index, width, index);
869 break;
870 case Instruction::kIndexFieldOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700871 outSize = snprintf(buf.get(), bufSize, "[obj+%0*x]", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700872 break;
873 // SOME NOT SUPPORTED:
874 // case Instruction::kIndexVaries:
875 // case Instruction::kIndexInlineMethod:
876 default:
Aart Bika0e33fd2016-07-08 18:32:45 -0700877 outSize = snprintf(buf.get(), bufSize, "<?>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700878 break;
879 } // switch
880
881 // Determine success of string construction.
882 if (outSize >= bufSize) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700883 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
884 // doesn't count/ the '\0' as part of its returned size, so we add explicit
885 // space for it here.
886 return indexString(pDexFile, pDecInsn, outSize + 1);
Aart Bik69ae54a2015-07-01 14:52:26 -0700887 }
888 return buf;
889}
890
891/*
892 * Dumps a single instruction.
893 */
894static void dumpInstruction(const DexFile* pDexFile,
895 const DexFile::CodeItem* pCode,
896 u4 codeOffset, u4 insnIdx, u4 insnWidth,
897 const Instruction* pDecInsn) {
898 // Address of instruction (expressed as byte offset).
899 fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
900
901 // Dump (part of) raw bytes.
902 const u2* insns = pCode->insns_;
903 for (u4 i = 0; i < 8; i++) {
904 if (i < insnWidth) {
905 if (i == 7) {
906 fprintf(gOutFile, " ... ");
907 } else {
908 // Print 16-bit value in little-endian order.
909 const u1* bytePtr = (const u1*) &insns[insnIdx + i];
910 fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
911 }
912 } else {
913 fputs(" ", gOutFile);
914 }
915 } // for
916
917 // Dump pseudo-instruction or opcode.
918 if (pDecInsn->Opcode() == Instruction::NOP) {
919 const u2 instr = get2LE((const u1*) &insns[insnIdx]);
920 if (instr == Instruction::kPackedSwitchSignature) {
921 fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
922 } else if (instr == Instruction::kSparseSwitchSignature) {
923 fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
924 } else if (instr == Instruction::kArrayDataSignature) {
925 fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
926 } else {
927 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
928 }
929 } else {
930 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
931 }
932
933 // Set up additional argument.
Aart Bika0e33fd2016-07-08 18:32:45 -0700934 std::unique_ptr<char[]> indexBuf;
Aart Bik69ae54a2015-07-01 14:52:26 -0700935 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700936 indexBuf = indexString(pDexFile, pDecInsn, 200);
Aart Bik69ae54a2015-07-01 14:52:26 -0700937 }
938
939 // Dump the instruction.
940 //
941 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
942 //
943 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
944 case Instruction::k10x: // op
945 break;
946 case Instruction::k12x: // op vA, vB
947 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
948 break;
949 case Instruction::k11n: // op vA, #+B
950 fprintf(gOutFile, " v%d, #int %d // #%x",
951 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
952 break;
953 case Instruction::k11x: // op vAA
954 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
955 break;
956 case Instruction::k10t: // op +AA
Aart Bikdce50862016-06-10 16:04:03 -0700957 case Instruction::k20t: { // op +AAAA
958 const s4 targ = (s4) pDecInsn->VRegA();
959 fprintf(gOutFile, " %04x // %c%04x",
960 insnIdx + targ,
961 (targ < 0) ? '-' : '+',
962 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -0700963 break;
Aart Bikdce50862016-06-10 16:04:03 -0700964 }
Aart Bik69ae54a2015-07-01 14:52:26 -0700965 case Instruction::k22x: // op vAA, vBBBB
966 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
967 break;
Aart Bikdce50862016-06-10 16:04:03 -0700968 case Instruction::k21t: { // op vAA, +BBBB
969 const s4 targ = (s4) pDecInsn->VRegB();
970 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
971 insnIdx + targ,
972 (targ < 0) ? '-' : '+',
973 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -0700974 break;
Aart Bikdce50862016-06-10 16:04:03 -0700975 }
Aart Bik69ae54a2015-07-01 14:52:26 -0700976 case Instruction::k21s: // op vAA, #+BBBB
977 fprintf(gOutFile, " v%d, #int %d // #%x",
978 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
979 break;
980 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
981 // The printed format varies a bit based on the actual opcode.
982 if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) {
983 const s4 value = pDecInsn->VRegB() << 16;
984 fprintf(gOutFile, " v%d, #int %d // #%x",
985 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
986 } else {
987 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
988 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
989 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
990 }
991 break;
992 case Instruction::k21c: // op vAA, thing@BBBB
993 case Instruction::k31c: // op vAA, thing@BBBBBBBB
Aart Bika0e33fd2016-07-08 18:32:45 -0700994 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -0700995 break;
996 case Instruction::k23x: // op vAA, vBB, vCC
997 fprintf(gOutFile, " v%d, v%d, v%d",
998 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
999 break;
1000 case Instruction::k22b: // op vAA, vBB, #+CC
1001 fprintf(gOutFile, " v%d, v%d, #int %d // #%02x",
1002 pDecInsn->VRegA(), pDecInsn->VRegB(),
1003 (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC());
1004 break;
Aart Bikdce50862016-06-10 16:04:03 -07001005 case Instruction::k22t: { // op vA, vB, +CCCC
1006 const s4 targ = (s4) pDecInsn->VRegC();
1007 fprintf(gOutFile, " v%d, v%d, %04x // %c%04x",
1008 pDecInsn->VRegA(), pDecInsn->VRegB(),
1009 insnIdx + targ,
1010 (targ < 0) ? '-' : '+',
1011 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001012 break;
Aart Bikdce50862016-06-10 16:04:03 -07001013 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001014 case Instruction::k22s: // op vA, vB, #+CCCC
1015 fprintf(gOutFile, " v%d, v%d, #int %d // #%04x",
1016 pDecInsn->VRegA(), pDecInsn->VRegB(),
1017 (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC());
1018 break;
1019 case Instruction::k22c: // op vA, vB, thing@CCCC
1020 // NOT SUPPORTED:
1021 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
1022 fprintf(gOutFile, " v%d, v%d, %s",
Aart Bika0e33fd2016-07-08 18:32:45 -07001023 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001024 break;
1025 case Instruction::k30t:
1026 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
1027 break;
Aart Bikdce50862016-06-10 16:04:03 -07001028 case Instruction::k31i: { // op vAA, #+BBBBBBBB
1029 // This is often, but not always, a float.
1030 union {
1031 float f;
1032 u4 i;
1033 } conv;
1034 conv.i = pDecInsn->VRegB();
1035 fprintf(gOutFile, " v%d, #float %g // #%08x",
1036 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001037 break;
Aart Bikdce50862016-06-10 16:04:03 -07001038 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001039 case Instruction::k31t: // op vAA, offset +BBBBBBBB
1040 fprintf(gOutFile, " v%d, %08x // +%08x",
1041 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
1042 break;
1043 case Instruction::k32x: // op vAAAA, vBBBB
1044 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1045 break;
Aart Bikdce50862016-06-10 16:04:03 -07001046 case Instruction::k35c: { // op {vC, vD, vE, vF, vG}, thing@BBBB
Aart Bik69ae54a2015-07-01 14:52:26 -07001047 // NOT SUPPORTED:
1048 // case Instruction::k35ms: // [opt] invoke-virtual+super
1049 // case Instruction::k35mi: // [opt] inline invoke
Aart Bikdce50862016-06-10 16:04:03 -07001050 u4 arg[Instruction::kMaxVarArgRegs];
1051 pDecInsn->GetVarArgs(arg);
1052 fputs(" {", gOutFile);
1053 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1054 if (i == 0) {
1055 fprintf(gOutFile, "v%d", arg[i]);
1056 } else {
1057 fprintf(gOutFile, ", v%d", arg[i]);
1058 }
1059 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001060 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001061 break;
Aart Bikdce50862016-06-10 16:04:03 -07001062 }
1063 case Instruction::k25x: { // op vC, {vD, vE, vF, vG} (B: count)
1064 u4 arg[Instruction::kMaxVarArgRegs25x];
1065 pDecInsn->GetAllArgs25x(arg);
1066 fprintf(gOutFile, " v%d, {", arg[0]);
1067 for (int i = 0, n = pDecInsn->VRegB(); i < n; i++) {
1068 if (i == 0) {
1069 fprintf(gOutFile, "v%d", arg[Instruction::kLambdaVirtualRegisterWidth + i]);
1070 } else {
1071 fprintf(gOutFile, ", v%d", arg[Instruction::kLambdaVirtualRegisterWidth + i]);
1072 }
1073 } // for
1074 fputc('}', gOutFile);
Aart Bika3bb7202015-10-26 17:24:09 -07001075 break;
Aart Bikdce50862016-06-10 16:04:03 -07001076 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001077 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
1078 // NOT SUPPORTED:
1079 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1080 // case Instruction::k3rmi: // [opt] execute-inline/range
1081 {
1082 // This doesn't match the "dx" output when some of the args are
1083 // 64-bit values -- dx only shows the first register.
1084 fputs(" {", gOutFile);
1085 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1086 if (i == 0) {
1087 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
1088 } else {
1089 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
1090 }
1091 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001092 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001093 }
1094 break;
Aart Bikdce50862016-06-10 16:04:03 -07001095 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1096 // This is often, but not always, a double.
1097 union {
1098 double d;
1099 u8 j;
1100 } conv;
1101 conv.j = pDecInsn->WideVRegB();
1102 fprintf(gOutFile, " v%d, #double %g // #%016" PRIx64,
1103 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001104 break;
Aart Bikdce50862016-06-10 16:04:03 -07001105 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001106 // NOT SUPPORTED:
1107 // case Instruction::k00x: // unknown op or breakpoint
1108 // break;
1109 default:
1110 fprintf(gOutFile, " ???");
1111 break;
1112 } // switch
1113
1114 fputc('\n', gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001115}
1116
1117/*
1118 * Dumps a bytecode disassembly.
1119 */
1120static void dumpBytecodes(const DexFile* pDexFile, u4 idx,
1121 const DexFile::CodeItem* pCode, u4 codeOffset) {
1122 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1123 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1124 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1125 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1126
1127 // Generate header.
Aart Bikc05e2f22016-07-12 15:53:13 -07001128 std::unique_ptr<char[]> dot(descriptorToDot(backDescriptor));
1129 fprintf(gOutFile, "%06x: |[%06x] %s.%s:%s\n",
1130 codeOffset, codeOffset, dot.get(), name, signature.ToString().c_str());
Aart Bik69ae54a2015-07-01 14:52:26 -07001131
1132 // Iterate over all instructions.
1133 const u2* insns = pCode->insns_;
1134 for (u4 insnIdx = 0; insnIdx < pCode->insns_size_in_code_units_;) {
1135 const Instruction* instruction = Instruction::At(&insns[insnIdx]);
1136 const u4 insnWidth = instruction->SizeInCodeUnits();
1137 if (insnWidth == 0) {
1138 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx);
1139 break;
1140 }
1141 dumpInstruction(pDexFile, pCode, codeOffset, insnIdx, insnWidth, instruction);
1142 insnIdx += insnWidth;
1143 } // for
1144}
1145
1146/*
1147 * Dumps code of a method.
1148 */
1149static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags,
1150 const DexFile::CodeItem* pCode, u4 codeOffset) {
1151 fprintf(gOutFile, " registers : %d\n", pCode->registers_size_);
1152 fprintf(gOutFile, " ins : %d\n", pCode->ins_size_);
1153 fprintf(gOutFile, " outs : %d\n", pCode->outs_size_);
1154 fprintf(gOutFile, " insns size : %d 16-bit code units\n",
1155 pCode->insns_size_in_code_units_);
1156
1157 // Bytecode disassembly, if requested.
1158 if (gOptions.disassemble) {
1159 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
1160 }
1161
1162 // Try-catch blocks.
1163 dumpCatches(pDexFile, pCode);
1164
1165 // Positions and locals table in the debug info.
1166 bool is_static = (flags & kAccStatic) != 0;
1167 fprintf(gOutFile, " positions : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +00001168 pDexFile->DecodeDebugPositionInfo(pCode, dumpPositionsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001169 fprintf(gOutFile, " locals : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +00001170 pDexFile->DecodeDebugLocalInfo(pCode, is_static, idx, dumpLocalsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001171}
1172
1173/*
1174 * Dumps a method.
1175 */
1176static void dumpMethod(const DexFile* pDexFile, u4 idx, u4 flags,
1177 const DexFile::CodeItem* pCode, u4 codeOffset, int i) {
1178 // Bail for anything private if export only requested.
1179 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1180 return;
1181 }
1182
1183 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1184 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1185 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1186 char* typeDescriptor = strdup(signature.ToString().c_str());
1187 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1188 char* accessStr = createAccessFlagStr(flags, kAccessForMethod);
1189
1190 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1191 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1192 fprintf(gOutFile, " name : '%s'\n", name);
1193 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1194 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
1195 if (pCode == nullptr) {
1196 fprintf(gOutFile, " code : (none)\n");
1197 } else {
1198 fprintf(gOutFile, " code -\n");
1199 dumpCode(pDexFile, idx, flags, pCode, codeOffset);
1200 }
1201 if (gOptions.disassemble) {
1202 fputc('\n', gOutFile);
1203 }
1204 } else if (gOptions.outputFormat == OUTPUT_XML) {
1205 const bool constructor = (name[0] == '<');
1206
1207 // Method name and prototype.
1208 if (constructor) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001209 std::unique_ptr<char[]> dot(descriptorClassToDot(backDescriptor));
1210 fprintf(gOutFile, "<constructor name=\"%s\"\n", dot.get());
1211 dot = descriptorToDot(backDescriptor);
1212 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001213 } else {
1214 fprintf(gOutFile, "<method name=\"%s\"\n", name);
1215 const char* returnType = strrchr(typeDescriptor, ')');
1216 if (returnType == nullptr) {
1217 fprintf(stderr, "bad method type descriptor '%s'\n", typeDescriptor);
1218 goto bail;
1219 }
Aart Bikc05e2f22016-07-12 15:53:13 -07001220 std::unique_ptr<char[]> dot(descriptorToDot(returnType + 1));
1221 fprintf(gOutFile, " return=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001222 fprintf(gOutFile, " abstract=%s\n", quotedBool((flags & kAccAbstract) != 0));
1223 fprintf(gOutFile, " native=%s\n", quotedBool((flags & kAccNative) != 0));
1224 fprintf(gOutFile, " synchronized=%s\n", quotedBool(
1225 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1226 }
1227
1228 // Additional method flags.
1229 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1230 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1231 // The "deprecated=" not knowable w/o parsing annotations.
1232 fprintf(gOutFile, " visibility=%s\n>\n", quotedVisibility(flags));
1233
1234 // Parameters.
1235 if (typeDescriptor[0] != '(') {
1236 fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor);
1237 goto bail;
1238 }
1239 char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1));
1240 const char* base = typeDescriptor + 1;
1241 int argNum = 0;
1242 while (*base != ')') {
1243 char* cp = tmpBuf;
1244 while (*base == '[') {
1245 *cp++ = *base++;
1246 }
1247 if (*base == 'L') {
1248 // Copy through ';'.
1249 do {
1250 *cp = *base++;
1251 } while (*cp++ != ';');
1252 } else {
1253 // Primitive char, copy it.
Aart Bikc05e2f22016-07-12 15:53:13 -07001254 if (strchr("ZBCSIFJD", *base) == nullptr) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001255 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
Aart Bika0e33fd2016-07-08 18:32:45 -07001256 break; // while
Aart Bik69ae54a2015-07-01 14:52:26 -07001257 }
1258 *cp++ = *base++;
1259 }
1260 // Null terminate and display.
1261 *cp++ = '\0';
Aart Bikc05e2f22016-07-12 15:53:13 -07001262 std::unique_ptr<char[]> dot(descriptorToDot(tmpBuf));
Aart Bik69ae54a2015-07-01 14:52:26 -07001263 fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n"
Aart Bikc05e2f22016-07-12 15:53:13 -07001264 "</parameter>\n", argNum++, dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001265 } // while
1266 free(tmpBuf);
1267 if (constructor) {
1268 fprintf(gOutFile, "</constructor>\n");
1269 } else {
1270 fprintf(gOutFile, "</method>\n");
1271 }
1272 }
1273
1274 bail:
1275 free(typeDescriptor);
1276 free(accessStr);
1277}
1278
1279/*
1280 * Dumps a static (class) field.
1281 */
Aart Bikdce50862016-06-10 16:04:03 -07001282static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i, const u1** data) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001283 // Bail for anything private if export only requested.
1284 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1285 return;
1286 }
1287
1288 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(idx);
1289 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
1290 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
1291 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
1292 char* accessStr = createAccessFlagStr(flags, kAccessForField);
1293
1294 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1295 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1296 fprintf(gOutFile, " name : '%s'\n", name);
1297 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1298 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
Aart Bikdce50862016-06-10 16:04:03 -07001299 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001300 fputs(" value : ", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001301 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001302 fputs("\n", gOutFile);
1303 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001304 } else if (gOptions.outputFormat == OUTPUT_XML) {
1305 fprintf(gOutFile, "<field name=\"%s\"\n", name);
Aart Bikc05e2f22016-07-12 15:53:13 -07001306 std::unique_ptr<char[]> dot(descriptorToDot(typeDescriptor));
1307 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001308 fprintf(gOutFile, " transient=%s\n", quotedBool((flags & kAccTransient) != 0));
1309 fprintf(gOutFile, " volatile=%s\n", quotedBool((flags & kAccVolatile) != 0));
1310 // The "value=" is not knowable w/o parsing annotations.
1311 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1312 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1313 // The "deprecated=" is not knowable w/o parsing annotations.
1314 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(flags));
Aart Bikdce50862016-06-10 16:04:03 -07001315 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001316 fputs(" value=\"", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001317 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001318 fputs("\"\n", gOutFile);
1319 }
1320 fputs(">\n</field>\n", gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001321 }
1322
1323 free(accessStr);
1324}
1325
1326/*
1327 * Dumps an instance field.
1328 */
1329static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
Aart Bikdce50862016-06-10 16:04:03 -07001330 dumpSField(pDexFile, idx, flags, i, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001331}
1332
1333/*
Andreas Gampe5073fed2015-08-10 11:40:25 -07001334 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1335 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1336 * tool, so this is not performance-critical.
1337 */
1338
1339static void dumpCfg(const DexFile* dex_file,
Aart Bikdce50862016-06-10 16:04:03 -07001340 u4 dex_method_idx,
Andreas Gampe5073fed2015-08-10 11:40:25 -07001341 const DexFile::CodeItem* code_item) {
1342 if (code_item != nullptr) {
1343 std::ostringstream oss;
1344 DumpMethodCFG(dex_file, dex_method_idx, oss);
1345 fprintf(gOutFile, "%s", oss.str().c_str());
1346 }
1347}
1348
1349static void dumpCfg(const DexFile* dex_file, int idx) {
1350 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
Aart Bikdce50862016-06-10 16:04:03 -07001351 const u1* class_data = dex_file->GetClassData(class_def);
Andreas Gampe5073fed2015-08-10 11:40:25 -07001352 if (class_data == nullptr) { // empty class such as a marker interface?
1353 return;
1354 }
1355 ClassDataItemIterator it(*dex_file, class_data);
1356 while (it.HasNextStaticField()) {
1357 it.Next();
1358 }
1359 while (it.HasNextInstanceField()) {
1360 it.Next();
1361 }
1362 while (it.HasNextDirectMethod()) {
1363 dumpCfg(dex_file,
1364 it.GetMemberIndex(),
1365 it.GetMethodCodeItem());
1366 it.Next();
1367 }
1368 while (it.HasNextVirtualMethod()) {
1369 dumpCfg(dex_file,
1370 it.GetMemberIndex(),
1371 it.GetMethodCodeItem());
1372 it.Next();
1373 }
1374}
1375
1376/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001377 * Dumps the class.
1378 *
1379 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1380 *
1381 * If "*pLastPackage" is nullptr or does not match the current class' package,
1382 * the value will be replaced with a newly-allocated string.
1383 */
1384static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1385 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1386
1387 // Omitting non-public class.
1388 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1389 return;
1390 }
1391
Aart Bikdce50862016-06-10 16:04:03 -07001392 if (gOptions.showSectionHeaders) {
1393 dumpClassDef(pDexFile, idx);
1394 }
1395
1396 if (gOptions.showAnnotations) {
1397 dumpClassAnnotations(pDexFile, idx);
1398 }
1399
1400 if (gOptions.showCfg) {
Andreas Gampe5073fed2015-08-10 11:40:25 -07001401 dumpCfg(pDexFile, idx);
1402 return;
1403 }
1404
Aart Bik69ae54a2015-07-01 14:52:26 -07001405 // For the XML output, show the package name. Ideally we'd gather
1406 // up the classes, sort them, and dump them alphabetically so the
1407 // package name wouldn't jump around, but that's not a great plan
1408 // for something that needs to run on the device.
1409 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1410 if (!(classDescriptor[0] == 'L' &&
1411 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1412 // Arrays and primitives should not be defined explicitly. Keep going?
1413 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1414 } else if (gOptions.outputFormat == OUTPUT_XML) {
1415 char* mangle = strdup(classDescriptor + 1);
1416 mangle[strlen(mangle)-1] = '\0';
1417
1418 // Reduce to just the package name.
1419 char* lastSlash = strrchr(mangle, '/');
1420 if (lastSlash != nullptr) {
1421 *lastSlash = '\0';
1422 } else {
1423 *mangle = '\0';
1424 }
1425
1426 for (char* cp = mangle; *cp != '\0'; cp++) {
1427 if (*cp == '/') {
1428 *cp = '.';
1429 }
1430 } // for
1431
1432 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1433 // Start of a new package.
1434 if (*pLastPackage != nullptr) {
1435 fprintf(gOutFile, "</package>\n");
1436 }
1437 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1438 free(*pLastPackage);
1439 *pLastPackage = mangle;
1440 } else {
1441 free(mangle);
1442 }
1443 }
1444
1445 // General class information.
1446 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1447 const char* superclassDescriptor;
1448 if (pClassDef.superclass_idx_ == DexFile::kDexNoIndex16) {
1449 superclassDescriptor = nullptr;
1450 } else {
1451 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1452 }
1453 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1454 fprintf(gOutFile, "Class #%d -\n", idx);
1455 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1456 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1457 if (superclassDescriptor != nullptr) {
1458 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1459 }
1460 fprintf(gOutFile, " Interfaces -\n");
1461 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -07001462 std::unique_ptr<char[]> dot(descriptorClassToDot(classDescriptor));
1463 fprintf(gOutFile, "<class name=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001464 if (superclassDescriptor != nullptr) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001465 dot = descriptorToDot(superclassDescriptor);
1466 fprintf(gOutFile, " extends=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001467 }
Alex Light1f12e282015-12-10 16:49:47 -08001468 fprintf(gOutFile, " interface=%s\n",
1469 quotedBool((pClassDef.access_flags_ & kAccInterface) != 0));
Aart Bik69ae54a2015-07-01 14:52:26 -07001470 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1471 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1472 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1473 // The "deprecated=" not knowable w/o parsing annotations.
1474 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1475 fprintf(gOutFile, ">\n");
1476 }
1477
1478 // Interfaces.
1479 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1480 if (pInterfaces != nullptr) {
1481 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1482 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1483 } // for
1484 }
1485
1486 // Fields and methods.
1487 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
1488 if (pEncodedData == nullptr) {
1489 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1490 fprintf(gOutFile, " Static fields -\n");
1491 fprintf(gOutFile, " Instance fields -\n");
1492 fprintf(gOutFile, " Direct methods -\n");
1493 fprintf(gOutFile, " Virtual methods -\n");
1494 }
1495 } else {
1496 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
Aart Bikdce50862016-06-10 16:04:03 -07001497
1498 // Prepare data for static fields.
1499 const u1* sData = pDexFile->GetEncodedStaticFieldValuesArray(pClassDef);
1500 const u4 sSize = sData != nullptr ? DecodeUnsignedLeb128(&sData) : 0;
1501
1502 // Static fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001503 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1504 fprintf(gOutFile, " Static fields -\n");
1505 }
Aart Bikdce50862016-06-10 16:04:03 -07001506 for (u4 i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) {
1507 dumpSField(pDexFile,
1508 pClassData.GetMemberIndex(),
1509 pClassData.GetRawMemberAccessFlags(),
1510 i,
1511 i < sSize ? &sData : nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001512 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001513
1514 // Instance fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001515 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1516 fprintf(gOutFile, " Instance fields -\n");
1517 }
Aart Bikdce50862016-06-10 16:04:03 -07001518 for (u4 i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) {
1519 dumpIField(pDexFile,
1520 pClassData.GetMemberIndex(),
1521 pClassData.GetRawMemberAccessFlags(),
1522 i);
Aart Bik69ae54a2015-07-01 14:52:26 -07001523 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001524
1525 // Direct methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001526 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1527 fprintf(gOutFile, " Direct methods -\n");
1528 }
1529 for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) {
1530 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1531 pClassData.GetRawMemberAccessFlags(),
1532 pClassData.GetMethodCodeItem(),
1533 pClassData.GetMethodCodeItemOffset(), i);
1534 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001535
1536 // Virtual methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001537 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1538 fprintf(gOutFile, " Virtual methods -\n");
1539 }
1540 for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) {
1541 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1542 pClassData.GetRawMemberAccessFlags(),
1543 pClassData.GetMethodCodeItem(),
1544 pClassData.GetMethodCodeItemOffset(), i);
1545 } // for
1546 }
1547
1548 // End of class.
1549 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1550 const char* fileName;
1551 if (pClassDef.source_file_idx_ != DexFile::kDexNoIndex) {
1552 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1553 } else {
1554 fileName = "unknown";
1555 }
1556 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
1557 pClassDef.source_file_idx_, fileName);
1558 } else if (gOptions.outputFormat == OUTPUT_XML) {
1559 fprintf(gOutFile, "</class>\n");
1560 }
1561
1562 free(accessStr);
1563}
1564
1565/*
1566 * Dumps the requested sections of the file.
1567 */
1568static void processDexFile(const char* fileName, const DexFile* pDexFile) {
1569 if (gOptions.verbose) {
1570 fprintf(gOutFile, "Opened '%s', DEX version '%.3s'\n",
1571 fileName, pDexFile->GetHeader().magic_ + 4);
1572 }
1573
1574 // Headers.
1575 if (gOptions.showFileHeaders) {
1576 dumpFileHeader(pDexFile);
1577 }
1578
1579 // Open XML context.
1580 if (gOptions.outputFormat == OUTPUT_XML) {
1581 fprintf(gOutFile, "<api>\n");
1582 }
1583
1584 // Iterate over all classes.
1585 char* package = nullptr;
1586 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
1587 for (u4 i = 0; i < classDefsSize; i++) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001588 dumpClass(pDexFile, i, &package);
1589 } // for
1590
1591 // Free the last package allocated.
1592 if (package != nullptr) {
1593 fprintf(gOutFile, "</package>\n");
1594 free(package);
1595 }
1596
1597 // Close XML context.
1598 if (gOptions.outputFormat == OUTPUT_XML) {
1599 fprintf(gOutFile, "</api>\n");
1600 }
1601}
1602
1603/*
1604 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1605 */
1606int processFile(const char* fileName) {
1607 if (gOptions.verbose) {
1608 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1609 }
1610
1611 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
Aart Bikdce50862016-06-10 16:04:03 -07001612 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -07001613 const bool kVerifyChecksum = !gOptions.ignoreBadChecksum;
Aart Bik69ae54a2015-07-01 14:52:26 -07001614 std::string error_msg;
1615 std::vector<std::unique_ptr<const DexFile>> dex_files;
Aart Bik37d6a3b2016-06-21 18:30:10 -07001616 if (!DexFile::Open(fileName, fileName, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001617 // Display returned error message to user. Note that this error behavior
1618 // differs from the error messages shown by the original Dalvik dexdump.
1619 fputs(error_msg.c_str(), stderr);
1620 fputc('\n', stderr);
1621 return -1;
1622 }
1623
Aart Bik4e149602015-07-09 11:45:28 -07001624 // Success. Either report checksum verification or process
1625 // all dex files found in given file.
Aart Bik69ae54a2015-07-01 14:52:26 -07001626 if (gOptions.checksumOnly) {
1627 fprintf(gOutFile, "Checksum verified\n");
1628 } else {
Aart Bik4e149602015-07-09 11:45:28 -07001629 for (size_t i = 0; i < dex_files.size(); i++) {
1630 processDexFile(fileName, dex_files[i].get());
1631 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001632 }
1633 return 0;
1634}
1635
1636} // namespace art