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