blob: 2c98e12741fd8171e8d072a2702005ccd56c18e4 [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
David Sehr9e734c72018-01-04 17:56:19 -080047#include "dex/code_item_accessors-no_art-inl.h"
48#include "dex/dex_file-inl.h"
49#include "dex/dex_file_exception_helpers.h"
50#include "dex/dex_file_loader.h"
51#include "dex/dex_file_types.h"
52#include "dex/dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070053#include "dexdump_cfg.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070054
55namespace art {
56
57/*
58 * Options parsed in main driver.
59 */
60struct Options gOptions;
61
62/*
Aart Bik4e149602015-07-09 11:45:28 -070063 * Output file. Defaults to stdout.
Aart Bik69ae54a2015-07-01 14:52:26 -070064 */
65FILE* gOutFile = stdout;
66
67/*
68 * Data types that match the definitions in the VM specification.
69 */
70typedef uint8_t u1;
71typedef uint16_t u2;
72typedef uint32_t u4;
73typedef uint64_t u8;
Aart Bikdce50862016-06-10 16:04:03 -070074typedef int8_t s1;
75typedef int16_t s2;
Aart Bik69ae54a2015-07-01 14:52:26 -070076typedef int32_t s4;
77typedef int64_t s8;
78
79/*
80 * Basic information about a field or a method.
81 */
82struct FieldMethodInfo {
83 const char* classDescriptor;
84 const char* name;
85 const char* signature;
86};
87
88/*
89 * Flags for use with createAccessFlagStr().
90 */
91enum AccessFor {
92 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
93};
94const int kNumFlags = 18;
95
96/*
97 * Gets 2 little-endian bytes.
98 */
99static inline u2 get2LE(unsigned char const* pSrc) {
100 return pSrc[0] | (pSrc[1] << 8);
101}
102
103/*
104 * Converts a single-character primitive type into human-readable form.
105 */
106static const char* primitiveTypeLabel(char typeChar) {
107 switch (typeChar) {
108 case 'B': return "byte";
109 case 'C': return "char";
110 case 'D': return "double";
111 case 'F': return "float";
112 case 'I': return "int";
113 case 'J': return "long";
114 case 'S': return "short";
115 case 'V': return "void";
116 case 'Z': return "boolean";
117 default: return "UNKNOWN";
118 } // switch
119}
120
121/*
122 * Converts a type descriptor to human-readable "dotted" form. For
123 * example, "Ljava/lang/String;" becomes "java.lang.String", and
124 * "[I" becomes "int[]". Also converts '$' to '.', which means this
125 * form can't be converted back to a descriptor.
126 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700127static std::unique_ptr<char[]> descriptorToDot(const char* str) {
Aart Bik69ae54a2015-07-01 14:52:26 -0700128 int targetLen = strlen(str);
129 int offset = 0;
130
131 // Strip leading [s; will be added to end.
132 while (targetLen > 1 && str[offset] == '[') {
133 offset++;
134 targetLen--;
135 } // while
136
137 const int arrayDepth = offset;
138
139 if (targetLen == 1) {
140 // Primitive type.
141 str = primitiveTypeLabel(str[offset]);
142 offset = 0;
143 targetLen = strlen(str);
144 } else {
145 // Account for leading 'L' and trailing ';'.
146 if (targetLen >= 2 && str[offset] == 'L' &&
147 str[offset + targetLen - 1] == ';') {
148 targetLen -= 2;
149 offset++;
150 }
151 }
152
153 // Copy class name over.
Aart Bikc05e2f22016-07-12 15:53:13 -0700154 std::unique_ptr<char[]> newStr(new char[targetLen + arrayDepth * 2 + 1]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700155 int i = 0;
156 for (; i < targetLen; i++) {
157 const char ch = str[offset + i];
158 newStr[i] = (ch == '/' || ch == '$') ? '.' : ch;
159 } // for
160
161 // Add the appropriate number of brackets for arrays.
162 for (int j = 0; j < arrayDepth; j++) {
163 newStr[i++] = '[';
164 newStr[i++] = ']';
165 } // for
166
167 newStr[i] = '\0';
168 return newStr;
169}
170
171/*
172 * Converts the class name portion of a type descriptor to human-readable
Aart Bikc05e2f22016-07-12 15:53:13 -0700173 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
Aart Bik69ae54a2015-07-01 14:52:26 -0700174 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700175static std::unique_ptr<char[]> descriptorClassToDot(const char* str) {
176 // Reduce to just the class name prefix.
Aart Bik69ae54a2015-07-01 14:52:26 -0700177 const char* lastSlash = strrchr(str, '/');
178 if (lastSlash == nullptr) {
179 lastSlash = str + 1; // start past 'L'
180 } else {
181 lastSlash++; // start past '/'
182 }
183
Aart Bikc05e2f22016-07-12 15:53:13 -0700184 // Copy class name over, trimming trailing ';'.
185 const int targetLen = strlen(lastSlash);
186 std::unique_ptr<char[]> newStr(new char[targetLen]);
187 for (int i = 0; i < targetLen - 1; i++) {
188 const char ch = lastSlash[i];
189 newStr[i] = ch == '$' ? '.' : ch;
Aart Bik69ae54a2015-07-01 14:52:26 -0700190 } // for
Aart Bikc05e2f22016-07-12 15:53:13 -0700191 newStr[targetLen - 1] = '\0';
Aart Bik69ae54a2015-07-01 14:52:26 -0700192 return newStr;
193}
194
195/*
Aart Bikdce50862016-06-10 16:04:03 -0700196 * Returns string representing the boolean value.
197 */
198static const char* strBool(bool val) {
199 return val ? "true" : "false";
200}
201
202/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700203 * Returns a quoted string representing the boolean value.
204 */
205static const char* quotedBool(bool val) {
206 return val ? "\"true\"" : "\"false\"";
207}
208
209/*
210 * Returns a quoted string representing the access flags.
211 */
212static const char* quotedVisibility(u4 accessFlags) {
213 if (accessFlags & kAccPublic) {
214 return "\"public\"";
215 } else if (accessFlags & kAccProtected) {
216 return "\"protected\"";
217 } else if (accessFlags & kAccPrivate) {
218 return "\"private\"";
219 } else {
220 return "\"package\"";
221 }
222}
223
224/*
225 * Counts the number of '1' bits in a word.
226 */
227static int countOnes(u4 val) {
228 val = val - ((val >> 1) & 0x55555555);
229 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
230 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
231}
232
233/*
234 * Creates a new string with human-readable access flags.
235 *
236 * In the base language the access_flags fields are type u2; in Dalvik
237 * they're u4.
238 */
239static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
240 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
241 {
242 "PUBLIC", /* 0x00001 */
243 "PRIVATE", /* 0x00002 */
244 "PROTECTED", /* 0x00004 */
245 "STATIC", /* 0x00008 */
246 "FINAL", /* 0x00010 */
247 "?", /* 0x00020 */
248 "?", /* 0x00040 */
249 "?", /* 0x00080 */
250 "?", /* 0x00100 */
251 "INTERFACE", /* 0x00200 */
252 "ABSTRACT", /* 0x00400 */
253 "?", /* 0x00800 */
254 "SYNTHETIC", /* 0x01000 */
255 "ANNOTATION", /* 0x02000 */
256 "ENUM", /* 0x04000 */
257 "?", /* 0x08000 */
258 "VERIFIED", /* 0x10000 */
259 "OPTIMIZED", /* 0x20000 */
260 }, {
261 "PUBLIC", /* 0x00001 */
262 "PRIVATE", /* 0x00002 */
263 "PROTECTED", /* 0x00004 */
264 "STATIC", /* 0x00008 */
265 "FINAL", /* 0x00010 */
266 "SYNCHRONIZED", /* 0x00020 */
267 "BRIDGE", /* 0x00040 */
268 "VARARGS", /* 0x00080 */
269 "NATIVE", /* 0x00100 */
270 "?", /* 0x00200 */
271 "ABSTRACT", /* 0x00400 */
272 "STRICT", /* 0x00800 */
273 "SYNTHETIC", /* 0x01000 */
274 "?", /* 0x02000 */
275 "?", /* 0x04000 */
276 "MIRANDA", /* 0x08000 */
277 "CONSTRUCTOR", /* 0x10000 */
278 "DECLARED_SYNCHRONIZED", /* 0x20000 */
279 }, {
280 "PUBLIC", /* 0x00001 */
281 "PRIVATE", /* 0x00002 */
282 "PROTECTED", /* 0x00004 */
283 "STATIC", /* 0x00008 */
284 "FINAL", /* 0x00010 */
285 "?", /* 0x00020 */
286 "VOLATILE", /* 0x00040 */
287 "TRANSIENT", /* 0x00080 */
288 "?", /* 0x00100 */
289 "?", /* 0x00200 */
290 "?", /* 0x00400 */
291 "?", /* 0x00800 */
292 "SYNTHETIC", /* 0x01000 */
293 "?", /* 0x02000 */
294 "ENUM", /* 0x04000 */
295 "?", /* 0x08000 */
296 "?", /* 0x10000 */
297 "?", /* 0x20000 */
298 },
299 };
300
301 // Allocate enough storage to hold the expected number of strings,
302 // plus a space between each. We over-allocate, using the longest
303 // string above as the base metric.
304 const int kLongest = 21; // The strlen of longest string above.
305 const int count = countOnes(flags);
306 char* str;
307 char* cp;
308 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
309
310 for (int i = 0; i < kNumFlags; i++) {
311 if (flags & 0x01) {
312 const char* accessStr = kAccessStrings[forWhat][i];
313 const int len = strlen(accessStr);
314 if (cp != str) {
315 *cp++ = ' ';
316 }
317 memcpy(cp, accessStr, len);
318 cp += len;
319 }
320 flags >>= 1;
321 } // for
322
323 *cp = '\0';
324 return str;
325}
326
327/*
328 * Copies character data from "data" to "out", converting non-ASCII values
329 * to fprintf format chars or an ASCII filler ('.' or '?').
330 *
331 * The output buffer must be able to hold (2*len)+1 bytes. The result is
332 * NULL-terminated.
333 */
334static void asciify(char* out, const unsigned char* data, size_t len) {
335 while (len--) {
336 if (*data < 0x20) {
337 // Could do more here, but we don't need them yet.
338 switch (*data) {
339 case '\0':
340 *out++ = '\\';
341 *out++ = '0';
342 break;
343 case '\n':
344 *out++ = '\\';
345 *out++ = 'n';
346 break;
347 default:
348 *out++ = '.';
349 break;
350 } // switch
351 } else if (*data >= 0x80) {
352 *out++ = '?';
353 } else {
354 *out++ = *data;
355 }
356 data++;
357 } // while
358 *out = '\0';
359}
360
361/*
Aart Bikdce50862016-06-10 16:04:03 -0700362 * Dumps a string value with some escape characters.
363 */
364static void dumpEscapedString(const char* p) {
365 fputs("\"", gOutFile);
366 for (; *p; p++) {
367 switch (*p) {
368 case '\\':
369 fputs("\\\\", gOutFile);
370 break;
371 case '\"':
372 fputs("\\\"", gOutFile);
373 break;
374 case '\t':
375 fputs("\\t", gOutFile);
376 break;
377 case '\n':
378 fputs("\\n", gOutFile);
379 break;
380 case '\r':
381 fputs("\\r", gOutFile);
382 break;
383 default:
384 putc(*p, gOutFile);
385 } // switch
386 } // for
387 fputs("\"", gOutFile);
388}
389
390/*
391 * Dumps a string as an XML attribute value.
392 */
393static void dumpXmlAttribute(const char* p) {
394 for (; *p; p++) {
395 switch (*p) {
396 case '&':
397 fputs("&amp;", gOutFile);
398 break;
399 case '<':
400 fputs("&lt;", gOutFile);
401 break;
402 case '>':
403 fputs("&gt;", gOutFile);
404 break;
405 case '"':
406 fputs("&quot;", gOutFile);
407 break;
408 case '\t':
409 fputs("&#x9;", gOutFile);
410 break;
411 case '\n':
412 fputs("&#xA;", gOutFile);
413 break;
414 case '\r':
415 fputs("&#xD;", gOutFile);
416 break;
417 default:
418 putc(*p, gOutFile);
419 } // switch
420 } // for
421}
422
423/*
424 * Reads variable width value, possibly sign extended at the last defined byte.
425 */
426static u8 readVarWidth(const u1** data, u1 arg, bool sign_extend) {
427 u8 value = 0;
428 for (u4 i = 0; i <= arg; i++) {
429 value |= static_cast<u8>(*(*data)++) << (i * 8);
430 }
431 if (sign_extend) {
432 int shift = (7 - arg) * 8;
433 return (static_cast<s8>(value) << shift) >> shift;
434 }
435 return value;
436}
437
438/*
439 * Dumps encoded value.
440 */
441static void dumpEncodedValue(const DexFile* pDexFile, const u1** data); // forward
442static void dumpEncodedValue(const DexFile* pDexFile, const u1** data, u1 type, u1 arg) {
443 switch (type) {
444 case DexFile::kDexAnnotationByte:
445 fprintf(gOutFile, "%" PRId8, static_cast<s1>(readVarWidth(data, arg, false)));
446 break;
447 case DexFile::kDexAnnotationShort:
448 fprintf(gOutFile, "%" PRId16, static_cast<s2>(readVarWidth(data, arg, true)));
449 break;
450 case DexFile::kDexAnnotationChar:
451 fprintf(gOutFile, "%" PRIu16, static_cast<u2>(readVarWidth(data, arg, false)));
452 break;
453 case DexFile::kDexAnnotationInt:
454 fprintf(gOutFile, "%" PRId32, static_cast<s4>(readVarWidth(data, arg, true)));
455 break;
456 case DexFile::kDexAnnotationLong:
457 fprintf(gOutFile, "%" PRId64, static_cast<s8>(readVarWidth(data, arg, true)));
458 break;
459 case DexFile::kDexAnnotationFloat: {
460 // Fill on right.
461 union {
462 float f;
463 u4 data;
464 } conv;
465 conv.data = static_cast<u4>(readVarWidth(data, arg, false)) << (3 - arg) * 8;
466 fprintf(gOutFile, "%g", conv.f);
467 break;
468 }
469 case DexFile::kDexAnnotationDouble: {
470 // Fill on right.
471 union {
472 double d;
473 u8 data;
474 } conv;
475 conv.data = readVarWidth(data, arg, false) << (7 - arg) * 8;
476 fprintf(gOutFile, "%g", conv.d);
477 break;
478 }
479 case DexFile::kDexAnnotationString: {
480 const u4 idx = static_cast<u4>(readVarWidth(data, arg, false));
481 if (gOptions.outputFormat == OUTPUT_PLAIN) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800482 dumpEscapedString(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
Aart Bikdce50862016-06-10 16:04:03 -0700483 } else {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800484 dumpXmlAttribute(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
Aart Bikdce50862016-06-10 16:04:03 -0700485 }
486 break;
487 }
488 case DexFile::kDexAnnotationType: {
489 const u4 str_idx = static_cast<u4>(readVarWidth(data, arg, false));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800490 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(str_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700491 break;
492 }
493 case DexFile::kDexAnnotationField:
494 case DexFile::kDexAnnotationEnum: {
495 const u4 field_idx = static_cast<u4>(readVarWidth(data, arg, false));
496 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
497 fputs(pDexFile->StringDataByIdx(pFieldId.name_idx_), gOutFile);
498 break;
499 }
500 case DexFile::kDexAnnotationMethod: {
501 const u4 method_idx = static_cast<u4>(readVarWidth(data, arg, false));
502 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
503 fputs(pDexFile->StringDataByIdx(pMethodId.name_idx_), gOutFile);
504 break;
505 }
506 case DexFile::kDexAnnotationArray: {
507 fputc('{', gOutFile);
508 // Decode and display all elements.
509 const u4 size = DecodeUnsignedLeb128(data);
510 for (u4 i = 0; i < size; i++) {
511 fputc(' ', gOutFile);
512 dumpEncodedValue(pDexFile, data);
513 }
514 fputs(" }", gOutFile);
515 break;
516 }
517 case DexFile::kDexAnnotationAnnotation: {
518 const u4 type_idx = DecodeUnsignedLeb128(data);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800519 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(type_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700520 // Decode and display all name=value pairs.
521 const u4 size = DecodeUnsignedLeb128(data);
522 for (u4 i = 0; i < size; i++) {
523 const u4 name_idx = DecodeUnsignedLeb128(data);
524 fputc(' ', gOutFile);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800525 fputs(pDexFile->StringDataByIdx(dex::StringIndex(name_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700526 fputc('=', gOutFile);
527 dumpEncodedValue(pDexFile, data);
528 }
529 break;
530 }
531 case DexFile::kDexAnnotationNull:
532 fputs("null", gOutFile);
533 break;
534 case DexFile::kDexAnnotationBoolean:
535 fputs(strBool(arg), gOutFile);
536 break;
537 default:
538 fputs("????", gOutFile);
539 break;
540 } // switch
541}
542
543/*
544 * Dumps encoded value with prefix.
545 */
546static void dumpEncodedValue(const DexFile* pDexFile, const u1** data) {
547 const u1 enc = *(*data)++;
548 dumpEncodedValue(pDexFile, data, enc & 0x1f, enc >> 5);
549}
550
551/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700552 * Dumps the file header.
Aart Bik69ae54a2015-07-01 14:52:26 -0700553 */
554static void dumpFileHeader(const DexFile* pDexFile) {
555 const DexFile::Header& pHeader = pDexFile->GetHeader();
556 char sanitized[sizeof(pHeader.magic_) * 2 + 1];
557 fprintf(gOutFile, "DEX file header:\n");
558 asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
559 fprintf(gOutFile, "magic : '%s'\n", sanitized);
560 fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
561 fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
562 pHeader.signature_[0], pHeader.signature_[1],
563 pHeader.signature_[DexFile::kSha1DigestSize - 2],
564 pHeader.signature_[DexFile::kSha1DigestSize - 1]);
565 fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
566 fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
567 fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
568 fprintf(gOutFile, "link_off : %d (0x%06x)\n",
569 pHeader.link_off_, pHeader.link_off_);
570 fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
571 fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
572 pHeader.string_ids_off_, pHeader.string_ids_off_);
573 fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
574 fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
575 pHeader.type_ids_off_, pHeader.type_ids_off_);
Aart Bikdce50862016-06-10 16:04:03 -0700576 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
577 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
Aart Bik69ae54a2015-07-01 14:52:26 -0700578 pHeader.proto_ids_off_, pHeader.proto_ids_off_);
579 fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
580 fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
581 pHeader.field_ids_off_, pHeader.field_ids_off_);
582 fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
583 fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
584 pHeader.method_ids_off_, pHeader.method_ids_off_);
585 fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
586 fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
587 pHeader.class_defs_off_, pHeader.class_defs_off_);
588 fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
589 fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
590 pHeader.data_off_, pHeader.data_off_);
591}
592
593/*
594 * Dumps a class_def_item.
595 */
596static void dumpClassDef(const DexFile* pDexFile, int idx) {
597 // General class information.
598 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
599 fprintf(gOutFile, "Class #%d header:\n", idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800600 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700601 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
602 pClassDef.access_flags_, pClassDef.access_flags_);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800603 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700604 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
605 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800606 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700607 fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
608 pClassDef.annotations_off_, pClassDef.annotations_off_);
609 fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
610 pClassDef.class_data_off_, pClassDef.class_data_off_);
611
612 // Fields and methods.
613 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
614 if (pEncodedData != nullptr) {
615 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
616 fprintf(gOutFile, "static_fields_size : %d\n", pClassData.NumStaticFields());
617 fprintf(gOutFile, "instance_fields_size: %d\n", pClassData.NumInstanceFields());
618 fprintf(gOutFile, "direct_methods_size : %d\n", pClassData.NumDirectMethods());
619 fprintf(gOutFile, "virtual_methods_size: %d\n", pClassData.NumVirtualMethods());
620 } else {
621 fprintf(gOutFile, "static_fields_size : 0\n");
622 fprintf(gOutFile, "instance_fields_size: 0\n");
623 fprintf(gOutFile, "direct_methods_size : 0\n");
624 fprintf(gOutFile, "virtual_methods_size: 0\n");
625 }
626 fprintf(gOutFile, "\n");
627}
628
Aart Bikdce50862016-06-10 16:04:03 -0700629/**
630 * Dumps an annotation set item.
631 */
632static void dumpAnnotationSetItem(const DexFile* pDexFile, const DexFile::AnnotationSetItem* set_item) {
633 if (set_item == nullptr || set_item->size_ == 0) {
634 fputs(" empty-annotation-set\n", gOutFile);
635 return;
636 }
637 for (u4 i = 0; i < set_item->size_; i++) {
638 const DexFile::AnnotationItem* annotation = pDexFile->GetAnnotationItem(set_item, i);
639 if (annotation == nullptr) {
640 continue;
641 }
642 fputs(" ", gOutFile);
643 switch (annotation->visibility_) {
644 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", gOutFile); break;
645 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", gOutFile); break;
646 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", gOutFile); break;
647 default: fputs("VISIBILITY_UNKNOWN ", gOutFile); break;
648 } // switch
649 // Decode raw bytes in annotation.
650 const u1* rData = annotation->annotation_;
651 dumpEncodedValue(pDexFile, &rData, DexFile::kDexAnnotationAnnotation, 0);
652 fputc('\n', gOutFile);
653 }
654}
655
656/*
657 * Dumps class annotations.
658 */
659static void dumpClassAnnotations(const DexFile* pDexFile, int idx) {
660 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
661 const DexFile::AnnotationsDirectoryItem* dir = pDexFile->GetAnnotationsDirectory(pClassDef);
662 if (dir == nullptr) {
663 return; // none
664 }
665
666 fprintf(gOutFile, "Class #%d annotations:\n", idx);
667
668 const DexFile::AnnotationSetItem* class_set_item = pDexFile->GetClassAnnotationSet(dir);
669 const DexFile::FieldAnnotationsItem* fields = pDexFile->GetFieldAnnotations(dir);
670 const DexFile::MethodAnnotationsItem* methods = pDexFile->GetMethodAnnotations(dir);
671 const DexFile::ParameterAnnotationsItem* pars = pDexFile->GetParameterAnnotations(dir);
672
673 // Annotations on the class itself.
674 if (class_set_item != nullptr) {
675 fprintf(gOutFile, "Annotations on class\n");
676 dumpAnnotationSetItem(pDexFile, class_set_item);
677 }
678
679 // Annotations on fields.
680 if (fields != nullptr) {
681 for (u4 i = 0; i < dir->fields_size_; i++) {
682 const u4 field_idx = fields[i].field_idx_;
683 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
684 const char* field_name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
685 fprintf(gOutFile, "Annotations on field #%u '%s'\n", field_idx, field_name);
686 dumpAnnotationSetItem(pDexFile, pDexFile->GetFieldAnnotationSetItem(fields[i]));
687 }
688 }
689
690 // Annotations on methods.
691 if (methods != nullptr) {
692 for (u4 i = 0; i < dir->methods_size_; i++) {
693 const u4 method_idx = methods[i].method_idx_;
694 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
695 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
696 fprintf(gOutFile, "Annotations on method #%u '%s'\n", method_idx, method_name);
697 dumpAnnotationSetItem(pDexFile, pDexFile->GetMethodAnnotationSetItem(methods[i]));
698 }
699 }
700
701 // Annotations on method parameters.
702 if (pars != nullptr) {
703 for (u4 i = 0; i < dir->parameters_size_; i++) {
704 const u4 method_idx = pars[i].method_idx_;
705 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
706 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
707 fprintf(gOutFile, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
708 const DexFile::AnnotationSetRefList*
709 list = pDexFile->GetParameterAnnotationSetRefList(&pars[i]);
710 if (list != nullptr) {
711 for (u4 j = 0; j < list->size_; j++) {
712 fprintf(gOutFile, "#%u\n", j);
713 dumpAnnotationSetItem(pDexFile, pDexFile->GetSetRefItemItem(&list->list_[j]));
714 }
715 }
716 }
717 }
718
719 fputc('\n', gOutFile);
720}
721
Aart Bik69ae54a2015-07-01 14:52:26 -0700722/*
723 * Dumps an interface that a class declares to implement.
724 */
725static void dumpInterface(const DexFile* pDexFile, const DexFile::TypeItem& pTypeItem, int i) {
726 const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
727 if (gOptions.outputFormat == OUTPUT_PLAIN) {
728 fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
729 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -0700730 std::unique_ptr<char[]> dot(descriptorToDot(interfaceName));
731 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -0700732 }
733}
734
735/*
736 * Dumps the catches table associated with the code.
737 */
738static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800739 CodeItemDataAccessor accessor(*pDexFile, pCode);
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800740 const u4 triesSize = accessor.TriesSize();
Aart Bik69ae54a2015-07-01 14:52:26 -0700741
742 // No catch table.
743 if (triesSize == 0) {
744 fprintf(gOutFile, " catches : (none)\n");
745 return;
746 }
747
748 // Dump all table entries.
749 fprintf(gOutFile, " catches : %d\n", triesSize);
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800750 for (const DexFile::TryItem& try_item : accessor.TryItems()) {
751 const u4 start = try_item.start_addr_;
752 const u4 end = start + try_item.insn_count_;
Aart Bik69ae54a2015-07-01 14:52:26 -0700753 fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800754 for (CatchHandlerIterator it(accessor, try_item); it.HasNext(); it.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800755 const dex::TypeIndex tidx = it.GetHandlerTypeIndex();
756 const char* descriptor = (!tidx.IsValid()) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
Aart Bik69ae54a2015-07-01 14:52:26 -0700757 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
758 } // for
759 } // for
760}
761
762/*
763 * Callback for dumping each positions table entry.
764 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000765static bool dumpPositionsCb(void* /*context*/, const DexFile::PositionInfo& entry) {
766 fprintf(gOutFile, " 0x%04x line=%d\n", entry.address_, entry.line_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700767 return false;
768}
769
770/*
771 * Callback for dumping locals table entry.
772 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000773static void dumpLocalsCb(void* /*context*/, const DexFile::LocalInfo& entry) {
774 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
Aart Bik69ae54a2015-07-01 14:52:26 -0700775 fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
David Srbeckyb06e28e2015-12-10 13:15:00 +0000776 entry.start_address_, entry.end_address_, entry.reg_,
777 entry.name_, entry.descriptor_, signature);
Aart Bik69ae54a2015-07-01 14:52:26 -0700778}
779
780/*
781 * Helper for dumpInstruction(), which builds the string
Aart Bika0e33fd2016-07-08 18:32:45 -0700782 * representation for the index in the given instruction.
783 * Returns a pointer to a buffer of sufficient size.
Aart Bik69ae54a2015-07-01 14:52:26 -0700784 */
Aart Bika0e33fd2016-07-08 18:32:45 -0700785static std::unique_ptr<char[]> indexString(const DexFile* pDexFile,
786 const Instruction* pDecInsn,
787 size_t bufSize) {
Orion Hodsonb34bb192016-10-18 17:02:58 +0100788 static const u4 kInvalidIndex = std::numeric_limits<u4>::max();
Aart Bika0e33fd2016-07-08 18:32:45 -0700789 std::unique_ptr<char[]> buf(new char[bufSize]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700790 // Determine index and width of the string.
791 u4 index = 0;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100792 u4 secondary_index = kInvalidIndex;
Aart Bik69ae54a2015-07-01 14:52:26 -0700793 u4 width = 4;
794 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
795 // SOME NOT SUPPORTED:
796 // case Instruction::k20bc:
797 case Instruction::k21c:
798 case Instruction::k35c:
799 // case Instruction::k35ms:
800 case Instruction::k3rc:
801 // case Instruction::k3rms:
802 // case Instruction::k35mi:
803 // case Instruction::k3rmi:
804 index = pDecInsn->VRegB();
805 width = 4;
806 break;
807 case Instruction::k31c:
808 index = pDecInsn->VRegB();
809 width = 8;
810 break;
811 case Instruction::k22c:
812 // case Instruction::k22cs:
813 index = pDecInsn->VRegC();
814 width = 4;
815 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100816 case Instruction::k45cc:
817 case Instruction::k4rcc:
818 index = pDecInsn->VRegB();
819 secondary_index = pDecInsn->VRegH();
820 width = 4;
821 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700822 default:
823 break;
824 } // switch
825
826 // Determine index type.
827 size_t outSize = 0;
828 switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
829 case Instruction::kIndexUnknown:
830 // This function should never get called for this type, but do
831 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700832 outSize = snprintf(buf.get(), bufSize, "<unknown-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700833 break;
834 case Instruction::kIndexNone:
835 // This function should never get called for this type, but do
836 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700837 outSize = snprintf(buf.get(), bufSize, "<no-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700838 break;
839 case Instruction::kIndexTypeRef:
840 if (index < pDexFile->GetHeader().type_ids_size_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800841 const char* tp = pDexFile->StringByTypeIdx(dex::TypeIndex(index));
Aart Bika0e33fd2016-07-08 18:32:45 -0700842 outSize = snprintf(buf.get(), bufSize, "%s // type@%0*x", tp, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700843 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700844 outSize = snprintf(buf.get(), bufSize, "<type?> // type@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700845 }
846 break;
847 case Instruction::kIndexStringRef:
848 if (index < pDexFile->GetHeader().string_ids_size_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800849 const char* st = pDexFile->StringDataByIdx(dex::StringIndex(index));
Aart Bika0e33fd2016-07-08 18:32:45 -0700850 outSize = snprintf(buf.get(), bufSize, "\"%s\" // string@%0*x", st, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700851 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700852 outSize = snprintf(buf.get(), bufSize, "<string?> // string@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700853 }
854 break;
855 case Instruction::kIndexMethodRef:
856 if (index < pDexFile->GetHeader().method_ids_size_) {
857 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
858 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
859 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
860 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700861 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // method@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700862 backDescriptor, name, signature.ToString().c_str(), width, index);
863 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700864 outSize = snprintf(buf.get(), bufSize, "<method?> // method@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700865 }
866 break;
867 case Instruction::kIndexFieldRef:
868 if (index < pDexFile->GetHeader().field_ids_size_) {
869 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index);
870 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
871 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
872 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700873 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // field@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700874 backDescriptor, name, typeDescriptor, width, index);
875 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700876 outSize = snprintf(buf.get(), bufSize, "<field?> // field@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700877 }
878 break;
879 case Instruction::kIndexVtableOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700880 outSize = snprintf(buf.get(), bufSize, "[%0*x] // vtable #%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700881 width, index, width, index);
882 break;
883 case Instruction::kIndexFieldOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700884 outSize = snprintf(buf.get(), bufSize, "[obj+%0*x]", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700885 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100886 case Instruction::kIndexMethodAndProtoRef: {
Orion Hodsonc069a302017-01-18 09:23:12 +0000887 std::string method("<method?>");
888 std::string proto("<proto?>");
889 if (index < pDexFile->GetHeader().method_ids_size_) {
890 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
891 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
892 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
893 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
894 method = android::base::StringPrintf("%s.%s:%s",
895 backDescriptor,
896 name,
897 signature.ToString().c_str());
Orion Hodsonb34bb192016-10-18 17:02:58 +0100898 }
Orion Hodsonc069a302017-01-18 09:23:12 +0000899 if (secondary_index < pDexFile->GetHeader().proto_ids_size_) {
900 const DexFile::ProtoId& protoId = pDexFile->GetProtoId(secondary_index);
901 const Signature signature = pDexFile->GetProtoSignature(protoId);
902 proto = signature.ToString();
903 }
904 outSize = snprintf(buf.get(), bufSize, "%s, %s // method@%0*x, proto@%0*x",
905 method.c_str(), proto.c_str(), width, index, width, secondary_index);
906 break;
907 }
908 case Instruction::kIndexCallSiteRef:
909 // Call site information is too large to detail in disassembly so just output the index.
910 outSize = snprintf(buf.get(), bufSize, "call_site@%0*x", width, index);
Orion Hodsonb34bb192016-10-18 17:02:58 +0100911 break;
Orion Hodson2e599942017-09-22 16:17:41 +0100912 case Instruction::kIndexMethodHandleRef:
913 // Method handle information is too large to detail in disassembly so just output the index.
914 outSize = snprintf(buf.get(), bufSize, "method_handle@%0*x", width, index);
915 break;
916 case Instruction::kIndexProtoRef:
917 if (index < pDexFile->GetHeader().proto_ids_size_) {
918 const DexFile::ProtoId& protoId = pDexFile->GetProtoId(index);
919 const Signature signature = pDexFile->GetProtoSignature(protoId);
920 const std::string& proto = signature.ToString();
921 outSize = snprintf(buf.get(), bufSize, "%s // proto@%0*x", proto.c_str(), width, index);
922 } else {
923 outSize = snprintf(buf.get(), bufSize, "<?> // proto@%0*x", width, index);
924 }
Aart Bik69ae54a2015-07-01 14:52:26 -0700925 break;
926 } // switch
927
Orion Hodson2e599942017-09-22 16:17:41 +0100928 if (outSize == 0) {
929 // The index type has not been handled in the switch above.
930 outSize = snprintf(buf.get(), bufSize, "<?>");
931 }
932
Aart Bik69ae54a2015-07-01 14:52:26 -0700933 // Determine success of string construction.
934 if (outSize >= bufSize) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700935 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
936 // doesn't count/ the '\0' as part of its returned size, so we add explicit
937 // space for it here.
938 return indexString(pDexFile, pDecInsn, outSize + 1);
Aart Bik69ae54a2015-07-01 14:52:26 -0700939 }
940 return buf;
941}
942
943/*
944 * Dumps a single instruction.
945 */
946static void dumpInstruction(const DexFile* pDexFile,
947 const DexFile::CodeItem* pCode,
948 u4 codeOffset, u4 insnIdx, u4 insnWidth,
949 const Instruction* pDecInsn) {
950 // Address of instruction (expressed as byte offset).
951 fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
952
953 // Dump (part of) raw bytes.
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800954 CodeItemInstructionAccessor accessor(*pDexFile, pCode);
Aart Bik69ae54a2015-07-01 14:52:26 -0700955 for (u4 i = 0; i < 8; i++) {
956 if (i < insnWidth) {
957 if (i == 7) {
958 fprintf(gOutFile, " ... ");
959 } else {
960 // Print 16-bit value in little-endian order.
Mathieu Chartier641a3af2017-12-15 11:42:58 -0800961 const u1* bytePtr = (const u1*) &accessor.Insns()[insnIdx + i];
Aart Bik69ae54a2015-07-01 14:52:26 -0700962 fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
963 }
964 } else {
965 fputs(" ", gOutFile);
966 }
967 } // for
968
969 // Dump pseudo-instruction or opcode.
970 if (pDecInsn->Opcode() == Instruction::NOP) {
Mathieu Chartier641a3af2017-12-15 11:42:58 -0800971 const u2 instr = get2LE((const u1*) &accessor.Insns()[insnIdx]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700972 if (instr == Instruction::kPackedSwitchSignature) {
973 fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
974 } else if (instr == Instruction::kSparseSwitchSignature) {
975 fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
976 } else if (instr == Instruction::kArrayDataSignature) {
977 fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
978 } else {
979 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
980 }
981 } else {
982 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
983 }
984
985 // Set up additional argument.
Aart Bika0e33fd2016-07-08 18:32:45 -0700986 std::unique_ptr<char[]> indexBuf;
Aart Bik69ae54a2015-07-01 14:52:26 -0700987 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700988 indexBuf = indexString(pDexFile, pDecInsn, 200);
Aart Bik69ae54a2015-07-01 14:52:26 -0700989 }
990
991 // Dump the instruction.
992 //
993 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
994 //
995 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
996 case Instruction::k10x: // op
997 break;
998 case Instruction::k12x: // op vA, vB
999 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1000 break;
1001 case Instruction::k11n: // op vA, #+B
1002 fprintf(gOutFile, " v%d, #int %d // #%x",
1003 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
1004 break;
1005 case Instruction::k11x: // op vAA
1006 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
1007 break;
1008 case Instruction::k10t: // op +AA
Aart Bikdce50862016-06-10 16:04:03 -07001009 case Instruction::k20t: { // op +AAAA
1010 const s4 targ = (s4) pDecInsn->VRegA();
1011 fprintf(gOutFile, " %04x // %c%04x",
1012 insnIdx + targ,
1013 (targ < 0) ? '-' : '+',
1014 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001015 break;
Aart Bikdce50862016-06-10 16:04:03 -07001016 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001017 case Instruction::k22x: // op vAA, vBBBB
1018 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1019 break;
Aart Bikdce50862016-06-10 16:04:03 -07001020 case Instruction::k21t: { // op vAA, +BBBB
1021 const s4 targ = (s4) pDecInsn->VRegB();
1022 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
1023 insnIdx + targ,
1024 (targ < 0) ? '-' : '+',
1025 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001026 break;
Aart Bikdce50862016-06-10 16:04:03 -07001027 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001028 case Instruction::k21s: // op vAA, #+BBBB
1029 fprintf(gOutFile, " v%d, #int %d // #%x",
1030 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
1031 break;
1032 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
1033 // The printed format varies a bit based on the actual opcode.
1034 if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) {
1035 const s4 value = pDecInsn->VRegB() << 16;
1036 fprintf(gOutFile, " v%d, #int %d // #%x",
1037 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1038 } else {
1039 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
1040 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
1041 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1042 }
1043 break;
1044 case Instruction::k21c: // op vAA, thing@BBBB
1045 case Instruction::k31c: // op vAA, thing@BBBBBBBB
Aart Bika0e33fd2016-07-08 18:32:45 -07001046 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001047 break;
1048 case Instruction::k23x: // op vAA, vBB, vCC
1049 fprintf(gOutFile, " v%d, v%d, v%d",
1050 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
1051 break;
1052 case Instruction::k22b: // op vAA, vBB, #+CC
1053 fprintf(gOutFile, " v%d, v%d, #int %d // #%02x",
1054 pDecInsn->VRegA(), pDecInsn->VRegB(),
1055 (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC());
1056 break;
Aart Bikdce50862016-06-10 16:04:03 -07001057 case Instruction::k22t: { // op vA, vB, +CCCC
1058 const s4 targ = (s4) pDecInsn->VRegC();
1059 fprintf(gOutFile, " v%d, v%d, %04x // %c%04x",
1060 pDecInsn->VRegA(), pDecInsn->VRegB(),
1061 insnIdx + targ,
1062 (targ < 0) ? '-' : '+',
1063 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001064 break;
Aart Bikdce50862016-06-10 16:04:03 -07001065 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001066 case Instruction::k22s: // op vA, vB, #+CCCC
1067 fprintf(gOutFile, " v%d, v%d, #int %d // #%04x",
1068 pDecInsn->VRegA(), pDecInsn->VRegB(),
1069 (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC());
1070 break;
1071 case Instruction::k22c: // op vA, vB, thing@CCCC
1072 // NOT SUPPORTED:
1073 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
1074 fprintf(gOutFile, " v%d, v%d, %s",
Aart Bika0e33fd2016-07-08 18:32:45 -07001075 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001076 break;
1077 case Instruction::k30t:
1078 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
1079 break;
Aart Bikdce50862016-06-10 16:04:03 -07001080 case Instruction::k31i: { // op vAA, #+BBBBBBBB
1081 // This is often, but not always, a float.
1082 union {
1083 float f;
1084 u4 i;
1085 } conv;
1086 conv.i = pDecInsn->VRegB();
1087 fprintf(gOutFile, " v%d, #float %g // #%08x",
1088 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001089 break;
Aart Bikdce50862016-06-10 16:04:03 -07001090 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001091 case Instruction::k31t: // op vAA, offset +BBBBBBBB
1092 fprintf(gOutFile, " v%d, %08x // +%08x",
1093 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
1094 break;
1095 case Instruction::k32x: // op vAAAA, vBBBB
1096 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1097 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +01001098 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
1099 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, method@BBBB, proto@HHHH
Aart Bik69ae54a2015-07-01 14:52:26 -07001100 // NOT SUPPORTED:
1101 // case Instruction::k35ms: // [opt] invoke-virtual+super
1102 // case Instruction::k35mi: // [opt] inline invoke
Aart Bikdce50862016-06-10 16:04:03 -07001103 u4 arg[Instruction::kMaxVarArgRegs];
1104 pDecInsn->GetVarArgs(arg);
1105 fputs(" {", gOutFile);
1106 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1107 if (i == 0) {
1108 fprintf(gOutFile, "v%d", arg[i]);
1109 } else {
1110 fprintf(gOutFile, ", v%d", arg[i]);
1111 }
1112 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001113 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001114 break;
Aart Bikdce50862016-06-10 16:04:03 -07001115 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001116 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
Orion Hodsonb34bb192016-10-18 17:02:58 +01001117 case Instruction::k4rcc: { // op {vCCCC .. v(CCCC+AA-1)}, method@BBBB, proto@HHHH
Aart Bik69ae54a2015-07-01 14:52:26 -07001118 // NOT SUPPORTED:
1119 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1120 // case Instruction::k3rmi: // [opt] execute-inline/range
Aart Bik69ae54a2015-07-01 14:52:26 -07001121 // This doesn't match the "dx" output when some of the args are
1122 // 64-bit values -- dx only shows the first register.
1123 fputs(" {", gOutFile);
1124 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1125 if (i == 0) {
1126 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
1127 } else {
1128 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
1129 }
1130 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001131 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001132 }
1133 break;
Aart Bikdce50862016-06-10 16:04:03 -07001134 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1135 // This is often, but not always, a double.
1136 union {
1137 double d;
1138 u8 j;
1139 } conv;
1140 conv.j = pDecInsn->WideVRegB();
1141 fprintf(gOutFile, " v%d, #double %g // #%016" PRIx64,
1142 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001143 break;
Aart Bikdce50862016-06-10 16:04:03 -07001144 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001145 // NOT SUPPORTED:
1146 // case Instruction::k00x: // unknown op or breakpoint
1147 // break;
1148 default:
1149 fprintf(gOutFile, " ???");
1150 break;
1151 } // switch
1152
1153 fputc('\n', gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001154}
1155
1156/*
1157 * Dumps a bytecode disassembly.
1158 */
1159static void dumpBytecodes(const DexFile* pDexFile, u4 idx,
1160 const DexFile::CodeItem* pCode, u4 codeOffset) {
1161 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1162 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1163 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1164 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1165
1166 // Generate header.
Aart Bikc05e2f22016-07-12 15:53:13 -07001167 std::unique_ptr<char[]> dot(descriptorToDot(backDescriptor));
1168 fprintf(gOutFile, "%06x: |[%06x] %s.%s:%s\n",
1169 codeOffset, codeOffset, dot.get(), name, signature.ToString().c_str());
Aart Bik69ae54a2015-07-01 14:52:26 -07001170
1171 // Iterate over all instructions.
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001172 CodeItemDataAccessor accessor(*pDexFile, pCode);
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001173 for (const DexInstructionPcPair& pair : accessor) {
1174 const Instruction* instruction = &pair.Inst();
Aart Bik69ae54a2015-07-01 14:52:26 -07001175 const u4 insnWidth = instruction->SizeInCodeUnits();
1176 if (insnWidth == 0) {
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001177 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", pair.DexPc());
Aart Bik69ae54a2015-07-01 14:52:26 -07001178 break;
1179 }
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001180 dumpInstruction(pDexFile, pCode, codeOffset, pair.DexPc(), insnWidth, instruction);
Aart Bik69ae54a2015-07-01 14:52:26 -07001181 } // for
1182}
1183
1184/*
1185 * Dumps code of a method.
1186 */
1187static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags,
1188 const DexFile::CodeItem* pCode, u4 codeOffset) {
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001189 CodeItemDebugInfoAccessor accessor(*pDexFile, pCode, pDexFile->GetDebugInfoOffset(pCode));
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001190
1191 fprintf(gOutFile, " registers : %d\n", accessor.RegistersSize());
1192 fprintf(gOutFile, " ins : %d\n", accessor.InsSize());
1193 fprintf(gOutFile, " outs : %d\n", accessor.OutsSize());
Aart Bik69ae54a2015-07-01 14:52:26 -07001194 fprintf(gOutFile, " insns size : %d 16-bit code units\n",
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001195 accessor.InsnsSizeInCodeUnits());
Aart Bik69ae54a2015-07-01 14:52:26 -07001196
1197 // Bytecode disassembly, if requested.
1198 if (gOptions.disassemble) {
1199 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
1200 }
1201
1202 // Try-catch blocks.
1203 dumpCatches(pDexFile, pCode);
1204
1205 // Positions and locals table in the debug info.
1206 bool is_static = (flags & kAccStatic) != 0;
1207 fprintf(gOutFile, " positions : \n");
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001208 pDexFile->DecodeDebugPositionInfo(accessor.DebugInfoOffset(), dumpPositionsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001209 fprintf(gOutFile, " locals : \n");
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001210 accessor.DecodeDebugLocalInfo(is_static, idx, dumpLocalsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001211}
1212
1213/*
1214 * Dumps a method.
1215 */
1216static void dumpMethod(const DexFile* pDexFile, u4 idx, u4 flags,
1217 const DexFile::CodeItem* pCode, u4 codeOffset, int i) {
1218 // Bail for anything private if export only requested.
1219 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1220 return;
1221 }
1222
1223 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1224 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1225 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1226 char* typeDescriptor = strdup(signature.ToString().c_str());
1227 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1228 char* accessStr = createAccessFlagStr(flags, kAccessForMethod);
1229
1230 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1231 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1232 fprintf(gOutFile, " name : '%s'\n", name);
1233 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1234 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
1235 if (pCode == nullptr) {
1236 fprintf(gOutFile, " code : (none)\n");
1237 } else {
1238 fprintf(gOutFile, " code -\n");
1239 dumpCode(pDexFile, idx, flags, pCode, codeOffset);
1240 }
1241 if (gOptions.disassemble) {
1242 fputc('\n', gOutFile);
1243 }
1244 } else if (gOptions.outputFormat == OUTPUT_XML) {
1245 const bool constructor = (name[0] == '<');
1246
1247 // Method name and prototype.
1248 if (constructor) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001249 std::unique_ptr<char[]> dot(descriptorClassToDot(backDescriptor));
1250 fprintf(gOutFile, "<constructor name=\"%s\"\n", dot.get());
1251 dot = descriptorToDot(backDescriptor);
1252 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001253 } else {
1254 fprintf(gOutFile, "<method name=\"%s\"\n", name);
1255 const char* returnType = strrchr(typeDescriptor, ')');
1256 if (returnType == nullptr) {
1257 fprintf(stderr, "bad method type descriptor '%s'\n", typeDescriptor);
1258 goto bail;
1259 }
Aart Bikc05e2f22016-07-12 15:53:13 -07001260 std::unique_ptr<char[]> dot(descriptorToDot(returnType + 1));
1261 fprintf(gOutFile, " return=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001262 fprintf(gOutFile, " abstract=%s\n", quotedBool((flags & kAccAbstract) != 0));
1263 fprintf(gOutFile, " native=%s\n", quotedBool((flags & kAccNative) != 0));
1264 fprintf(gOutFile, " synchronized=%s\n", quotedBool(
1265 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1266 }
1267
1268 // Additional method flags.
1269 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1270 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1271 // The "deprecated=" not knowable w/o parsing annotations.
1272 fprintf(gOutFile, " visibility=%s\n>\n", quotedVisibility(flags));
1273
1274 // Parameters.
1275 if (typeDescriptor[0] != '(') {
1276 fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor);
1277 goto bail;
1278 }
1279 char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1));
1280 const char* base = typeDescriptor + 1;
1281 int argNum = 0;
1282 while (*base != ')') {
1283 char* cp = tmpBuf;
1284 while (*base == '[') {
1285 *cp++ = *base++;
1286 }
1287 if (*base == 'L') {
1288 // Copy through ';'.
1289 do {
1290 *cp = *base++;
1291 } while (*cp++ != ';');
1292 } else {
1293 // Primitive char, copy it.
Aart Bikc05e2f22016-07-12 15:53:13 -07001294 if (strchr("ZBCSIFJD", *base) == nullptr) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001295 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
Aart Bika0e33fd2016-07-08 18:32:45 -07001296 break; // while
Aart Bik69ae54a2015-07-01 14:52:26 -07001297 }
1298 *cp++ = *base++;
1299 }
1300 // Null terminate and display.
1301 *cp++ = '\0';
Aart Bikc05e2f22016-07-12 15:53:13 -07001302 std::unique_ptr<char[]> dot(descriptorToDot(tmpBuf));
Aart Bik69ae54a2015-07-01 14:52:26 -07001303 fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n"
Aart Bikc05e2f22016-07-12 15:53:13 -07001304 "</parameter>\n", argNum++, dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001305 } // while
1306 free(tmpBuf);
1307 if (constructor) {
1308 fprintf(gOutFile, "</constructor>\n");
1309 } else {
1310 fprintf(gOutFile, "</method>\n");
1311 }
1312 }
1313
1314 bail:
1315 free(typeDescriptor);
1316 free(accessStr);
1317}
1318
1319/*
1320 * Dumps a static (class) field.
1321 */
Aart Bikdce50862016-06-10 16:04:03 -07001322static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i, const u1** data) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001323 // Bail for anything private if export only requested.
1324 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1325 return;
1326 }
1327
1328 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(idx);
1329 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
1330 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
1331 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
1332 char* accessStr = createAccessFlagStr(flags, kAccessForField);
1333
1334 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1335 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1336 fprintf(gOutFile, " name : '%s'\n", name);
1337 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1338 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
Aart Bikdce50862016-06-10 16:04:03 -07001339 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001340 fputs(" value : ", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001341 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001342 fputs("\n", gOutFile);
1343 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001344 } else if (gOptions.outputFormat == OUTPUT_XML) {
1345 fprintf(gOutFile, "<field name=\"%s\"\n", name);
Aart Bikc05e2f22016-07-12 15:53:13 -07001346 std::unique_ptr<char[]> dot(descriptorToDot(typeDescriptor));
1347 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001348 fprintf(gOutFile, " transient=%s\n", quotedBool((flags & kAccTransient) != 0));
1349 fprintf(gOutFile, " volatile=%s\n", quotedBool((flags & kAccVolatile) != 0));
1350 // The "value=" is not knowable w/o parsing annotations.
1351 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1352 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1353 // The "deprecated=" is not knowable w/o parsing annotations.
1354 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(flags));
Aart Bikdce50862016-06-10 16:04:03 -07001355 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001356 fputs(" value=\"", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001357 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001358 fputs("\"\n", gOutFile);
1359 }
1360 fputs(">\n</field>\n", gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001361 }
1362
1363 free(accessStr);
1364}
1365
1366/*
1367 * Dumps an instance field.
1368 */
1369static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
Aart Bikdce50862016-06-10 16:04:03 -07001370 dumpSField(pDexFile, idx, flags, i, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001371}
1372
1373/*
Andreas Gampe5073fed2015-08-10 11:40:25 -07001374 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1375 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1376 * tool, so this is not performance-critical.
1377 */
1378
1379static void dumpCfg(const DexFile* dex_file,
Aart Bikdce50862016-06-10 16:04:03 -07001380 u4 dex_method_idx,
Andreas Gampe5073fed2015-08-10 11:40:25 -07001381 const DexFile::CodeItem* code_item) {
1382 if (code_item != nullptr) {
1383 std::ostringstream oss;
1384 DumpMethodCFG(dex_file, dex_method_idx, oss);
David Sehrcaacd112016-10-20 16:27:02 -07001385 fputs(oss.str().c_str(), gOutFile);
Andreas Gampe5073fed2015-08-10 11:40:25 -07001386 }
1387}
1388
1389static void dumpCfg(const DexFile* dex_file, int idx) {
1390 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
Aart Bikdce50862016-06-10 16:04:03 -07001391 const u1* class_data = dex_file->GetClassData(class_def);
Andreas Gampe5073fed2015-08-10 11:40:25 -07001392 if (class_data == nullptr) { // empty class such as a marker interface?
1393 return;
1394 }
1395 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001396 it.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -08001397 while (it.HasNextMethod()) {
Andreas Gampe5073fed2015-08-10 11:40:25 -07001398 dumpCfg(dex_file,
1399 it.GetMemberIndex(),
1400 it.GetMethodCodeItem());
1401 it.Next();
1402 }
Andreas Gampe5073fed2015-08-10 11:40:25 -07001403}
1404
1405/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001406 * Dumps the class.
1407 *
1408 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1409 *
1410 * If "*pLastPackage" is nullptr or does not match the current class' package,
1411 * the value will be replaced with a newly-allocated string.
1412 */
1413static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1414 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1415
1416 // Omitting non-public class.
1417 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1418 return;
1419 }
1420
Aart Bikdce50862016-06-10 16:04:03 -07001421 if (gOptions.showSectionHeaders) {
1422 dumpClassDef(pDexFile, idx);
1423 }
1424
1425 if (gOptions.showAnnotations) {
1426 dumpClassAnnotations(pDexFile, idx);
1427 }
1428
1429 if (gOptions.showCfg) {
Andreas Gampe5073fed2015-08-10 11:40:25 -07001430 dumpCfg(pDexFile, idx);
1431 return;
1432 }
1433
Aart Bik69ae54a2015-07-01 14:52:26 -07001434 // For the XML output, show the package name. Ideally we'd gather
1435 // up the classes, sort them, and dump them alphabetically so the
1436 // package name wouldn't jump around, but that's not a great plan
1437 // for something that needs to run on the device.
1438 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1439 if (!(classDescriptor[0] == 'L' &&
1440 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1441 // Arrays and primitives should not be defined explicitly. Keep going?
1442 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1443 } else if (gOptions.outputFormat == OUTPUT_XML) {
1444 char* mangle = strdup(classDescriptor + 1);
1445 mangle[strlen(mangle)-1] = '\0';
1446
1447 // Reduce to just the package name.
1448 char* lastSlash = strrchr(mangle, '/');
1449 if (lastSlash != nullptr) {
1450 *lastSlash = '\0';
1451 } else {
1452 *mangle = '\0';
1453 }
1454
1455 for (char* cp = mangle; *cp != '\0'; cp++) {
1456 if (*cp == '/') {
1457 *cp = '.';
1458 }
1459 } // for
1460
1461 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1462 // Start of a new package.
1463 if (*pLastPackage != nullptr) {
1464 fprintf(gOutFile, "</package>\n");
1465 }
1466 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1467 free(*pLastPackage);
1468 *pLastPackage = mangle;
1469 } else {
1470 free(mangle);
1471 }
1472 }
1473
1474 // General class information.
1475 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1476 const char* superclassDescriptor;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001477 if (!pClassDef.superclass_idx_.IsValid()) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001478 superclassDescriptor = nullptr;
1479 } else {
1480 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1481 }
1482 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1483 fprintf(gOutFile, "Class #%d -\n", idx);
1484 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1485 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1486 if (superclassDescriptor != nullptr) {
1487 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1488 }
1489 fprintf(gOutFile, " Interfaces -\n");
1490 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -07001491 std::unique_ptr<char[]> dot(descriptorClassToDot(classDescriptor));
1492 fprintf(gOutFile, "<class name=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001493 if (superclassDescriptor != nullptr) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001494 dot = descriptorToDot(superclassDescriptor);
1495 fprintf(gOutFile, " extends=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001496 }
Alex Light1f12e282015-12-10 16:49:47 -08001497 fprintf(gOutFile, " interface=%s\n",
1498 quotedBool((pClassDef.access_flags_ & kAccInterface) != 0));
Aart Bik69ae54a2015-07-01 14:52:26 -07001499 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1500 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1501 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1502 // The "deprecated=" not knowable w/o parsing annotations.
1503 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1504 fprintf(gOutFile, ">\n");
1505 }
1506
1507 // Interfaces.
1508 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1509 if (pInterfaces != nullptr) {
1510 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1511 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1512 } // for
1513 }
1514
1515 // Fields and methods.
1516 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
1517 if (pEncodedData == nullptr) {
1518 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1519 fprintf(gOutFile, " Static fields -\n");
1520 fprintf(gOutFile, " Instance fields -\n");
1521 fprintf(gOutFile, " Direct methods -\n");
1522 fprintf(gOutFile, " Virtual methods -\n");
1523 }
1524 } else {
1525 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
Aart Bikdce50862016-06-10 16:04:03 -07001526
1527 // Prepare data for static fields.
1528 const u1* sData = pDexFile->GetEncodedStaticFieldValuesArray(pClassDef);
1529 const u4 sSize = sData != nullptr ? DecodeUnsignedLeb128(&sData) : 0;
1530
1531 // Static fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001532 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1533 fprintf(gOutFile, " Static fields -\n");
1534 }
Aart Bikdce50862016-06-10 16:04:03 -07001535 for (u4 i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) {
1536 dumpSField(pDexFile,
1537 pClassData.GetMemberIndex(),
1538 pClassData.GetRawMemberAccessFlags(),
1539 i,
1540 i < sSize ? &sData : nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001541 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001542
1543 // Instance fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001544 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1545 fprintf(gOutFile, " Instance fields -\n");
1546 }
Aart Bikdce50862016-06-10 16:04:03 -07001547 for (u4 i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) {
1548 dumpIField(pDexFile,
1549 pClassData.GetMemberIndex(),
1550 pClassData.GetRawMemberAccessFlags(),
1551 i);
Aart Bik69ae54a2015-07-01 14:52:26 -07001552 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001553
1554 // Direct methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001555 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1556 fprintf(gOutFile, " Direct methods -\n");
1557 }
1558 for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) {
1559 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1560 pClassData.GetRawMemberAccessFlags(),
1561 pClassData.GetMethodCodeItem(),
1562 pClassData.GetMethodCodeItemOffset(), i);
1563 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001564
1565 // Virtual methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001566 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1567 fprintf(gOutFile, " Virtual methods -\n");
1568 }
1569 for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) {
1570 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1571 pClassData.GetRawMemberAccessFlags(),
1572 pClassData.GetMethodCodeItem(),
1573 pClassData.GetMethodCodeItemOffset(), i);
1574 } // for
1575 }
1576
1577 // End of class.
1578 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1579 const char* fileName;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001580 if (pClassDef.source_file_idx_.IsValid()) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001581 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1582 } else {
1583 fileName = "unknown";
1584 }
1585 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001586 pClassDef.source_file_idx_.index_, fileName);
Aart Bik69ae54a2015-07-01 14:52:26 -07001587 } else if (gOptions.outputFormat == OUTPUT_XML) {
1588 fprintf(gOutFile, "</class>\n");
1589 }
1590
1591 free(accessStr);
1592}
1593
Orion Hodsonc069a302017-01-18 09:23:12 +00001594static void dumpMethodHandle(const DexFile* pDexFile, u4 idx) {
1595 const DexFile::MethodHandleItem& mh = pDexFile->GetMethodHandle(idx);
Orion Hodson631827d2017-04-10 14:53:47 +01001596 const char* type = nullptr;
1597 bool is_instance = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001598 bool is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001599 switch (static_cast<DexFile::MethodHandleType>(mh.method_handle_type_)) {
1600 case DexFile::MethodHandleType::kStaticPut:
1601 type = "put-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001602 is_instance = false;
1603 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001604 break;
1605 case DexFile::MethodHandleType::kStaticGet:
1606 type = "get-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001607 is_instance = false;
1608 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001609 break;
1610 case DexFile::MethodHandleType::kInstancePut:
1611 type = "put-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001612 is_instance = true;
1613 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001614 break;
1615 case DexFile::MethodHandleType::kInstanceGet:
1616 type = "get-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001617 is_instance = true;
1618 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001619 break;
1620 case DexFile::MethodHandleType::kInvokeStatic:
1621 type = "invoke-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001622 is_instance = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001623 is_invoke = true;
1624 break;
1625 case DexFile::MethodHandleType::kInvokeInstance:
1626 type = "invoke-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001627 is_instance = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00001628 is_invoke = true;
1629 break;
1630 case DexFile::MethodHandleType::kInvokeConstructor:
1631 type = "invoke-constructor";
Orion Hodson631827d2017-04-10 14:53:47 +01001632 is_instance = true;
1633 is_invoke = true;
1634 break;
1635 case DexFile::MethodHandleType::kInvokeDirect:
1636 type = "invoke-direct";
1637 is_instance = true;
1638 is_invoke = true;
1639 break;
1640 case DexFile::MethodHandleType::kInvokeInterface:
1641 type = "invoke-interface";
1642 is_instance = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00001643 is_invoke = true;
1644 break;
1645 }
1646
1647 const char* declaring_class;
1648 const char* member;
1649 std::string member_type;
Orion Hodson631827d2017-04-10 14:53:47 +01001650 if (type != nullptr) {
1651 if (is_invoke) {
1652 const DexFile::MethodId& method_id = pDexFile->GetMethodId(mh.field_or_method_idx_);
1653 declaring_class = pDexFile->GetMethodDeclaringClassDescriptor(method_id);
1654 member = pDexFile->GetMethodName(method_id);
1655 member_type = pDexFile->GetMethodSignature(method_id).ToString();
1656 } else {
1657 const DexFile::FieldId& field_id = pDexFile->GetFieldId(mh.field_or_method_idx_);
1658 declaring_class = pDexFile->GetFieldDeclaringClassDescriptor(field_id);
1659 member = pDexFile->GetFieldName(field_id);
1660 member_type = pDexFile->GetFieldTypeDescriptor(field_id);
1661 }
1662 if (is_instance) {
1663 member_type = android::base::StringPrintf("(%s%s", declaring_class, member_type.c_str() + 1);
1664 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001665 } else {
Orion Hodson631827d2017-04-10 14:53:47 +01001666 type = "?";
1667 declaring_class = "?";
1668 member = "?";
1669 member_type = "?";
Orion Hodsonc069a302017-01-18 09:23:12 +00001670 }
1671
1672 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1673 fprintf(gOutFile, "Method handle #%u:\n", idx);
1674 fprintf(gOutFile, " type : %s\n", type);
1675 fprintf(gOutFile, " target : %s %s\n", declaring_class, member);
1676 fprintf(gOutFile, " target_type : %s\n", member_type.c_str());
1677 } else {
1678 fprintf(gOutFile, "<method_handle index=\"%u\"\n", idx);
1679 fprintf(gOutFile, " type=\"%s\"\n", type);
1680 fprintf(gOutFile, " target_class=\"%s\"\n", declaring_class);
1681 fprintf(gOutFile, " target_member=\"%s\"\n", member);
1682 fprintf(gOutFile, " target_member_type=");
1683 dumpEscapedString(member_type.c_str());
1684 fprintf(gOutFile, "\n>\n</method_handle>\n");
1685 }
1686}
1687
1688static void dumpCallSite(const DexFile* pDexFile, u4 idx) {
1689 const DexFile::CallSiteIdItem& call_site_id = pDexFile->GetCallSiteId(idx);
1690 CallSiteArrayValueIterator it(*pDexFile, call_site_id);
1691 if (it.Size() < 3) {
1692 fprintf(stderr, "ERROR: Call site %u has too few values.\n", idx);
1693 return;
1694 }
1695
1696 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1697 it.Next();
1698 dex::StringIndex method_name_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1699 const char* method_name = pDexFile->StringDataByIdx(method_name_idx);
1700 it.Next();
1701 uint32_t method_type_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1702 const DexFile::ProtoId& method_type_id = pDexFile->GetProtoId(method_type_idx);
1703 std::string method_type = pDexFile->GetProtoSignature(method_type_id).ToString();
1704 it.Next();
1705
1706 if (gOptions.outputFormat == OUTPUT_PLAIN) {
Orion Hodson775224d2017-07-05 11:04:01 +01001707 fprintf(gOutFile, "Call site #%u: // offset %u\n", idx, call_site_id.data_off_);
Orion Hodsonc069a302017-01-18 09:23:12 +00001708 fprintf(gOutFile, " link_argument[0] : %u (MethodHandle)\n", method_handle_idx);
1709 fprintf(gOutFile, " link_argument[1] : %s (String)\n", method_name);
1710 fprintf(gOutFile, " link_argument[2] : %s (MethodType)\n", method_type.c_str());
1711 } else {
Orion Hodson775224d2017-07-05 11:04:01 +01001712 fprintf(gOutFile, "<call_site index=\"%u\" offset=\"%u\">\n", idx, call_site_id.data_off_);
Orion Hodsonc069a302017-01-18 09:23:12 +00001713 fprintf(gOutFile,
1714 "<link_argument index=\"0\" type=\"MethodHandle\" value=\"%u\"/>\n",
1715 method_handle_idx);
1716 fprintf(gOutFile,
1717 "<link_argument index=\"1\" type=\"String\" values=\"%s\"/>\n",
1718 method_name);
1719 fprintf(gOutFile,
1720 "<link_argument index=\"2\" type=\"MethodType\" value=\"%s\"/>\n",
1721 method_type.c_str());
1722 }
1723
1724 size_t argument = 3;
1725 while (it.HasNext()) {
1726 const char* type;
1727 std::string value;
1728 switch (it.GetValueType()) {
1729 case EncodedArrayValueIterator::ValueType::kByte:
1730 type = "byte";
1731 value = android::base::StringPrintf("%u", it.GetJavaValue().b);
1732 break;
1733 case EncodedArrayValueIterator::ValueType::kShort:
1734 type = "short";
1735 value = android::base::StringPrintf("%d", it.GetJavaValue().s);
1736 break;
1737 case EncodedArrayValueIterator::ValueType::kChar:
1738 type = "char";
1739 value = android::base::StringPrintf("%u", it.GetJavaValue().c);
1740 break;
1741 case EncodedArrayValueIterator::ValueType::kInt:
1742 type = "int";
1743 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1744 break;
1745 case EncodedArrayValueIterator::ValueType::kLong:
1746 type = "long";
1747 value = android::base::StringPrintf("%" PRId64, it.GetJavaValue().j);
1748 break;
1749 case EncodedArrayValueIterator::ValueType::kFloat:
1750 type = "float";
1751 value = android::base::StringPrintf("%g", it.GetJavaValue().f);
1752 break;
1753 case EncodedArrayValueIterator::ValueType::kDouble:
1754 type = "double";
1755 value = android::base::StringPrintf("%g", it.GetJavaValue().d);
1756 break;
1757 case EncodedArrayValueIterator::ValueType::kMethodType: {
1758 type = "MethodType";
1759 uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1760 const DexFile::ProtoId& proto_id = pDexFile->GetProtoId(proto_idx);
1761 value = pDexFile->GetProtoSignature(proto_id).ToString();
1762 break;
1763 }
1764 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1765 type = "MethodHandle";
1766 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1767 break;
1768 case EncodedArrayValueIterator::ValueType::kString: {
1769 type = "String";
1770 dex::StringIndex string_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1771 value = pDexFile->StringDataByIdx(string_idx);
1772 break;
1773 }
1774 case EncodedArrayValueIterator::ValueType::kType: {
1775 type = "Class";
1776 dex::TypeIndex type_idx = static_cast<dex::TypeIndex>(it.GetJavaValue().i);
1777 const DexFile::ClassDef* class_def = pDexFile->FindClassDef(type_idx);
1778 value = pDexFile->GetClassDescriptor(*class_def);
1779 value = descriptorClassToDot(value.c_str()).get();
1780 break;
1781 }
1782 case EncodedArrayValueIterator::ValueType::kField:
1783 case EncodedArrayValueIterator::ValueType::kMethod:
1784 case EncodedArrayValueIterator::ValueType::kEnum:
1785 case EncodedArrayValueIterator::ValueType::kArray:
1786 case EncodedArrayValueIterator::ValueType::kAnnotation:
1787 // Unreachable based on current EncodedArrayValueIterator::Next().
Andreas Gampef45d61c2017-06-07 10:29:33 -07001788 UNIMPLEMENTED(FATAL) << " type " << it.GetValueType();
Orion Hodsonc069a302017-01-18 09:23:12 +00001789 UNREACHABLE();
Orion Hodsonc069a302017-01-18 09:23:12 +00001790 case EncodedArrayValueIterator::ValueType::kNull:
1791 type = "Null";
1792 value = "null";
1793 break;
1794 case EncodedArrayValueIterator::ValueType::kBoolean:
1795 type = "boolean";
1796 value = it.GetJavaValue().z ? "true" : "false";
1797 break;
1798 }
1799
1800 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1801 fprintf(gOutFile, " link_argument[%zu] : %s (%s)\n", argument, value.c_str(), type);
1802 } else {
1803 fprintf(gOutFile, "<link_argument index=\"%zu\" type=\"%s\" value=", argument, type);
1804 dumpEscapedString(value.c_str());
1805 fprintf(gOutFile, "/>\n");
1806 }
1807
1808 it.Next();
1809 argument++;
1810 }
1811
1812 if (gOptions.outputFormat == OUTPUT_XML) {
1813 fprintf(gOutFile, "</call_site>\n");
1814 }
1815}
1816
Aart Bik69ae54a2015-07-01 14:52:26 -07001817/*
1818 * Dumps the requested sections of the file.
1819 */
Aart Bik7b45a8a2016-10-24 16:07:59 -07001820static void processDexFile(const char* fileName,
1821 const DexFile* pDexFile, size_t i, size_t n) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001822 if (gOptions.verbose) {
Aart Bik7b45a8a2016-10-24 16:07:59 -07001823 fputs("Opened '", gOutFile);
1824 fputs(fileName, gOutFile);
1825 if (n > 1) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001826 fprintf(gOutFile, ":%s", DexFileLoader::GetMultiDexClassesDexName(i).c_str());
Aart Bik7b45a8a2016-10-24 16:07:59 -07001827 }
1828 fprintf(gOutFile, "', DEX version '%.3s'\n", pDexFile->GetHeader().magic_ + 4);
Aart Bik69ae54a2015-07-01 14:52:26 -07001829 }
1830
1831 // Headers.
1832 if (gOptions.showFileHeaders) {
1833 dumpFileHeader(pDexFile);
1834 }
1835
1836 // Open XML context.
1837 if (gOptions.outputFormat == OUTPUT_XML) {
1838 fprintf(gOutFile, "<api>\n");
1839 }
1840
1841 // Iterate over all classes.
1842 char* package = nullptr;
1843 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
1844 for (u4 i = 0; i < classDefsSize; i++) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001845 dumpClass(pDexFile, i, &package);
1846 } // for
1847
Orion Hodsonc069a302017-01-18 09:23:12 +00001848 // Iterate over all method handles.
1849 for (u4 i = 0; i < pDexFile->NumMethodHandles(); ++i) {
1850 dumpMethodHandle(pDexFile, i);
1851 } // for
1852
1853 // Iterate over all call site ids.
1854 for (u4 i = 0; i < pDexFile->NumCallSiteIds(); ++i) {
1855 dumpCallSite(pDexFile, i);
1856 } // for
1857
Aart Bik69ae54a2015-07-01 14:52:26 -07001858 // Free the last package allocated.
1859 if (package != nullptr) {
1860 fprintf(gOutFile, "</package>\n");
1861 free(package);
1862 }
1863
1864 // Close XML context.
1865 if (gOptions.outputFormat == OUTPUT_XML) {
1866 fprintf(gOutFile, "</api>\n");
1867 }
1868}
1869
1870/*
1871 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1872 */
1873int processFile(const char* fileName) {
1874 if (gOptions.verbose) {
1875 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1876 }
1877
1878 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
Aart Bikdce50862016-06-10 16:04:03 -07001879 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -07001880 const bool kVerifyChecksum = !gOptions.ignoreBadChecksum;
Aart Bik69ae54a2015-07-01 14:52:26 -07001881 std::string error_msg;
1882 std::vector<std::unique_ptr<const DexFile>> dex_files;
Nicolas Geoffray095c6c92017-10-19 13:59:55 +01001883 if (!DexFileLoader::Open(
1884 fileName, fileName, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001885 // Display returned error message to user. Note that this error behavior
1886 // differs from the error messages shown by the original Dalvik dexdump.
1887 fputs(error_msg.c_str(), stderr);
1888 fputc('\n', stderr);
1889 return -1;
1890 }
1891
Aart Bik4e149602015-07-09 11:45:28 -07001892 // Success. Either report checksum verification or process
1893 // all dex files found in given file.
Aart Bik69ae54a2015-07-01 14:52:26 -07001894 if (gOptions.checksumOnly) {
1895 fprintf(gOutFile, "Checksum verified\n");
1896 } else {
Aart Bik7b45a8a2016-10-24 16:07:59 -07001897 for (size_t i = 0, n = dex_files.size(); i < n; i++) {
1898 processDexFile(fileName, dex_files[i].get(), i, n);
Aart Bik4e149602015-07-09 11:45:28 -07001899 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001900 }
1901 return 0;
1902}
1903
1904} // namespace art