blob: 3648a3edd0d96d10a2616dae16e15560f262ce13 [file] [log] [blame]
Aart Bik69ae54a2015-07-01 14:52:26 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Implementation file of the dexdump utility.
17 *
18 * This is a re-implementation of the original dexdump utility that was
19 * based on Dalvik functions in libdex into a new dexdump that is now
Aart Bikdce50862016-06-10 16:04:03 -070020 * based on Art functions in libart instead. The output is very similar to
21 * to the original for correct DEX files. Error messages may differ, however.
Aart Bik69ae54a2015-07-01 14:52:26 -070022 * Also, ODEX files are no longer supported.
23 *
24 * The dexdump tool is intended to mimic objdump. When possible, use
25 * similar command-line arguments.
26 *
27 * Differences between XML output and the "current.xml" file:
28 * - classes in same package are not all grouped together; nothing is sorted
29 * - no "deprecated" on fields and methods
Aart Bik69ae54a2015-07-01 14:52:26 -070030 * - no parameter names
31 * - no generic signatures on parameters, e.g. type="java.lang.Class<?>"
32 * - class shows declared fields and methods; does not show inherited fields
33 */
34
35#include "dexdump.h"
36
37#include <inttypes.h>
38#include <stdio.h>
39
Andreas Gampe5073fed2015-08-10 11:40:25 -070040#include <iostream>
Aart Bik69ae54a2015-07-01 14:52:26 -070041#include <memory>
Andreas Gampe5073fed2015-08-10 11:40:25 -070042#include <sstream>
Aart Bik69ae54a2015-07-01 14:52:26 -070043#include <vector>
44
Andreas Gampe46ee31b2016-12-14 10:11:49 -080045#include "android-base/stringprintf.h"
46
Aart Bik69ae54a2015-07-01 14:52:26 -070047#include "dex_file-inl.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070048#include "dex_file_loader.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080049#include "dex_file_types.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070050#include "dex_instruction-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070051#include "dexdump_cfg.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070052
53namespace art {
54
55/*
56 * Options parsed in main driver.
57 */
58struct Options gOptions;
59
60/*
Aart Bik4e149602015-07-09 11:45:28 -070061 * Output file. Defaults to stdout.
Aart Bik69ae54a2015-07-01 14:52:26 -070062 */
63FILE* gOutFile = stdout;
64
65/*
66 * Data types that match the definitions in the VM specification.
67 */
68typedef uint8_t u1;
69typedef uint16_t u2;
70typedef uint32_t u4;
71typedef uint64_t u8;
Aart Bikdce50862016-06-10 16:04:03 -070072typedef int8_t s1;
73typedef int16_t s2;
Aart Bik69ae54a2015-07-01 14:52:26 -070074typedef int32_t s4;
75typedef int64_t s8;
76
77/*
78 * Basic information about a field or a method.
79 */
80struct FieldMethodInfo {
81 const char* classDescriptor;
82 const char* name;
83 const char* signature;
84};
85
86/*
87 * Flags for use with createAccessFlagStr().
88 */
89enum AccessFor {
90 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
91};
92const int kNumFlags = 18;
93
94/*
95 * Gets 2 little-endian bytes.
96 */
97static inline u2 get2LE(unsigned char const* pSrc) {
98 return pSrc[0] | (pSrc[1] << 8);
99}
100
101/*
102 * Converts a single-character primitive type into human-readable form.
103 */
104static const char* primitiveTypeLabel(char typeChar) {
105 switch (typeChar) {
106 case 'B': return "byte";
107 case 'C': return "char";
108 case 'D': return "double";
109 case 'F': return "float";
110 case 'I': return "int";
111 case 'J': return "long";
112 case 'S': return "short";
113 case 'V': return "void";
114 case 'Z': return "boolean";
115 default: return "UNKNOWN";
116 } // switch
117}
118
119/*
120 * Converts a type descriptor to human-readable "dotted" form. For
121 * example, "Ljava/lang/String;" becomes "java.lang.String", and
122 * "[I" becomes "int[]". Also converts '$' to '.', which means this
123 * form can't be converted back to a descriptor.
124 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700125static std::unique_ptr<char[]> descriptorToDot(const char* str) {
Aart Bik69ae54a2015-07-01 14:52:26 -0700126 int targetLen = strlen(str);
127 int offset = 0;
128
129 // Strip leading [s; will be added to end.
130 while (targetLen > 1 && str[offset] == '[') {
131 offset++;
132 targetLen--;
133 } // while
134
135 const int arrayDepth = offset;
136
137 if (targetLen == 1) {
138 // Primitive type.
139 str = primitiveTypeLabel(str[offset]);
140 offset = 0;
141 targetLen = strlen(str);
142 } else {
143 // Account for leading 'L' and trailing ';'.
144 if (targetLen >= 2 && str[offset] == 'L' &&
145 str[offset + targetLen - 1] == ';') {
146 targetLen -= 2;
147 offset++;
148 }
149 }
150
151 // Copy class name over.
Aart Bikc05e2f22016-07-12 15:53:13 -0700152 std::unique_ptr<char[]> newStr(new char[targetLen + arrayDepth * 2 + 1]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700153 int i = 0;
154 for (; i < targetLen; i++) {
155 const char ch = str[offset + i];
156 newStr[i] = (ch == '/' || ch == '$') ? '.' : ch;
157 } // for
158
159 // Add the appropriate number of brackets for arrays.
160 for (int j = 0; j < arrayDepth; j++) {
161 newStr[i++] = '[';
162 newStr[i++] = ']';
163 } // for
164
165 newStr[i] = '\0';
166 return newStr;
167}
168
169/*
170 * Converts the class name portion of a type descriptor to human-readable
Aart Bikc05e2f22016-07-12 15:53:13 -0700171 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
Aart Bik69ae54a2015-07-01 14:52:26 -0700172 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700173static std::unique_ptr<char[]> descriptorClassToDot(const char* str) {
174 // Reduce to just the class name prefix.
Aart Bik69ae54a2015-07-01 14:52:26 -0700175 const char* lastSlash = strrchr(str, '/');
176 if (lastSlash == nullptr) {
177 lastSlash = str + 1; // start past 'L'
178 } else {
179 lastSlash++; // start past '/'
180 }
181
Aart Bikc05e2f22016-07-12 15:53:13 -0700182 // Copy class name over, trimming trailing ';'.
183 const int targetLen = strlen(lastSlash);
184 std::unique_ptr<char[]> newStr(new char[targetLen]);
185 for (int i = 0; i < targetLen - 1; i++) {
186 const char ch = lastSlash[i];
187 newStr[i] = ch == '$' ? '.' : ch;
Aart Bik69ae54a2015-07-01 14:52:26 -0700188 } // for
Aart Bikc05e2f22016-07-12 15:53:13 -0700189 newStr[targetLen - 1] = '\0';
Aart Bik69ae54a2015-07-01 14:52:26 -0700190 return newStr;
191}
192
193/*
Aart Bikdce50862016-06-10 16:04:03 -0700194 * Returns string representing the boolean value.
195 */
196static const char* strBool(bool val) {
197 return val ? "true" : "false";
198}
199
200/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700201 * Returns a quoted string representing the boolean value.
202 */
203static const char* quotedBool(bool val) {
204 return val ? "\"true\"" : "\"false\"";
205}
206
207/*
208 * Returns a quoted string representing the access flags.
209 */
210static const char* quotedVisibility(u4 accessFlags) {
211 if (accessFlags & kAccPublic) {
212 return "\"public\"";
213 } else if (accessFlags & kAccProtected) {
214 return "\"protected\"";
215 } else if (accessFlags & kAccPrivate) {
216 return "\"private\"";
217 } else {
218 return "\"package\"";
219 }
220}
221
222/*
223 * Counts the number of '1' bits in a word.
224 */
225static int countOnes(u4 val) {
226 val = val - ((val >> 1) & 0x55555555);
227 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
228 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
229}
230
231/*
232 * Creates a new string with human-readable access flags.
233 *
234 * In the base language the access_flags fields are type u2; in Dalvik
235 * they're u4.
236 */
237static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
238 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
239 {
240 "PUBLIC", /* 0x00001 */
241 "PRIVATE", /* 0x00002 */
242 "PROTECTED", /* 0x00004 */
243 "STATIC", /* 0x00008 */
244 "FINAL", /* 0x00010 */
245 "?", /* 0x00020 */
246 "?", /* 0x00040 */
247 "?", /* 0x00080 */
248 "?", /* 0x00100 */
249 "INTERFACE", /* 0x00200 */
250 "ABSTRACT", /* 0x00400 */
251 "?", /* 0x00800 */
252 "SYNTHETIC", /* 0x01000 */
253 "ANNOTATION", /* 0x02000 */
254 "ENUM", /* 0x04000 */
255 "?", /* 0x08000 */
256 "VERIFIED", /* 0x10000 */
257 "OPTIMIZED", /* 0x20000 */
258 }, {
259 "PUBLIC", /* 0x00001 */
260 "PRIVATE", /* 0x00002 */
261 "PROTECTED", /* 0x00004 */
262 "STATIC", /* 0x00008 */
263 "FINAL", /* 0x00010 */
264 "SYNCHRONIZED", /* 0x00020 */
265 "BRIDGE", /* 0x00040 */
266 "VARARGS", /* 0x00080 */
267 "NATIVE", /* 0x00100 */
268 "?", /* 0x00200 */
269 "ABSTRACT", /* 0x00400 */
270 "STRICT", /* 0x00800 */
271 "SYNTHETIC", /* 0x01000 */
272 "?", /* 0x02000 */
273 "?", /* 0x04000 */
274 "MIRANDA", /* 0x08000 */
275 "CONSTRUCTOR", /* 0x10000 */
276 "DECLARED_SYNCHRONIZED", /* 0x20000 */
277 }, {
278 "PUBLIC", /* 0x00001 */
279 "PRIVATE", /* 0x00002 */
280 "PROTECTED", /* 0x00004 */
281 "STATIC", /* 0x00008 */
282 "FINAL", /* 0x00010 */
283 "?", /* 0x00020 */
284 "VOLATILE", /* 0x00040 */
285 "TRANSIENT", /* 0x00080 */
286 "?", /* 0x00100 */
287 "?", /* 0x00200 */
288 "?", /* 0x00400 */
289 "?", /* 0x00800 */
290 "SYNTHETIC", /* 0x01000 */
291 "?", /* 0x02000 */
292 "ENUM", /* 0x04000 */
293 "?", /* 0x08000 */
294 "?", /* 0x10000 */
295 "?", /* 0x20000 */
296 },
297 };
298
299 // Allocate enough storage to hold the expected number of strings,
300 // plus a space between each. We over-allocate, using the longest
301 // string above as the base metric.
302 const int kLongest = 21; // The strlen of longest string above.
303 const int count = countOnes(flags);
304 char* str;
305 char* cp;
306 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
307
308 for (int i = 0; i < kNumFlags; i++) {
309 if (flags & 0x01) {
310 const char* accessStr = kAccessStrings[forWhat][i];
311 const int len = strlen(accessStr);
312 if (cp != str) {
313 *cp++ = ' ';
314 }
315 memcpy(cp, accessStr, len);
316 cp += len;
317 }
318 flags >>= 1;
319 } // for
320
321 *cp = '\0';
322 return str;
323}
324
325/*
326 * Copies character data from "data" to "out", converting non-ASCII values
327 * to fprintf format chars or an ASCII filler ('.' or '?').
328 *
329 * The output buffer must be able to hold (2*len)+1 bytes. The result is
330 * NULL-terminated.
331 */
332static void asciify(char* out, const unsigned char* data, size_t len) {
333 while (len--) {
334 if (*data < 0x20) {
335 // Could do more here, but we don't need them yet.
336 switch (*data) {
337 case '\0':
338 *out++ = '\\';
339 *out++ = '0';
340 break;
341 case '\n':
342 *out++ = '\\';
343 *out++ = 'n';
344 break;
345 default:
346 *out++ = '.';
347 break;
348 } // switch
349 } else if (*data >= 0x80) {
350 *out++ = '?';
351 } else {
352 *out++ = *data;
353 }
354 data++;
355 } // while
356 *out = '\0';
357}
358
359/*
Aart Bikdce50862016-06-10 16:04:03 -0700360 * Dumps a string value with some escape characters.
361 */
362static void dumpEscapedString(const char* p) {
363 fputs("\"", gOutFile);
364 for (; *p; p++) {
365 switch (*p) {
366 case '\\':
367 fputs("\\\\", gOutFile);
368 break;
369 case '\"':
370 fputs("\\\"", gOutFile);
371 break;
372 case '\t':
373 fputs("\\t", gOutFile);
374 break;
375 case '\n':
376 fputs("\\n", gOutFile);
377 break;
378 case '\r':
379 fputs("\\r", gOutFile);
380 break;
381 default:
382 putc(*p, gOutFile);
383 } // switch
384 } // for
385 fputs("\"", gOutFile);
386}
387
388/*
389 * Dumps a string as an XML attribute value.
390 */
391static void dumpXmlAttribute(const char* p) {
392 for (; *p; p++) {
393 switch (*p) {
394 case '&':
395 fputs("&amp;", gOutFile);
396 break;
397 case '<':
398 fputs("&lt;", gOutFile);
399 break;
400 case '>':
401 fputs("&gt;", gOutFile);
402 break;
403 case '"':
404 fputs("&quot;", gOutFile);
405 break;
406 case '\t':
407 fputs("&#x9;", gOutFile);
408 break;
409 case '\n':
410 fputs("&#xA;", gOutFile);
411 break;
412 case '\r':
413 fputs("&#xD;", gOutFile);
414 break;
415 default:
416 putc(*p, gOutFile);
417 } // switch
418 } // for
419}
420
421/*
422 * Reads variable width value, possibly sign extended at the last defined byte.
423 */
424static u8 readVarWidth(const u1** data, u1 arg, bool sign_extend) {
425 u8 value = 0;
426 for (u4 i = 0; i <= arg; i++) {
427 value |= static_cast<u8>(*(*data)++) << (i * 8);
428 }
429 if (sign_extend) {
430 int shift = (7 - arg) * 8;
431 return (static_cast<s8>(value) << shift) >> shift;
432 }
433 return value;
434}
435
436/*
437 * Dumps encoded value.
438 */
439static void dumpEncodedValue(const DexFile* pDexFile, const u1** data); // forward
440static void dumpEncodedValue(const DexFile* pDexFile, const u1** data, u1 type, u1 arg) {
441 switch (type) {
442 case DexFile::kDexAnnotationByte:
443 fprintf(gOutFile, "%" PRId8, static_cast<s1>(readVarWidth(data, arg, false)));
444 break;
445 case DexFile::kDexAnnotationShort:
446 fprintf(gOutFile, "%" PRId16, static_cast<s2>(readVarWidth(data, arg, true)));
447 break;
448 case DexFile::kDexAnnotationChar:
449 fprintf(gOutFile, "%" PRIu16, static_cast<u2>(readVarWidth(data, arg, false)));
450 break;
451 case DexFile::kDexAnnotationInt:
452 fprintf(gOutFile, "%" PRId32, static_cast<s4>(readVarWidth(data, arg, true)));
453 break;
454 case DexFile::kDexAnnotationLong:
455 fprintf(gOutFile, "%" PRId64, static_cast<s8>(readVarWidth(data, arg, true)));
456 break;
457 case DexFile::kDexAnnotationFloat: {
458 // Fill on right.
459 union {
460 float f;
461 u4 data;
462 } conv;
463 conv.data = static_cast<u4>(readVarWidth(data, arg, false)) << (3 - arg) * 8;
464 fprintf(gOutFile, "%g", conv.f);
465 break;
466 }
467 case DexFile::kDexAnnotationDouble: {
468 // Fill on right.
469 union {
470 double d;
471 u8 data;
472 } conv;
473 conv.data = readVarWidth(data, arg, false) << (7 - arg) * 8;
474 fprintf(gOutFile, "%g", conv.d);
475 break;
476 }
477 case DexFile::kDexAnnotationString: {
478 const u4 idx = static_cast<u4>(readVarWidth(data, arg, false));
479 if (gOptions.outputFormat == OUTPUT_PLAIN) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800480 dumpEscapedString(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
Aart Bikdce50862016-06-10 16:04:03 -0700481 } else {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800482 dumpXmlAttribute(pDexFile->StringDataByIdx(dex::StringIndex(idx)));
Aart Bikdce50862016-06-10 16:04:03 -0700483 }
484 break;
485 }
486 case DexFile::kDexAnnotationType: {
487 const u4 str_idx = static_cast<u4>(readVarWidth(data, arg, false));
Andreas Gampea5b09a62016-11-17 15:21:22 -0800488 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(str_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700489 break;
490 }
491 case DexFile::kDexAnnotationField:
492 case DexFile::kDexAnnotationEnum: {
493 const u4 field_idx = static_cast<u4>(readVarWidth(data, arg, false));
494 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
495 fputs(pDexFile->StringDataByIdx(pFieldId.name_idx_), gOutFile);
496 break;
497 }
498 case DexFile::kDexAnnotationMethod: {
499 const u4 method_idx = static_cast<u4>(readVarWidth(data, arg, false));
500 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
501 fputs(pDexFile->StringDataByIdx(pMethodId.name_idx_), gOutFile);
502 break;
503 }
504 case DexFile::kDexAnnotationArray: {
505 fputc('{', gOutFile);
506 // Decode and display all elements.
507 const u4 size = DecodeUnsignedLeb128(data);
508 for (u4 i = 0; i < size; i++) {
509 fputc(' ', gOutFile);
510 dumpEncodedValue(pDexFile, data);
511 }
512 fputs(" }", gOutFile);
513 break;
514 }
515 case DexFile::kDexAnnotationAnnotation: {
516 const u4 type_idx = DecodeUnsignedLeb128(data);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800517 fputs(pDexFile->StringByTypeIdx(dex::TypeIndex(type_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700518 // Decode and display all name=value pairs.
519 const u4 size = DecodeUnsignedLeb128(data);
520 for (u4 i = 0; i < size; i++) {
521 const u4 name_idx = DecodeUnsignedLeb128(data);
522 fputc(' ', gOutFile);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800523 fputs(pDexFile->StringDataByIdx(dex::StringIndex(name_idx)), gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -0700524 fputc('=', gOutFile);
525 dumpEncodedValue(pDexFile, data);
526 }
527 break;
528 }
529 case DexFile::kDexAnnotationNull:
530 fputs("null", gOutFile);
531 break;
532 case DexFile::kDexAnnotationBoolean:
533 fputs(strBool(arg), gOutFile);
534 break;
535 default:
536 fputs("????", gOutFile);
537 break;
538 } // switch
539}
540
541/*
542 * Dumps encoded value with prefix.
543 */
544static void dumpEncodedValue(const DexFile* pDexFile, const u1** data) {
545 const u1 enc = *(*data)++;
546 dumpEncodedValue(pDexFile, data, enc & 0x1f, enc >> 5);
547}
548
549/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700550 * Dumps the file header.
Aart Bik69ae54a2015-07-01 14:52:26 -0700551 */
552static void dumpFileHeader(const DexFile* pDexFile) {
553 const DexFile::Header& pHeader = pDexFile->GetHeader();
554 char sanitized[sizeof(pHeader.magic_) * 2 + 1];
555 fprintf(gOutFile, "DEX file header:\n");
556 asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
557 fprintf(gOutFile, "magic : '%s'\n", sanitized);
558 fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
559 fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
560 pHeader.signature_[0], pHeader.signature_[1],
561 pHeader.signature_[DexFile::kSha1DigestSize - 2],
562 pHeader.signature_[DexFile::kSha1DigestSize - 1]);
563 fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
564 fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
565 fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
566 fprintf(gOutFile, "link_off : %d (0x%06x)\n",
567 pHeader.link_off_, pHeader.link_off_);
568 fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
569 fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
570 pHeader.string_ids_off_, pHeader.string_ids_off_);
571 fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
572 fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
573 pHeader.type_ids_off_, pHeader.type_ids_off_);
Aart Bikdce50862016-06-10 16:04:03 -0700574 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
575 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
Aart Bik69ae54a2015-07-01 14:52:26 -0700576 pHeader.proto_ids_off_, pHeader.proto_ids_off_);
577 fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
578 fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
579 pHeader.field_ids_off_, pHeader.field_ids_off_);
580 fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
581 fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
582 pHeader.method_ids_off_, pHeader.method_ids_off_);
583 fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
584 fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
585 pHeader.class_defs_off_, pHeader.class_defs_off_);
586 fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
587 fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
588 pHeader.data_off_, pHeader.data_off_);
589}
590
591/*
592 * Dumps a class_def_item.
593 */
594static void dumpClassDef(const DexFile* pDexFile, int idx) {
595 // General class information.
596 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
597 fprintf(gOutFile, "Class #%d header:\n", idx);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800598 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700599 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
600 pClassDef.access_flags_, pClassDef.access_flags_);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800601 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700602 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
603 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800604 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_.index_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700605 fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
606 pClassDef.annotations_off_, pClassDef.annotations_off_);
607 fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
608 pClassDef.class_data_off_, pClassDef.class_data_off_);
609
610 // Fields and methods.
611 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
612 if (pEncodedData != nullptr) {
613 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
614 fprintf(gOutFile, "static_fields_size : %d\n", pClassData.NumStaticFields());
615 fprintf(gOutFile, "instance_fields_size: %d\n", pClassData.NumInstanceFields());
616 fprintf(gOutFile, "direct_methods_size : %d\n", pClassData.NumDirectMethods());
617 fprintf(gOutFile, "virtual_methods_size: %d\n", pClassData.NumVirtualMethods());
618 } else {
619 fprintf(gOutFile, "static_fields_size : 0\n");
620 fprintf(gOutFile, "instance_fields_size: 0\n");
621 fprintf(gOutFile, "direct_methods_size : 0\n");
622 fprintf(gOutFile, "virtual_methods_size: 0\n");
623 }
624 fprintf(gOutFile, "\n");
625}
626
Aart Bikdce50862016-06-10 16:04:03 -0700627/**
628 * Dumps an annotation set item.
629 */
630static void dumpAnnotationSetItem(const DexFile* pDexFile, const DexFile::AnnotationSetItem* set_item) {
631 if (set_item == nullptr || set_item->size_ == 0) {
632 fputs(" empty-annotation-set\n", gOutFile);
633 return;
634 }
635 for (u4 i = 0; i < set_item->size_; i++) {
636 const DexFile::AnnotationItem* annotation = pDexFile->GetAnnotationItem(set_item, i);
637 if (annotation == nullptr) {
638 continue;
639 }
640 fputs(" ", gOutFile);
641 switch (annotation->visibility_) {
642 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", gOutFile); break;
643 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", gOutFile); break;
644 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", gOutFile); break;
645 default: fputs("VISIBILITY_UNKNOWN ", gOutFile); break;
646 } // switch
647 // Decode raw bytes in annotation.
648 const u1* rData = annotation->annotation_;
649 dumpEncodedValue(pDexFile, &rData, DexFile::kDexAnnotationAnnotation, 0);
650 fputc('\n', gOutFile);
651 }
652}
653
654/*
655 * Dumps class annotations.
656 */
657static void dumpClassAnnotations(const DexFile* pDexFile, int idx) {
658 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
659 const DexFile::AnnotationsDirectoryItem* dir = pDexFile->GetAnnotationsDirectory(pClassDef);
660 if (dir == nullptr) {
661 return; // none
662 }
663
664 fprintf(gOutFile, "Class #%d annotations:\n", idx);
665
666 const DexFile::AnnotationSetItem* class_set_item = pDexFile->GetClassAnnotationSet(dir);
667 const DexFile::FieldAnnotationsItem* fields = pDexFile->GetFieldAnnotations(dir);
668 const DexFile::MethodAnnotationsItem* methods = pDexFile->GetMethodAnnotations(dir);
669 const DexFile::ParameterAnnotationsItem* pars = pDexFile->GetParameterAnnotations(dir);
670
671 // Annotations on the class itself.
672 if (class_set_item != nullptr) {
673 fprintf(gOutFile, "Annotations on class\n");
674 dumpAnnotationSetItem(pDexFile, class_set_item);
675 }
676
677 // Annotations on fields.
678 if (fields != nullptr) {
679 for (u4 i = 0; i < dir->fields_size_; i++) {
680 const u4 field_idx = fields[i].field_idx_;
681 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
682 const char* field_name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
683 fprintf(gOutFile, "Annotations on field #%u '%s'\n", field_idx, field_name);
684 dumpAnnotationSetItem(pDexFile, pDexFile->GetFieldAnnotationSetItem(fields[i]));
685 }
686 }
687
688 // Annotations on methods.
689 if (methods != nullptr) {
690 for (u4 i = 0; i < dir->methods_size_; i++) {
691 const u4 method_idx = methods[i].method_idx_;
692 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
693 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
694 fprintf(gOutFile, "Annotations on method #%u '%s'\n", method_idx, method_name);
695 dumpAnnotationSetItem(pDexFile, pDexFile->GetMethodAnnotationSetItem(methods[i]));
696 }
697 }
698
699 // Annotations on method parameters.
700 if (pars != nullptr) {
701 for (u4 i = 0; i < dir->parameters_size_; i++) {
702 const u4 method_idx = pars[i].method_idx_;
703 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
704 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
705 fprintf(gOutFile, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
706 const DexFile::AnnotationSetRefList*
707 list = pDexFile->GetParameterAnnotationSetRefList(&pars[i]);
708 if (list != nullptr) {
709 for (u4 j = 0; j < list->size_; j++) {
710 fprintf(gOutFile, "#%u\n", j);
711 dumpAnnotationSetItem(pDexFile, pDexFile->GetSetRefItemItem(&list->list_[j]));
712 }
713 }
714 }
715 }
716
717 fputc('\n', gOutFile);
718}
719
Aart Bik69ae54a2015-07-01 14:52:26 -0700720/*
721 * Dumps an interface that a class declares to implement.
722 */
723static void dumpInterface(const DexFile* pDexFile, const DexFile::TypeItem& pTypeItem, int i) {
724 const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
725 if (gOptions.outputFormat == OUTPUT_PLAIN) {
726 fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
727 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -0700728 std::unique_ptr<char[]> dot(descriptorToDot(interfaceName));
729 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -0700730 }
731}
732
733/*
734 * Dumps the catches table associated with the code.
735 */
736static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
737 const u4 triesSize = pCode->tries_size_;
738
739 // No catch table.
740 if (triesSize == 0) {
741 fprintf(gOutFile, " catches : (none)\n");
742 return;
743 }
744
745 // Dump all table entries.
746 fprintf(gOutFile, " catches : %d\n", triesSize);
747 for (u4 i = 0; i < triesSize; i++) {
748 const DexFile::TryItem* pTry = pDexFile->GetTryItems(*pCode, i);
749 const u4 start = pTry->start_addr_;
750 const u4 end = start + pTry->insn_count_;
751 fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
752 for (CatchHandlerIterator it(*pCode, *pTry); it.HasNext(); it.Next()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800753 const dex::TypeIndex tidx = it.GetHandlerTypeIndex();
754 const char* descriptor = (!tidx.IsValid()) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
Aart Bik69ae54a2015-07-01 14:52:26 -0700755 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
756 } // for
757 } // for
758}
759
760/*
761 * Callback for dumping each positions table entry.
762 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000763static bool dumpPositionsCb(void* /*context*/, const DexFile::PositionInfo& entry) {
764 fprintf(gOutFile, " 0x%04x line=%d\n", entry.address_, entry.line_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700765 return false;
766}
767
768/*
769 * Callback for dumping locals table entry.
770 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000771static void dumpLocalsCb(void* /*context*/, const DexFile::LocalInfo& entry) {
772 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
Aart Bik69ae54a2015-07-01 14:52:26 -0700773 fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
David Srbeckyb06e28e2015-12-10 13:15:00 +0000774 entry.start_address_, entry.end_address_, entry.reg_,
775 entry.name_, entry.descriptor_, signature);
Aart Bik69ae54a2015-07-01 14:52:26 -0700776}
777
778/*
779 * Helper for dumpInstruction(), which builds the string
Aart Bika0e33fd2016-07-08 18:32:45 -0700780 * representation for the index in the given instruction.
781 * Returns a pointer to a buffer of sufficient size.
Aart Bik69ae54a2015-07-01 14:52:26 -0700782 */
Aart Bika0e33fd2016-07-08 18:32:45 -0700783static std::unique_ptr<char[]> indexString(const DexFile* pDexFile,
784 const Instruction* pDecInsn,
785 size_t bufSize) {
Orion Hodsonb34bb192016-10-18 17:02:58 +0100786 static const u4 kInvalidIndex = std::numeric_limits<u4>::max();
Aart Bika0e33fd2016-07-08 18:32:45 -0700787 std::unique_ptr<char[]> buf(new char[bufSize]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700788 // Determine index and width of the string.
789 u4 index = 0;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100790 u4 secondary_index = kInvalidIndex;
Aart Bik69ae54a2015-07-01 14:52:26 -0700791 u4 width = 4;
792 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
793 // SOME NOT SUPPORTED:
794 // case Instruction::k20bc:
795 case Instruction::k21c:
796 case Instruction::k35c:
797 // case Instruction::k35ms:
798 case Instruction::k3rc:
799 // case Instruction::k3rms:
800 // case Instruction::k35mi:
801 // case Instruction::k3rmi:
802 index = pDecInsn->VRegB();
803 width = 4;
804 break;
805 case Instruction::k31c:
806 index = pDecInsn->VRegB();
807 width = 8;
808 break;
809 case Instruction::k22c:
810 // case Instruction::k22cs:
811 index = pDecInsn->VRegC();
812 width = 4;
813 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100814 case Instruction::k45cc:
815 case Instruction::k4rcc:
816 index = pDecInsn->VRegB();
817 secondary_index = pDecInsn->VRegH();
818 width = 4;
819 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700820 default:
821 break;
822 } // switch
823
824 // Determine index type.
825 size_t outSize = 0;
826 switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
827 case Instruction::kIndexUnknown:
828 // This function should never get called for this type, but do
829 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700830 outSize = snprintf(buf.get(), bufSize, "<unknown-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700831 break;
832 case Instruction::kIndexNone:
833 // This function should never get called for this type, but do
834 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700835 outSize = snprintf(buf.get(), bufSize, "<no-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700836 break;
837 case Instruction::kIndexTypeRef:
838 if (index < pDexFile->GetHeader().type_ids_size_) {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800839 const char* tp = pDexFile->StringByTypeIdx(dex::TypeIndex(index));
Aart Bika0e33fd2016-07-08 18:32:45 -0700840 outSize = snprintf(buf.get(), bufSize, "%s // type@%0*x", tp, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700841 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700842 outSize = snprintf(buf.get(), bufSize, "<type?> // type@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700843 }
844 break;
845 case Instruction::kIndexStringRef:
846 if (index < pDexFile->GetHeader().string_ids_size_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800847 const char* st = pDexFile->StringDataByIdx(dex::StringIndex(index));
Aart Bika0e33fd2016-07-08 18:32:45 -0700848 outSize = snprintf(buf.get(), bufSize, "\"%s\" // string@%0*x", st, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700849 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700850 outSize = snprintf(buf.get(), bufSize, "<string?> // string@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700851 }
852 break;
853 case Instruction::kIndexMethodRef:
854 if (index < pDexFile->GetHeader().method_ids_size_) {
855 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
856 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
857 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
858 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700859 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // method@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700860 backDescriptor, name, signature.ToString().c_str(), width, index);
861 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700862 outSize = snprintf(buf.get(), bufSize, "<method?> // method@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700863 }
864 break;
865 case Instruction::kIndexFieldRef:
866 if (index < pDexFile->GetHeader().field_ids_size_) {
867 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index);
868 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
869 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
870 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700871 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // field@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700872 backDescriptor, name, typeDescriptor, width, index);
873 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700874 outSize = snprintf(buf.get(), bufSize, "<field?> // field@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700875 }
876 break;
877 case Instruction::kIndexVtableOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700878 outSize = snprintf(buf.get(), bufSize, "[%0*x] // vtable #%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700879 width, index, width, index);
880 break;
881 case Instruction::kIndexFieldOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700882 outSize = snprintf(buf.get(), bufSize, "[obj+%0*x]", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700883 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +0100884 case Instruction::kIndexMethodAndProtoRef: {
Orion Hodsonc069a302017-01-18 09:23:12 +0000885 std::string method("<method?>");
886 std::string proto("<proto?>");
887 if (index < pDexFile->GetHeader().method_ids_size_) {
888 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
889 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
890 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
891 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
892 method = android::base::StringPrintf("%s.%s:%s",
893 backDescriptor,
894 name,
895 signature.ToString().c_str());
Orion Hodsonb34bb192016-10-18 17:02:58 +0100896 }
Orion Hodsonc069a302017-01-18 09:23:12 +0000897 if (secondary_index < pDexFile->GetHeader().proto_ids_size_) {
898 const DexFile::ProtoId& protoId = pDexFile->GetProtoId(secondary_index);
899 const Signature signature = pDexFile->GetProtoSignature(protoId);
900 proto = signature.ToString();
901 }
902 outSize = snprintf(buf.get(), bufSize, "%s, %s // method@%0*x, proto@%0*x",
903 method.c_str(), proto.c_str(), width, index, width, secondary_index);
904 break;
905 }
906 case Instruction::kIndexCallSiteRef:
907 // Call site information is too large to detail in disassembly so just output the index.
908 outSize = snprintf(buf.get(), bufSize, "call_site@%0*x", width, index);
Orion Hodsonb34bb192016-10-18 17:02:58 +0100909 break;
Orion Hodson2e599942017-09-22 16:17:41 +0100910 case Instruction::kIndexMethodHandleRef:
911 // Method handle information is too large to detail in disassembly so just output the index.
912 outSize = snprintf(buf.get(), bufSize, "method_handle@%0*x", width, index);
913 break;
914 case Instruction::kIndexProtoRef:
915 if (index < pDexFile->GetHeader().proto_ids_size_) {
916 const DexFile::ProtoId& protoId = pDexFile->GetProtoId(index);
917 const Signature signature = pDexFile->GetProtoSignature(protoId);
918 const std::string& proto = signature.ToString();
919 outSize = snprintf(buf.get(), bufSize, "%s // proto@%0*x", proto.c_str(), width, index);
920 } else {
921 outSize = snprintf(buf.get(), bufSize, "<?> // proto@%0*x", width, index);
922 }
Aart Bik69ae54a2015-07-01 14:52:26 -0700923 break;
924 } // switch
925
Orion Hodson2e599942017-09-22 16:17:41 +0100926 if (outSize == 0) {
927 // The index type has not been handled in the switch above.
928 outSize = snprintf(buf.get(), bufSize, "<?>");
929 }
930
Aart Bik69ae54a2015-07-01 14:52:26 -0700931 // Determine success of string construction.
932 if (outSize >= bufSize) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700933 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
934 // doesn't count/ the '\0' as part of its returned size, so we add explicit
935 // space for it here.
936 return indexString(pDexFile, pDecInsn, outSize + 1);
Aart Bik69ae54a2015-07-01 14:52:26 -0700937 }
938 return buf;
939}
940
941/*
942 * Dumps a single instruction.
943 */
944static void dumpInstruction(const DexFile* pDexFile,
945 const DexFile::CodeItem* pCode,
946 u4 codeOffset, u4 insnIdx, u4 insnWidth,
947 const Instruction* pDecInsn) {
948 // Address of instruction (expressed as byte offset).
949 fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
950
951 // Dump (part of) raw bytes.
952 const u2* insns = pCode->insns_;
953 for (u4 i = 0; i < 8; i++) {
954 if (i < insnWidth) {
955 if (i == 7) {
956 fprintf(gOutFile, " ... ");
957 } else {
958 // Print 16-bit value in little-endian order.
959 const u1* bytePtr = (const u1*) &insns[insnIdx + i];
960 fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
961 }
962 } else {
963 fputs(" ", gOutFile);
964 }
965 } // for
966
967 // Dump pseudo-instruction or opcode.
968 if (pDecInsn->Opcode() == Instruction::NOP) {
969 const u2 instr = get2LE((const u1*) &insns[insnIdx]);
970 if (instr == Instruction::kPackedSwitchSignature) {
971 fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
972 } else if (instr == Instruction::kSparseSwitchSignature) {
973 fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
974 } else if (instr == Instruction::kArrayDataSignature) {
975 fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
976 } else {
977 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
978 }
979 } else {
980 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
981 }
982
983 // Set up additional argument.
Aart Bika0e33fd2016-07-08 18:32:45 -0700984 std::unique_ptr<char[]> indexBuf;
Aart Bik69ae54a2015-07-01 14:52:26 -0700985 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700986 indexBuf = indexString(pDexFile, pDecInsn, 200);
Aart Bik69ae54a2015-07-01 14:52:26 -0700987 }
988
989 // Dump the instruction.
990 //
991 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
992 //
993 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
994 case Instruction::k10x: // op
995 break;
996 case Instruction::k12x: // op vA, vB
997 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
998 break;
999 case Instruction::k11n: // op vA, #+B
1000 fprintf(gOutFile, " v%d, #int %d // #%x",
1001 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
1002 break;
1003 case Instruction::k11x: // op vAA
1004 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
1005 break;
1006 case Instruction::k10t: // op +AA
Aart Bikdce50862016-06-10 16:04:03 -07001007 case Instruction::k20t: { // op +AAAA
1008 const s4 targ = (s4) pDecInsn->VRegA();
1009 fprintf(gOutFile, " %04x // %c%04x",
1010 insnIdx + targ,
1011 (targ < 0) ? '-' : '+',
1012 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001013 break;
Aart Bikdce50862016-06-10 16:04:03 -07001014 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001015 case Instruction::k22x: // op vAA, vBBBB
1016 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1017 break;
Aart Bikdce50862016-06-10 16:04:03 -07001018 case Instruction::k21t: { // op vAA, +BBBB
1019 const s4 targ = (s4) pDecInsn->VRegB();
1020 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
1021 insnIdx + targ,
1022 (targ < 0) ? '-' : '+',
1023 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001024 break;
Aart Bikdce50862016-06-10 16:04:03 -07001025 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001026 case Instruction::k21s: // op vAA, #+BBBB
1027 fprintf(gOutFile, " v%d, #int %d // #%x",
1028 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
1029 break;
1030 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
1031 // The printed format varies a bit based on the actual opcode.
1032 if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) {
1033 const s4 value = pDecInsn->VRegB() << 16;
1034 fprintf(gOutFile, " v%d, #int %d // #%x",
1035 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1036 } else {
1037 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
1038 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
1039 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
1040 }
1041 break;
1042 case Instruction::k21c: // op vAA, thing@BBBB
1043 case Instruction::k31c: // op vAA, thing@BBBBBBBB
Aart Bika0e33fd2016-07-08 18:32:45 -07001044 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001045 break;
1046 case Instruction::k23x: // op vAA, vBB, vCC
1047 fprintf(gOutFile, " v%d, v%d, v%d",
1048 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
1049 break;
1050 case Instruction::k22b: // op vAA, vBB, #+CC
1051 fprintf(gOutFile, " v%d, v%d, #int %d // #%02x",
1052 pDecInsn->VRegA(), pDecInsn->VRegB(),
1053 (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC());
1054 break;
Aart Bikdce50862016-06-10 16:04:03 -07001055 case Instruction::k22t: { // op vA, vB, +CCCC
1056 const s4 targ = (s4) pDecInsn->VRegC();
1057 fprintf(gOutFile, " v%d, v%d, %04x // %c%04x",
1058 pDecInsn->VRegA(), pDecInsn->VRegB(),
1059 insnIdx + targ,
1060 (targ < 0) ? '-' : '+',
1061 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -07001062 break;
Aart Bikdce50862016-06-10 16:04:03 -07001063 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001064 case Instruction::k22s: // op vA, vB, #+CCCC
1065 fprintf(gOutFile, " v%d, v%d, #int %d // #%04x",
1066 pDecInsn->VRegA(), pDecInsn->VRegB(),
1067 (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC());
1068 break;
1069 case Instruction::k22c: // op vA, vB, thing@CCCC
1070 // NOT SUPPORTED:
1071 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
1072 fprintf(gOutFile, " v%d, v%d, %s",
Aart Bika0e33fd2016-07-08 18:32:45 -07001073 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001074 break;
1075 case Instruction::k30t:
1076 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
1077 break;
Aart Bikdce50862016-06-10 16:04:03 -07001078 case Instruction::k31i: { // op vAA, #+BBBBBBBB
1079 // This is often, but not always, a float.
1080 union {
1081 float f;
1082 u4 i;
1083 } conv;
1084 conv.i = pDecInsn->VRegB();
1085 fprintf(gOutFile, " v%d, #float %g // #%08x",
1086 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001087 break;
Aart Bikdce50862016-06-10 16:04:03 -07001088 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001089 case Instruction::k31t: // op vAA, offset +BBBBBBBB
1090 fprintf(gOutFile, " v%d, %08x // +%08x",
1091 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
1092 break;
1093 case Instruction::k32x: // op vAAAA, vBBBB
1094 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1095 break;
Orion Hodsonb34bb192016-10-18 17:02:58 +01001096 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
1097 case Instruction::k45cc: { // op {vC, vD, vE, vF, vG}, method@BBBB, proto@HHHH
Aart Bik69ae54a2015-07-01 14:52:26 -07001098 // NOT SUPPORTED:
1099 // case Instruction::k35ms: // [opt] invoke-virtual+super
1100 // case Instruction::k35mi: // [opt] inline invoke
Aart Bikdce50862016-06-10 16:04:03 -07001101 u4 arg[Instruction::kMaxVarArgRegs];
1102 pDecInsn->GetVarArgs(arg);
1103 fputs(" {", gOutFile);
1104 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1105 if (i == 0) {
1106 fprintf(gOutFile, "v%d", arg[i]);
1107 } else {
1108 fprintf(gOutFile, ", v%d", arg[i]);
1109 }
1110 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001111 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001112 break;
Aart Bikdce50862016-06-10 16:04:03 -07001113 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001114 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
Orion Hodsonb34bb192016-10-18 17:02:58 +01001115 case Instruction::k4rcc: { // op {vCCCC .. v(CCCC+AA-1)}, method@BBBB, proto@HHHH
Aart Bik69ae54a2015-07-01 14:52:26 -07001116 // NOT SUPPORTED:
1117 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1118 // case Instruction::k3rmi: // [opt] execute-inline/range
Aart Bik69ae54a2015-07-01 14:52:26 -07001119 // This doesn't match the "dx" output when some of the args are
1120 // 64-bit values -- dx only shows the first register.
1121 fputs(" {", gOutFile);
1122 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1123 if (i == 0) {
1124 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
1125 } else {
1126 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
1127 }
1128 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001129 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001130 }
1131 break;
Aart Bikdce50862016-06-10 16:04:03 -07001132 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1133 // This is often, but not always, a double.
1134 union {
1135 double d;
1136 u8 j;
1137 } conv;
1138 conv.j = pDecInsn->WideVRegB();
1139 fprintf(gOutFile, " v%d, #double %g // #%016" PRIx64,
1140 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001141 break;
Aart Bikdce50862016-06-10 16:04:03 -07001142 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001143 // NOT SUPPORTED:
1144 // case Instruction::k00x: // unknown op or breakpoint
1145 // break;
1146 default:
1147 fprintf(gOutFile, " ???");
1148 break;
1149 } // switch
1150
1151 fputc('\n', gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001152}
1153
1154/*
1155 * Dumps a bytecode disassembly.
1156 */
1157static void dumpBytecodes(const DexFile* pDexFile, u4 idx,
1158 const DexFile::CodeItem* pCode, u4 codeOffset) {
1159 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1160 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1161 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1162 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1163
1164 // Generate header.
Aart Bikc05e2f22016-07-12 15:53:13 -07001165 std::unique_ptr<char[]> dot(descriptorToDot(backDescriptor));
1166 fprintf(gOutFile, "%06x: |[%06x] %s.%s:%s\n",
1167 codeOffset, codeOffset, dot.get(), name, signature.ToString().c_str());
Aart Bik69ae54a2015-07-01 14:52:26 -07001168
1169 // Iterate over all instructions.
1170 const u2* insns = pCode->insns_;
1171 for (u4 insnIdx = 0; insnIdx < pCode->insns_size_in_code_units_;) {
1172 const Instruction* instruction = Instruction::At(&insns[insnIdx]);
1173 const u4 insnWidth = instruction->SizeInCodeUnits();
1174 if (insnWidth == 0) {
1175 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx);
1176 break;
1177 }
1178 dumpInstruction(pDexFile, pCode, codeOffset, insnIdx, insnWidth, instruction);
1179 insnIdx += insnWidth;
1180 } // for
1181}
1182
1183/*
1184 * Dumps code of a method.
1185 */
1186static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags,
1187 const DexFile::CodeItem* pCode, u4 codeOffset) {
1188 fprintf(gOutFile, " registers : %d\n", pCode->registers_size_);
1189 fprintf(gOutFile, " ins : %d\n", pCode->ins_size_);
1190 fprintf(gOutFile, " outs : %d\n", pCode->outs_size_);
1191 fprintf(gOutFile, " insns size : %d 16-bit code units\n",
1192 pCode->insns_size_in_code_units_);
1193
1194 // Bytecode disassembly, if requested.
1195 if (gOptions.disassemble) {
1196 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
1197 }
1198
1199 // Try-catch blocks.
1200 dumpCatches(pDexFile, pCode);
1201
1202 // Positions and locals table in the debug info.
1203 bool is_static = (flags & kAccStatic) != 0;
1204 fprintf(gOutFile, " positions : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +00001205 pDexFile->DecodeDebugPositionInfo(pCode, dumpPositionsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001206 fprintf(gOutFile, " locals : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +00001207 pDexFile->DecodeDebugLocalInfo(pCode, is_static, idx, dumpLocalsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001208}
1209
1210/*
1211 * Dumps a method.
1212 */
1213static void dumpMethod(const DexFile* pDexFile, u4 idx, u4 flags,
1214 const DexFile::CodeItem* pCode, u4 codeOffset, int i) {
1215 // Bail for anything private if export only requested.
1216 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1217 return;
1218 }
1219
1220 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1221 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1222 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1223 char* typeDescriptor = strdup(signature.ToString().c_str());
1224 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1225 char* accessStr = createAccessFlagStr(flags, kAccessForMethod);
1226
1227 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1228 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1229 fprintf(gOutFile, " name : '%s'\n", name);
1230 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1231 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
1232 if (pCode == nullptr) {
1233 fprintf(gOutFile, " code : (none)\n");
1234 } else {
1235 fprintf(gOutFile, " code -\n");
1236 dumpCode(pDexFile, idx, flags, pCode, codeOffset);
1237 }
1238 if (gOptions.disassemble) {
1239 fputc('\n', gOutFile);
1240 }
1241 } else if (gOptions.outputFormat == OUTPUT_XML) {
1242 const bool constructor = (name[0] == '<');
1243
1244 // Method name and prototype.
1245 if (constructor) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001246 std::unique_ptr<char[]> dot(descriptorClassToDot(backDescriptor));
1247 fprintf(gOutFile, "<constructor name=\"%s\"\n", dot.get());
1248 dot = descriptorToDot(backDescriptor);
1249 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001250 } else {
1251 fprintf(gOutFile, "<method name=\"%s\"\n", name);
1252 const char* returnType = strrchr(typeDescriptor, ')');
1253 if (returnType == nullptr) {
1254 fprintf(stderr, "bad method type descriptor '%s'\n", typeDescriptor);
1255 goto bail;
1256 }
Aart Bikc05e2f22016-07-12 15:53:13 -07001257 std::unique_ptr<char[]> dot(descriptorToDot(returnType + 1));
1258 fprintf(gOutFile, " return=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001259 fprintf(gOutFile, " abstract=%s\n", quotedBool((flags & kAccAbstract) != 0));
1260 fprintf(gOutFile, " native=%s\n", quotedBool((flags & kAccNative) != 0));
1261 fprintf(gOutFile, " synchronized=%s\n", quotedBool(
1262 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1263 }
1264
1265 // Additional method flags.
1266 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1267 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1268 // The "deprecated=" not knowable w/o parsing annotations.
1269 fprintf(gOutFile, " visibility=%s\n>\n", quotedVisibility(flags));
1270
1271 // Parameters.
1272 if (typeDescriptor[0] != '(') {
1273 fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor);
1274 goto bail;
1275 }
1276 char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1));
1277 const char* base = typeDescriptor + 1;
1278 int argNum = 0;
1279 while (*base != ')') {
1280 char* cp = tmpBuf;
1281 while (*base == '[') {
1282 *cp++ = *base++;
1283 }
1284 if (*base == 'L') {
1285 // Copy through ';'.
1286 do {
1287 *cp = *base++;
1288 } while (*cp++ != ';');
1289 } else {
1290 // Primitive char, copy it.
Aart Bikc05e2f22016-07-12 15:53:13 -07001291 if (strchr("ZBCSIFJD", *base) == nullptr) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001292 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
Aart Bika0e33fd2016-07-08 18:32:45 -07001293 break; // while
Aart Bik69ae54a2015-07-01 14:52:26 -07001294 }
1295 *cp++ = *base++;
1296 }
1297 // Null terminate and display.
1298 *cp++ = '\0';
Aart Bikc05e2f22016-07-12 15:53:13 -07001299 std::unique_ptr<char[]> dot(descriptorToDot(tmpBuf));
Aart Bik69ae54a2015-07-01 14:52:26 -07001300 fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n"
Aart Bikc05e2f22016-07-12 15:53:13 -07001301 "</parameter>\n", argNum++, dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001302 } // while
1303 free(tmpBuf);
1304 if (constructor) {
1305 fprintf(gOutFile, "</constructor>\n");
1306 } else {
1307 fprintf(gOutFile, "</method>\n");
1308 }
1309 }
1310
1311 bail:
1312 free(typeDescriptor);
1313 free(accessStr);
1314}
1315
1316/*
1317 * Dumps a static (class) field.
1318 */
Aart Bikdce50862016-06-10 16:04:03 -07001319static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i, const u1** data) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001320 // Bail for anything private if export only requested.
1321 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1322 return;
1323 }
1324
1325 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(idx);
1326 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
1327 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
1328 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
1329 char* accessStr = createAccessFlagStr(flags, kAccessForField);
1330
1331 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1332 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1333 fprintf(gOutFile, " name : '%s'\n", name);
1334 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1335 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
Aart Bikdce50862016-06-10 16:04:03 -07001336 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001337 fputs(" value : ", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001338 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001339 fputs("\n", gOutFile);
1340 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001341 } else if (gOptions.outputFormat == OUTPUT_XML) {
1342 fprintf(gOutFile, "<field name=\"%s\"\n", name);
Aart Bikc05e2f22016-07-12 15:53:13 -07001343 std::unique_ptr<char[]> dot(descriptorToDot(typeDescriptor));
1344 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001345 fprintf(gOutFile, " transient=%s\n", quotedBool((flags & kAccTransient) != 0));
1346 fprintf(gOutFile, " volatile=%s\n", quotedBool((flags & kAccVolatile) != 0));
1347 // The "value=" is not knowable w/o parsing annotations.
1348 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1349 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1350 // The "deprecated=" is not knowable w/o parsing annotations.
1351 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(flags));
Aart Bikdce50862016-06-10 16:04:03 -07001352 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001353 fputs(" value=\"", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001354 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001355 fputs("\"\n", gOutFile);
1356 }
1357 fputs(">\n</field>\n", gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001358 }
1359
1360 free(accessStr);
1361}
1362
1363/*
1364 * Dumps an instance field.
1365 */
1366static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
Aart Bikdce50862016-06-10 16:04:03 -07001367 dumpSField(pDexFile, idx, flags, i, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001368}
1369
1370/*
Andreas Gampe5073fed2015-08-10 11:40:25 -07001371 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1372 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1373 * tool, so this is not performance-critical.
1374 */
1375
1376static void dumpCfg(const DexFile* dex_file,
Aart Bikdce50862016-06-10 16:04:03 -07001377 u4 dex_method_idx,
Andreas Gampe5073fed2015-08-10 11:40:25 -07001378 const DexFile::CodeItem* code_item) {
1379 if (code_item != nullptr) {
1380 std::ostringstream oss;
1381 DumpMethodCFG(dex_file, dex_method_idx, oss);
David Sehrcaacd112016-10-20 16:27:02 -07001382 fputs(oss.str().c_str(), gOutFile);
Andreas Gampe5073fed2015-08-10 11:40:25 -07001383 }
1384}
1385
1386static void dumpCfg(const DexFile* dex_file, int idx) {
1387 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
Aart Bikdce50862016-06-10 16:04:03 -07001388 const u1* class_data = dex_file->GetClassData(class_def);
Andreas Gampe5073fed2015-08-10 11:40:25 -07001389 if (class_data == nullptr) { // empty class such as a marker interface?
1390 return;
1391 }
1392 ClassDataItemIterator it(*dex_file, class_data);
Mathieu Chartiere17cf242017-06-19 11:05:51 -07001393 it.SkipAllFields();
Andreas Gampe5073fed2015-08-10 11:40:25 -07001394 while (it.HasNextDirectMethod()) {
1395 dumpCfg(dex_file,
1396 it.GetMemberIndex(),
1397 it.GetMethodCodeItem());
1398 it.Next();
1399 }
1400 while (it.HasNextVirtualMethod()) {
1401 dumpCfg(dex_file,
1402 it.GetMemberIndex(),
1403 it.GetMethodCodeItem());
1404 it.Next();
1405 }
1406}
1407
1408/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001409 * Dumps the class.
1410 *
1411 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1412 *
1413 * If "*pLastPackage" is nullptr or does not match the current class' package,
1414 * the value will be replaced with a newly-allocated string.
1415 */
1416static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1417 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1418
1419 // Omitting non-public class.
1420 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1421 return;
1422 }
1423
Aart Bikdce50862016-06-10 16:04:03 -07001424 if (gOptions.showSectionHeaders) {
1425 dumpClassDef(pDexFile, idx);
1426 }
1427
1428 if (gOptions.showAnnotations) {
1429 dumpClassAnnotations(pDexFile, idx);
1430 }
1431
1432 if (gOptions.showCfg) {
Andreas Gampe5073fed2015-08-10 11:40:25 -07001433 dumpCfg(pDexFile, idx);
1434 return;
1435 }
1436
Aart Bik69ae54a2015-07-01 14:52:26 -07001437 // For the XML output, show the package name. Ideally we'd gather
1438 // up the classes, sort them, and dump them alphabetically so the
1439 // package name wouldn't jump around, but that's not a great plan
1440 // for something that needs to run on the device.
1441 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1442 if (!(classDescriptor[0] == 'L' &&
1443 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1444 // Arrays and primitives should not be defined explicitly. Keep going?
1445 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1446 } else if (gOptions.outputFormat == OUTPUT_XML) {
1447 char* mangle = strdup(classDescriptor + 1);
1448 mangle[strlen(mangle)-1] = '\0';
1449
1450 // Reduce to just the package name.
1451 char* lastSlash = strrchr(mangle, '/');
1452 if (lastSlash != nullptr) {
1453 *lastSlash = '\0';
1454 } else {
1455 *mangle = '\0';
1456 }
1457
1458 for (char* cp = mangle; *cp != '\0'; cp++) {
1459 if (*cp == '/') {
1460 *cp = '.';
1461 }
1462 } // for
1463
1464 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1465 // Start of a new package.
1466 if (*pLastPackage != nullptr) {
1467 fprintf(gOutFile, "</package>\n");
1468 }
1469 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1470 free(*pLastPackage);
1471 *pLastPackage = mangle;
1472 } else {
1473 free(mangle);
1474 }
1475 }
1476
1477 // General class information.
1478 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1479 const char* superclassDescriptor;
Andreas Gampea5b09a62016-11-17 15:21:22 -08001480 if (!pClassDef.superclass_idx_.IsValid()) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001481 superclassDescriptor = nullptr;
1482 } else {
1483 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1484 }
1485 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1486 fprintf(gOutFile, "Class #%d -\n", idx);
1487 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1488 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1489 if (superclassDescriptor != nullptr) {
1490 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1491 }
1492 fprintf(gOutFile, " Interfaces -\n");
1493 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -07001494 std::unique_ptr<char[]> dot(descriptorClassToDot(classDescriptor));
1495 fprintf(gOutFile, "<class name=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001496 if (superclassDescriptor != nullptr) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001497 dot = descriptorToDot(superclassDescriptor);
1498 fprintf(gOutFile, " extends=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001499 }
Alex Light1f12e282015-12-10 16:49:47 -08001500 fprintf(gOutFile, " interface=%s\n",
1501 quotedBool((pClassDef.access_flags_ & kAccInterface) != 0));
Aart Bik69ae54a2015-07-01 14:52:26 -07001502 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1503 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1504 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1505 // The "deprecated=" not knowable w/o parsing annotations.
1506 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1507 fprintf(gOutFile, ">\n");
1508 }
1509
1510 // Interfaces.
1511 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1512 if (pInterfaces != nullptr) {
1513 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1514 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1515 } // for
1516 }
1517
1518 // Fields and methods.
1519 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
1520 if (pEncodedData == nullptr) {
1521 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1522 fprintf(gOutFile, " Static fields -\n");
1523 fprintf(gOutFile, " Instance fields -\n");
1524 fprintf(gOutFile, " Direct methods -\n");
1525 fprintf(gOutFile, " Virtual methods -\n");
1526 }
1527 } else {
1528 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
Aart Bikdce50862016-06-10 16:04:03 -07001529
1530 // Prepare data for static fields.
1531 const u1* sData = pDexFile->GetEncodedStaticFieldValuesArray(pClassDef);
1532 const u4 sSize = sData != nullptr ? DecodeUnsignedLeb128(&sData) : 0;
1533
1534 // Static fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001535 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1536 fprintf(gOutFile, " Static fields -\n");
1537 }
Aart Bikdce50862016-06-10 16:04:03 -07001538 for (u4 i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) {
1539 dumpSField(pDexFile,
1540 pClassData.GetMemberIndex(),
1541 pClassData.GetRawMemberAccessFlags(),
1542 i,
1543 i < sSize ? &sData : nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001544 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001545
1546 // Instance fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001547 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1548 fprintf(gOutFile, " Instance fields -\n");
1549 }
Aart Bikdce50862016-06-10 16:04:03 -07001550 for (u4 i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) {
1551 dumpIField(pDexFile,
1552 pClassData.GetMemberIndex(),
1553 pClassData.GetRawMemberAccessFlags(),
1554 i);
Aart Bik69ae54a2015-07-01 14:52:26 -07001555 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001556
1557 // Direct methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001558 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1559 fprintf(gOutFile, " Direct methods -\n");
1560 }
1561 for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) {
1562 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1563 pClassData.GetRawMemberAccessFlags(),
1564 pClassData.GetMethodCodeItem(),
1565 pClassData.GetMethodCodeItemOffset(), i);
1566 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001567
1568 // Virtual methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001569 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1570 fprintf(gOutFile, " Virtual methods -\n");
1571 }
1572 for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) {
1573 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1574 pClassData.GetRawMemberAccessFlags(),
1575 pClassData.GetMethodCodeItem(),
1576 pClassData.GetMethodCodeItemOffset(), i);
1577 } // for
1578 }
1579
1580 // End of class.
1581 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1582 const char* fileName;
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001583 if (pClassDef.source_file_idx_.IsValid()) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001584 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1585 } else {
1586 fileName = "unknown";
1587 }
1588 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
Andreas Gampe8a0128a2016-11-28 07:38:35 -08001589 pClassDef.source_file_idx_.index_, fileName);
Aart Bik69ae54a2015-07-01 14:52:26 -07001590 } else if (gOptions.outputFormat == OUTPUT_XML) {
1591 fprintf(gOutFile, "</class>\n");
1592 }
1593
1594 free(accessStr);
1595}
1596
Orion Hodsonc069a302017-01-18 09:23:12 +00001597static void dumpMethodHandle(const DexFile* pDexFile, u4 idx) {
1598 const DexFile::MethodHandleItem& mh = pDexFile->GetMethodHandle(idx);
Orion Hodson631827d2017-04-10 14:53:47 +01001599 const char* type = nullptr;
1600 bool is_instance = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001601 bool is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001602 switch (static_cast<DexFile::MethodHandleType>(mh.method_handle_type_)) {
1603 case DexFile::MethodHandleType::kStaticPut:
1604 type = "put-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001605 is_instance = false;
1606 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001607 break;
1608 case DexFile::MethodHandleType::kStaticGet:
1609 type = "get-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001610 is_instance = false;
1611 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001612 break;
1613 case DexFile::MethodHandleType::kInstancePut:
1614 type = "put-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001615 is_instance = true;
1616 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001617 break;
1618 case DexFile::MethodHandleType::kInstanceGet:
1619 type = "get-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001620 is_instance = true;
1621 is_invoke = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001622 break;
1623 case DexFile::MethodHandleType::kInvokeStatic:
1624 type = "invoke-static";
Orion Hodson631827d2017-04-10 14:53:47 +01001625 is_instance = false;
Orion Hodsonc069a302017-01-18 09:23:12 +00001626 is_invoke = true;
1627 break;
1628 case DexFile::MethodHandleType::kInvokeInstance:
1629 type = "invoke-instance";
Orion Hodson631827d2017-04-10 14:53:47 +01001630 is_instance = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00001631 is_invoke = true;
1632 break;
1633 case DexFile::MethodHandleType::kInvokeConstructor:
1634 type = "invoke-constructor";
Orion Hodson631827d2017-04-10 14:53:47 +01001635 is_instance = true;
1636 is_invoke = true;
1637 break;
1638 case DexFile::MethodHandleType::kInvokeDirect:
1639 type = "invoke-direct";
1640 is_instance = true;
1641 is_invoke = true;
1642 break;
1643 case DexFile::MethodHandleType::kInvokeInterface:
1644 type = "invoke-interface";
1645 is_instance = true;
Orion Hodsonc069a302017-01-18 09:23:12 +00001646 is_invoke = true;
1647 break;
1648 }
1649
1650 const char* declaring_class;
1651 const char* member;
1652 std::string member_type;
Orion Hodson631827d2017-04-10 14:53:47 +01001653 if (type != nullptr) {
1654 if (is_invoke) {
1655 const DexFile::MethodId& method_id = pDexFile->GetMethodId(mh.field_or_method_idx_);
1656 declaring_class = pDexFile->GetMethodDeclaringClassDescriptor(method_id);
1657 member = pDexFile->GetMethodName(method_id);
1658 member_type = pDexFile->GetMethodSignature(method_id).ToString();
1659 } else {
1660 const DexFile::FieldId& field_id = pDexFile->GetFieldId(mh.field_or_method_idx_);
1661 declaring_class = pDexFile->GetFieldDeclaringClassDescriptor(field_id);
1662 member = pDexFile->GetFieldName(field_id);
1663 member_type = pDexFile->GetFieldTypeDescriptor(field_id);
1664 }
1665 if (is_instance) {
1666 member_type = android::base::StringPrintf("(%s%s", declaring_class, member_type.c_str() + 1);
1667 }
Orion Hodsonc069a302017-01-18 09:23:12 +00001668 } else {
Orion Hodson631827d2017-04-10 14:53:47 +01001669 type = "?";
1670 declaring_class = "?";
1671 member = "?";
1672 member_type = "?";
Orion Hodsonc069a302017-01-18 09:23:12 +00001673 }
1674
1675 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1676 fprintf(gOutFile, "Method handle #%u:\n", idx);
1677 fprintf(gOutFile, " type : %s\n", type);
1678 fprintf(gOutFile, " target : %s %s\n", declaring_class, member);
1679 fprintf(gOutFile, " target_type : %s\n", member_type.c_str());
1680 } else {
1681 fprintf(gOutFile, "<method_handle index=\"%u\"\n", idx);
1682 fprintf(gOutFile, " type=\"%s\"\n", type);
1683 fprintf(gOutFile, " target_class=\"%s\"\n", declaring_class);
1684 fprintf(gOutFile, " target_member=\"%s\"\n", member);
1685 fprintf(gOutFile, " target_member_type=");
1686 dumpEscapedString(member_type.c_str());
1687 fprintf(gOutFile, "\n>\n</method_handle>\n");
1688 }
1689}
1690
1691static void dumpCallSite(const DexFile* pDexFile, u4 idx) {
1692 const DexFile::CallSiteIdItem& call_site_id = pDexFile->GetCallSiteId(idx);
1693 CallSiteArrayValueIterator it(*pDexFile, call_site_id);
1694 if (it.Size() < 3) {
1695 fprintf(stderr, "ERROR: Call site %u has too few values.\n", idx);
1696 return;
1697 }
1698
1699 uint32_t method_handle_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1700 it.Next();
1701 dex::StringIndex method_name_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1702 const char* method_name = pDexFile->StringDataByIdx(method_name_idx);
1703 it.Next();
1704 uint32_t method_type_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1705 const DexFile::ProtoId& method_type_id = pDexFile->GetProtoId(method_type_idx);
1706 std::string method_type = pDexFile->GetProtoSignature(method_type_id).ToString();
1707 it.Next();
1708
1709 if (gOptions.outputFormat == OUTPUT_PLAIN) {
Orion Hodson775224d2017-07-05 11:04:01 +01001710 fprintf(gOutFile, "Call site #%u: // offset %u\n", idx, call_site_id.data_off_);
Orion Hodsonc069a302017-01-18 09:23:12 +00001711 fprintf(gOutFile, " link_argument[0] : %u (MethodHandle)\n", method_handle_idx);
1712 fprintf(gOutFile, " link_argument[1] : %s (String)\n", method_name);
1713 fprintf(gOutFile, " link_argument[2] : %s (MethodType)\n", method_type.c_str());
1714 } else {
Orion Hodson775224d2017-07-05 11:04:01 +01001715 fprintf(gOutFile, "<call_site index=\"%u\" offset=\"%u\">\n", idx, call_site_id.data_off_);
Orion Hodsonc069a302017-01-18 09:23:12 +00001716 fprintf(gOutFile,
1717 "<link_argument index=\"0\" type=\"MethodHandle\" value=\"%u\"/>\n",
1718 method_handle_idx);
1719 fprintf(gOutFile,
1720 "<link_argument index=\"1\" type=\"String\" values=\"%s\"/>\n",
1721 method_name);
1722 fprintf(gOutFile,
1723 "<link_argument index=\"2\" type=\"MethodType\" value=\"%s\"/>\n",
1724 method_type.c_str());
1725 }
1726
1727 size_t argument = 3;
1728 while (it.HasNext()) {
1729 const char* type;
1730 std::string value;
1731 switch (it.GetValueType()) {
1732 case EncodedArrayValueIterator::ValueType::kByte:
1733 type = "byte";
1734 value = android::base::StringPrintf("%u", it.GetJavaValue().b);
1735 break;
1736 case EncodedArrayValueIterator::ValueType::kShort:
1737 type = "short";
1738 value = android::base::StringPrintf("%d", it.GetJavaValue().s);
1739 break;
1740 case EncodedArrayValueIterator::ValueType::kChar:
1741 type = "char";
1742 value = android::base::StringPrintf("%u", it.GetJavaValue().c);
1743 break;
1744 case EncodedArrayValueIterator::ValueType::kInt:
1745 type = "int";
1746 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1747 break;
1748 case EncodedArrayValueIterator::ValueType::kLong:
1749 type = "long";
1750 value = android::base::StringPrintf("%" PRId64, it.GetJavaValue().j);
1751 break;
1752 case EncodedArrayValueIterator::ValueType::kFloat:
1753 type = "float";
1754 value = android::base::StringPrintf("%g", it.GetJavaValue().f);
1755 break;
1756 case EncodedArrayValueIterator::ValueType::kDouble:
1757 type = "double";
1758 value = android::base::StringPrintf("%g", it.GetJavaValue().d);
1759 break;
1760 case EncodedArrayValueIterator::ValueType::kMethodType: {
1761 type = "MethodType";
1762 uint32_t proto_idx = static_cast<uint32_t>(it.GetJavaValue().i);
1763 const DexFile::ProtoId& proto_id = pDexFile->GetProtoId(proto_idx);
1764 value = pDexFile->GetProtoSignature(proto_id).ToString();
1765 break;
1766 }
1767 case EncodedArrayValueIterator::ValueType::kMethodHandle:
1768 type = "MethodHandle";
1769 value = android::base::StringPrintf("%d", it.GetJavaValue().i);
1770 break;
1771 case EncodedArrayValueIterator::ValueType::kString: {
1772 type = "String";
1773 dex::StringIndex string_idx = static_cast<dex::StringIndex>(it.GetJavaValue().i);
1774 value = pDexFile->StringDataByIdx(string_idx);
1775 break;
1776 }
1777 case EncodedArrayValueIterator::ValueType::kType: {
1778 type = "Class";
1779 dex::TypeIndex type_idx = static_cast<dex::TypeIndex>(it.GetJavaValue().i);
1780 const DexFile::ClassDef* class_def = pDexFile->FindClassDef(type_idx);
1781 value = pDexFile->GetClassDescriptor(*class_def);
1782 value = descriptorClassToDot(value.c_str()).get();
1783 break;
1784 }
1785 case EncodedArrayValueIterator::ValueType::kField:
1786 case EncodedArrayValueIterator::ValueType::kMethod:
1787 case EncodedArrayValueIterator::ValueType::kEnum:
1788 case EncodedArrayValueIterator::ValueType::kArray:
1789 case EncodedArrayValueIterator::ValueType::kAnnotation:
1790 // Unreachable based on current EncodedArrayValueIterator::Next().
Andreas Gampef45d61c2017-06-07 10:29:33 -07001791 UNIMPLEMENTED(FATAL) << " type " << it.GetValueType();
Orion Hodsonc069a302017-01-18 09:23:12 +00001792 UNREACHABLE();
Orion Hodsonc069a302017-01-18 09:23:12 +00001793 case EncodedArrayValueIterator::ValueType::kNull:
1794 type = "Null";
1795 value = "null";
1796 break;
1797 case EncodedArrayValueIterator::ValueType::kBoolean:
1798 type = "boolean";
1799 value = it.GetJavaValue().z ? "true" : "false";
1800 break;
1801 }
1802
1803 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1804 fprintf(gOutFile, " link_argument[%zu] : %s (%s)\n", argument, value.c_str(), type);
1805 } else {
1806 fprintf(gOutFile, "<link_argument index=\"%zu\" type=\"%s\" value=", argument, type);
1807 dumpEscapedString(value.c_str());
1808 fprintf(gOutFile, "/>\n");
1809 }
1810
1811 it.Next();
1812 argument++;
1813 }
1814
1815 if (gOptions.outputFormat == OUTPUT_XML) {
1816 fprintf(gOutFile, "</call_site>\n");
1817 }
1818}
1819
Aart Bik69ae54a2015-07-01 14:52:26 -07001820/*
1821 * Dumps the requested sections of the file.
1822 */
Aart Bik7b45a8a2016-10-24 16:07:59 -07001823static void processDexFile(const char* fileName,
1824 const DexFile* pDexFile, size_t i, size_t n) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001825 if (gOptions.verbose) {
Aart Bik7b45a8a2016-10-24 16:07:59 -07001826 fputs("Opened '", gOutFile);
1827 fputs(fileName, gOutFile);
1828 if (n > 1) {
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001829 fprintf(gOutFile, ":%s", DexFileLoader::GetMultiDexClassesDexName(i).c_str());
Aart Bik7b45a8a2016-10-24 16:07:59 -07001830 }
1831 fprintf(gOutFile, "', DEX version '%.3s'\n", pDexFile->GetHeader().magic_ + 4);
Aart Bik69ae54a2015-07-01 14:52:26 -07001832 }
1833
1834 // Headers.
1835 if (gOptions.showFileHeaders) {
1836 dumpFileHeader(pDexFile);
1837 }
1838
1839 // Open XML context.
1840 if (gOptions.outputFormat == OUTPUT_XML) {
1841 fprintf(gOutFile, "<api>\n");
1842 }
1843
1844 // Iterate over all classes.
1845 char* package = nullptr;
1846 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
1847 for (u4 i = 0; i < classDefsSize; i++) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001848 dumpClass(pDexFile, i, &package);
1849 } // for
1850
Orion Hodsonc069a302017-01-18 09:23:12 +00001851 // Iterate over all method handles.
1852 for (u4 i = 0; i < pDexFile->NumMethodHandles(); ++i) {
1853 dumpMethodHandle(pDexFile, i);
1854 } // for
1855
1856 // Iterate over all call site ids.
1857 for (u4 i = 0; i < pDexFile->NumCallSiteIds(); ++i) {
1858 dumpCallSite(pDexFile, i);
1859 } // for
1860
Aart Bik69ae54a2015-07-01 14:52:26 -07001861 // Free the last package allocated.
1862 if (package != nullptr) {
1863 fprintf(gOutFile, "</package>\n");
1864 free(package);
1865 }
1866
1867 // Close XML context.
1868 if (gOptions.outputFormat == OUTPUT_XML) {
1869 fprintf(gOutFile, "</api>\n");
1870 }
1871}
1872
1873/*
1874 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1875 */
1876int processFile(const char* fileName) {
1877 if (gOptions.verbose) {
1878 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1879 }
1880
1881 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
Aart Bikdce50862016-06-10 16:04:03 -07001882 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -07001883 const bool kVerifyChecksum = !gOptions.ignoreBadChecksum;
Aart Bik69ae54a2015-07-01 14:52:26 -07001884 std::string error_msg;
1885 std::vector<std::unique_ptr<const DexFile>> dex_files;
Mathieu Chartier79c87da2017-10-10 11:54:29 -07001886 if (!DexFileLoader::Open(fileName, fileName, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001887 // Display returned error message to user. Note that this error behavior
1888 // differs from the error messages shown by the original Dalvik dexdump.
1889 fputs(error_msg.c_str(), stderr);
1890 fputc('\n', stderr);
1891 return -1;
1892 }
1893
Aart Bik4e149602015-07-09 11:45:28 -07001894 // Success. Either report checksum verification or process
1895 // all dex files found in given file.
Aart Bik69ae54a2015-07-01 14:52:26 -07001896 if (gOptions.checksumOnly) {
1897 fprintf(gOutFile, "Checksum verified\n");
1898 } else {
Aart Bik7b45a8a2016-10-24 16:07:59 -07001899 for (size_t i = 0, n = dex_files.size(); i < n; i++) {
1900 processDexFile(fileName, dex_files[i].get(), i, n);
Aart Bik4e149602015-07-09 11:45:28 -07001901 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001902 }
1903 return 0;
1904}
1905
1906} // namespace art