blob: 2cf05678c6fb59410399b1ccc3b522f7c0f9d32a [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
40#include <memory>
Andreas Gampe5073fed2015-08-10 11:40:25 -070041#include <sstream>
Aart Bik69ae54a2015-07-01 14:52:26 -070042#include <vector>
43
David Sehr999646d2018-02-16 10:22:33 -080044#include "android-base/file.h"
Andreas Gampe221d9812018-01-22 17:48:56 -080045#include "android-base/logging.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080046#include "android-base/stringprintf.h"
47
Mathieu Chartierc2b4db62018-05-18 13:58:12 -070048#include "dex/class_accessor-inl.h"
David Sehr0225f8e2018-01-31 08:52:24 +000049#include "dex/code_item_accessors-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080050#include "dex/dex_file-inl.h"
51#include "dex/dex_file_exception_helpers.h"
52#include "dex/dex_file_loader.h"
53#include "dex/dex_file_types.h"
54#include "dex/dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070055#include "dexdump_cfg.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070056
57namespace art {
58
59/*
60 * Options parsed in main driver.
61 */
62struct Options gOptions;
63
64/*
Aart Bik4e149602015-07-09 11:45:28 -070065 * Output file. Defaults to stdout.
Aart Bik69ae54a2015-07-01 14:52:26 -070066 */
67FILE* gOutFile = stdout;
68
69/*
70 * Data types that match the definitions in the VM specification.
71 */
72typedef uint8_t u1;
73typedef uint16_t u2;
74typedef uint32_t u4;
75typedef uint64_t u8;
Aart Bikdce50862016-06-10 16:04:03 -070076typedef int8_t s1;
77typedef int16_t s2;
Aart Bik69ae54a2015-07-01 14:52:26 -070078typedef int32_t s4;
79typedef int64_t s8;
80
81/*
82 * Basic information about a field or a method.
83 */
84struct FieldMethodInfo {
85 const char* classDescriptor;
86 const char* name;
87 const char* signature;
88};
89
90/*
91 * Flags for use with createAccessFlagStr().
92 */
93enum AccessFor {
94 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
95};
96const int kNumFlags = 18;
97
98/*
99 * Gets 2 little-endian bytes.
100 */
101static inline u2 get2LE(unsigned char const* pSrc) {
102 return pSrc[0] | (pSrc[1] << 8);
103}
104
105/*
106 * Converts a single-character primitive type into human-readable form.
107 */
108static const char* primitiveTypeLabel(char typeChar) {
109 switch (typeChar) {
110 case 'B': return "byte";
111 case 'C': return "char";
112 case 'D': return "double";
113 case 'F': return "float";
114 case 'I': return "int";
115 case 'J': return "long";
116 case 'S': return "short";
117 case 'V': return "void";
118 case 'Z': return "boolean";
119 default: return "UNKNOWN";
120 } // switch
121}
122
123/*
124 * Converts a type descriptor to human-readable "dotted" form. For
125 * example, "Ljava/lang/String;" becomes "java.lang.String", and
Orion Hodsonfe42d212018-08-24 14:01:14 +0100126 * "[I" becomes "int[]".
Aart Bik69ae54a2015-07-01 14:52:26 -0700127 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700128static std::unique_ptr<char[]> descriptorToDot(const char* str) {
Aart Bik69ae54a2015-07-01 14:52:26 -0700129 int targetLen = strlen(str);
130 int offset = 0;
131
132 // Strip leading [s; will be added to end.
133 while (targetLen > 1 && str[offset] == '[') {
134 offset++;
135 targetLen--;
136 } // while
137
138 const int arrayDepth = offset;
139
140 if (targetLen == 1) {
141 // Primitive type.
142 str = primitiveTypeLabel(str[offset]);
143 offset = 0;
144 targetLen = strlen(str);
145 } else {
146 // Account for leading 'L' and trailing ';'.
147 if (targetLen >= 2 && str[offset] == 'L' &&
148 str[offset + targetLen - 1] == ';') {
149 targetLen -= 2;
150 offset++;
151 }
152 }
153
154 // Copy class name over.
Aart Bikc05e2f22016-07-12 15:53:13 -0700155 std::unique_ptr<char[]> newStr(new char[targetLen + arrayDepth * 2 + 1]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700156 int i = 0;
157 for (; i < targetLen; i++) {
158 const char ch = str[offset + i];
Orion Hodsonfe42d212018-08-24 14:01:14 +0100159 newStr[i] = (ch == '/') ? '.' : ch;
Aart Bik69ae54a2015-07-01 14:52:26 -0700160 } // for
161
162 // Add the appropriate number of brackets for arrays.
163 for (int j = 0; j < arrayDepth; j++) {
164 newStr[i++] = '[';
165 newStr[i++] = ']';
166 } // for
167
168 newStr[i] = '\0';
169 return newStr;
170}
171
172/*
Orion Hodsonfe42d212018-08-24 14:01:14 +0100173 * Retrieves the class name portion of a type descriptor.
Aart Bik69ae54a2015-07-01 14:52:26 -0700174 */
Orion Hodsonfe42d212018-08-24 14:01:14 +0100175static std::unique_ptr<char[]> descriptorClassToName(const char* str) {
Aart Bikc05e2f22016-07-12 15:53:13 -0700176 // 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++) {
Orion Hodsonfe42d212018-08-24 14:01:14 +0100188 newStr[i] = lastSlash[i];
Aart Bik69ae54a2015-07-01 14:52:26 -0700189 } // for
Aart Bikc05e2f22016-07-12 15:53:13 -0700190 newStr[targetLen - 1] = '\0';
Aart Bik69ae54a2015-07-01 14:52:26 -0700191 return newStr;
192}
193
194/*
Aart Bikdce50862016-06-10 16:04:03 -0700195 * Returns string representing the boolean value.
196 */
197static const char* strBool(bool val) {
198 return val ? "true" : "false";
199}
200
201/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700202 * Returns a quoted string representing the boolean value.
203 */
204static const char* quotedBool(bool val) {
205 return val ? "\"true\"" : "\"false\"";
206}
207
208/*
209 * Returns a quoted string representing the access flags.
210 */
211static const char* quotedVisibility(u4 accessFlags) {
212 if (accessFlags & kAccPublic) {
213 return "\"public\"";
214 } else if (accessFlags & kAccProtected) {
215 return "\"protected\"";
216 } else if (accessFlags & kAccPrivate) {
217 return "\"private\"";
218 } else {
219 return "\"package\"";
220 }
221}
222
223/*
224 * Counts the number of '1' bits in a word.
225 */
226static int countOnes(u4 val) {
227 val = val - ((val >> 1) & 0x55555555);
228 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
229 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
230}
231
232/*
233 * Creates a new string with human-readable access flags.
234 *
235 * In the base language the access_flags fields are type u2; in Dalvik
236 * they're u4.
237 */
238static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
239 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
240 {
241 "PUBLIC", /* 0x00001 */
242 "PRIVATE", /* 0x00002 */
243 "PROTECTED", /* 0x00004 */
244 "STATIC", /* 0x00008 */
245 "FINAL", /* 0x00010 */
246 "?", /* 0x00020 */
247 "?", /* 0x00040 */
248 "?", /* 0x00080 */
249 "?", /* 0x00100 */
250 "INTERFACE", /* 0x00200 */
251 "ABSTRACT", /* 0x00400 */
252 "?", /* 0x00800 */
253 "SYNTHETIC", /* 0x01000 */
254 "ANNOTATION", /* 0x02000 */
255 "ENUM", /* 0x04000 */
256 "?", /* 0x08000 */
257 "VERIFIED", /* 0x10000 */
258 "OPTIMIZED", /* 0x20000 */
259 }, {
260 "PUBLIC", /* 0x00001 */
261 "PRIVATE", /* 0x00002 */
262 "PROTECTED", /* 0x00004 */
263 "STATIC", /* 0x00008 */
264 "FINAL", /* 0x00010 */
265 "SYNCHRONIZED", /* 0x00020 */
266 "BRIDGE", /* 0x00040 */
267 "VARARGS", /* 0x00080 */
268 "NATIVE", /* 0x00100 */
269 "?", /* 0x00200 */
270 "ABSTRACT", /* 0x00400 */
271 "STRICT", /* 0x00800 */
272 "SYNTHETIC", /* 0x01000 */
273 "?", /* 0x02000 */
274 "?", /* 0x04000 */
275 "MIRANDA", /* 0x08000 */
276 "CONSTRUCTOR", /* 0x10000 */
277 "DECLARED_SYNCHRONIZED", /* 0x20000 */
278 }, {
279 "PUBLIC", /* 0x00001 */
280 "PRIVATE", /* 0x00002 */
281 "PROTECTED", /* 0x00004 */
282 "STATIC", /* 0x00008 */
283 "FINAL", /* 0x00010 */
284 "?", /* 0x00020 */
285 "VOLATILE", /* 0x00040 */
286 "TRANSIENT", /* 0x00080 */
287 "?", /* 0x00100 */
288 "?", /* 0x00200 */
289 "?", /* 0x00400 */
290 "?", /* 0x00800 */
291 "SYNTHETIC", /* 0x01000 */
292 "?", /* 0x02000 */
293 "ENUM", /* 0x04000 */
294 "?", /* 0x08000 */
295 "?", /* 0x10000 */
296 "?", /* 0x20000 */
297 },
298 };
299
300 // Allocate enough storage to hold the expected number of strings,
301 // plus a space between each. We over-allocate, using the longest
302 // string above as the base metric.
303 const int kLongest = 21; // The strlen of longest string above.
304 const int count = countOnes(flags);
305 char* str;
306 char* cp;
307 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
308
309 for (int i = 0; i < kNumFlags; i++) {
310 if (flags & 0x01) {
311 const char* accessStr = kAccessStrings[forWhat][i];
312 const int len = strlen(accessStr);
313 if (cp != str) {
314 *cp++ = ' ';
315 }
316 memcpy(cp, accessStr, len);
317 cp += len;
318 }
319 flags >>= 1;
320 } // for
321
322 *cp = '\0';
323 return str;
324}
325
326/*
327 * Copies character data from "data" to "out", converting non-ASCII values
328 * to fprintf format chars or an ASCII filler ('.' or '?').
329 *
330 * The output buffer must be able to hold (2*len)+1 bytes. The result is
331 * NULL-terminated.
332 */
333static void asciify(char* out, const unsigned char* data, size_t len) {
334 while (len--) {
335 if (*data < 0x20) {
336 // Could do more here, but we don't need them yet.
337 switch (*data) {
338 case '\0':
339 *out++ = '\\';
340 *out++ = '0';
341 break;
342 case '\n':
343 *out++ = '\\';
344 *out++ = 'n';
345 break;
346 default:
347 *out++ = '.';
348 break;
349 } // switch
350 } else if (*data >= 0x80) {
351 *out++ = '?';
352 } else {
353 *out++ = *data;
354 }
355 data++;
356 } // while
357 *out = '\0';
358}
359
360/*
Aart Bikdce50862016-06-10 16:04:03 -0700361 * Dumps a string value with some escape characters.
362 */
363static void dumpEscapedString(const char* p) {
364 fputs("\"", gOutFile);
365 for (; *p; p++) {
366 switch (*p) {
367 case '\\':
368 fputs("\\\\", gOutFile);
369 break;
370 case '\"':
371 fputs("\\\"", gOutFile);
372 break;
373 case '\t':
374 fputs("\\t", gOutFile);
375 break;
376 case '\n':
377 fputs("\\n", gOutFile);
378 break;
379 case '\r':
380 fputs("\\r", gOutFile);
381 break;
382 default:
383 putc(*p, gOutFile);
384 } // switch
385 } // for
386 fputs("\"", gOutFile);
387}
388
389/*
390 * Dumps a string as an XML attribute value.
391 */
392static void dumpXmlAttribute(const char* p) {
393 for (; *p; p++) {
394 switch (*p) {
395 case '&':
396 fputs("&amp;", gOutFile);
397 break;
398 case '<':
399 fputs("&lt;", gOutFile);
400 break;
401 case '>':
402 fputs("&gt;", gOutFile);
403 break;
404 case '"':
405 fputs("&quot;", gOutFile);
406 break;
407 case '\t':
408 fputs("&#x9;", gOutFile);
409 break;
410 case '\n':
411 fputs("&#xA;", gOutFile);
412 break;
413 case '\r':
414 fputs("&#xD;", gOutFile);
415 break;
416 default:
417 putc(*p, gOutFile);
418 } // switch
419 } // for
420}
421
422/*
423 * Reads variable width value, possibly sign extended at the last defined byte.
424 */
425static u8 readVarWidth(const u1** data, u1 arg, bool sign_extend) {
426 u8 value = 0;
427 for (u4 i = 0; i <= arg; i++) {
428 value |= static_cast<u8>(*(*data)++) << (i * 8);
429 }
430 if (sign_extend) {
431 int shift = (7 - arg) * 8;
432 return (static_cast<s8>(value) << shift) >> shift;
433 }
434 return value;
435}
436
437/*
438 * Dumps encoded value.
439 */
440static void dumpEncodedValue(const DexFile* pDexFile, const u1** data); // forward
441static void dumpEncodedValue(const DexFile* pDexFile, const u1** data, u1 type, u1 arg) {
442 switch (type) {
443 case DexFile::kDexAnnotationByte:
444 fprintf(gOutFile, "%" PRId8, static_cast<s1>(readVarWidth(data, arg, false)));
445 break;
446 case DexFile::kDexAnnotationShort:
447 fprintf(gOutFile, "%" PRId16, static_cast<s2>(readVarWidth(data, arg, true)));
448 break;
449 case DexFile::kDexAnnotationChar:
450 fprintf(gOutFile, "%" PRIu16, static_cast<u2>(readVarWidth(data, arg, false)));
451 break;
452 case DexFile::kDexAnnotationInt:
453 fprintf(gOutFile, "%" PRId32, static_cast<s4>(readVarWidth(data, arg, true)));
454 break;
455 case DexFile::kDexAnnotationLong:
456 fprintf(gOutFile, "%" PRId64, static_cast<s8>(readVarWidth(data, arg, true)));
457 break;
458 case DexFile::kDexAnnotationFloat: {
459 // Fill on right.
460 union {
461 float f;
462 u4 data;
463 } conv;
464 conv.data = static_cast<u4>(readVarWidth(data, arg, false)) << (3 - arg) * 8;
465 fprintf(gOutFile, "%g", conv.f);
466 break;
467 }
468 case DexFile::kDexAnnotationDouble: {
469 // Fill on right.
470 union {
471 double d;
472 u8 data;
473 } conv;
474 conv.data = readVarWidth(data, arg, false) << (7 - arg) * 8;
475 fprintf(gOutFile, "%g", conv.d);
476 break;
477 }
478 case DexFile::kDexAnnotationString: {
479 const u4 idx = static_cast<u4>(readVarWidth(data, arg, false));
480 if (gOptions.outputFormat == OUTPUT_PLAIN) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800481 dumpEscapedString(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
Aart Bikdce50862016-06-10 16:04:03 -0700482 } else {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800483 dumpXmlAttribute(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
Aart Bikdce50862016-06-10 16:04:03 -0700484 }
485 break;
486 }
487 case DexFile::kDexAnnotationType: {
488 const u4 str_idx = static_cast<u4>(readVarWidth(data, arg, false));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800489 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(str_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700490 break;
491 }
492 case DexFile::kDexAnnotationField:
493 case DexFile::kDexAnnotationEnum: {
494 const u4 field_idx = static_cast<u4>(readVarWidth(data, arg, false));
495 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
496 fputs(pDexFile->StringDataByIdx(pFieldId.name_idx_), gOutFile);
497 break;
498 }
499 case DexFile::kDexAnnotationMethod: {
500 const u4 method_idx = static_cast<u4>(readVarWidth(data, arg, false));
501 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
502 fputs(pDexFile->StringDataByIdx(pMethodId.name_idx_), gOutFile);
503 break;
504 }
505 case DexFile::kDexAnnotationArray: {
506 fputc('{', gOutFile);
507 // Decode and display all elements.
508 const u4 size = DecodeUnsignedLeb128(data);
509 for (u4 i = 0; i < size; i++) {
510 fputc(' ', gOutFile);
511 dumpEncodedValue(pDexFile, data);
512 }
513 fputs(" }", gOutFile);
514 break;
515 }
516 case DexFile::kDexAnnotationAnnotation: {
517 const u4 type_idx = DecodeUnsignedLeb128(data);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800518 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(type_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700519 // Decode and display all name=value pairs.
520 const u4 size = DecodeUnsignedLeb128(data);
521 for (u4 i = 0; i < size; i++) {
522 const u4 name_idx = DecodeUnsignedLeb128(data);
523 fputc(' ', gOutFile);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800524 fputs(pDexFile->StringDataByIdx(dex::StringIndex(name_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700525 fputc('=', gOutFile);
526 dumpEncodedValue(pDexFile, data);
527 }
528 break;
529 }
530 case DexFile::kDexAnnotationNull:
531 fputs("null", gOutFile);
532 break;
533 case DexFile::kDexAnnotationBoolean:
534 fputs(strBool(arg), gOutFile);
535 break;
536 default:
537 fputs("????", gOutFile);
538 break;
539 } // switch
540}
541
542/*
543 * Dumps encoded value with prefix.
544 */
545static void dumpEncodedValue(const DexFile* pDexFile, const u1** data) {
546 const u1 enc = *(*data)++;
547 dumpEncodedValue(pDexFile, data, enc & 0x1f, enc >> 5);
548}
549
550/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700551 * Dumps the file header.
Aart Bik69ae54a2015-07-01 14:52:26 -0700552 */
553static void dumpFileHeader(const DexFile* pDexFile) {
554 const DexFile::Header& pHeader = pDexFile->GetHeader();
555 char sanitized[sizeof(pHeader.magic_) * 2 + 1];
556 fprintf(gOutFile, "DEX file header:\n");
557 asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
558 fprintf(gOutFile, "magic : '%s'\n", sanitized);
559 fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
560 fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
561 pHeader.signature_[0], pHeader.signature_[1],
562 pHeader.signature_[DexFile::kSha1DigestSize - 2],
563 pHeader.signature_[DexFile::kSha1DigestSize - 1]);
564 fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
565 fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
566 fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
567 fprintf(gOutFile, "link_off : %d (0x%06x)\n",
568 pHeader.link_off_, pHeader.link_off_);
569 fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
570 fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
571 pHeader.string_ids_off_, pHeader.string_ids_off_);
572 fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
573 fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
574 pHeader.type_ids_off_, pHeader.type_ids_off_);
Aart Bikdce50862016-06-10 16:04:03 -0700575 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
576 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
Aart Bik69ae54a2015-07-01 14:52:26 -0700577 pHeader.proto_ids_off_, pHeader.proto_ids_off_);
578 fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
579 fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
580 pHeader.field_ids_off_, pHeader.field_ids_off_);
581 fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
582 fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
583 pHeader.method_ids_off_, pHeader.method_ids_off_);
584 fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
585 fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
586 pHeader.class_defs_off_, pHeader.class_defs_off_);
587 fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
588 fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
589 pHeader.data_off_, pHeader.data_off_);
590}
591
592/*
593 * Dumps a class_def_item.
594 */
595static void dumpClassDef(const DexFile* pDexFile, int idx) {
596 // General class information.
597 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
598 fprintf(gOutFile, "Class #%d header:\n", idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800599 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700600 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
601 pClassDef.access_flags_, pClassDef.access_flags_);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800602 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700603 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
604 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800605 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700606 fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
607 pClassDef.annotations_off_, pClassDef.annotations_off_);
608 fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
609 pClassDef.class_data_off_, pClassDef.class_data_off_);
610
611 // Fields and methods.
Mathieu Chartier18e26872018-06-04 17:19:02 -0700612 ClassAccessor accessor(*pDexFile, idx);
Mathieu Chartierc2b4db62018-05-18 13:58:12 -0700613 fprintf(gOutFile, "static_fields_size : %d\n", accessor.NumStaticFields());
614 fprintf(gOutFile, "instance_fields_size: %d\n", accessor.NumInstanceFields());
615 fprintf(gOutFile, "direct_methods_size : %d\n", accessor.NumDirectMethods());
616 fprintf(gOutFile, "virtual_methods_size: %d\n", accessor.NumVirtualMethods());
Aart Bik69ae54a2015-07-01 14:52:26 -0700617 fprintf(gOutFile, "\n");
618}
619
Aart Bikdce50862016-06-10 16:04:03 -0700620/**
621 * Dumps an annotation set item.
622 */
623static void dumpAnnotationSetItem(const DexFile* pDexFile, const DexFile::AnnotationSetItem* set_item) {
624 if (set_item == nullptr || set_item->size_ == 0) {
625 fputs(" empty-annotation-set\n", gOutFile);
626 return;
627 }
628 for (u4 i = 0; i < set_item->size_; i++) {
629 const DexFile::AnnotationItem* annotation = pDexFile->GetAnnotationItem(set_item, i);
630 if (annotation == nullptr) {
631 continue;
632 }
633 fputs(" ", gOutFile);
634 switch (annotation->visibility_) {
635 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", gOutFile); break;
636 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", gOutFile); break;
637 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", gOutFile); break;
638 default: fputs("VISIBILITY_UNKNOWN ", gOutFile); break;
639 } // switch
640 // Decode raw bytes in annotation.
641 const u1* rData = annotation->annotation_;
642 dumpEncodedValue(pDexFile, &rData, DexFile::kDexAnnotationAnnotation, 0);
643 fputc('\n', gOutFile);
644 }
645}
646
647/*
648 * Dumps class annotations.
649 */
650static void dumpClassAnnotations(const DexFile* pDexFile, int idx) {
651 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
652 const DexFile::AnnotationsDirectoryItem* dir = pDexFile->GetAnnotationsDirectory(pClassDef);
653 if (dir == nullptr) {
654 return; // none
655 }
656
657 fprintf(gOutFile, "Class #%d annotations:\n", idx);
658
659 const DexFile::AnnotationSetItem* class_set_item = pDexFile->GetClassAnnotationSet(dir);
660 const DexFile::FieldAnnotationsItem* fields = pDexFile->GetFieldAnnotations(dir);
661 const DexFile::MethodAnnotationsItem* methods = pDexFile->GetMethodAnnotations(dir);
662 const DexFile::ParameterAnnotationsItem* pars = pDexFile->GetParameterAnnotations(dir);
663
664 // Annotations on the class itself.
665 if (class_set_item != nullptr) {
666 fprintf(gOutFile, "Annotations on class\n");
667 dumpAnnotationSetItem(pDexFile, class_set_item);
668 }
669
670 // Annotations on fields.
671 if (fields != nullptr) {
672 for (u4 i = 0; i < dir->fields_size_; i++) {
673 const u4 field_idx = fields[i].field_idx_;
674 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
675 const char* field_name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
676 fprintf(gOutFile, "Annotations on field #%u '%s'\n", field_idx, field_name);
677 dumpAnnotationSetItem(pDexFile, pDexFile->GetFieldAnnotationSetItem(fields[i]));
678 }
679 }
680
681 // Annotations on methods.
682 if (methods != nullptr) {
683 for (u4 i = 0; i < dir->methods_size_; i++) {
684 const u4 method_idx = methods[i].method_idx_;
685 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
686 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
687 fprintf(gOutFile, "Annotations on method #%u '%s'\n", method_idx, method_name);
688 dumpAnnotationSetItem(pDexFile, pDexFile->GetMethodAnnotationSetItem(methods[i]));
689 }
690 }
691
692 // Annotations on method parameters.
693 if (pars != nullptr) {
694 for (u4 i = 0; i < dir->parameters_size_; i++) {
695 const u4 method_idx = pars[i].method_idx_;
696 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
697 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
698 fprintf(gOutFile, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
699 const DexFile::AnnotationSetRefList*
700 list = pDexFile->GetParameterAnnotationSetRefList(&pars[i]);
701 if (list != nullptr) {
702 for (u4 j = 0; j < list->size_; j++) {
703 fprintf(gOutFile, "#%u\n", j);
704 dumpAnnotationSetItem(pDexFile, pDexFile->GetSetRefItemItem(&list->list_[j]));
705 }
706 }
707 }
708 }
709
710 fputc('\n', gOutFile);
711}
712
Aart Bik69ae54a2015-07-01 14:52:26 -0700713/*
714 * Dumps an interface that a class declares to implement.
715 */
716static void dumpInterface(const DexFile* pDexFile, const DexFile::TypeItem& pTypeItem, int i) {
717 const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
718 if (gOptions.outputFormat == OUTPUT_PLAIN) {
719 fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
720 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -0700721 std::unique_ptr<char[]> dot(descriptorToDot(interfaceName));
722 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -0700723 }
724}
725
726/*
727 * Dumps the catches table associated with the code.
728 */
729static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800730 CodeItemDataAccessor accessor(*pDexFile, pCode);
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800731 const u4 triesSize = accessor.TriesSize();
Aart Bik69ae54a2015-07-01 14:52:26 -0700732
733 // No catch table.
734 if (triesSize == 0) {
735 fprintf(gOutFile, " catches : (none)\n");
736 return;
737 }
738
739 // Dump all table entries.
740 fprintf(gOutFile, " catches : %d\n", triesSize);
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800741 for (const DexFile::TryItem& try_item : accessor.TryItems()) {
742 const u4 start = try_item.start_addr_;
743 const u4 end = start + try_item.insn_count_;
Aart Bik69ae54a2015-07-01 14:52:26 -0700744 fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
Mathieu Chartierdc578c72017-12-27 11:51:45 -0800745 for (CatchHandlerIterator it(accessor, try_item); it.HasNext(); it.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800746 const dex::TypeIndex tidx = it.GetHandlerTypeIndex();
747 const char* descriptor = (!tidx.IsValid()) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
Aart Bik69ae54a2015-07-01 14:52:26 -0700748 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
749 } // for
750 } // for
751}
752
753/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700754 * Helper for dumpInstruction(), which builds the string
Aart Bika0e33fd2016-07-08 18:32:45 -0700755 * representation for the index in the given instruction.
756 * Returns a pointer to a buffer of sufficient size.
Aart Bik69ae54a2015-07-01 14:52:26 -0700757 */
Aart Bika0e33fd2016-07-08 18:32:45 -0700758static std::unique_ptr<char[]> indexString(const DexFile* pDexFile,
759 const Instruction* pDecInsn,
760 size_t bufSize) {
761 std::unique_ptr<char[]> buf(new char[bufSize]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700762 // Determine index and width of the string.
763 u4 index = 0;
Orion Hodson06d10a72018-05-14 08:53:38 +0100764 u2 secondary_index = 0;
Aart Bik69ae54a2015-07-01 14:52:26 -0700765 u4 width = 4;
766 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
767 // SOME NOT SUPPORTED:
768 // case Instruction::k20bc:
769 case Instruction::k21c:
770 case Instruction::k35c:
771 // case Instruction::k35ms:
772 case Instruction::k3rc:
773 // case Instruction::k3rms:
774 // case Instruction::k35mi:
775 // case Instruction::k3rmi:
776 index = pDecInsn->VRegB();
777 width = 4;
778 break;
779 case Instruction::k31c:
780 index = pDecInsn->VRegB();
781 width = 8;
782 break;
783 case Instruction::k22c:
784 // case Instruction::k22cs:
785 index = pDecInsn->VRegC();
786 width = 4;
787 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100788 case Instruction::k45cc:
789 case Instruction::k4rcc:
790 index = pDecInsn->VRegB();
791 secondary_index = pDecInsn->VRegH();
792 width = 4;
793 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700794 default:
795 break;
796 } // switch
797
798 // Determine index type.
799 size_t outSize = 0;
800 switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
801 case Instruction::kIndexUnknown:
802 // This function should never get called for this type, but do
803 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700804 outSize = snprintf(buf.get(), bufSize, "<unknown-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700805 break;
806 case Instruction::kIndexNone:
807 // This function should never get called for this type, but do
808 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700809 outSize = snprintf(buf.get(), bufSize, "<no-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700810 break;
811 case Instruction::kIndexTypeRef:
812 if (index < pDexFile->GetHeader().type_ids_size_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800813 const char* tp = pDexFile->StringByTypeIdx(dex::TypeIndex(index));
Aart Bika0e33fd2016-07-08 18:32:45 -0700814 outSize = snprintf(buf.get(), bufSize, "%s // type@%0*x", tp, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700815 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700816 outSize = snprintf(buf.get(), bufSize, "<type?> // type@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700817 }
818 break;
819 case Instruction::kIndexStringRef:
820 if (index < pDexFile->GetHeader().string_ids_size_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800821 const char* st = pDexFile->StringDataByIdx(dex::StringIndex(index));
Aart Bika0e33fd2016-07-08 18:32:45 -0700822 outSize = snprintf(buf.get(), bufSize, "\"%s\" // string@%0*x", st, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700823 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700824 outSize = snprintf(buf.get(), bufSize, "<string?> // string@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700825 }
826 break;
827 case Instruction::kIndexMethodRef:
828 if (index < pDexFile->GetHeader().method_ids_size_) {
829 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
830 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
831 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
832 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700833 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // method@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700834 backDescriptor, name, signature.ToString().c_str(), width, index);
835 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700836 outSize = snprintf(buf.get(), bufSize, "<method?> // method@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700837 }
838 break;
839 case Instruction::kIndexFieldRef:
840 if (index < pDexFile->GetHeader().field_ids_size_) {
841 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index);
842 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
843 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
844 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700845 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // field@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700846 backDescriptor, name, typeDescriptor, width, index);
847 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700848 outSize = snprintf(buf.get(), bufSize, "<field?> // field@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700849 }
850 break;
851 case Instruction::kIndexVtableOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700852 outSize = snprintf(buf.get(), bufSize, "[%0*x] // vtable #%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700853 width, index, width, index);
854 break;
855 case Instruction::kIndexFieldOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700856 outSize = snprintf(buf.get(), bufSize, "[obj+%0*x]", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700857 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100858 case Instruction::kIndexMethodAndProtoRef: {
Orion Hodsonc069a302017-01-18 09:23:12 +0000859 std::string method("<method?>");
860 std::string proto("<proto?>");
861 if (index < pDexFile->GetHeader().method_ids_size_) {
862 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
863 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
864 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
865 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
866 method = android::base::StringPrintf("%s.%s:%s",
867 backDescriptor,
868 name,
869 signature.ToString().c_str());
Orion Hodsonb34bb192016-10-18 17:02:58 +0100870 }
Orion Hodsonc069a302017-01-18 09:23:12 +0000871 if (secondary_index < pDexFile->GetHeader().proto_ids_size_) {
Orion Hodson06d10a72018-05-14 08:53:38 +0100872 const DexFile::ProtoId& protoId = pDexFile->GetProtoId(dex::ProtoIndex(secondary_index));
Orion Hodsonc069a302017-01-18 09:23:12 +0000873 const Signature signature = pDexFile->GetProtoSignature(protoId);
874 proto = signature.ToString();
875 }
876 outSize = snprintf(buf.get(), bufSize, "%s, %s // method@%0*x, proto@%0*x",
877 method.c_str(), proto.c_str(), width, index, width, secondary_index);
878 break;
879 }
880 case Instruction::kIndexCallSiteRef:
881 // Call site information is too large to detail in disassembly so just output the index.
882 outSize = snprintf(buf.get(), bufSize, "call_site@%0*x", width, index);
Orion Hodsonb34bb192016-10-18 17:02:58 +0100883 break;
Orion Hodson2e599942017-09-22 16:17:41 +0100884 case Instruction::kIndexMethodHandleRef:
885 // Method handle information is too large to detail in disassembly so just output the index.
886 outSize = snprintf(buf.get(), bufSize, "method_handle@%0*x", width, index);
887 break;
888 case Instruction::kIndexProtoRef:
889 if (index < pDexFile->GetHeader().proto_ids_size_) {
Orion Hodson06d10a72018-05-14 08:53:38 +0100890 const DexFile::ProtoId& protoId = pDexFile->GetProtoId(dex::ProtoIndex(index));
Orion Hodson2e599942017-09-22 16:17:41 +0100891 const Signature signature = pDexFile->GetProtoSignature(protoId);
892 const std::string& proto = signature.ToString();
893 outSize = snprintf(buf.get(), bufSize, "%s // proto@%0*x", proto.c_str(), width, index);
894 } else {
895 outSize = snprintf(buf.get(), bufSize, "<?> // proto@%0*x", width, index);
896 }
Aart Bik69ae54a2015-07-01 14:52:26 -0700897 break;
898 } // switch
899
Orion Hodson2e599942017-09-22 16:17:41 +0100900 if (outSize == 0) {
901 // The index type has not been handled in the switch above.
902 outSize = snprintf(buf.get(), bufSize, "<?>");
903 }
904
Aart Bik69ae54a2015-07-01 14:52:26 -0700905 // Determine success of string construction.
906 if (outSize >= bufSize) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700907 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
908 // doesn't count/ the '\0' as part of its returned size, so we add explicit
909 // space for it here.
910 return indexString(pDexFile, pDecInsn, outSize + 1);
Aart Bik69ae54a2015-07-01 14:52:26 -0700911 }
912 return buf;
913}
914
915/*
916 * Dumps a single instruction.
917 */
918static void dumpInstruction(const DexFile* pDexFile,
919 const DexFile::CodeItem* pCode,
920 u4 codeOffset, u4 insnIdx, u4 insnWidth,
921 const Instruction* pDecInsn) {
922 // Address of instruction (expressed as byte offset).
923 fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
924
925 // Dump (part of) raw bytes.
Mathieu Chartier698ebbc2018-01-05 11:00:42 -0800926 CodeItemInstructionAccessor accessor(*pDexFile, pCode);
Aart Bik69ae54a2015-07-01 14:52:26 -0700927 for (u4 i = 0; i < 8; i++) {
928 if (i < insnWidth) {
929 if (i == 7) {
930 fprintf(gOutFile, " ... ");
931 } else {
932 // Print 16-bit value in little-endian order.
Mathieu Chartier641a3af2017-12-15 11:42:58 -0800933 const u1* bytePtr = (const u1*) &accessor.Insns()[insnIdx + i];
Aart Bik69ae54a2015-07-01 14:52:26 -0700934 fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
935 }
936 } else {
937 fputs(" ", gOutFile);
938 }
939 } // for
940
941 // Dump pseudo-instruction or opcode.
942 if (pDecInsn->Opcode() == Instruction::NOP) {
Mathieu Chartier641a3af2017-12-15 11:42:58 -0800943 const u2 instr = get2LE((const u1*) &accessor.Insns()[insnIdx]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700944 if (instr == Instruction::kPackedSwitchSignature) {
945 fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
946 } else if (instr == Instruction::kSparseSwitchSignature) {
947 fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
948 } else if (instr == Instruction::kArrayDataSignature) {
949 fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
950 } else {
951 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
952 }
953 } else {
954 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
955 }
956
957 // Set up additional argument.
Aart Bika0e33fd2016-07-08 18:32:45 -0700958 std::unique_ptr<char[]> indexBuf;
Aart Bik69ae54a2015-07-01 14:52:26 -0700959 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700960 indexBuf = indexString(pDexFile, pDecInsn, 200);
Aart Bik69ae54a2015-07-01 14:52:26 -0700961 }
962
963 // Dump the instruction.
964 //
965 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
966 //
967 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
968 case Instruction::k10x: // op
969 break;
970 case Instruction::k12x: // op vA, vB
971 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
972 break;
973 case Instruction::k11n: // op vA, #+B
974 fprintf(gOutFile, " v%d, #int %d // #%x",
975 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
976 break;
977 case Instruction::k11x: // op vAA
978 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
979 break;
980 case Instruction::k10t: // op +AA
Aart Bikdce50862016-06-10 16:04:03 -0700981 case Instruction::k20t: { // op +AAAA
982 const s4 targ = (s4) pDecInsn->VRegA();
983 fprintf(gOutFile, " %04x // %c%04x",
984 insnIdx + targ,
985 (targ < 0) ? '-' : '+',
986 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -0700987 break;
Aart Bikdce50862016-06-10 16:04:03 -0700988 }
Aart Bik69ae54a2015-07-01 14:52:26 -0700989 case Instruction::k22x: // op vAA, vBBBB
990 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
991 break;
Aart Bikdce50862016-06-10 16:04:03 -0700992 case Instruction::k21t: { // op vAA, +BBBB
993 const s4 targ = (s4) pDecInsn->VRegB();
994 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
995 insnIdx + targ,
996 (targ < 0) ? '-' : '+',
997 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -0700998 break;
Aart Bikdce50862016-06-10 16:04:03 -0700999 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001000 case Instruction::k21s: // op vAA, #+BBBB
1001 fprintf(gOutFile, " v%d, #int %d // #%x",
1002 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
1003 break;
1004 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
1005 // The printed format varies a bit based on the actual opcode.
1006 if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) {
1007 const s4 value = pDecInsn->VRegB() << 16;
1008 fprintf(gOutFile, " v%d, #int %d // #%x",
1009 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1010 } else {
1011 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
1012 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
1013 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1014 }
1015 break;
1016 case Instruction::k21c: // op vAA, thing@BBBB
1017 case Instruction::k31c: // op vAA, thing@BBBBBBBB
Aart Bika0e33fd2016-07-08 18:32:45 -07001018 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001019 break;
1020 case Instruction::k23x: // op vAA, vBB, vCC
1021 fprintf(gOutFile, " v%d, v%d, v%d",
1022 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
1023 break;
1024 case Instruction::k22b: // op vAA, vBB, #+CC
1025 fprintf(gOutFile, " v%d, v%d, #int %d // #%02x",
1026 pDecInsn->VRegA(), pDecInsn->VRegB(),
1027 (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC());
1028 break;
Aart Bikdce50862016-06-10 16:04:03 -07001029 case Instruction::k22t: { // op vA, vB, +CCCC
1030 const s4 targ = (s4) pDecInsn->VRegC();
1031 fprintf(gOutFile, " v%d, v%d, %04x // %c%04x",
1032 pDecInsn->VRegA(), pDecInsn->VRegB(),
1033 insnIdx + targ,
1034 (targ < 0) ? '-' : '+',
1035 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001036 break;
Aart Bikdce50862016-06-10 16:04:03 -07001037 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001038 case Instruction::k22s: // op vA, vB, #+CCCC
1039 fprintf(gOutFile, " v%d, v%d, #int %d // #%04x",
1040 pDecInsn->VRegA(), pDecInsn->VRegB(),
1041 (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC());
1042 break;
1043 case Instruction::k22c: // op vA, vB, thing@CCCC
1044 // NOT SUPPORTED:
1045 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
1046 fprintf(gOutFile, " v%d, v%d, %s",
Aart Bika0e33fd2016-07-08 18:32:45 -07001047 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001048 break;
1049 case Instruction::k30t:
1050 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
1051 break;
Aart Bikdce50862016-06-10 16:04:03 -07001052 case Instruction::k31i: { // op vAA, #+BBBBBBBB
1053 // This is often, but not always, a float.
1054 union {
1055 float f;
1056 u4 i;
1057 } conv;
1058 conv.i = pDecInsn->VRegB();
1059 fprintf(gOutFile, " v%d, #float %g // #%08x",
1060 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001061 break;
Aart Bikdce50862016-06-10 16:04:03 -07001062 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001063 case Instruction::k31t: // op vAA, offset +BBBBBBBB
1064 fprintf(gOutFile, " v%d, %08x // +%08x",
1065 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
1066 break;
1067 case Instruction::k32x: // op vAAAA, vBBBB
1068 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1069 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +01001070 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
1071 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, method@BBBB, proto@HHHH
Aart Bik69ae54a2015-07-01 14:52:26 -07001072 // NOT SUPPORTED:
1073 // case Instruction::k35ms: // [opt] invoke-virtual+super
1074 // case Instruction::k35mi: // [opt] inline invoke
Aart Bikdce50862016-06-10 16:04:03 -07001075 u4 arg[Instruction::kMaxVarArgRegs];
1076 pDecInsn->GetVarArgs(arg);
1077 fputs(" {", gOutFile);
1078 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1079 if (i == 0) {
1080 fprintf(gOutFile, "v%d", arg[i]);
1081 } else {
1082 fprintf(gOutFile, ", v%d", arg[i]);
1083 }
1084 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001085 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001086 break;
Aart Bikdce50862016-06-10 16:04:03 -07001087 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001088 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
Orion Hodsonb34bb192016-10-18 17:02:58 +01001089 case Instruction::k4rcc: { // op {vCCCC .. v(CCCC+AA-1)}, method@BBBB, proto@HHHH
Aart Bik69ae54a2015-07-01 14:52:26 -07001090 // NOT SUPPORTED:
1091 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1092 // case Instruction::k3rmi: // [opt] execute-inline/range
Aart Bik69ae54a2015-07-01 14:52:26 -07001093 // This doesn't match the "dx" output when some of the args are
1094 // 64-bit values -- dx only shows the first register.
1095 fputs(" {", gOutFile);
1096 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1097 if (i == 0) {
1098 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
1099 } else {
1100 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
1101 }
1102 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001103 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001104 }
1105 break;
Aart Bikdce50862016-06-10 16:04:03 -07001106 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1107 // This is often, but not always, a double.
1108 union {
1109 double d;
1110 u8 j;
1111 } conv;
1112 conv.j = pDecInsn->WideVRegB();
1113 fprintf(gOutFile, " v%d, #double %g // #%016" PRIx64,
1114 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001115 break;
Aart Bikdce50862016-06-10 16:04:03 -07001116 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001117 // NOT SUPPORTED:
1118 // case Instruction::k00x: // unknown op or breakpoint
1119 // break;
1120 default:
1121 fprintf(gOutFile, " ???");
1122 break;
1123 } // switch
1124
1125 fputc('\n', gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001126}
1127
1128/*
1129 * Dumps a bytecode disassembly.
1130 */
1131static void dumpBytecodes(const DexFile* pDexFile, u4 idx,
1132 const DexFile::CodeItem* pCode, u4 codeOffset) {
1133 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1134 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1135 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1136 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1137
1138 // Generate header.
Aart Bikc05e2f22016-07-12 15:53:13 -07001139 std::unique_ptr<char[]> dot(descriptorToDot(backDescriptor));
1140 fprintf(gOutFile, "%06x: |[%06x] %s.%s:%s\n",
1141 codeOffset, codeOffset, dot.get(), name, signature.ToString().c_str());
Aart Bik69ae54a2015-07-01 14:52:26 -07001142
1143 // Iterate over all instructions.
Mathieu Chartier698ebbc2018-01-05 11:00:42 -08001144 CodeItemDataAccessor accessor(*pDexFile, pCode);
Aart Bik7a9aaf12018-02-05 17:00:40 -08001145 const u4 maxPc = accessor.InsnsSizeInCodeUnits();
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001146 for (const DexInstructionPcPair& pair : accessor) {
Aart Bik7a9aaf12018-02-05 17:00:40 -08001147 const u4 dexPc = pair.DexPc();
1148 if (dexPc >= maxPc) {
1149 LOG(WARNING) << "GLITCH: run-away instruction at idx=0x" << std::hex << dexPc;
1150 break;
1151 }
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001152 const Instruction* instruction = &pair.Inst();
Aart Bik69ae54a2015-07-01 14:52:26 -07001153 const u4 insnWidth = instruction->SizeInCodeUnits();
1154 if (insnWidth == 0) {
Aart Bik7a9aaf12018-02-05 17:00:40 -08001155 LOG(WARNING) << "GLITCH: zero-width instruction at idx=0x" << std::hex << dexPc;
Aart Bik69ae54a2015-07-01 14:52:26 -07001156 break;
1157 }
Aart Bik7a9aaf12018-02-05 17:00:40 -08001158 dumpInstruction(pDexFile, pCode, codeOffset, dexPc, insnWidth, instruction);
Aart Bik69ae54a2015-07-01 14:52:26 -07001159 } // for
1160}
1161
1162/*
1163 * Dumps code of a method.
1164 */
1165static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags,
1166 const DexFile::CodeItem* pCode, u4 codeOffset) {
Mathieu Chartier8892c6b2018-01-09 15:10:17 -08001167 CodeItemDebugInfoAccessor accessor(*pDexFile, pCode, idx);
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001168
1169 fprintf(gOutFile, " registers : %d\n", accessor.RegistersSize());
1170 fprintf(gOutFile, " ins : %d\n", accessor.InsSize());
1171 fprintf(gOutFile, " outs : %d\n", accessor.OutsSize());
Aart Bik69ae54a2015-07-01 14:52:26 -07001172 fprintf(gOutFile, " insns size : %d 16-bit code units\n",
Mathieu Chartier641a3af2017-12-15 11:42:58 -08001173 accessor.InsnsSizeInCodeUnits());
Aart Bik69ae54a2015-07-01 14:52:26 -07001174
1175 // Bytecode disassembly, if requested.
1176 if (gOptions.disassemble) {
1177 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
1178 }
1179
1180 // Try-catch blocks.
1181 dumpCatches(pDexFile, pCode);
1182
1183 // Positions and locals table in the debug info.
1184 bool is_static = (flags & kAccStatic) != 0;
1185 fprintf(gOutFile, " positions : \n");
Mathieu Chartier3e2e1232018-09-11 12:35:30 -07001186 accessor.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
1187 fprintf(gOutFile, " 0x%04x line=%d\n", entry.address_, entry.line_);
1188 return false;
1189 });
Aart Bik69ae54a2015-07-01 14:52:26 -07001190 fprintf(gOutFile, " locals : \n");
Mathieu Chartiere5afbf32018-09-12 17:51:54 -07001191 accessor.DecodeDebugLocalInfo(is_static,
1192 idx,
1193 [&](const DexFile::LocalInfo& entry) {
1194 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
1195 fprintf(gOutFile,
1196 " 0x%04x - 0x%04x reg=%d %s %s %s\n",
1197 entry.start_address_,
1198 entry.end_address_,
1199 entry.reg_,
1200 entry.name_,
1201 entry.descriptor_,
1202 signature);
1203 });
Aart Bik69ae54a2015-07-01 14:52:26 -07001204}
1205
1206/*
1207 * Dumps a method.
1208 */
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001209static void dumpMethod(const ClassAccessor::Method& method, int i) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001210 // Bail for anything private if export only requested.
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001211 const uint32_t flags = method.GetRawAccessFlags();
Aart Bik69ae54a2015-07-01 14:52:26 -07001212 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1213 return;
1214 }
1215
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001216 const DexFile& dex_file = method.GetDexFile();
1217 const DexFile::MethodId& pMethodId = dex_file.GetMethodId(method.GetIndex());
1218 const char* name = dex_file.StringDataByIdx(pMethodId.name_idx_);
1219 const Signature signature = dex_file.GetMethodSignature(pMethodId);
Aart Bik69ae54a2015-07-01 14:52:26 -07001220 char* typeDescriptor = strdup(signature.ToString().c_str());
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001221 const char* backDescriptor = dex_file.StringByTypeIdx(pMethodId.class_idx_);
Aart Bik69ae54a2015-07-01 14:52:26 -07001222 char* accessStr = createAccessFlagStr(flags, kAccessForMethod);
1223
1224 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1225 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1226 fprintf(gOutFile, " name : '%s'\n", name);
1227 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1228 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001229 if (method.GetCodeItem() == nullptr) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001230 fprintf(gOutFile, " code : (none)\n");
1231 } else {
1232 fprintf(gOutFile, " code -\n");
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001233 dumpCode(&dex_file,
1234 method.GetIndex(),
1235 flags,
1236 method.GetCodeItem(),
1237 method.GetCodeItemOffset());
Aart Bik69ae54a2015-07-01 14:52:26 -07001238 }
1239 if (gOptions.disassemble) {
1240 fputc('\n', gOutFile);
1241 }
1242 } else if (gOptions.outputFormat == OUTPUT_XML) {
1243 const bool constructor = (name[0] == '<');
1244
1245 // Method name and prototype.
1246 if (constructor) {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001247 std::unique_ptr<char[]> dot(descriptorClassToName(backDescriptor));
Aart Bikc05e2f22016-07-12 15:53:13 -07001248 fprintf(gOutFile, "<constructor name=\"%s\"\n", dot.get());
1249 dot = descriptorToDot(backDescriptor);
1250 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001251 } else {
1252 fprintf(gOutFile, "<method name=\"%s\"\n", name);
1253 const char* returnType = strrchr(typeDescriptor, ')');
1254 if (returnType == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001255 LOG(ERROR) << "bad method type descriptor '" << typeDescriptor << "'";
Aart Bik69ae54a2015-07-01 14:52:26 -07001256 goto bail;
1257 }
Aart Bikc05e2f22016-07-12 15:53:13 -07001258 std::unique_ptr<char[]> dot(descriptorToDot(returnType + 1));
1259 fprintf(gOutFile, " return=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001260 fprintf(gOutFile, " abstract=%s\n", quotedBool((flags & kAccAbstract) != 0));
1261 fprintf(gOutFile, " native=%s\n", quotedBool((flags & kAccNative) != 0));
1262 fprintf(gOutFile, " synchronized=%s\n", quotedBool(
1263 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1264 }
1265
1266 // Additional method flags.
1267 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1268 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1269 // The "deprecated=" not knowable w/o parsing annotations.
1270 fprintf(gOutFile, " visibility=%s\n>\n", quotedVisibility(flags));
1271
1272 // Parameters.
1273 if (typeDescriptor[0] != '(') {
Andreas Gampe221d9812018-01-22 17:48:56 -08001274 LOG(ERROR) << "ERROR: bad descriptor '" << typeDescriptor << "'";
Aart Bik69ae54a2015-07-01 14:52:26 -07001275 goto bail;
1276 }
1277 char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1));
1278 const char* base = typeDescriptor + 1;
1279 int argNum = 0;
1280 while (*base != ')') {
1281 char* cp = tmpBuf;
1282 while (*base == '[') {
1283 *cp++ = *base++;
1284 }
1285 if (*base == 'L') {
1286 // Copy through ';'.
1287 do {
1288 *cp = *base++;
1289 } while (*cp++ != ';');
1290 } else {
1291 // Primitive char, copy it.
Aart Bikc05e2f22016-07-12 15:53:13 -07001292 if (strchr("ZBCSIFJD", *base) == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001293 LOG(ERROR) << "ERROR: bad method signature '" << base << "'";
Aart Bika0e33fd2016-07-08 18:32:45 -07001294 break; // while
Aart Bik69ae54a2015-07-01 14:52:26 -07001295 }
1296 *cp++ = *base++;
1297 }
1298 // Null terminate and display.
1299 *cp++ = '\0';
Aart Bikc05e2f22016-07-12 15:53:13 -07001300 std::unique_ptr<char[]> dot(descriptorToDot(tmpBuf));
Aart Bik69ae54a2015-07-01 14:52:26 -07001301 fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n"
Aart Bikc05e2f22016-07-12 15:53:13 -07001302 "</parameter>\n", argNum++, dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001303 } // while
1304 free(tmpBuf);
1305 if (constructor) {
1306 fprintf(gOutFile, "</constructor>\n");
1307 } else {
1308 fprintf(gOutFile, "</method>\n");
1309 }
1310 }
1311
1312 bail:
1313 free(typeDescriptor);
1314 free(accessStr);
1315}
1316
1317/*
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001318 * Dumps a static or instance (class) field.
Aart Bik69ae54a2015-07-01 14:52:26 -07001319 */
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001320static void dumpField(const ClassAccessor::Field& field, int i, const u1** data = nullptr) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001321 // Bail for anything private if export only requested.
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001322 const uint32_t flags = field.GetRawAccessFlags();
Aart Bik69ae54a2015-07-01 14:52:26 -07001323 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1324 return;
1325 }
1326
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001327 const DexFile& dex_file = field.GetDexFile();
1328 const DexFile::FieldId& field_id = dex_file.GetFieldId(field.GetIndex());
1329 const char* name = dex_file.StringDataByIdx(field_id.name_idx_);
1330 const char* typeDescriptor = dex_file.StringByTypeIdx(field_id.type_idx_);
1331 const char* backDescriptor = dex_file.StringByTypeIdx(field_id.class_idx_);
Aart Bik69ae54a2015-07-01 14:52:26 -07001332 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);
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001341 dumpEncodedValue(&dex_file, 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);
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001357 dumpEncodedValue(&dex_file, 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/*
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001367 * Dumping a CFG.
Aart Bik69ae54a2015-07-01 14:52:26 -07001368 */
Andreas Gampe5073fed2015-08-10 11:40:25 -07001369static void dumpCfg(const DexFile* dex_file, int idx) {
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001370 ClassAccessor accessor(*dex_file, dex_file->GetClassDef(idx));
1371 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
1372 if (method.GetCodeItem() != nullptr) {
1373 std::ostringstream oss;
1374 DumpMethodCFG(method, oss);
1375 fputs(oss.str().c_str(), gOutFile);
1376 }
Andreas Gampe5073fed2015-08-10 11:40:25 -07001377 }
Andreas Gampe5073fed2015-08-10 11:40:25 -07001378}
1379
1380/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001381 * Dumps the class.
1382 *
1383 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1384 *
1385 * If "*pLastPackage" is nullptr or does not match the current class' package,
1386 * the value will be replaced with a newly-allocated string.
1387 */
1388static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1389 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1390
1391 // Omitting non-public class.
1392 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1393 return;
1394 }
1395
Aart Bikdce50862016-06-10 16:04:03 -07001396 if (gOptions.showSectionHeaders) {
1397 dumpClassDef(pDexFile, idx);
1398 }
1399
1400 if (gOptions.showAnnotations) {
1401 dumpClassAnnotations(pDexFile, idx);
1402 }
1403
1404 if (gOptions.showCfg) {
Andreas Gampe5073fed2015-08-10 11:40:25 -07001405 dumpCfg(pDexFile, idx);
1406 return;
1407 }
1408
Aart Bik69ae54a2015-07-01 14:52:26 -07001409 // For the XML output, show the package name. Ideally we'd gather
1410 // up the classes, sort them, and dump them alphabetically so the
1411 // package name wouldn't jump around, but that's not a great plan
1412 // for something that needs to run on the device.
1413 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1414 if (!(classDescriptor[0] == 'L' &&
1415 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1416 // Arrays and primitives should not be defined explicitly. Keep going?
Andreas Gampe221d9812018-01-22 17:48:56 -08001417 LOG(WARNING) << "Malformed class name '" << classDescriptor << "'";
Aart Bik69ae54a2015-07-01 14:52:26 -07001418 } else if (gOptions.outputFormat == OUTPUT_XML) {
1419 char* mangle = strdup(classDescriptor + 1);
1420 mangle[strlen(mangle)-1] = '\0';
1421
1422 // Reduce to just the package name.
1423 char* lastSlash = strrchr(mangle, '/');
1424 if (lastSlash != nullptr) {
1425 *lastSlash = '\0';
1426 } else {
1427 *mangle = '\0';
1428 }
1429
1430 for (char* cp = mangle; *cp != '\0'; cp++) {
1431 if (*cp == '/') {
1432 *cp = '.';
1433 }
1434 } // for
1435
1436 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1437 // Start of a new package.
1438 if (*pLastPackage != nullptr) {
1439 fprintf(gOutFile, "</package>\n");
1440 }
1441 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1442 free(*pLastPackage);
1443 *pLastPackage = mangle;
1444 } else {
1445 free(mangle);
1446 }
1447 }
1448
1449 // General class information.
1450 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1451 const char* superclassDescriptor;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001452 if (!pClassDef.superclass_idx_.IsValid()) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001453 superclassDescriptor = nullptr;
1454 } else {
1455 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1456 }
1457 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1458 fprintf(gOutFile, "Class #%d -\n", idx);
1459 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1460 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1461 if (superclassDescriptor != nullptr) {
1462 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1463 }
1464 fprintf(gOutFile, " Interfaces -\n");
1465 } else {
Orion Hodsonfe42d212018-08-24 14:01:14 +01001466 std::unique_ptr<char[]> dot(descriptorClassToName(classDescriptor));
Aart Bikc05e2f22016-07-12 15:53:13 -07001467 fprintf(gOutFile, "<class name=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001468 if (superclassDescriptor != nullptr) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001469 dot = descriptorToDot(superclassDescriptor);
1470 fprintf(gOutFile, " extends=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001471 }
Alex Light1f12e282015-12-10 16:49:47 -08001472 fprintf(gOutFile, " interface=%s\n",
1473 quotedBool((pClassDef.access_flags_ & kAccInterface) != 0));
Aart Bik69ae54a2015-07-01 14:52:26 -07001474 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1475 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1476 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1477 // The "deprecated=" not knowable w/o parsing annotations.
1478 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1479 fprintf(gOutFile, ">\n");
1480 }
1481
1482 // Interfaces.
1483 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1484 if (pInterfaces != nullptr) {
1485 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1486 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1487 } // for
1488 }
1489
1490 // Fields and methods.
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001491 ClassAccessor accessor(*pDexFile, pClassDef);
Aart Bikdce50862016-06-10 16:04:03 -07001492
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001493 // Prepare data for static fields.
1494 const u1* sData = pDexFile->GetEncodedStaticFieldValuesArray(pClassDef);
1495 const u4 sSize = sData != nullptr ? DecodeUnsignedLeb128(&sData) : 0;
Aart Bikdce50862016-06-10 16:04:03 -07001496
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001497 // Static fields.
1498 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1499 fprintf(gOutFile, " Static fields -\n");
1500 }
1501 uint32_t i = 0u;
1502 for (const ClassAccessor::Field& field : accessor.GetStaticFields()) {
1503 dumpField(field, i, i < sSize ? &sData : nullptr);
1504 ++i;
1505 }
Aart Bikdce50862016-06-10 16:04:03 -07001506
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001507 // Instance fields.
1508 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1509 fprintf(gOutFile, " Instance fields -\n");
1510 }
1511 i = 0u;
1512 for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
1513 dumpField(field, i);
1514 ++i;
1515 }
Aart Bikdce50862016-06-10 16:04:03 -07001516
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001517 // Direct methods.
1518 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1519 fprintf(gOutFile, " Direct methods -\n");
1520 }
1521 i = 0u;
1522 for (const ClassAccessor::Method& method : accessor.GetDirectMethods()) {
1523 dumpMethod(method, i);
1524 ++i;
1525 }
Aart Bikdce50862016-06-10 16:04:03 -07001526
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -07001527 // Virtual methods.
1528 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1529 fprintf(gOutFile, " Virtual methods -\n");
1530 }
1531 i = 0u;
1532 for (const ClassAccessor::Method& method : accessor.GetVirtualMethods()) {
1533 dumpMethod(method, i);
1534 ++i;
Aart Bik69ae54a2015-07-01 14:52:26 -07001535 }
1536
1537 // End of class.
1538 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1539 const char* fileName;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001540 if (pClassDef.source_file_idx_.IsValid()) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001541 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1542 } else {
1543 fileName = "unknown";
1544 }
1545 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001546 pClassDef.source_file_idx_.index_, fileName);
Aart Bik69ae54a2015-07-01 14:52:26 -07001547 } else if (gOptions.outputFormat == OUTPUT_XML) {
1548 fprintf(gOutFile, "</class>\n");
1549 }
1550
1551 free(accessStr);
1552}
1553
Orion Hodsonc069a302017-01-18 09:23:12 +00001554static void dumpMethodHandle(const DexFile* pDexFile, u4 idx) {
1555 const DexFile::MethodHandleItem& mh = pDexFile->GetMethodHandle(idx);
Orion Hodson631827d2017-04-10 14:53:47 +01001556 const char* type = nullptr;
1557 bool is_instance = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001558 bool is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001559 switch (static_cast<DexFile::MethodHandleType>(mh.method_handle_type_)) {
1560 case DexFile::MethodHandleType::kStaticPut:
1561 type = "put-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001562 is_instance = false;
1563 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001564 break;
1565 case DexFile::MethodHandleType::kStaticGet:
1566 type = "get-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001567 is_instance = false;
1568 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001569 break;
1570 case DexFile::MethodHandleType::kInstancePut:
1571 type = "put-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001572 is_instance = true;
1573 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001574 break;
1575 case DexFile::MethodHandleType::kInstanceGet:
1576 type = "get-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001577 is_instance = true;
1578 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001579 break;
1580 case DexFile::MethodHandleType::kInvokeStatic:
1581 type = "invoke-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001582 is_instance = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001583 is_invoke = true;
1584 break;
1585 case DexFile::MethodHandleType::kInvokeInstance:
1586 type = "invoke-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001587 is_instance = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00001588 is_invoke = true;
1589 break;
1590 case DexFile::MethodHandleType::kInvokeConstructor:
1591 type = "invoke-constructor";
Orion Hodson631827d2017-04-10 14:53:47 +01001592 is_instance = true;
1593 is_invoke = true;
1594 break;
1595 case DexFile::MethodHandleType::kInvokeDirect:
1596 type = "invoke-direct";
1597 is_instance = true;
1598 is_invoke = true;
1599 break;
1600 case DexFile::MethodHandleType::kInvokeInterface:
1601 type = "invoke-interface";
1602 is_instance = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00001603 is_invoke = true;
1604 break;
1605 }
1606
1607 const char* declaring_class;
1608 const char* member;
1609 std::string member_type;
Orion Hodson631827d2017-04-10 14:53:47 +01001610 if (type != nullptr) {
1611 if (is_invoke) {
1612 const DexFile::MethodId& method_id = pDexFile->GetMethodId(mh.field_or_method_idx_);
1613 declaring_class = pDexFile->GetMethodDeclaringClassDescriptor(method_id);
1614 member = pDexFile->GetMethodName(method_id);
1615 member_type = pDexFile->GetMethodSignature(method_id).ToString();
1616 } else {
1617 const DexFile::FieldId& field_id = pDexFile->GetFieldId(mh.field_or_method_idx_);
1618 declaring_class = pDexFile->GetFieldDeclaringClassDescriptor(field_id);
1619 member = pDexFile->GetFieldName(field_id);
1620 member_type = pDexFile->GetFieldTypeDescriptor(field_id);
1621 }
1622 if (is_instance) {
1623 member_type = android::base::StringPrintf("(%s%s", declaring_class, member_type.c_str() + 1);
1624 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001625 } else {
Orion Hodson631827d2017-04-10 14:53:47 +01001626 type = "?";
1627 declaring_class = "?";
1628 member = "?";
1629 member_type = "?";
Orion Hodsonc069a302017-01-18 09:23:12 +00001630 }
1631
1632 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1633 fprintf(gOutFile, "Method handle #%u:\n", idx);
1634 fprintf(gOutFile, " type : %s\n", type);
1635 fprintf(gOutFile, " target : %s %s\n", declaring_class, member);
1636 fprintf(gOutFile, " target_type : %s\n", member_type.c_str());
1637 } else {
1638 fprintf(gOutFile, "<method_handle index=\"%u\"\n", idx);
1639 fprintf(gOutFile, " type=\"%s\"\n", type);
1640 fprintf(gOutFile, " target_class=\"%s\"\n", declaring_class);
1641 fprintf(gOutFile, " target_member=\"%s\"\n", member);
1642 fprintf(gOutFile, " target_member_type=");
1643 dumpEscapedString(member_type.c_str());
1644 fprintf(gOutFile, "\n>\n</method_handle>\n");
1645 }
1646}
1647
1648static void dumpCallSite(const DexFile* pDexFile, u4 idx) {
1649 const DexFile::CallSiteIdItem& call_site_id = pDexFile->GetCallSiteId(idx);
1650 CallSiteArrayValueIterator it(*pDexFile, call_site_id);
1651 if (it.Size() < 3) {
Andreas Gampe221d9812018-01-22 17:48:56 -08001652 LOG(ERROR) << "ERROR: Call site " << idx << " has too few values.";
Orion Hodsonc069a302017-01-18 09:23:12 +00001653 return;
1654 }
1655
1656 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1657 it.Next();
1658 dex::StringIndex method_name_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1659 const char* method_name = pDexFile->StringDataByIdx(method_name_idx);
1660 it.Next();
Orion Hodson06d10a72018-05-14 08:53:38 +01001661 dex::ProtoIndex method_type_idx = static_cast<dex::ProtoIndex>(it.GetJavaValue().i);
Orion Hodsonc069a302017-01-18 09:23:12 +00001662 const DexFile::ProtoId& method_type_id = pDexFile->GetProtoId(method_type_idx);
1663 std::string method_type = pDexFile->GetProtoSignature(method_type_id).ToString();
1664 it.Next();
1665
1666 if (gOptions.outputFormat == OUTPUT_PLAIN) {
Orion Hodson775224d2017-07-05 11:04:01 +01001667 fprintf(gOutFile, "Call site #%u: // offset %u\n", idx, call_site_id.data_off_);
Orion Hodsonc069a302017-01-18 09:23:12 +00001668 fprintf(gOutFile, " link_argument[0] : %u (MethodHandle)\n", method_handle_idx);
1669 fprintf(gOutFile, " link_argument[1] : %s (String)\n", method_name);
1670 fprintf(gOutFile, " link_argument[2] : %s (MethodType)\n", method_type.c_str());
1671 } else {
Orion Hodson775224d2017-07-05 11:04:01 +01001672 fprintf(gOutFile, "<call_site index=\"%u\" offset=\"%u\">\n", idx, call_site_id.data_off_);
Orion Hodsonc069a302017-01-18 09:23:12 +00001673 fprintf(gOutFile,
1674 "<link_argument index=\"0\" type=\"MethodHandle\" value=\"%u\"/>\n",
1675 method_handle_idx);
1676 fprintf(gOutFile,
1677 "<link_argument index=\"1\" type=\"String\" values=\"%s\"/>\n",
1678 method_name);
1679 fprintf(gOutFile,
1680 "<link_argument index=\"2\" type=\"MethodType\" value=\"%s\"/>\n",
1681 method_type.c_str());
1682 }
1683
1684 size_t argument = 3;
1685 while (it.HasNext()) {
1686 const char* type;
1687 std::string value;
1688 switch (it.GetValueType()) {
1689 case EncodedArrayValueIterator::ValueType::kByte:
1690 type = "byte";
1691 value = android::base::StringPrintf("%u", it.GetJavaValue().b);
1692 break;
1693 case EncodedArrayValueIterator::ValueType::kShort:
1694 type = "short";
1695 value = android::base::StringPrintf("%d", it.GetJavaValue().s);
1696 break;
1697 case EncodedArrayValueIterator::ValueType::kChar:
1698 type = "char";
1699 value = android::base::StringPrintf("%u", it.GetJavaValue().c);
1700 break;
1701 case EncodedArrayValueIterator::ValueType::kInt:
1702 type = "int";
1703 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1704 break;
1705 case EncodedArrayValueIterator::ValueType::kLong:
1706 type = "long";
1707 value = android::base::StringPrintf("%" PRId64, it.GetJavaValue().j);
1708 break;
1709 case EncodedArrayValueIterator::ValueType::kFloat:
1710 type = "float";
1711 value = android::base::StringPrintf("%g", it.GetJavaValue().f);
1712 break;
1713 case EncodedArrayValueIterator::ValueType::kDouble:
1714 type = "double";
1715 value = android::base::StringPrintf("%g", it.GetJavaValue().d);
1716 break;
1717 case EncodedArrayValueIterator::ValueType::kMethodType: {
1718 type = "MethodType";
Orion Hodson06d10a72018-05-14 08:53:38 +01001719 dex::ProtoIndex proto_idx = static_cast<dex::ProtoIndex>(it.GetJavaValue().i);
Orion Hodsonc069a302017-01-18 09:23:12 +00001720 const DexFile::ProtoId& proto_id = pDexFile->GetProtoId(proto_idx);
1721 value = pDexFile->GetProtoSignature(proto_id).ToString();
1722 break;
1723 }
1724 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1725 type = "MethodHandle";
1726 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1727 break;
1728 case EncodedArrayValueIterator::ValueType::kString: {
1729 type = "String";
1730 dex::StringIndex string_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1731 value = pDexFile->StringDataByIdx(string_idx);
1732 break;
1733 }
1734 case EncodedArrayValueIterator::ValueType::kType: {
1735 type = "Class";
1736 dex::TypeIndex type_idx = static_cast<dex::TypeIndex>(it.GetJavaValue().i);
Orion Hodson0f6cc7f2018-05-25 15:33:44 +01001737 const DexFile::TypeId& type_id = pDexFile->GetTypeId(type_idx);
1738 value = pDexFile->GetTypeDescriptor(type_id);
Orion Hodsonc069a302017-01-18 09:23:12 +00001739 break;
1740 }
1741 case EncodedArrayValueIterator::ValueType::kField:
1742 case EncodedArrayValueIterator::ValueType::kMethod:
1743 case EncodedArrayValueIterator::ValueType::kEnum:
1744 case EncodedArrayValueIterator::ValueType::kArray:
1745 case EncodedArrayValueIterator::ValueType::kAnnotation:
1746 // Unreachable based on current EncodedArrayValueIterator::Next().
Andreas Gampef45d61c2017-06-07 10:29:33 -07001747 UNIMPLEMENTED(FATAL) << " type " << it.GetValueType();
Orion Hodsonc069a302017-01-18 09:23:12 +00001748 UNREACHABLE();
Orion Hodsonc069a302017-01-18 09:23:12 +00001749 case EncodedArrayValueIterator::ValueType::kNull:
1750 type = "Null";
1751 value = "null";
1752 break;
1753 case EncodedArrayValueIterator::ValueType::kBoolean:
1754 type = "boolean";
1755 value = it.GetJavaValue().z ? "true" : "false";
1756 break;
1757 }
1758
1759 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1760 fprintf(gOutFile, " link_argument[%zu] : %s (%s)\n", argument, value.c_str(), type);
1761 } else {
1762 fprintf(gOutFile, "<link_argument index=\"%zu\" type=\"%s\" value=", argument, type);
1763 dumpEscapedString(value.c_str());
1764 fprintf(gOutFile, "/>\n");
1765 }
1766
1767 it.Next();
1768 argument++;
1769 }
1770
1771 if (gOptions.outputFormat == OUTPUT_XML) {
1772 fprintf(gOutFile, "</call_site>\n");
1773 }
1774}
1775
Aart Bik69ae54a2015-07-01 14:52:26 -07001776/*
1777 * Dumps the requested sections of the file.
1778 */
Aart Bik7b45a8a2016-10-24 16:07:59 -07001779static void processDexFile(const char* fileName,
1780 const DexFile* pDexFile, size_t i, size_t n) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001781 if (gOptions.verbose) {
Aart Bik7b45a8a2016-10-24 16:07:59 -07001782 fputs("Opened '", gOutFile);
1783 fputs(fileName, gOutFile);
1784 if (n > 1) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001785 fprintf(gOutFile, ":%s", DexFileLoader::GetMultiDexClassesDexName(i).c_str());
Aart Bik7b45a8a2016-10-24 16:07:59 -07001786 }
1787 fprintf(gOutFile, "', DEX version '%.3s'\n", pDexFile->GetHeader().magic_ + 4);
Aart Bik69ae54a2015-07-01 14:52:26 -07001788 }
1789
1790 // Headers.
1791 if (gOptions.showFileHeaders) {
1792 dumpFileHeader(pDexFile);
1793 }
1794
1795 // Open XML context.
1796 if (gOptions.outputFormat == OUTPUT_XML) {
1797 fprintf(gOutFile, "<api>\n");
1798 }
1799
1800 // Iterate over all classes.
1801 char* package = nullptr;
1802 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
Andreas Gampe70dfb692018-09-18 16:50:18 -07001803 for (u4 j = 0; j < classDefsSize; j++) {
1804 dumpClass(pDexFile, j, &package);
Aart Bik69ae54a2015-07-01 14:52:26 -07001805 } // for
1806
Orion Hodsonc069a302017-01-18 09:23:12 +00001807 // Iterate over all method handles.
Andreas Gampe70dfb692018-09-18 16:50:18 -07001808 for (u4 j = 0; j < pDexFile->NumMethodHandles(); ++j) {
1809 dumpMethodHandle(pDexFile, j);
Orion Hodsonc069a302017-01-18 09:23:12 +00001810 } // for
1811
1812 // Iterate over all call site ids.
Andreas Gampe70dfb692018-09-18 16:50:18 -07001813 for (u4 j = 0; j < pDexFile->NumCallSiteIds(); ++j) {
1814 dumpCallSite(pDexFile, j);
Orion Hodsonc069a302017-01-18 09:23:12 +00001815 } // for
1816
Aart Bik69ae54a2015-07-01 14:52:26 -07001817 // Free the last package allocated.
1818 if (package != nullptr) {
1819 fprintf(gOutFile, "</package>\n");
1820 free(package);
1821 }
1822
1823 // Close XML context.
1824 if (gOptions.outputFormat == OUTPUT_XML) {
1825 fprintf(gOutFile, "</api>\n");
1826 }
1827}
1828
1829/*
1830 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1831 */
1832int processFile(const char* fileName) {
1833 if (gOptions.verbose) {
1834 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1835 }
1836
Nicolas Geoffrayc1d8caa2018-02-27 10:15:14 +00001837 const bool kVerifyChecksum = !gOptions.ignoreBadChecksum;
1838 const bool kVerify = !gOptions.disableVerifier;
1839 std::string content;
Aart Bik69ae54a2015-07-01 14:52:26 -07001840 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
Aart Bikdce50862016-06-10 16:04:03 -07001841 // all of which are Zip archives with "classes.dex" inside.
David Sehr999646d2018-02-16 10:22:33 -08001842 // TODO: add an api to android::base to read a std::vector<uint8_t>.
1843 if (!android::base::ReadFileToString(fileName, &content)) {
1844 LOG(ERROR) << "ReadFileToString failed";
David Sehr5a1f6292018-01-19 11:08:51 -08001845 return -1;
1846 }
1847 const DexFileLoader dex_file_loader;
Dario Frenie166fac2018-07-16 11:08:03 +01001848 DexFileLoaderErrorCode error_code;
David Sehr999646d2018-02-16 10:22:33 -08001849 std::string error_msg;
Aart Bik69ae54a2015-07-01 14:52:26 -07001850 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr999646d2018-02-16 10:22:33 -08001851 if (!dex_file_loader.OpenAll(reinterpret_cast<const uint8_t*>(content.data()),
1852 content.size(),
1853 fileName,
Nicolas Geoffrayc1d8caa2018-02-27 10:15:14 +00001854 kVerify,
David Sehr999646d2018-02-16 10:22:33 -08001855 kVerifyChecksum,
Dario Frenie166fac2018-07-16 11:08:03 +01001856 &error_code,
David Sehr999646d2018-02-16 10:22:33 -08001857 &error_msg,
1858 &dex_files)) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001859 // Display returned error message to user. Note that this error behavior
1860 // differs from the error messages shown by the original Dalvik dexdump.
Andreas Gampe221d9812018-01-22 17:48:56 -08001861 LOG(ERROR) << error_msg;
Aart Bik69ae54a2015-07-01 14:52:26 -07001862 return -1;
1863 }
1864
Aart Bik4e149602015-07-09 11:45:28 -07001865 // Success. Either report checksum verification or process
1866 // all dex files found in given file.
Aart Bik69ae54a2015-07-01 14:52:26 -07001867 if (gOptions.checksumOnly) {
1868 fprintf(gOutFile, "Checksum verified\n");
1869 } else {
Aart Bik7b45a8a2016-10-24 16:07:59 -07001870 for (size_t i = 0, n = dex_files.size(); i < n; i++) {
1871 processDexFile(fileName, dex_files[i].get(), i, n);
Aart Bik4e149602015-07-09 11:45:28 -07001872 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001873 }
1874 return 0;
1875}
1876
1877} // namespace art