blob: 0db790b47ad20e6320666273896af442a5befbfb [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
Andreas Gampe46ee31b2016-12-14 10:11:49 -080045#include "android-base/stringprintf.h"
46
Aart Bik69ae54a2015-07-01 14:52:26 -070047#include "dex_file-inl.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080048#include "dex_file_types.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070049#include "dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070050#include "dexdump_cfg.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070051
52namespace art {
53
54/*
55 * Options parsed in main driver.
56 */
57struct Options gOptions;
58
59/*
Aart Bik4e149602015-07-09 11:45:28 -070060 * Output file. Defaults to stdout.
Aart Bik69ae54a2015-07-01 14:52:26 -070061 */
62FILE* gOutFile = stdout;
63
64/*
65 * Data types that match the definitions in the VM specification.
66 */
67typedef uint8_t u1;
68typedef uint16_t u2;
69typedef uint32_t u4;
70typedef uint64_t u8;
Aart Bikdce50862016-06-10 16:04:03 -070071typedef int8_t s1;
72typedef int16_t s2;
Aart Bik69ae54a2015-07-01 14:52:26 -070073typedef int32_t s4;
74typedef int64_t s8;
75
76/*
77 * Basic information about a field or a method.
78 */
79struct FieldMethodInfo {
80 const char* classDescriptor;
81 const char* name;
82 const char* signature;
83};
84
85/*
86 * Flags for use with createAccessFlagStr().
87 */
88enum AccessFor {
89 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
90};
91const int kNumFlags = 18;
92
93/*
94 * Gets 2 little-endian bytes.
95 */
96static inline u2 get2LE(unsigned char const* pSrc) {
97 return pSrc[0] | (pSrc[1] << 8);
98}
99
100/*
101 * Converts a single-character primitive type into human-readable form.
102 */
103static const char* primitiveTypeLabel(char typeChar) {
104 switch (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";
115 } // switch
116}
117
118/*
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.
123 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700124static std::unique_ptr<char[]> descriptorToDot(const char* str) {
Aart Bik69ae54a2015-07-01 14:52:26 -0700125 int targetLen = strlen(str);
126 int offset = 0;
127
128 // Strip leading [s; will be added to end.
129 while (targetLen > 1 && str[offset] == '[') {
130 offset++;
131 targetLen--;
132 } // while
133
134 const int arrayDepth = offset;
135
136 if (targetLen == 1) {
137 // Primitive type.
138 str = primitiveTypeLabel(str[offset]);
139 offset = 0;
140 targetLen = strlen(str);
141 } else {
142 // Account for leading 'L' and trailing ';'.
143 if (targetLen >= 2 && str[offset] == 'L' &&
144 str[offset + targetLen - 1] == ';') {
145 targetLen -= 2;
146 offset++;
147 }
148 }
149
150 // Copy class name over.
Aart Bikc05e2f22016-07-12 15:53:13 -0700151 std::unique_ptr<char[]> newStr(new char[targetLen + arrayDepth * 2 + 1]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700152 int i = 0;
153 for (; i < targetLen; i++) {
154 const char ch = str[offset + i];
155 newStr[i] = (ch == '/' || ch == '$') ? '.' : ch;
156 } // for
157
158 // Add the appropriate number of brackets for arrays.
159 for (int j = 0; j < arrayDepth; j++) {
160 newStr[i++] = '[';
161 newStr[i++] = ']';
162 } // for
163
164 newStr[i] = '\0';
165 return newStr;
166}
167
168/*
169 * Converts the class name portion of a type descriptor to human-readable
Aart Bikc05e2f22016-07-12 15:53:13 -0700170 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
Aart Bik69ae54a2015-07-01 14:52:26 -0700171 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700172static std::unique_ptr<char[]> descriptorClassToDot(const char* str) {
173 // Reduce to just the class name prefix.
Aart Bik69ae54a2015-07-01 14:52:26 -0700174 const char* lastSlash = strrchr(str, '/');
175 if (lastSlash == nullptr) {
176 lastSlash = str + 1; // start past 'L'
177 } else {
178 lastSlash++; // start past '/'
179 }
180
Aart Bikc05e2f22016-07-12 15:53:13 -0700181 // 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;
Aart Bik69ae54a2015-07-01 14:52:26 -0700187 } // for
Aart Bikc05e2f22016-07-12 15:53:13 -0700188 newStr[targetLen - 1] = '\0';
Aart Bik69ae54a2015-07-01 14:52:26 -0700189 return newStr;
190}
191
192/*
Aart Bikdce50862016-06-10 16:04:03 -0700193 * Returns string representing the boolean value.
194 */
195static const char* strBool(bool val) {
196 return val ? "true" : "false";
197}
198
199/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700200 * Returns a quoted string representing the boolean value.
201 */
202static const char* quotedBool(bool val) {
203 return val ? "\"true\"" : "\"false\"";
204}
205
206/*
207 * Returns a quoted string representing the access flags.
208 */
209static const char* quotedVisibility(u4 accessFlags) {
210 if (accessFlags & kAccPublic) {
211 return "\"public\"";
212 } else if (accessFlags & kAccProtected) {
213 return "\"protected\"";
214 } else if (accessFlags & kAccPrivate) {
215 return "\"private\"";
216 } else {
217 return "\"package\"";
218 }
219}
220
221/*
222 * Counts the number of '1' bits in a word.
223 */
224static 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;
228}
229
230/*
231 * Creates a new string with human-readable access flags.
232 *
233 * In the base language the access_flags fields are type u2; in Dalvik
234 * they're u4.
235 */
236static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
237 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
238 {
239 "PUBLIC", /* 0x00001 */
240 "PRIVATE", /* 0x00002 */
241 "PROTECTED", /* 0x00004 */
242 "STATIC", /* 0x00008 */
243 "FINAL", /* 0x00010 */
244 "?", /* 0x00020 */
245 "?", /* 0x00040 */
246 "?", /* 0x00080 */
247 "?", /* 0x00100 */
248 "INTERFACE", /* 0x00200 */
249 "ABSTRACT", /* 0x00400 */
250 "?", /* 0x00800 */
251 "SYNTHETIC", /* 0x01000 */
252 "ANNOTATION", /* 0x02000 */
253 "ENUM", /* 0x04000 */
254 "?", /* 0x08000 */
255 "VERIFIED", /* 0x10000 */
256 "OPTIMIZED", /* 0x20000 */
257 }, {
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 */
267 "?", /* 0x00200 */
268 "ABSTRACT", /* 0x00400 */
269 "STRICT", /* 0x00800 */
270 "SYNTHETIC", /* 0x01000 */
271 "?", /* 0x02000 */
272 "?", /* 0x04000 */
273 "MIRANDA", /* 0x08000 */
274 "CONSTRUCTOR", /* 0x10000 */
275 "DECLARED_SYNCHRONIZED", /* 0x20000 */
276 }, {
277 "PUBLIC", /* 0x00001 */
278 "PRIVATE", /* 0x00002 */
279 "PROTECTED", /* 0x00004 */
280 "STATIC", /* 0x00008 */
281 "FINAL", /* 0x00010 */
282 "?", /* 0x00020 */
283 "VOLATILE", /* 0x00040 */
284 "TRANSIENT", /* 0x00080 */
285 "?", /* 0x00100 */
286 "?", /* 0x00200 */
287 "?", /* 0x00400 */
288 "?", /* 0x00800 */
289 "SYNTHETIC", /* 0x01000 */
290 "?", /* 0x02000 */
291 "ENUM", /* 0x04000 */
292 "?", /* 0x08000 */
293 "?", /* 0x10000 */
294 "?", /* 0x20000 */
295 },
296 };
297
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);
303 char* str;
304 char* cp;
305 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
306
307 for (int i = 0; i < kNumFlags; i++) {
308 if (flags & 0x01) {
309 const char* accessStr = kAccessStrings[forWhat][i];
310 const int len = strlen(accessStr);
311 if (cp != str) {
312 *cp++ = ' ';
313 }
314 memcpy(cp, accessStr, len);
315 cp += len;
316 }
317 flags >>= 1;
318 } // for
319
320 *cp = '\0';
321 return str;
322}
323
324/*
325 * Copies character data from "data" to "out", converting non-ASCII values
326 * to fprintf format chars or an ASCII filler ('.' or '?').
327 *
328 * The output buffer must be able to hold (2*len)+1 bytes. The result is
329 * NULL-terminated.
330 */
331static void asciify(char* out, const unsigned char* data, size_t len) {
332 while (len--) {
333 if (*data < 0x20) {
334 // Could do more here, but we don't need them yet.
335 switch (*data) {
336 case '\0':
337 *out++ = '\\';
338 *out++ = '0';
339 break;
340 case '\n':
341 *out++ = '\\';
342 *out++ = 'n';
343 break;
344 default:
345 *out++ = '.';
346 break;
347 } // switch
348 } else if (*data >= 0x80) {
349 *out++ = '?';
350 } else {
351 *out++ = *data;
352 }
353 data++;
354 } // while
355 *out = '\0';
356}
357
358/*
Aart Bikdce50862016-06-10 16:04:03 -0700359 * Dumps a string value with some escape characters.
360 */
361static void dumpEscapedString(const char* p) {
362 fputs("\"", gOutFile);
363 for (; *p; p++) {
364 switch (*p) {
365 case '\\':
366 fputs("\\\\", gOutFile);
367 break;
368 case '\"':
369 fputs("\\\"", gOutFile);
370 break;
371 case '\t':
372 fputs("\\t", gOutFile);
373 break;
374 case '\n':
375 fputs("\\n", gOutFile);
376 break;
377 case '\r':
378 fputs("\\r", gOutFile);
379 break;
380 default:
381 putc(*p, gOutFile);
382 } // switch
383 } // for
384 fputs("\"", gOutFile);
385}
386
387/*
388 * Dumps a string as an XML attribute value.
389 */
390static void dumpXmlAttribute(const char* p) {
391 for (; *p; p++) {
392 switch (*p) {
393 case '&':
394 fputs("&amp;", gOutFile);
395 break;
396 case '<':
397 fputs("&lt;", gOutFile);
398 break;
399 case '>':
400 fputs("&gt;", gOutFile);
401 break;
402 case '"':
403 fputs("&quot;", gOutFile);
404 break;
405 case '\t':
406 fputs("&#x9;", gOutFile);
407 break;
408 case '\n':
409 fputs("&#xA;", gOutFile);
410 break;
411 case '\r':
412 fputs("&#xD;", gOutFile);
413 break;
414 default:
415 putc(*p, gOutFile);
416 } // switch
417 } // for
418}
419
420/*
421 * Reads variable width value, possibly sign extended at the last defined byte.
422 */
423static u8 readVarWidth(const u1** data, u1 arg, bool sign_extend) {
424 u8 value = 0;
425 for (u4 i = 0; i <= arg; i++) {
426 value |= static_cast<u8>(*(*data)++) << (i * 8);
427 }
428 if (sign_extend) {
429 int shift = (7 - arg) * 8;
430 return (static_cast<s8>(value) << shift) >> shift;
431 }
432 return value;
433}
434
435/*
436 * Dumps encoded value.
437 */
438static void dumpEncodedValue(const DexFile* pDexFile, const u1** data); // forward
439static void dumpEncodedValue(const DexFile* pDexFile, const u1** data, u1 type, u1 arg) {
440 switch (type) {
441 case DexFile::kDexAnnotationByte:
442 fprintf(gOutFile, "%" PRId8, static_cast<s1>(readVarWidth(data, arg, false)));
443 break;
444 case DexFile::kDexAnnotationShort:
445 fprintf(gOutFile, "%" PRId16, static_cast<s2>(readVarWidth(data, arg, true)));
446 break;
447 case DexFile::kDexAnnotationChar:
448 fprintf(gOutFile, "%" PRIu16, static_cast<u2>(readVarWidth(data, arg, false)));
449 break;
450 case DexFile::kDexAnnotationInt:
451 fprintf(gOutFile, "%" PRId32, static_cast<s4>(readVarWidth(data, arg, true)));
452 break;
453 case DexFile::kDexAnnotationLong:
454 fprintf(gOutFile, "%" PRId64, static_cast<s8>(readVarWidth(data, arg, true)));
455 break;
456 case DexFile::kDexAnnotationFloat: {
457 // Fill on right.
458 union {
459 float f;
460 u4 data;
461 } conv;
462 conv.data = static_cast<u4>(readVarWidth(data, arg, false)) << (3 - arg) * 8;
463 fprintf(gOutFile, "%g", conv.f);
464 break;
465 }
466 case DexFile::kDexAnnotationDouble: {
467 // Fill on right.
468 union {
469 double d;
470 u8 data;
471 } conv;
472 conv.data = readVarWidth(data, arg, false) << (7 - arg) * 8;
473 fprintf(gOutFile, "%g", conv.d);
474 break;
475 }
476 case DexFile::kDexAnnotationString: {
477 const u4 idx = static_cast<u4>(readVarWidth(data, arg, false));
478 if (gOptions.outputFormat == OUTPUT_PLAIN) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800479 dumpEscapedString(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
Aart Bikdce50862016-06-10 16:04:03 -0700480 } else {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800481 dumpXmlAttribute(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
Aart Bikdce50862016-06-10 16:04:03 -0700482 }
483 break;
484 }
485 case DexFile::kDexAnnotationType: {
486 const u4 str_idx = static_cast<u4>(readVarWidth(data, arg, false));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800487 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(str_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700488 break;
489 }
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);
495 break;
496 }
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);
501 break;
502 }
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);
510 }
511 fputs(" }", gOutFile);
512 break;
513 }
514 case DexFile::kDexAnnotationAnnotation: {
515 const u4 type_idx = DecodeUnsignedLeb128(data);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800516 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(type_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700517 // 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);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800522 fputs(pDexFile->StringDataByIdx(dex::StringIndex(name_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700523 fputc('=', gOutFile);
524 dumpEncodedValue(pDexFile, data);
525 }
526 break;
527 }
528 case DexFile::kDexAnnotationNull:
529 fputs("null", gOutFile);
530 break;
531 case DexFile::kDexAnnotationBoolean:
532 fputs(strBool(arg), gOutFile);
533 break;
534 default:
535 fputs("????", gOutFile);
536 break;
537 } // switch
538}
539
540/*
541 * Dumps encoded value with prefix.
542 */
543static void dumpEncodedValue(const DexFile* pDexFile, const u1** data) {
544 const u1 enc = *(*data)++;
545 dumpEncodedValue(pDexFile, data, enc & 0x1f, enc >> 5);
546}
547
548/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700549 * Dumps the file header.
Aart Bik69ae54a2015-07-01 14:52:26 -0700550 */
551static 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_);
Aart Bikdce50862016-06-10 16:04:03 -0700573 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
574 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
Aart Bik69ae54a2015-07-01 14:52:26 -0700575 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_);
588}
589
590/*
591 * Dumps a class_def_item.
592 */
593static 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);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800597 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700598 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
599 pClassDef.access_flags_, pClassDef.access_flags_);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800600 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700601 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
602 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800603 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700604 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_);
608
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());
617 } else {
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");
622 }
623 fprintf(gOutFile, "\n");
624}
625
Aart Bikdce50862016-06-10 16:04:03 -0700626/**
627 * Dumps an annotation set item.
628 */
629static 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);
632 return;
633 }
634 for (u4 i = 0; i < set_item->size_; i++) {
635 const DexFile::AnnotationItem* annotation = pDexFile->GetAnnotationItem(set_item, i);
636 if (annotation == nullptr) {
637 continue;
638 }
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;
645 } // switch
646 // Decode raw bytes in annotation.
647 const u1* rData = annotation->annotation_;
648 dumpEncodedValue(pDexFile, &rData, DexFile::kDexAnnotationAnnotation, 0);
649 fputc('\n', gOutFile);
650 }
651}
652
653/*
654 * Dumps class annotations.
655 */
656static 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) {
660 return; // none
661 }
662
663 fprintf(gOutFile, "Class #%d annotations:\n", idx);
664
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);
669
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);
674 }
675
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]));
684 }
685 }
686
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]));
695 }
696 }
697
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]));
711 }
712 }
713 }
714 }
715
716 fputc('\n', gOutFile);
717}
718
Aart Bik69ae54a2015-07-01 14:52:26 -0700719/*
720 * Dumps an interface that a class declares to implement.
721 */
722static 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);
726 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -0700727 std::unique_ptr<char[]> dot(descriptorToDot(interfaceName));
728 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -0700729 }
730}
731
732/*
733 * Dumps the catches table associated with the code.
734 */
735static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
736 const u4 triesSize = pCode->tries_size_;
737
738 // No catch table.
739 if (triesSize == 0) {
740 fprintf(gOutFile, " catches : (none)\n");
741 return;
742 }
743
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()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800752 const dex::TypeIndex tidx = it.GetHandlerTypeIndex();
753 const char* descriptor = (!tidx.IsValid()) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
Aart Bik69ae54a2015-07-01 14:52:26 -0700754 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
755 } // for
756 } // for
757}
758
759/*
760 * Callback for dumping each positions table entry.
761 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000762static bool dumpPositionsCb(void* /*context*/, const DexFile::PositionInfo& entry) {
763 fprintf(gOutFile, " 0x%04x line=%d\n", entry.address_, entry.line_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700764 return false;
765}
766
767/*
768 * Callback for dumping locals table entry.
769 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000770static void dumpLocalsCb(void* /*context*/, const DexFile::LocalInfo& entry) {
771 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
Aart Bik69ae54a2015-07-01 14:52:26 -0700772 fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
David Srbeckyb06e28e2015-12-10 13:15:00 +0000773 entry.start_address_, entry.end_address_, entry.reg_,
774 entry.name_, entry.descriptor_, signature);
Aart Bik69ae54a2015-07-01 14:52:26 -0700775}
776
777/*
778 * Helper for dumpInstruction(), which builds the string
Aart Bika0e33fd2016-07-08 18:32:45 -0700779 * representation for the index in the given instruction.
780 * Returns a pointer to a buffer of sufficient size.
Aart Bik69ae54a2015-07-01 14:52:26 -0700781 */
Aart Bika0e33fd2016-07-08 18:32:45 -0700782static std::unique_ptr<char[]> indexString(const DexFile* pDexFile,
783 const Instruction* pDecInsn,
784 size_t bufSize) {
Orion Hodsonb34bb192016-10-18 17:02:58 +0100785 static const u4 kInvalidIndex = std::numeric_limits<u4>::max();
Aart Bika0e33fd2016-07-08 18:32:45 -0700786 std::unique_ptr<char[]> buf(new char[bufSize]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700787 // Determine index and width of the string.
788 u4 index = 0;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100789 u4 secondary_index = kInvalidIndex;
Aart Bik69ae54a2015-07-01 14:52:26 -0700790 u4 width = 4;
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();
802 width = 4;
803 break;
804 case Instruction::k31c:
805 index = pDecInsn->VRegB();
806 width = 8;
807 break;
808 case Instruction::k22c:
809 // case Instruction::k22cs:
810 index = pDecInsn->VRegC();
811 width = 4;
812 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100813 case Instruction::k45cc:
814 case Instruction::k4rcc:
815 index = pDecInsn->VRegB();
816 secondary_index = pDecInsn->VRegH();
817 width = 4;
818 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700819 default:
820 break;
821 } // switch
822
823 // Determine index type.
824 size_t outSize = 0;
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.
Aart Bika0e33fd2016-07-08 18:32:45 -0700829 outSize = snprintf(buf.get(), bufSize, "<unknown-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700830 break;
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.
Aart Bika0e33fd2016-07-08 18:32:45 -0700834 outSize = snprintf(buf.get(), bufSize, "<no-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700835 break;
836 case Instruction::kIndexTypeRef:
837 if (index < pDexFile->GetHeader().type_ids_size_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800838 const char* tp = pDexFile->StringByTypeIdx(dex::TypeIndex(index));
Aart Bika0e33fd2016-07-08 18:32:45 -0700839 outSize = snprintf(buf.get(), bufSize, "%s // type@%0*x", tp, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700840 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700841 outSize = snprintf(buf.get(), bufSize, "<type?> // type@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700842 }
843 break;
844 case Instruction::kIndexStringRef:
845 if (index < pDexFile->GetHeader().string_ids_size_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800846 const char* st = pDexFile->StringDataByIdx(dex::StringIndex(index));
Aart Bika0e33fd2016-07-08 18:32:45 -0700847 outSize = snprintf(buf.get(), bufSize, "\"%s\" // string@%0*x", st, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700848 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700849 outSize = snprintf(buf.get(), bufSize, "<string?> // string@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700850 }
851 break;
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_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700858 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // method@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700859 backDescriptor, name, signature.ToString().c_str(), width, index);
860 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700861 outSize = snprintf(buf.get(), bufSize, "<method?> // method@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700862 }
863 break;
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_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700870 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // field@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700871 backDescriptor, name, typeDescriptor, width, index);
872 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700873 outSize = snprintf(buf.get(), bufSize, "<field?> // field@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700874 }
875 break;
876 case Instruction::kIndexVtableOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700877 outSize = snprintf(buf.get(), bufSize, "[%0*x] // vtable #%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700878 width, index, width, index);
879 break;
880 case Instruction::kIndexFieldOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700881 outSize = snprintf(buf.get(), bufSize, "[obj+%0*x]", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700882 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100883 case Instruction::kIndexMethodAndProtoRef: {
Orion Hodsonc069a302017-01-18 09:23:12 +0000884 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",
892 backDescriptor,
893 name,
894 signature.ToString().c_str());
Orion Hodsonb34bb192016-10-18 17:02:58 +0100895 }
Orion Hodsonc069a302017-01-18 09:23:12 +0000896 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();
900 }
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);
903 break;
904 }
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);
Orion Hodsonb34bb192016-10-18 17:02:58 +0100908 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700909 // SOME NOT SUPPORTED:
910 // case Instruction::kIndexVaries:
911 // case Instruction::kIndexInlineMethod:
912 default:
Aart Bika0e33fd2016-07-08 18:32:45 -0700913 outSize = snprintf(buf.get(), bufSize, "<?>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700914 break;
915 } // switch
916
917 // Determine success of string construction.
918 if (outSize >= bufSize) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700919 // 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);
Aart Bik69ae54a2015-07-01 14:52:26 -0700923 }
924 return buf;
925}
926
927/*
928 * Dumps a single instruction.
929 */
930static 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);
936
937 // Dump (part of) raw bytes.
938 const u2* insns = pCode->insns_;
939 for (u4 i = 0; i < 8; i++) {
940 if (i < insnWidth) {
941 if (i == 7) {
942 fprintf(gOutFile, " ... ");
943 } else {
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]);
947 }
948 } else {
949 fputs(" ", gOutFile);
950 }
951 } // for
952
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);
962 } else {
963 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
964 }
965 } else {
966 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
967 }
968
969 // Set up additional argument.
Aart Bika0e33fd2016-07-08 18:32:45 -0700970 std::unique_ptr<char[]> indexBuf;
Aart Bik69ae54a2015-07-01 14:52:26 -0700971 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700972 indexBuf = indexString(pDexFile, pDecInsn, 200);
Aart Bik69ae54a2015-07-01 14:52:26 -0700973 }
974
975 // Dump the instruction.
976 //
977 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
978 //
979 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
980 case Instruction::k10x: // op
981 break;
982 case Instruction::k12x: // op vA, vB
983 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
984 break;
985 case Instruction::k11n: // op vA, #+B
986 fprintf(gOutFile, " v%d, #int %d // #%x",
987 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
988 break;
989 case Instruction::k11x: // op vAA
990 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
991 break;
992 case Instruction::k10t: // op +AA
Aart Bikdce50862016-06-10 16:04:03 -0700993 case Instruction::k20t: { // op +AAAA
994 const s4 targ = (s4) pDecInsn->VRegA();
995 fprintf(gOutFile, " %04x // %c%04x",
996 insnIdx + targ,
997 (targ < 0) ? '-' : '+',
998 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -0700999 break;
Aart Bikdce50862016-06-10 16:04:03 -07001000 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001001 case Instruction::k22x: // op vAA, vBBBB
1002 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1003 break;
Aart Bikdce50862016-06-10 16:04:03 -07001004 case Instruction::k21t: { // op vAA, +BBBB
1005 const s4 targ = (s4) pDecInsn->VRegB();
1006 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
1007 insnIdx + targ,
1008 (targ < 0) ? '-' : '+',
1009 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001010 break;
Aart Bikdce50862016-06-10 16:04:03 -07001011 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001012 case Instruction::k21s: // op vAA, #+BBBB
1013 fprintf(gOutFile, " v%d, #int %d // #%x",
1014 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
1015 break;
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());
1022 } else {
1023 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
1024 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
1025 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1026 }
1027 break;
1028 case Instruction::k21c: // op vAA, thing@BBBB
1029 case Instruction::k31c: // op vAA, thing@BBBBBBBB
Aart Bika0e33fd2016-07-08 18:32:45 -07001030 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001031 break;
1032 case Instruction::k23x: // op vAA, vBB, vCC
1033 fprintf(gOutFile, " v%d, v%d, v%d",
1034 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
1035 break;
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());
1040 break;
Aart Bikdce50862016-06-10 16:04:03 -07001041 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(),
1045 insnIdx + targ,
1046 (targ < 0) ? '-' : '+',
1047 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001048 break;
Aart Bikdce50862016-06-10 16:04:03 -07001049 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001050 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());
1054 break;
1055 case Instruction::k22c: // op vA, vB, thing@CCCC
1056 // NOT SUPPORTED:
1057 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
1058 fprintf(gOutFile, " v%d, v%d, %s",
Aart Bika0e33fd2016-07-08 18:32:45 -07001059 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001060 break;
1061 case Instruction::k30t:
1062 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
1063 break;
Aart Bikdce50862016-06-10 16:04:03 -07001064 case Instruction::k31i: { // op vAA, #+BBBBBBBB
1065 // This is often, but not always, a float.
1066 union {
1067 float f;
1068 u4 i;
1069 } conv;
1070 conv.i = pDecInsn->VRegB();
1071 fprintf(gOutFile, " v%d, #float %g // #%08x",
1072 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001073 break;
Aart Bikdce50862016-06-10 16:04:03 -07001074 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001075 case Instruction::k31t: // op vAA, offset +BBBBBBBB
1076 fprintf(gOutFile, " v%d, %08x // +%08x",
1077 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
1078 break;
1079 case Instruction::k32x: // op vAAAA, vBBBB
1080 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1081 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +01001082 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
Aart Bik69ae54a2015-07-01 14:52:26 -07001084 // NOT SUPPORTED:
1085 // case Instruction::k35ms: // [opt] invoke-virtual+super
1086 // case Instruction::k35mi: // [opt] inline invoke
Aart Bikdce50862016-06-10 16:04:03 -07001087 u4 arg[Instruction::kMaxVarArgRegs];
1088 pDecInsn->GetVarArgs(arg);
1089 fputs(" {", gOutFile);
1090 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1091 if (i == 0) {
1092 fprintf(gOutFile, "v%d", arg[i]);
1093 } else {
1094 fprintf(gOutFile, ", v%d", arg[i]);
1095 }
1096 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001097 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001098 break;
Aart Bikdce50862016-06-10 16:04:03 -07001099 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001100 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
Orion Hodsonb34bb192016-10-18 17:02:58 +01001101 case Instruction::k4rcc: { // op {vCCCC .. v(CCCC+AA-1)}, method@BBBB, proto@HHHH
Aart Bik69ae54a2015-07-01 14:52:26 -07001102 // NOT SUPPORTED:
1103 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1104 // case Instruction::k3rmi: // [opt] execute-inline/range
Aart Bik69ae54a2015-07-01 14:52:26 -07001105 // 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++) {
1109 if (i == 0) {
1110 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
1111 } else {
1112 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
1113 }
1114 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001115 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001116 }
1117 break;
Aart Bikdce50862016-06-10 16:04:03 -07001118 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1119 // This is often, but not always, a double.
1120 union {
1121 double d;
1122 u8 j;
1123 } conv;
1124 conv.j = pDecInsn->WideVRegB();
1125 fprintf(gOutFile, " v%d, #double %g // #%016" PRIx64,
1126 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001127 break;
Aart Bikdce50862016-06-10 16:04:03 -07001128 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001129 // NOT SUPPORTED:
1130 // case Instruction::k00x: // unknown op or breakpoint
1131 // break;
1132 default:
1133 fprintf(gOutFile, " ???");
1134 break;
1135 } // switch
1136
1137 fputc('\n', gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001138}
1139
1140/*
1141 * Dumps a bytecode disassembly.
1142 */
1143static 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_);
1149
1150 // Generate header.
Aart Bikc05e2f22016-07-12 15:53:13 -07001151 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());
Aart Bik69ae54a2015-07-01 14:52:26 -07001154
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);
1162 break;
1163 }
1164 dumpInstruction(pDexFile, pCode, codeOffset, insnIdx, insnWidth, instruction);
1165 insnIdx += insnWidth;
1166 } // for
1167}
1168
1169/*
1170 * Dumps code of a method.
1171 */
1172static 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_);
1179
1180 // Bytecode disassembly, if requested.
1181 if (gOptions.disassemble) {
1182 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
1183 }
1184
1185 // Try-catch blocks.
1186 dumpCatches(pDexFile, pCode);
1187
1188 // Positions and locals table in the debug info.
1189 bool is_static = (flags & kAccStatic) != 0;
1190 fprintf(gOutFile, " positions : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +00001191 pDexFile->DecodeDebugPositionInfo(pCode, dumpPositionsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001192 fprintf(gOutFile, " locals : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +00001193 pDexFile->DecodeDebugLocalInfo(pCode, is_static, idx, dumpLocalsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001194}
1195
1196/*
1197 * Dumps a method.
1198 */
1199static 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) {
1203 return;
1204 }
1205
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);
1212
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");
1220 } else {
1221 fprintf(gOutFile, " code -\n");
1222 dumpCode(pDexFile, idx, flags, pCode, codeOffset);
1223 }
1224 if (gOptions.disassemble) {
1225 fputc('\n', gOutFile);
1226 }
1227 } else if (gOptions.outputFormat == OUTPUT_XML) {
1228 const bool constructor = (name[0] == '<');
1229
1230 // Method name and prototype.
1231 if (constructor) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001232 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());
Aart Bik69ae54a2015-07-01 14:52:26 -07001236 } else {
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);
1241 goto bail;
1242 }
Aart Bikc05e2f22016-07-12 15:53:13 -07001243 std::unique_ptr<char[]> dot(descriptorToDot(returnType + 1));
1244 fprintf(gOutFile, " return=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001245 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));
1249 }
1250
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));
1256
1257 // Parameters.
1258 if (typeDescriptor[0] != '(') {
1259 fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor);
1260 goto bail;
1261 }
1262 char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1));
1263 const char* base = typeDescriptor + 1;
1264 int argNum = 0;
1265 while (*base != ')') {
1266 char* cp = tmpBuf;
1267 while (*base == '[') {
1268 *cp++ = *base++;
1269 }
1270 if (*base == 'L') {
1271 // Copy through ';'.
1272 do {
1273 *cp = *base++;
1274 } while (*cp++ != ';');
1275 } else {
1276 // Primitive char, copy it.
Aart Bikc05e2f22016-07-12 15:53:13 -07001277 if (strchr("ZBCSIFJD", *base) == nullptr) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001278 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
Aart Bika0e33fd2016-07-08 18:32:45 -07001279 break; // while
Aart Bik69ae54a2015-07-01 14:52:26 -07001280 }
1281 *cp++ = *base++;
1282 }
1283 // Null terminate and display.
1284 *cp++ = '\0';
Aart Bikc05e2f22016-07-12 15:53:13 -07001285 std::unique_ptr<char[]> dot(descriptorToDot(tmpBuf));
Aart Bik69ae54a2015-07-01 14:52:26 -07001286 fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n"
Aart Bikc05e2f22016-07-12 15:53:13 -07001287 "</parameter>\n", argNum++, dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001288 } // while
1289 free(tmpBuf);
1290 if (constructor) {
1291 fprintf(gOutFile, "</constructor>\n");
1292 } else {
1293 fprintf(gOutFile, "</method>\n");
1294 }
1295 }
1296
1297 bail:
1298 free(typeDescriptor);
1299 free(accessStr);
1300}
1301
1302/*
1303 * Dumps a static (class) field.
1304 */
Aart Bikdce50862016-06-10 16:04:03 -07001305static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i, const u1** data) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001306 // Bail for anything private if export only requested.
1307 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1308 return;
1309 }
1310
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);
1316
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);
Aart Bikdce50862016-06-10 16:04:03 -07001322 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001323 fputs(" value : ", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001324 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001325 fputs("\n", gOutFile);
1326 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001327 } else if (gOptions.outputFormat == OUTPUT_XML) {
1328 fprintf(gOutFile, "<field name=\"%s\"\n", name);
Aart Bikc05e2f22016-07-12 15:53:13 -07001329 std::unique_ptr<char[]> dot(descriptorToDot(typeDescriptor));
1330 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001331 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));
Aart Bikdce50862016-06-10 16:04:03 -07001338 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001339 fputs(" value=\"", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001340 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001341 fputs("\"\n", gOutFile);
1342 }
1343 fputs(">\n</field>\n", gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001344 }
1345
1346 free(accessStr);
1347}
1348
1349/*
1350 * Dumps an instance field.
1351 */
1352static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
Aart Bikdce50862016-06-10 16:04:03 -07001353 dumpSField(pDexFile, idx, flags, i, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001354}
1355
1356/*
Andreas Gampe5073fed2015-08-10 11:40:25 -07001357 * 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.
1360 */
1361
1362static void dumpCfg(const DexFile* dex_file,
Aart Bikdce50862016-06-10 16:04:03 -07001363 u4 dex_method_idx,
Andreas Gampe5073fed2015-08-10 11:40:25 -07001364 const DexFile::CodeItem* code_item) {
1365 if (code_item != nullptr) {
1366 std::ostringstream oss;
1367 DumpMethodCFG(dex_file, dex_method_idx, oss);
David Sehrcaacd112016-10-20 16:27:02 -07001368 fputs(oss.str().c_str(), gOutFile);
Andreas Gampe5073fed2015-08-10 11:40:25 -07001369 }
1370}
1371
1372static void dumpCfg(const DexFile* dex_file, int idx) {
1373 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
Aart Bikdce50862016-06-10 16:04:03 -07001374 const u1* class_data = dex_file->GetClassData(class_def);
Andreas Gampe5073fed2015-08-10 11:40:25 -07001375 if (class_data == nullptr) { // empty class such as a marker interface?
1376 return;
1377 }
1378 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001379 it.SkipAllFields();
Andreas Gampe5073fed2015-08-10 11:40:25 -07001380 while (it.HasNextDirectMethod()) {
1381 dumpCfg(dex_file,
1382 it.GetMemberIndex(),
1383 it.GetMethodCodeItem());
1384 it.Next();
1385 }
1386 while (it.HasNextVirtualMethod()) {
1387 dumpCfg(dex_file,
1388 it.GetMemberIndex(),
1389 it.GetMethodCodeItem());
1390 it.Next();
1391 }
1392}
1393
1394/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001395 * Dumps the class.
1396 *
1397 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1398 *
1399 * If "*pLastPackage" is nullptr or does not match the current class' package,
1400 * the value will be replaced with a newly-allocated string.
1401 */
1402static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1403 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1404
1405 // Omitting non-public class.
1406 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1407 return;
1408 }
1409
Aart Bikdce50862016-06-10 16:04:03 -07001410 if (gOptions.showSectionHeaders) {
1411 dumpClassDef(pDexFile, idx);
1412 }
1413
1414 if (gOptions.showAnnotations) {
1415 dumpClassAnnotations(pDexFile, idx);
1416 }
1417
1418 if (gOptions.showCfg) {
Andreas Gampe5073fed2015-08-10 11:40:25 -07001419 dumpCfg(pDexFile, idx);
1420 return;
1421 }
1422
Aart Bik69ae54a2015-07-01 14:52:26 -07001423 // For the XML output, show the package name. Ideally we'd gather
1424 // up the classes, sort them, and dump them alphabetically so the
1425 // package name wouldn't jump around, but that's not a great plan
1426 // for something that needs to run on the device.
1427 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1428 if (!(classDescriptor[0] == 'L' &&
1429 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1430 // Arrays and primitives should not be defined explicitly. Keep going?
1431 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1432 } else if (gOptions.outputFormat == OUTPUT_XML) {
1433 char* mangle = strdup(classDescriptor + 1);
1434 mangle[strlen(mangle)-1] = '\0';
1435
1436 // Reduce to just the package name.
1437 char* lastSlash = strrchr(mangle, '/');
1438 if (lastSlash != nullptr) {
1439 *lastSlash = '\0';
1440 } else {
1441 *mangle = '\0';
1442 }
1443
1444 for (char* cp = mangle; *cp != '\0'; cp++) {
1445 if (*cp == '/') {
1446 *cp = '.';
1447 }
1448 } // for
1449
1450 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1451 // Start of a new package.
1452 if (*pLastPackage != nullptr) {
1453 fprintf(gOutFile, "</package>\n");
1454 }
1455 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1456 free(*pLastPackage);
1457 *pLastPackage = mangle;
1458 } else {
1459 free(mangle);
1460 }
1461 }
1462
1463 // General class information.
1464 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1465 const char* superclassDescriptor;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001466 if (!pClassDef.superclass_idx_.IsValid()) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001467 superclassDescriptor = nullptr;
1468 } else {
1469 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1470 }
1471 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1472 fprintf(gOutFile, "Class #%d -\n", idx);
1473 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1474 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1475 if (superclassDescriptor != nullptr) {
1476 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1477 }
1478 fprintf(gOutFile, " Interfaces -\n");
1479 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -07001480 std::unique_ptr<char[]> dot(descriptorClassToDot(classDescriptor));
1481 fprintf(gOutFile, "<class name=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001482 if (superclassDescriptor != nullptr) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001483 dot = descriptorToDot(superclassDescriptor);
1484 fprintf(gOutFile, " extends=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001485 }
Alex Light1f12e282015-12-10 16:49:47 -08001486 fprintf(gOutFile, " interface=%s\n",
1487 quotedBool((pClassDef.access_flags_ & kAccInterface) != 0));
Aart Bik69ae54a2015-07-01 14:52:26 -07001488 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1489 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1490 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1491 // The "deprecated=" not knowable w/o parsing annotations.
1492 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1493 fprintf(gOutFile, ">\n");
1494 }
1495
1496 // Interfaces.
1497 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1498 if (pInterfaces != nullptr) {
1499 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1500 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1501 } // for
1502 }
1503
1504 // Fields and methods.
1505 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
1506 if (pEncodedData == nullptr) {
1507 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1508 fprintf(gOutFile, " Static fields -\n");
1509 fprintf(gOutFile, " Instance fields -\n");
1510 fprintf(gOutFile, " Direct methods -\n");
1511 fprintf(gOutFile, " Virtual methods -\n");
1512 }
1513 } else {
1514 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
Aart Bikdce50862016-06-10 16:04:03 -07001515
1516 // Prepare data for static fields.
1517 const u1* sData = pDexFile->GetEncodedStaticFieldValuesArray(pClassDef);
1518 const u4 sSize = sData != nullptr ? DecodeUnsignedLeb128(&sData) : 0;
1519
1520 // Static fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001521 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1522 fprintf(gOutFile, " Static fields -\n");
1523 }
Aart Bikdce50862016-06-10 16:04:03 -07001524 for (u4 i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) {
1525 dumpSField(pDexFile,
1526 pClassData.GetMemberIndex(),
1527 pClassData.GetRawMemberAccessFlags(),
1528 i,
1529 i < sSize ? &sData : nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001530 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001531
1532 // Instance fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001533 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1534 fprintf(gOutFile, " Instance fields -\n");
1535 }
Aart Bikdce50862016-06-10 16:04:03 -07001536 for (u4 i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) {
1537 dumpIField(pDexFile,
1538 pClassData.GetMemberIndex(),
1539 pClassData.GetRawMemberAccessFlags(),
1540 i);
Aart Bik69ae54a2015-07-01 14:52:26 -07001541 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001542
1543 // Direct methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001544 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1545 fprintf(gOutFile, " Direct methods -\n");
1546 }
1547 for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) {
1548 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1549 pClassData.GetRawMemberAccessFlags(),
1550 pClassData.GetMethodCodeItem(),
1551 pClassData.GetMethodCodeItemOffset(), i);
1552 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001553
1554 // Virtual methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001555 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1556 fprintf(gOutFile, " Virtual methods -\n");
1557 }
1558 for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) {
1559 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1560 pClassData.GetRawMemberAccessFlags(),
1561 pClassData.GetMethodCodeItem(),
1562 pClassData.GetMethodCodeItemOffset(), i);
1563 } // for
1564 }
1565
1566 // End of class.
1567 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1568 const char* fileName;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001569 if (pClassDef.source_file_idx_.IsValid()) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001570 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1571 } else {
1572 fileName = "unknown";
1573 }
1574 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001575 pClassDef.source_file_idx_.index_, fileName);
Aart Bik69ae54a2015-07-01 14:52:26 -07001576 } else if (gOptions.outputFormat == OUTPUT_XML) {
1577 fprintf(gOutFile, "</class>\n");
1578 }
1579
1580 free(accessStr);
1581}
1582
Orion Hodsonc069a302017-01-18 09:23:12 +00001583static void dumpMethodHandle(const DexFile* pDexFile, u4 idx) {
1584 const DexFile::MethodHandleItem& mh = pDexFile->GetMethodHandle(idx);
Orion Hodson631827d2017-04-10 14:53:47 +01001585 const char* type = nullptr;
1586 bool is_instance = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001587 bool is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001588 switch (static_cast<DexFile::MethodHandleType>(mh.method_handle_type_)) {
1589 case DexFile::MethodHandleType::kStaticPut:
1590 type = "put-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001591 is_instance = false;
1592 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001593 break;
1594 case DexFile::MethodHandleType::kStaticGet:
1595 type = "get-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001596 is_instance = false;
1597 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001598 break;
1599 case DexFile::MethodHandleType::kInstancePut:
1600 type = "put-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001601 is_instance = true;
1602 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001603 break;
1604 case DexFile::MethodHandleType::kInstanceGet:
1605 type = "get-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001606 is_instance = true;
1607 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001608 break;
1609 case DexFile::MethodHandleType::kInvokeStatic:
1610 type = "invoke-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001611 is_instance = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001612 is_invoke = true;
1613 break;
1614 case DexFile::MethodHandleType::kInvokeInstance:
1615 type = "invoke-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001616 is_instance = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00001617 is_invoke = true;
1618 break;
1619 case DexFile::MethodHandleType::kInvokeConstructor:
1620 type = "invoke-constructor";
Orion Hodson631827d2017-04-10 14:53:47 +01001621 is_instance = true;
1622 is_invoke = true;
1623 break;
1624 case DexFile::MethodHandleType::kInvokeDirect:
1625 type = "invoke-direct";
1626 is_instance = true;
1627 is_invoke = true;
1628 break;
1629 case DexFile::MethodHandleType::kInvokeInterface:
1630 type = "invoke-interface";
1631 is_instance = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00001632 is_invoke = true;
1633 break;
1634 }
1635
1636 const char* declaring_class;
1637 const char* member;
1638 std::string member_type;
Orion Hodson631827d2017-04-10 14:53:47 +01001639 if (type != nullptr) {
1640 if (is_invoke) {
1641 const DexFile::MethodId& method_id = pDexFile->GetMethodId(mh.field_or_method_idx_);
1642 declaring_class = pDexFile->GetMethodDeclaringClassDescriptor(method_id);
1643 member = pDexFile->GetMethodName(method_id);
1644 member_type = pDexFile->GetMethodSignature(method_id).ToString();
1645 } else {
1646 const DexFile::FieldId& field_id = pDexFile->GetFieldId(mh.field_or_method_idx_);
1647 declaring_class = pDexFile->GetFieldDeclaringClassDescriptor(field_id);
1648 member = pDexFile->GetFieldName(field_id);
1649 member_type = pDexFile->GetFieldTypeDescriptor(field_id);
1650 }
1651 if (is_instance) {
1652 member_type = android::base::StringPrintf("(%s%s", declaring_class, member_type.c_str() + 1);
1653 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001654 } else {
Orion Hodson631827d2017-04-10 14:53:47 +01001655 type = "?";
1656 declaring_class = "?";
1657 member = "?";
1658 member_type = "?";
Orion Hodsonc069a302017-01-18 09:23:12 +00001659 }
1660
1661 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1662 fprintf(gOutFile, "Method handle #%u:\n", idx);
1663 fprintf(gOutFile, " type : %s\n", type);
1664 fprintf(gOutFile, " target : %s %s\n", declaring_class, member);
1665 fprintf(gOutFile, " target_type : %s\n", member_type.c_str());
1666 } else {
1667 fprintf(gOutFile, "<method_handle index=\"%u\"\n", idx);
1668 fprintf(gOutFile, " type=\"%s\"\n", type);
1669 fprintf(gOutFile, " target_class=\"%s\"\n", declaring_class);
1670 fprintf(gOutFile, " target_member=\"%s\"\n", member);
1671 fprintf(gOutFile, " target_member_type=");
1672 dumpEscapedString(member_type.c_str());
1673 fprintf(gOutFile, "\n>\n</method_handle>\n");
1674 }
1675}
1676
1677static void dumpCallSite(const DexFile* pDexFile, u4 idx) {
1678 const DexFile::CallSiteIdItem& call_site_id = pDexFile->GetCallSiteId(idx);
1679 CallSiteArrayValueIterator it(*pDexFile, call_site_id);
1680 if (it.Size() < 3) {
1681 fprintf(stderr, "ERROR: Call site %u has too few values.\n", idx);
1682 return;
1683 }
1684
1685 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1686 it.Next();
1687 dex::StringIndex method_name_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1688 const char* method_name = pDexFile->StringDataByIdx(method_name_idx);
1689 it.Next();
1690 uint32_t method_type_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1691 const DexFile::ProtoId& method_type_id = pDexFile->GetProtoId(method_type_idx);
1692 std::string method_type = pDexFile->GetProtoSignature(method_type_id).ToString();
1693 it.Next();
1694
1695 if (gOptions.outputFormat == OUTPUT_PLAIN) {
Orion Hodson775224d2017-07-05 11:04:01 +01001696 fprintf(gOutFile, "Call site #%u: // offset %u\n", idx, call_site_id.data_off_);
Orion Hodsonc069a302017-01-18 09:23:12 +00001697 fprintf(gOutFile, " link_argument[0] : %u (MethodHandle)\n", method_handle_idx);
1698 fprintf(gOutFile, " link_argument[1] : %s (String)\n", method_name);
1699 fprintf(gOutFile, " link_argument[2] : %s (MethodType)\n", method_type.c_str());
1700 } else {
Orion Hodson775224d2017-07-05 11:04:01 +01001701 fprintf(gOutFile, "<call_site index=\"%u\" offset=\"%u\">\n", idx, call_site_id.data_off_);
Orion Hodsonc069a302017-01-18 09:23:12 +00001702 fprintf(gOutFile,
1703 "<link_argument index=\"0\" type=\"MethodHandle\" value=\"%u\"/>\n",
1704 method_handle_idx);
1705 fprintf(gOutFile,
1706 "<link_argument index=\"1\" type=\"String\" values=\"%s\"/>\n",
1707 method_name);
1708 fprintf(gOutFile,
1709 "<link_argument index=\"2\" type=\"MethodType\" value=\"%s\"/>\n",
1710 method_type.c_str());
1711 }
1712
1713 size_t argument = 3;
1714 while (it.HasNext()) {
1715 const char* type;
1716 std::string value;
1717 switch (it.GetValueType()) {
1718 case EncodedArrayValueIterator::ValueType::kByte:
1719 type = "byte";
1720 value = android::base::StringPrintf("%u", it.GetJavaValue().b);
1721 break;
1722 case EncodedArrayValueIterator::ValueType::kShort:
1723 type = "short";
1724 value = android::base::StringPrintf("%d", it.GetJavaValue().s);
1725 break;
1726 case EncodedArrayValueIterator::ValueType::kChar:
1727 type = "char";
1728 value = android::base::StringPrintf("%u", it.GetJavaValue().c);
1729 break;
1730 case EncodedArrayValueIterator::ValueType::kInt:
1731 type = "int";
1732 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1733 break;
1734 case EncodedArrayValueIterator::ValueType::kLong:
1735 type = "long";
1736 value = android::base::StringPrintf("%" PRId64, it.GetJavaValue().j);
1737 break;
1738 case EncodedArrayValueIterator::ValueType::kFloat:
1739 type = "float";
1740 value = android::base::StringPrintf("%g", it.GetJavaValue().f);
1741 break;
1742 case EncodedArrayValueIterator::ValueType::kDouble:
1743 type = "double";
1744 value = android::base::StringPrintf("%g", it.GetJavaValue().d);
1745 break;
1746 case EncodedArrayValueIterator::ValueType::kMethodType: {
1747 type = "MethodType";
1748 uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1749 const DexFile::ProtoId& proto_id = pDexFile->GetProtoId(proto_idx);
1750 value = pDexFile->GetProtoSignature(proto_id).ToString();
1751 break;
1752 }
1753 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1754 type = "MethodHandle";
1755 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1756 break;
1757 case EncodedArrayValueIterator::ValueType::kString: {
1758 type = "String";
1759 dex::StringIndex string_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1760 value = pDexFile->StringDataByIdx(string_idx);
1761 break;
1762 }
1763 case EncodedArrayValueIterator::ValueType::kType: {
1764 type = "Class";
1765 dex::TypeIndex type_idx = static_cast<dex::TypeIndex>(it.GetJavaValue().i);
1766 const DexFile::ClassDef* class_def = pDexFile->FindClassDef(type_idx);
1767 value = pDexFile->GetClassDescriptor(*class_def);
1768 value = descriptorClassToDot(value.c_str()).get();
1769 break;
1770 }
1771 case EncodedArrayValueIterator::ValueType::kField:
1772 case EncodedArrayValueIterator::ValueType::kMethod:
1773 case EncodedArrayValueIterator::ValueType::kEnum:
1774 case EncodedArrayValueIterator::ValueType::kArray:
1775 case EncodedArrayValueIterator::ValueType::kAnnotation:
1776 // Unreachable based on current EncodedArrayValueIterator::Next().
Andreas Gampef45d61c2017-06-07 10:29:33 -07001777 UNIMPLEMENTED(FATAL) << " type " << it.GetValueType();
Orion Hodsonc069a302017-01-18 09:23:12 +00001778 UNREACHABLE();
Orion Hodsonc069a302017-01-18 09:23:12 +00001779 case EncodedArrayValueIterator::ValueType::kNull:
1780 type = "Null";
1781 value = "null";
1782 break;
1783 case EncodedArrayValueIterator::ValueType::kBoolean:
1784 type = "boolean";
1785 value = it.GetJavaValue().z ? "true" : "false";
1786 break;
1787 }
1788
1789 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1790 fprintf(gOutFile, " link_argument[%zu] : %s (%s)\n", argument, value.c_str(), type);
1791 } else {
1792 fprintf(gOutFile, "<link_argument index=\"%zu\" type=\"%s\" value=", argument, type);
1793 dumpEscapedString(value.c_str());
1794 fprintf(gOutFile, "/>\n");
1795 }
1796
1797 it.Next();
1798 argument++;
1799 }
1800
1801 if (gOptions.outputFormat == OUTPUT_XML) {
1802 fprintf(gOutFile, "</call_site>\n");
1803 }
1804}
1805
Aart Bik69ae54a2015-07-01 14:52:26 -07001806/*
1807 * Dumps the requested sections of the file.
1808 */
Aart Bik7b45a8a2016-10-24 16:07:59 -07001809static void processDexFile(const char* fileName,
1810 const DexFile* pDexFile, size_t i, size_t n) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001811 if (gOptions.verbose) {
Aart Bik7b45a8a2016-10-24 16:07:59 -07001812 fputs("Opened '", gOutFile);
1813 fputs(fileName, gOutFile);
1814 if (n > 1) {
1815 fprintf(gOutFile, ":%s", DexFile::GetMultiDexClassesDexName(i).c_str());
1816 }
1817 fprintf(gOutFile, "', DEX version '%.3s'\n", pDexFile->GetHeader().magic_ + 4);
Aart Bik69ae54a2015-07-01 14:52:26 -07001818 }
1819
1820 // Headers.
1821 if (gOptions.showFileHeaders) {
1822 dumpFileHeader(pDexFile);
1823 }
1824
1825 // Open XML context.
1826 if (gOptions.outputFormat == OUTPUT_XML) {
1827 fprintf(gOutFile, "<api>\n");
1828 }
1829
1830 // Iterate over all classes.
1831 char* package = nullptr;
1832 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
1833 for (u4 i = 0; i < classDefsSize; i++) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001834 dumpClass(pDexFile, i, &package);
1835 } // for
1836
Orion Hodsonc069a302017-01-18 09:23:12 +00001837 // Iterate over all method handles.
1838 for (u4 i = 0; i < pDexFile->NumMethodHandles(); ++i) {
1839 dumpMethodHandle(pDexFile, i);
1840 } // for
1841
1842 // Iterate over all call site ids.
1843 for (u4 i = 0; i < pDexFile->NumCallSiteIds(); ++i) {
1844 dumpCallSite(pDexFile, i);
1845 } // for
1846
Aart Bik69ae54a2015-07-01 14:52:26 -07001847 // Free the last package allocated.
1848 if (package != nullptr) {
1849 fprintf(gOutFile, "</package>\n");
1850 free(package);
1851 }
1852
1853 // Close XML context.
1854 if (gOptions.outputFormat == OUTPUT_XML) {
1855 fprintf(gOutFile, "</api>\n");
1856 }
1857}
1858
1859/*
1860 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1861 */
1862int processFile(const char* fileName) {
1863 if (gOptions.verbose) {
1864 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1865 }
1866
1867 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
Aart Bikdce50862016-06-10 16:04:03 -07001868 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -07001869 const bool kVerifyChecksum = !gOptions.ignoreBadChecksum;
Aart Bik69ae54a2015-07-01 14:52:26 -07001870 std::string error_msg;
1871 std::vector<std::unique_ptr<const DexFile>> dex_files;
Aart Bik37d6a3b2016-06-21 18:30:10 -07001872 if (!DexFile::Open(fileName, fileName, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001873 // Display returned error message to user. Note that this error behavior
1874 // differs from the error messages shown by the original Dalvik dexdump.
1875 fputs(error_msg.c_str(), stderr);
1876 fputc('\n', stderr);
1877 return -1;
1878 }
1879
Aart Bik4e149602015-07-09 11:45:28 -07001880 // Success. Either report checksum verification or process
1881 // all dex files found in given file.
Aart Bik69ae54a2015-07-01 14:52:26 -07001882 if (gOptions.checksumOnly) {
1883 fprintf(gOutFile, "Checksum verified\n");
1884 } else {
Aart Bik7b45a8a2016-10-24 16:07:59 -07001885 for (size_t i = 0, n = dex_files.size(); i < n; i++) {
1886 processDexFile(fileName, dex_files[i].get(), i, n);
Aart Bik4e149602015-07-09 11:45:28 -07001887 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001888 }
1889 return 0;
1890}
1891
1892} // namespace art