blob: 204293499c400691de2fef47c589998fb21da878 [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
45#include "dex_file-inl.h"
46#include "dex_instruction-inl.h"
Andreas Gampe5073fed2015-08-10 11:40:25 -070047#include "utils.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070048
49namespace art {
50
51/*
52 * Options parsed in main driver.
53 */
54struct Options gOptions;
55
56/*
Aart Bik4e149602015-07-09 11:45:28 -070057 * Output file. Defaults to stdout.
Aart Bik69ae54a2015-07-01 14:52:26 -070058 */
59FILE* gOutFile = stdout;
60
61/*
62 * Data types that match the definitions in the VM specification.
63 */
64typedef uint8_t u1;
65typedef uint16_t u2;
66typedef uint32_t u4;
67typedef uint64_t u8;
Aart Bikdce50862016-06-10 16:04:03 -070068typedef int8_t s1;
69typedef int16_t s2;
Aart Bik69ae54a2015-07-01 14:52:26 -070070typedef int32_t s4;
71typedef int64_t s8;
72
73/*
74 * Basic information about a field or a method.
75 */
76struct FieldMethodInfo {
77 const char* classDescriptor;
78 const char* name;
79 const char* signature;
80};
81
82/*
83 * Flags for use with createAccessFlagStr().
84 */
85enum AccessFor {
86 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
87};
88const int kNumFlags = 18;
89
90/*
91 * Gets 2 little-endian bytes.
92 */
93static inline u2 get2LE(unsigned char const* pSrc) {
94 return pSrc[0] | (pSrc[1] << 8);
95}
96
97/*
98 * Converts a single-character primitive type into human-readable form.
99 */
100static const char* primitiveTypeLabel(char typeChar) {
101 switch (typeChar) {
102 case 'B': return "byte";
103 case 'C': return "char";
104 case 'D': return "double";
105 case 'F': return "float";
106 case 'I': return "int";
107 case 'J': return "long";
108 case 'S': return "short";
109 case 'V': return "void";
110 case 'Z': return "boolean";
111 default: return "UNKNOWN";
112 } // switch
113}
114
115/*
116 * Converts a type descriptor to human-readable "dotted" form. For
117 * example, "Ljava/lang/String;" becomes "java.lang.String", and
118 * "[I" becomes "int[]". Also converts '$' to '.', which means this
119 * form can't be converted back to a descriptor.
120 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700121static std::unique_ptr<char[]> descriptorToDot(const char* str) {
Aart Bik69ae54a2015-07-01 14:52:26 -0700122 int targetLen = strlen(str);
123 int offset = 0;
124
125 // Strip leading [s; will be added to end.
126 while (targetLen > 1 && str[offset] == '[') {
127 offset++;
128 targetLen--;
129 } // while
130
131 const int arrayDepth = offset;
132
133 if (targetLen == 1) {
134 // Primitive type.
135 str = primitiveTypeLabel(str[offset]);
136 offset = 0;
137 targetLen = strlen(str);
138 } else {
139 // Account for leading 'L' and trailing ';'.
140 if (targetLen >= 2 && str[offset] == 'L' &&
141 str[offset + targetLen - 1] == ';') {
142 targetLen -= 2;
143 offset++;
144 }
145 }
146
147 // Copy class name over.
Aart Bikc05e2f22016-07-12 15:53:13 -0700148 std::unique_ptr<char[]> newStr(new char[targetLen + arrayDepth * 2 + 1]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700149 int i = 0;
150 for (; i < targetLen; i++) {
151 const char ch = str[offset + i];
152 newStr[i] = (ch == '/' || ch == '$') ? '.' : ch;
153 } // for
154
155 // Add the appropriate number of brackets for arrays.
156 for (int j = 0; j < arrayDepth; j++) {
157 newStr[i++] = '[';
158 newStr[i++] = ']';
159 } // for
160
161 newStr[i] = '\0';
162 return newStr;
163}
164
165/*
166 * Converts the class name portion of a type descriptor to human-readable
Aart Bikc05e2f22016-07-12 15:53:13 -0700167 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
Aart Bik69ae54a2015-07-01 14:52:26 -0700168 */
Aart Bikc05e2f22016-07-12 15:53:13 -0700169static std::unique_ptr<char[]> descriptorClassToDot(const char* str) {
170 // Reduce to just the class name prefix.
Aart Bik69ae54a2015-07-01 14:52:26 -0700171 const char* lastSlash = strrchr(str, '/');
172 if (lastSlash == nullptr) {
173 lastSlash = str + 1; // start past 'L'
174 } else {
175 lastSlash++; // start past '/'
176 }
177
Aart Bikc05e2f22016-07-12 15:53:13 -0700178 // Copy class name over, trimming trailing ';'.
179 const int targetLen = strlen(lastSlash);
180 std::unique_ptr<char[]> newStr(new char[targetLen]);
181 for (int i = 0; i < targetLen - 1; i++) {
182 const char ch = lastSlash[i];
183 newStr[i] = ch == '$' ? '.' : ch;
Aart Bik69ae54a2015-07-01 14:52:26 -0700184 } // for
Aart Bikc05e2f22016-07-12 15:53:13 -0700185 newStr[targetLen - 1] = '\0';
Aart Bik69ae54a2015-07-01 14:52:26 -0700186 return newStr;
187}
188
189/*
Aart Bikdce50862016-06-10 16:04:03 -0700190 * Returns string representing the boolean value.
191 */
192static const char* strBool(bool val) {
193 return val ? "true" : "false";
194}
195
196/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700197 * Returns a quoted string representing the boolean value.
198 */
199static const char* quotedBool(bool val) {
200 return val ? "\"true\"" : "\"false\"";
201}
202
203/*
204 * Returns a quoted string representing the access flags.
205 */
206static const char* quotedVisibility(u4 accessFlags) {
207 if (accessFlags & kAccPublic) {
208 return "\"public\"";
209 } else if (accessFlags & kAccProtected) {
210 return "\"protected\"";
211 } else if (accessFlags & kAccPrivate) {
212 return "\"private\"";
213 } else {
214 return "\"package\"";
215 }
216}
217
218/*
219 * Counts the number of '1' bits in a word.
220 */
221static int countOnes(u4 val) {
222 val = val - ((val >> 1) & 0x55555555);
223 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
224 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
225}
226
227/*
228 * Creates a new string with human-readable access flags.
229 *
230 * In the base language the access_flags fields are type u2; in Dalvik
231 * they're u4.
232 */
233static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
234 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
235 {
236 "PUBLIC", /* 0x00001 */
237 "PRIVATE", /* 0x00002 */
238 "PROTECTED", /* 0x00004 */
239 "STATIC", /* 0x00008 */
240 "FINAL", /* 0x00010 */
241 "?", /* 0x00020 */
242 "?", /* 0x00040 */
243 "?", /* 0x00080 */
244 "?", /* 0x00100 */
245 "INTERFACE", /* 0x00200 */
246 "ABSTRACT", /* 0x00400 */
247 "?", /* 0x00800 */
248 "SYNTHETIC", /* 0x01000 */
249 "ANNOTATION", /* 0x02000 */
250 "ENUM", /* 0x04000 */
251 "?", /* 0x08000 */
252 "VERIFIED", /* 0x10000 */
253 "OPTIMIZED", /* 0x20000 */
254 }, {
255 "PUBLIC", /* 0x00001 */
256 "PRIVATE", /* 0x00002 */
257 "PROTECTED", /* 0x00004 */
258 "STATIC", /* 0x00008 */
259 "FINAL", /* 0x00010 */
260 "SYNCHRONIZED", /* 0x00020 */
261 "BRIDGE", /* 0x00040 */
262 "VARARGS", /* 0x00080 */
263 "NATIVE", /* 0x00100 */
264 "?", /* 0x00200 */
265 "ABSTRACT", /* 0x00400 */
266 "STRICT", /* 0x00800 */
267 "SYNTHETIC", /* 0x01000 */
268 "?", /* 0x02000 */
269 "?", /* 0x04000 */
270 "MIRANDA", /* 0x08000 */
271 "CONSTRUCTOR", /* 0x10000 */
272 "DECLARED_SYNCHRONIZED", /* 0x20000 */
273 }, {
274 "PUBLIC", /* 0x00001 */
275 "PRIVATE", /* 0x00002 */
276 "PROTECTED", /* 0x00004 */
277 "STATIC", /* 0x00008 */
278 "FINAL", /* 0x00010 */
279 "?", /* 0x00020 */
280 "VOLATILE", /* 0x00040 */
281 "TRANSIENT", /* 0x00080 */
282 "?", /* 0x00100 */
283 "?", /* 0x00200 */
284 "?", /* 0x00400 */
285 "?", /* 0x00800 */
286 "SYNTHETIC", /* 0x01000 */
287 "?", /* 0x02000 */
288 "ENUM", /* 0x04000 */
289 "?", /* 0x08000 */
290 "?", /* 0x10000 */
291 "?", /* 0x20000 */
292 },
293 };
294
295 // Allocate enough storage to hold the expected number of strings,
296 // plus a space between each. We over-allocate, using the longest
297 // string above as the base metric.
298 const int kLongest = 21; // The strlen of longest string above.
299 const int count = countOnes(flags);
300 char* str;
301 char* cp;
302 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
303
304 for (int i = 0; i < kNumFlags; i++) {
305 if (flags & 0x01) {
306 const char* accessStr = kAccessStrings[forWhat][i];
307 const int len = strlen(accessStr);
308 if (cp != str) {
309 *cp++ = ' ';
310 }
311 memcpy(cp, accessStr, len);
312 cp += len;
313 }
314 flags >>= 1;
315 } // for
316
317 *cp = '\0';
318 return str;
319}
320
321/*
322 * Copies character data from "data" to "out", converting non-ASCII values
323 * to fprintf format chars or an ASCII filler ('.' or '?').
324 *
325 * The output buffer must be able to hold (2*len)+1 bytes. The result is
326 * NULL-terminated.
327 */
328static void asciify(char* out, const unsigned char* data, size_t len) {
329 while (len--) {
330 if (*data < 0x20) {
331 // Could do more here, but we don't need them yet.
332 switch (*data) {
333 case '\0':
334 *out++ = '\\';
335 *out++ = '0';
336 break;
337 case '\n':
338 *out++ = '\\';
339 *out++ = 'n';
340 break;
341 default:
342 *out++ = '.';
343 break;
344 } // switch
345 } else if (*data >= 0x80) {
346 *out++ = '?';
347 } else {
348 *out++ = *data;
349 }
350 data++;
351 } // while
352 *out = '\0';
353}
354
355/*
Aart Bikdce50862016-06-10 16:04:03 -0700356 * Dumps a string value with some escape characters.
357 */
358static void dumpEscapedString(const char* p) {
359 fputs("\"", gOutFile);
360 for (; *p; p++) {
361 switch (*p) {
362 case '\\':
363 fputs("\\\\", gOutFile);
364 break;
365 case '\"':
366 fputs("\\\"", gOutFile);
367 break;
368 case '\t':
369 fputs("\\t", gOutFile);
370 break;
371 case '\n':
372 fputs("\\n", gOutFile);
373 break;
374 case '\r':
375 fputs("\\r", gOutFile);
376 break;
377 default:
378 putc(*p, gOutFile);
379 } // switch
380 } // for
381 fputs("\"", gOutFile);
382}
383
384/*
385 * Dumps a string as an XML attribute value.
386 */
387static void dumpXmlAttribute(const char* p) {
388 for (; *p; p++) {
389 switch (*p) {
390 case '&':
391 fputs("&amp;", gOutFile);
392 break;
393 case '<':
394 fputs("&lt;", gOutFile);
395 break;
396 case '>':
397 fputs("&gt;", gOutFile);
398 break;
399 case '"':
400 fputs("&quot;", gOutFile);
401 break;
402 case '\t':
403 fputs("&#x9;", gOutFile);
404 break;
405 case '\n':
406 fputs("&#xA;", gOutFile);
407 break;
408 case '\r':
409 fputs("&#xD;", gOutFile);
410 break;
411 default:
412 putc(*p, gOutFile);
413 } // switch
414 } // for
415}
416
417/*
418 * Reads variable width value, possibly sign extended at the last defined byte.
419 */
420static u8 readVarWidth(const u1** data, u1 arg, bool sign_extend) {
421 u8 value = 0;
422 for (u4 i = 0; i <= arg; i++) {
423 value |= static_cast<u8>(*(*data)++) << (i * 8);
424 }
425 if (sign_extend) {
426 int shift = (7 - arg) * 8;
427 return (static_cast<s8>(value) << shift) >> shift;
428 }
429 return value;
430}
431
432/*
433 * Dumps encoded value.
434 */
435static void dumpEncodedValue(const DexFile* pDexFile, const u1** data); // forward
436static void dumpEncodedValue(const DexFile* pDexFile, const u1** data, u1 type, u1 arg) {
437 switch (type) {
438 case DexFile::kDexAnnotationByte:
439 fprintf(gOutFile, "%" PRId8, static_cast<s1>(readVarWidth(data, arg, false)));
440 break;
441 case DexFile::kDexAnnotationShort:
442 fprintf(gOutFile, "%" PRId16, static_cast<s2>(readVarWidth(data, arg, true)));
443 break;
444 case DexFile::kDexAnnotationChar:
445 fprintf(gOutFile, "%" PRIu16, static_cast<u2>(readVarWidth(data, arg, false)));
446 break;
447 case DexFile::kDexAnnotationInt:
448 fprintf(gOutFile, "%" PRId32, static_cast<s4>(readVarWidth(data, arg, true)));
449 break;
450 case DexFile::kDexAnnotationLong:
451 fprintf(gOutFile, "%" PRId64, static_cast<s8>(readVarWidth(data, arg, true)));
452 break;
453 case DexFile::kDexAnnotationFloat: {
454 // Fill on right.
455 union {
456 float f;
457 u4 data;
458 } conv;
459 conv.data = static_cast<u4>(readVarWidth(data, arg, false)) << (3 - arg) * 8;
460 fprintf(gOutFile, "%g", conv.f);
461 break;
462 }
463 case DexFile::kDexAnnotationDouble: {
464 // Fill on right.
465 union {
466 double d;
467 u8 data;
468 } conv;
469 conv.data = readVarWidth(data, arg, false) << (7 - arg) * 8;
470 fprintf(gOutFile, "%g", conv.d);
471 break;
472 }
473 case DexFile::kDexAnnotationString: {
474 const u4 idx = static_cast<u4>(readVarWidth(data, arg, false));
475 if (gOptions.outputFormat == OUTPUT_PLAIN) {
476 dumpEscapedString(pDexFile->StringDataByIdx(idx));
477 } else {
478 dumpXmlAttribute(pDexFile->StringDataByIdx(idx));
479 }
480 break;
481 }
482 case DexFile::kDexAnnotationType: {
483 const u4 str_idx = static_cast<u4>(readVarWidth(data, arg, false));
484 fputs(pDexFile->StringByTypeIdx(str_idx), gOutFile);
485 break;
486 }
487 case DexFile::kDexAnnotationField:
488 case DexFile::kDexAnnotationEnum: {
489 const u4 field_idx = static_cast<u4>(readVarWidth(data, arg, false));
490 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
491 fputs(pDexFile->StringDataByIdx(pFieldId.name_idx_), gOutFile);
492 break;
493 }
494 case DexFile::kDexAnnotationMethod: {
495 const u4 method_idx = static_cast<u4>(readVarWidth(data, arg, false));
496 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
497 fputs(pDexFile->StringDataByIdx(pMethodId.name_idx_), gOutFile);
498 break;
499 }
500 case DexFile::kDexAnnotationArray: {
501 fputc('{', gOutFile);
502 // Decode and display all elements.
503 const u4 size = DecodeUnsignedLeb128(data);
504 for (u4 i = 0; i < size; i++) {
505 fputc(' ', gOutFile);
506 dumpEncodedValue(pDexFile, data);
507 }
508 fputs(" }", gOutFile);
509 break;
510 }
511 case DexFile::kDexAnnotationAnnotation: {
512 const u4 type_idx = DecodeUnsignedLeb128(data);
513 fputs(pDexFile->StringByTypeIdx(type_idx), gOutFile);
514 // Decode and display all name=value pairs.
515 const u4 size = DecodeUnsignedLeb128(data);
516 for (u4 i = 0; i < size; i++) {
517 const u4 name_idx = DecodeUnsignedLeb128(data);
518 fputc(' ', gOutFile);
519 fputs(pDexFile->StringDataByIdx(name_idx), gOutFile);
520 fputc('=', gOutFile);
521 dumpEncodedValue(pDexFile, data);
522 }
523 break;
524 }
525 case DexFile::kDexAnnotationNull:
526 fputs("null", gOutFile);
527 break;
528 case DexFile::kDexAnnotationBoolean:
529 fputs(strBool(arg), gOutFile);
530 break;
531 default:
532 fputs("????", gOutFile);
533 break;
534 } // switch
535}
536
537/*
538 * Dumps encoded value with prefix.
539 */
540static void dumpEncodedValue(const DexFile* pDexFile, const u1** data) {
541 const u1 enc = *(*data)++;
542 dumpEncodedValue(pDexFile, data, enc & 0x1f, enc >> 5);
543}
544
545/*
Aart Bik69ae54a2015-07-01 14:52:26 -0700546 * Dumps the file header.
Aart Bik69ae54a2015-07-01 14:52:26 -0700547 */
548static void dumpFileHeader(const DexFile* pDexFile) {
549 const DexFile::Header& pHeader = pDexFile->GetHeader();
550 char sanitized[sizeof(pHeader.magic_) * 2 + 1];
551 fprintf(gOutFile, "DEX file header:\n");
552 asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
553 fprintf(gOutFile, "magic : '%s'\n", sanitized);
554 fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
555 fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
556 pHeader.signature_[0], pHeader.signature_[1],
557 pHeader.signature_[DexFile::kSha1DigestSize - 2],
558 pHeader.signature_[DexFile::kSha1DigestSize - 1]);
559 fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
560 fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
561 fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
562 fprintf(gOutFile, "link_off : %d (0x%06x)\n",
563 pHeader.link_off_, pHeader.link_off_);
564 fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
565 fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
566 pHeader.string_ids_off_, pHeader.string_ids_off_);
567 fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
568 fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
569 pHeader.type_ids_off_, pHeader.type_ids_off_);
Aart Bikdce50862016-06-10 16:04:03 -0700570 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
571 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
Aart Bik69ae54a2015-07-01 14:52:26 -0700572 pHeader.proto_ids_off_, pHeader.proto_ids_off_);
573 fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
574 fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
575 pHeader.field_ids_off_, pHeader.field_ids_off_);
576 fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
577 fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
578 pHeader.method_ids_off_, pHeader.method_ids_off_);
579 fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
580 fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
581 pHeader.class_defs_off_, pHeader.class_defs_off_);
582 fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
583 fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
584 pHeader.data_off_, pHeader.data_off_);
585}
586
587/*
588 * Dumps a class_def_item.
589 */
590static void dumpClassDef(const DexFile* pDexFile, int idx) {
591 // General class information.
592 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
593 fprintf(gOutFile, "Class #%d header:\n", idx);
594 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_);
595 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
596 pClassDef.access_flags_, pClassDef.access_flags_);
597 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_);
598 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
599 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
600 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_);
601 fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
602 pClassDef.annotations_off_, pClassDef.annotations_off_);
603 fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
604 pClassDef.class_data_off_, pClassDef.class_data_off_);
605
606 // Fields and methods.
607 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
608 if (pEncodedData != nullptr) {
609 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
610 fprintf(gOutFile, "static_fields_size : %d\n", pClassData.NumStaticFields());
611 fprintf(gOutFile, "instance_fields_size: %d\n", pClassData.NumInstanceFields());
612 fprintf(gOutFile, "direct_methods_size : %d\n", pClassData.NumDirectMethods());
613 fprintf(gOutFile, "virtual_methods_size: %d\n", pClassData.NumVirtualMethods());
614 } else {
615 fprintf(gOutFile, "static_fields_size : 0\n");
616 fprintf(gOutFile, "instance_fields_size: 0\n");
617 fprintf(gOutFile, "direct_methods_size : 0\n");
618 fprintf(gOutFile, "virtual_methods_size: 0\n");
619 }
620 fprintf(gOutFile, "\n");
621}
622
Aart Bikdce50862016-06-10 16:04:03 -0700623/**
624 * Dumps an annotation set item.
625 */
626static void dumpAnnotationSetItem(const DexFile* pDexFile, const DexFile::AnnotationSetItem* set_item) {
627 if (set_item == nullptr || set_item->size_ == 0) {
628 fputs(" empty-annotation-set\n", gOutFile);
629 return;
630 }
631 for (u4 i = 0; i < set_item->size_; i++) {
632 const DexFile::AnnotationItem* annotation = pDexFile->GetAnnotationItem(set_item, i);
633 if (annotation == nullptr) {
634 continue;
635 }
636 fputs(" ", gOutFile);
637 switch (annotation->visibility_) {
638 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", gOutFile); break;
639 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", gOutFile); break;
640 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", gOutFile); break;
641 default: fputs("VISIBILITY_UNKNOWN ", gOutFile); break;
642 } // switch
643 // Decode raw bytes in annotation.
644 const u1* rData = annotation->annotation_;
645 dumpEncodedValue(pDexFile, &rData, DexFile::kDexAnnotationAnnotation, 0);
646 fputc('\n', gOutFile);
647 }
648}
649
650/*
651 * Dumps class annotations.
652 */
653static void dumpClassAnnotations(const DexFile* pDexFile, int idx) {
654 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
655 const DexFile::AnnotationsDirectoryItem* dir = pDexFile->GetAnnotationsDirectory(pClassDef);
656 if (dir == nullptr) {
657 return; // none
658 }
659
660 fprintf(gOutFile, "Class #%d annotations:\n", idx);
661
662 const DexFile::AnnotationSetItem* class_set_item = pDexFile->GetClassAnnotationSet(dir);
663 const DexFile::FieldAnnotationsItem* fields = pDexFile->GetFieldAnnotations(dir);
664 const DexFile::MethodAnnotationsItem* methods = pDexFile->GetMethodAnnotations(dir);
665 const DexFile::ParameterAnnotationsItem* pars = pDexFile->GetParameterAnnotations(dir);
666
667 // Annotations on the class itself.
668 if (class_set_item != nullptr) {
669 fprintf(gOutFile, "Annotations on class\n");
670 dumpAnnotationSetItem(pDexFile, class_set_item);
671 }
672
673 // Annotations on fields.
674 if (fields != nullptr) {
675 for (u4 i = 0; i < dir->fields_size_; i++) {
676 const u4 field_idx = fields[i].field_idx_;
677 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(field_idx);
678 const char* field_name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
679 fprintf(gOutFile, "Annotations on field #%u '%s'\n", field_idx, field_name);
680 dumpAnnotationSetItem(pDexFile, pDexFile->GetFieldAnnotationSetItem(fields[i]));
681 }
682 }
683
684 // Annotations on methods.
685 if (methods != nullptr) {
686 for (u4 i = 0; i < dir->methods_size_; i++) {
687 const u4 method_idx = methods[i].method_idx_;
688 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
689 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
690 fprintf(gOutFile, "Annotations on method #%u '%s'\n", method_idx, method_name);
691 dumpAnnotationSetItem(pDexFile, pDexFile->GetMethodAnnotationSetItem(methods[i]));
692 }
693 }
694
695 // Annotations on method parameters.
696 if (pars != nullptr) {
697 for (u4 i = 0; i < dir->parameters_size_; i++) {
698 const u4 method_idx = pars[i].method_idx_;
699 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(method_idx);
700 const char* method_name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
701 fprintf(gOutFile, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
702 const DexFile::AnnotationSetRefList*
703 list = pDexFile->GetParameterAnnotationSetRefList(&pars[i]);
704 if (list != nullptr) {
705 for (u4 j = 0; j < list->size_; j++) {
706 fprintf(gOutFile, "#%u\n", j);
707 dumpAnnotationSetItem(pDexFile, pDexFile->GetSetRefItemItem(&list->list_[j]));
708 }
709 }
710 }
711 }
712
713 fputc('\n', gOutFile);
714}
715
Aart Bik69ae54a2015-07-01 14:52:26 -0700716/*
717 * Dumps an interface that a class declares to implement.
718 */
719static void dumpInterface(const DexFile* pDexFile, const DexFile::TypeItem& pTypeItem, int i) {
720 const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
721 if (gOptions.outputFormat == OUTPUT_PLAIN) {
722 fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
723 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -0700724 std::unique_ptr<char[]> dot(descriptorToDot(interfaceName));
725 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -0700726 }
727}
728
729/*
730 * Dumps the catches table associated with the code.
731 */
732static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
733 const u4 triesSize = pCode->tries_size_;
734
735 // No catch table.
736 if (triesSize == 0) {
737 fprintf(gOutFile, " catches : (none)\n");
738 return;
739 }
740
741 // Dump all table entries.
742 fprintf(gOutFile, " catches : %d\n", triesSize);
743 for (u4 i = 0; i < triesSize; i++) {
744 const DexFile::TryItem* pTry = pDexFile->GetTryItems(*pCode, i);
745 const u4 start = pTry->start_addr_;
746 const u4 end = start + pTry->insn_count_;
747 fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
748 for (CatchHandlerIterator it(*pCode, *pTry); it.HasNext(); it.Next()) {
749 const u2 tidx = it.GetHandlerTypeIndex();
750 const char* descriptor =
751 (tidx == DexFile::kDexNoIndex16) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
752 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
753 } // for
754 } // for
755}
756
757/*
758 * Callback for dumping each positions table entry.
759 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000760static bool dumpPositionsCb(void* /*context*/, const DexFile::PositionInfo& entry) {
761 fprintf(gOutFile, " 0x%04x line=%d\n", entry.address_, entry.line_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700762 return false;
763}
764
765/*
766 * Callback for dumping locals table entry.
767 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000768static void dumpLocalsCb(void* /*context*/, const DexFile::LocalInfo& entry) {
769 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
Aart Bik69ae54a2015-07-01 14:52:26 -0700770 fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
David Srbeckyb06e28e2015-12-10 13:15:00 +0000771 entry.start_address_, entry.end_address_, entry.reg_,
772 entry.name_, entry.descriptor_, signature);
Aart Bik69ae54a2015-07-01 14:52:26 -0700773}
774
775/*
776 * Helper for dumpInstruction(), which builds the string
Aart Bika0e33fd2016-07-08 18:32:45 -0700777 * representation for the index in the given instruction.
778 * Returns a pointer to a buffer of sufficient size.
Aart Bik69ae54a2015-07-01 14:52:26 -0700779 */
Aart Bika0e33fd2016-07-08 18:32:45 -0700780static std::unique_ptr<char[]> indexString(const DexFile* pDexFile,
781 const Instruction* pDecInsn,
782 size_t bufSize) {
783 std::unique_ptr<char[]> buf(new char[bufSize]);
Aart Bik69ae54a2015-07-01 14:52:26 -0700784 // Determine index and width of the string.
785 u4 index = 0;
786 u4 width = 4;
787 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
788 // SOME NOT SUPPORTED:
789 // case Instruction::k20bc:
790 case Instruction::k21c:
791 case Instruction::k35c:
792 // case Instruction::k35ms:
793 case Instruction::k3rc:
794 // case Instruction::k3rms:
795 // case Instruction::k35mi:
796 // case Instruction::k3rmi:
797 index = pDecInsn->VRegB();
798 width = 4;
799 break;
800 case Instruction::k31c:
801 index = pDecInsn->VRegB();
802 width = 8;
803 break;
804 case Instruction::k22c:
805 // case Instruction::k22cs:
806 index = pDecInsn->VRegC();
807 width = 4;
808 break;
809 default:
810 break;
811 } // switch
812
813 // Determine index type.
814 size_t outSize = 0;
815 switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
816 case Instruction::kIndexUnknown:
817 // This function should never get called for this type, but do
818 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700819 outSize = snprintf(buf.get(), bufSize, "<unknown-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700820 break;
821 case Instruction::kIndexNone:
822 // This function should never get called for this type, but do
823 // something sensible here, just to help with debugging.
Aart Bika0e33fd2016-07-08 18:32:45 -0700824 outSize = snprintf(buf.get(), bufSize, "<no-index>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700825 break;
826 case Instruction::kIndexTypeRef:
827 if (index < pDexFile->GetHeader().type_ids_size_) {
828 const char* tp = pDexFile->StringByTypeIdx(index);
Aart Bika0e33fd2016-07-08 18:32:45 -0700829 outSize = snprintf(buf.get(), bufSize, "%s // type@%0*x", tp, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700830 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700831 outSize = snprintf(buf.get(), bufSize, "<type?> // type@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700832 }
833 break;
834 case Instruction::kIndexStringRef:
835 if (index < pDexFile->GetHeader().string_ids_size_) {
836 const char* st = pDexFile->StringDataByIdx(index);
Aart Bika0e33fd2016-07-08 18:32:45 -0700837 outSize = snprintf(buf.get(), bufSize, "\"%s\" // string@%0*x", st, width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700838 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700839 outSize = snprintf(buf.get(), bufSize, "<string?> // string@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700840 }
841 break;
842 case Instruction::kIndexMethodRef:
843 if (index < pDexFile->GetHeader().method_ids_size_) {
844 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
845 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
846 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
847 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700848 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // method@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700849 backDescriptor, name, signature.ToString().c_str(), width, index);
850 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700851 outSize = snprintf(buf.get(), bufSize, "<method?> // method@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700852 }
853 break;
854 case Instruction::kIndexFieldRef:
855 if (index < pDexFile->GetHeader().field_ids_size_) {
856 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index);
857 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
858 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
859 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
Aart Bika0e33fd2016-07-08 18:32:45 -0700860 outSize = snprintf(buf.get(), bufSize, "%s.%s:%s // field@%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700861 backDescriptor, name, typeDescriptor, width, index);
862 } else {
Aart Bika0e33fd2016-07-08 18:32:45 -0700863 outSize = snprintf(buf.get(), bufSize, "<field?> // field@%0*x", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700864 }
865 break;
866 case Instruction::kIndexVtableOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700867 outSize = snprintf(buf.get(), bufSize, "[%0*x] // vtable #%0*x",
Aart Bik69ae54a2015-07-01 14:52:26 -0700868 width, index, width, index);
869 break;
870 case Instruction::kIndexFieldOffset:
Aart Bika0e33fd2016-07-08 18:32:45 -0700871 outSize = snprintf(buf.get(), bufSize, "[obj+%0*x]", width, index);
Aart Bik69ae54a2015-07-01 14:52:26 -0700872 break;
873 // SOME NOT SUPPORTED:
874 // case Instruction::kIndexVaries:
875 // case Instruction::kIndexInlineMethod:
876 default:
Aart Bika0e33fd2016-07-08 18:32:45 -0700877 outSize = snprintf(buf.get(), bufSize, "<?>");
Aart Bik69ae54a2015-07-01 14:52:26 -0700878 break;
879 } // switch
880
881 // Determine success of string construction.
882 if (outSize >= bufSize) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700883 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
884 // doesn't count/ the '\0' as part of its returned size, so we add explicit
885 // space for it here.
886 return indexString(pDexFile, pDecInsn, outSize + 1);
Aart Bik69ae54a2015-07-01 14:52:26 -0700887 }
888 return buf;
889}
890
891/*
892 * Dumps a single instruction.
893 */
894static void dumpInstruction(const DexFile* pDexFile,
895 const DexFile::CodeItem* pCode,
896 u4 codeOffset, u4 insnIdx, u4 insnWidth,
897 const Instruction* pDecInsn) {
898 // Address of instruction (expressed as byte offset).
899 fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
900
901 // Dump (part of) raw bytes.
902 const u2* insns = pCode->insns_;
903 for (u4 i = 0; i < 8; i++) {
904 if (i < insnWidth) {
905 if (i == 7) {
906 fprintf(gOutFile, " ... ");
907 } else {
908 // Print 16-bit value in little-endian order.
909 const u1* bytePtr = (const u1*) &insns[insnIdx + i];
910 fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
911 }
912 } else {
913 fputs(" ", gOutFile);
914 }
915 } // for
916
917 // Dump pseudo-instruction or opcode.
918 if (pDecInsn->Opcode() == Instruction::NOP) {
919 const u2 instr = get2LE((const u1*) &insns[insnIdx]);
920 if (instr == Instruction::kPackedSwitchSignature) {
921 fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
922 } else if (instr == Instruction::kSparseSwitchSignature) {
923 fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
924 } else if (instr == Instruction::kArrayDataSignature) {
925 fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
926 } else {
927 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
928 }
929 } else {
930 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
931 }
932
933 // Set up additional argument.
Aart Bika0e33fd2016-07-08 18:32:45 -0700934 std::unique_ptr<char[]> indexBuf;
Aart Bik69ae54a2015-07-01 14:52:26 -0700935 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
Aart Bika0e33fd2016-07-08 18:32:45 -0700936 indexBuf = indexString(pDexFile, pDecInsn, 200);
Aart Bik69ae54a2015-07-01 14:52:26 -0700937 }
938
939 // Dump the instruction.
940 //
941 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
942 //
943 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
944 case Instruction::k10x: // op
945 break;
946 case Instruction::k12x: // op vA, vB
947 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
948 break;
949 case Instruction::k11n: // op vA, #+B
950 fprintf(gOutFile, " v%d, #int %d // #%x",
951 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
952 break;
953 case Instruction::k11x: // op vAA
954 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
955 break;
956 case Instruction::k10t: // op +AA
Aart Bikdce50862016-06-10 16:04:03 -0700957 case Instruction::k20t: { // op +AAAA
958 const s4 targ = (s4) pDecInsn->VRegA();
959 fprintf(gOutFile, " %04x // %c%04x",
960 insnIdx + targ,
961 (targ < 0) ? '-' : '+',
962 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -0700963 break;
Aart Bikdce50862016-06-10 16:04:03 -0700964 }
Aart Bik69ae54a2015-07-01 14:52:26 -0700965 case Instruction::k22x: // op vAA, vBBBB
966 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
967 break;
Aart Bikdce50862016-06-10 16:04:03 -0700968 case Instruction::k21t: { // op vAA, +BBBB
969 const s4 targ = (s4) pDecInsn->VRegB();
970 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
971 insnIdx + targ,
972 (targ < 0) ? '-' : '+',
973 (targ < 0) ? -targ : targ);
Aart Bik69ae54a2015-07-01 14:52:26 -0700974 break;
Aart Bikdce50862016-06-10 16:04:03 -0700975 }
Aart Bik69ae54a2015-07-01 14:52:26 -0700976 case Instruction::k21s: // op vAA, #+BBBB
977 fprintf(gOutFile, " v%d, #int %d // #%x",
978 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
979 break;
980 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
981 // The printed format varies a bit based on the actual opcode.
982 if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) {
983 const s4 value = pDecInsn->VRegB() << 16;
984 fprintf(gOutFile, " v%d, #int %d // #%x",
985 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
986 } else {
987 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
988 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
989 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
990 }
991 break;
992 case Instruction::k21c: // op vAA, thing@BBBB
993 case Instruction::k31c: // op vAA, thing@BBBBBBBB
Aart Bika0e33fd2016-07-08 18:32:45 -0700994 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -0700995 break;
996 case Instruction::k23x: // op vAA, vBB, vCC
997 fprintf(gOutFile, " v%d, v%d, v%d",
998 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
999 break;
1000 case Instruction::k22b: // op vAA, vBB, #+CC
1001 fprintf(gOutFile, " v%d, v%d, #int %d // #%02x",
1002 pDecInsn->VRegA(), pDecInsn->VRegB(),
1003 (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC());
1004 break;
Aart Bikdce50862016-06-10 16:04:03 -07001005 case Instruction::k22t: { // op vA, vB, +CCCC
1006 const s4 targ = (s4) pDecInsn->VRegC();
1007 fprintf(gOutFile, " v%d, v%d, %04x // %c%04x",
1008 pDecInsn->VRegA(), pDecInsn->VRegB(),
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::k22s: // op vA, vB, #+CCCC
1015 fprintf(gOutFile, " v%d, v%d, #int %d // #%04x",
1016 pDecInsn->VRegA(), pDecInsn->VRegB(),
1017 (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC());
1018 break;
1019 case Instruction::k22c: // op vA, vB, thing@CCCC
1020 // NOT SUPPORTED:
1021 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
1022 fprintf(gOutFile, " v%d, v%d, %s",
Aart Bika0e33fd2016-07-08 18:32:45 -07001023 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001024 break;
1025 case Instruction::k30t:
1026 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
1027 break;
Aart Bikdce50862016-06-10 16:04:03 -07001028 case Instruction::k31i: { // op vAA, #+BBBBBBBB
1029 // This is often, but not always, a float.
1030 union {
1031 float f;
1032 u4 i;
1033 } conv;
1034 conv.i = pDecInsn->VRegB();
1035 fprintf(gOutFile, " v%d, #float %g // #%08x",
1036 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001037 break;
Aart Bikdce50862016-06-10 16:04:03 -07001038 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001039 case Instruction::k31t: // op vAA, offset +BBBBBBBB
1040 fprintf(gOutFile, " v%d, %08x // +%08x",
1041 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
1042 break;
1043 case Instruction::k32x: // op vAAAA, vBBBB
1044 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
1045 break;
Aart Bikdce50862016-06-10 16:04:03 -07001046 case Instruction::k35c: { // op {vC, vD, vE, vF, vG}, thing@BBBB
Aart Bik69ae54a2015-07-01 14:52:26 -07001047 // NOT SUPPORTED:
1048 // case Instruction::k35ms: // [opt] invoke-virtual+super
1049 // case Instruction::k35mi: // [opt] inline invoke
Aart Bikdce50862016-06-10 16:04:03 -07001050 u4 arg[Instruction::kMaxVarArgRegs];
1051 pDecInsn->GetVarArgs(arg);
1052 fputs(" {", gOutFile);
1053 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1054 if (i == 0) {
1055 fprintf(gOutFile, "v%d", arg[i]);
1056 } else {
1057 fprintf(gOutFile, ", v%d", arg[i]);
1058 }
1059 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001060 fprintf(gOutFile, "}, %s", indexBuf.get());
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::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
1064 // NOT SUPPORTED:
1065 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
1066 // case Instruction::k3rmi: // [opt] execute-inline/range
1067 {
1068 // This doesn't match the "dx" output when some of the args are
1069 // 64-bit values -- dx only shows the first register.
1070 fputs(" {", gOutFile);
1071 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
1072 if (i == 0) {
1073 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
1074 } else {
1075 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
1076 }
1077 } // for
Aart Bika0e33fd2016-07-08 18:32:45 -07001078 fprintf(gOutFile, "}, %s", indexBuf.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001079 }
1080 break;
Aart Bikdce50862016-06-10 16:04:03 -07001081 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1082 // This is often, but not always, a double.
1083 union {
1084 double d;
1085 u8 j;
1086 } conv;
1087 conv.j = pDecInsn->WideVRegB();
1088 fprintf(gOutFile, " v%d, #double %g // #%016" PRIx64,
1089 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
Aart Bik69ae54a2015-07-01 14:52:26 -07001090 break;
Aart Bikdce50862016-06-10 16:04:03 -07001091 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001092 // NOT SUPPORTED:
1093 // case Instruction::k00x: // unknown op or breakpoint
1094 // break;
1095 default:
1096 fprintf(gOutFile, " ???");
1097 break;
1098 } // switch
1099
1100 fputc('\n', gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001101}
1102
1103/*
1104 * Dumps a bytecode disassembly.
1105 */
1106static void dumpBytecodes(const DexFile* pDexFile, u4 idx,
1107 const DexFile::CodeItem* pCode, u4 codeOffset) {
1108 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1109 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1110 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1111 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1112
1113 // Generate header.
Aart Bikc05e2f22016-07-12 15:53:13 -07001114 std::unique_ptr<char[]> dot(descriptorToDot(backDescriptor));
1115 fprintf(gOutFile, "%06x: |[%06x] %s.%s:%s\n",
1116 codeOffset, codeOffset, dot.get(), name, signature.ToString().c_str());
Aart Bik69ae54a2015-07-01 14:52:26 -07001117
1118 // Iterate over all instructions.
1119 const u2* insns = pCode->insns_;
1120 for (u4 insnIdx = 0; insnIdx < pCode->insns_size_in_code_units_;) {
1121 const Instruction* instruction = Instruction::At(&insns[insnIdx]);
1122 const u4 insnWidth = instruction->SizeInCodeUnits();
1123 if (insnWidth == 0) {
1124 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx);
1125 break;
1126 }
1127 dumpInstruction(pDexFile, pCode, codeOffset, insnIdx, insnWidth, instruction);
1128 insnIdx += insnWidth;
1129 } // for
1130}
1131
1132/*
1133 * Dumps code of a method.
1134 */
1135static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags,
1136 const DexFile::CodeItem* pCode, u4 codeOffset) {
1137 fprintf(gOutFile, " registers : %d\n", pCode->registers_size_);
1138 fprintf(gOutFile, " ins : %d\n", pCode->ins_size_);
1139 fprintf(gOutFile, " outs : %d\n", pCode->outs_size_);
1140 fprintf(gOutFile, " insns size : %d 16-bit code units\n",
1141 pCode->insns_size_in_code_units_);
1142
1143 // Bytecode disassembly, if requested.
1144 if (gOptions.disassemble) {
1145 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
1146 }
1147
1148 // Try-catch blocks.
1149 dumpCatches(pDexFile, pCode);
1150
1151 // Positions and locals table in the debug info.
1152 bool is_static = (flags & kAccStatic) != 0;
1153 fprintf(gOutFile, " positions : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +00001154 pDexFile->DecodeDebugPositionInfo(pCode, dumpPositionsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001155 fprintf(gOutFile, " locals : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +00001156 pDexFile->DecodeDebugLocalInfo(pCode, is_static, idx, dumpLocalsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001157}
1158
1159/*
1160 * Dumps a method.
1161 */
1162static void dumpMethod(const DexFile* pDexFile, u4 idx, u4 flags,
1163 const DexFile::CodeItem* pCode, u4 codeOffset, int i) {
1164 // Bail for anything private if export only requested.
1165 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1166 return;
1167 }
1168
1169 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
1170 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
1171 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
1172 char* typeDescriptor = strdup(signature.ToString().c_str());
1173 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
1174 char* accessStr = createAccessFlagStr(flags, kAccessForMethod);
1175
1176 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1177 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1178 fprintf(gOutFile, " name : '%s'\n", name);
1179 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1180 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
1181 if (pCode == nullptr) {
1182 fprintf(gOutFile, " code : (none)\n");
1183 } else {
1184 fprintf(gOutFile, " code -\n");
1185 dumpCode(pDexFile, idx, flags, pCode, codeOffset);
1186 }
1187 if (gOptions.disassemble) {
1188 fputc('\n', gOutFile);
1189 }
1190 } else if (gOptions.outputFormat == OUTPUT_XML) {
1191 const bool constructor = (name[0] == '<');
1192
1193 // Method name and prototype.
1194 if (constructor) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001195 std::unique_ptr<char[]> dot(descriptorClassToDot(backDescriptor));
1196 fprintf(gOutFile, "<constructor name=\"%s\"\n", dot.get());
1197 dot = descriptorToDot(backDescriptor);
1198 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001199 } else {
1200 fprintf(gOutFile, "<method name=\"%s\"\n", name);
1201 const char* returnType = strrchr(typeDescriptor, ')');
1202 if (returnType == nullptr) {
1203 fprintf(stderr, "bad method type descriptor '%s'\n", typeDescriptor);
1204 goto bail;
1205 }
Aart Bikc05e2f22016-07-12 15:53:13 -07001206 std::unique_ptr<char[]> dot(descriptorToDot(returnType + 1));
1207 fprintf(gOutFile, " return=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001208 fprintf(gOutFile, " abstract=%s\n", quotedBool((flags & kAccAbstract) != 0));
1209 fprintf(gOutFile, " native=%s\n", quotedBool((flags & kAccNative) != 0));
1210 fprintf(gOutFile, " synchronized=%s\n", quotedBool(
1211 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1212 }
1213
1214 // Additional method flags.
1215 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1216 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1217 // The "deprecated=" not knowable w/o parsing annotations.
1218 fprintf(gOutFile, " visibility=%s\n>\n", quotedVisibility(flags));
1219
1220 // Parameters.
1221 if (typeDescriptor[0] != '(') {
1222 fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor);
1223 goto bail;
1224 }
1225 char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1));
1226 const char* base = typeDescriptor + 1;
1227 int argNum = 0;
1228 while (*base != ')') {
1229 char* cp = tmpBuf;
1230 while (*base == '[') {
1231 *cp++ = *base++;
1232 }
1233 if (*base == 'L') {
1234 // Copy through ';'.
1235 do {
1236 *cp = *base++;
1237 } while (*cp++ != ';');
1238 } else {
1239 // Primitive char, copy it.
Aart Bikc05e2f22016-07-12 15:53:13 -07001240 if (strchr("ZBCSIFJD", *base) == nullptr) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001241 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
Aart Bika0e33fd2016-07-08 18:32:45 -07001242 break; // while
Aart Bik69ae54a2015-07-01 14:52:26 -07001243 }
1244 *cp++ = *base++;
1245 }
1246 // Null terminate and display.
1247 *cp++ = '\0';
Aart Bikc05e2f22016-07-12 15:53:13 -07001248 std::unique_ptr<char[]> dot(descriptorToDot(tmpBuf));
Aart Bik69ae54a2015-07-01 14:52:26 -07001249 fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n"
Aart Bikc05e2f22016-07-12 15:53:13 -07001250 "</parameter>\n", argNum++, dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001251 } // while
1252 free(tmpBuf);
1253 if (constructor) {
1254 fprintf(gOutFile, "</constructor>\n");
1255 } else {
1256 fprintf(gOutFile, "</method>\n");
1257 }
1258 }
1259
1260 bail:
1261 free(typeDescriptor);
1262 free(accessStr);
1263}
1264
1265/*
1266 * Dumps a static (class) field.
1267 */
Aart Bikdce50862016-06-10 16:04:03 -07001268static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i, const u1** data) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001269 // Bail for anything private if export only requested.
1270 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1271 return;
1272 }
1273
1274 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(idx);
1275 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
1276 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
1277 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
1278 char* accessStr = createAccessFlagStr(flags, kAccessForField);
1279
1280 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1281 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1282 fprintf(gOutFile, " name : '%s'\n", name);
1283 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1284 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
Aart Bikdce50862016-06-10 16:04:03 -07001285 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001286 fputs(" value : ", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001287 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001288 fputs("\n", gOutFile);
1289 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001290 } else if (gOptions.outputFormat == OUTPUT_XML) {
1291 fprintf(gOutFile, "<field name=\"%s\"\n", name);
Aart Bikc05e2f22016-07-12 15:53:13 -07001292 std::unique_ptr<char[]> dot(descriptorToDot(typeDescriptor));
1293 fprintf(gOutFile, " type=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001294 fprintf(gOutFile, " transient=%s\n", quotedBool((flags & kAccTransient) != 0));
1295 fprintf(gOutFile, " volatile=%s\n", quotedBool((flags & kAccVolatile) != 0));
1296 // The "value=" is not knowable w/o parsing annotations.
1297 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1298 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1299 // The "deprecated=" is not knowable w/o parsing annotations.
1300 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(flags));
Aart Bikdce50862016-06-10 16:04:03 -07001301 if (data != nullptr) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001302 fputs(" value=\"", gOutFile);
Aart Bikdce50862016-06-10 16:04:03 -07001303 dumpEncodedValue(pDexFile, data);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001304 fputs("\"\n", gOutFile);
1305 }
1306 fputs(">\n</field>\n", gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001307 }
1308
1309 free(accessStr);
1310}
1311
1312/*
1313 * Dumps an instance field.
1314 */
1315static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
Aart Bikdce50862016-06-10 16:04:03 -07001316 dumpSField(pDexFile, idx, flags, i, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001317}
1318
1319/*
Andreas Gampe5073fed2015-08-10 11:40:25 -07001320 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1321 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1322 * tool, so this is not performance-critical.
1323 */
1324
1325static void dumpCfg(const DexFile* dex_file,
Aart Bikdce50862016-06-10 16:04:03 -07001326 u4 dex_method_idx,
Andreas Gampe5073fed2015-08-10 11:40:25 -07001327 const DexFile::CodeItem* code_item) {
1328 if (code_item != nullptr) {
1329 std::ostringstream oss;
1330 DumpMethodCFG(dex_file, dex_method_idx, oss);
1331 fprintf(gOutFile, "%s", oss.str().c_str());
1332 }
1333}
1334
1335static void dumpCfg(const DexFile* dex_file, int idx) {
1336 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
Aart Bikdce50862016-06-10 16:04:03 -07001337 const u1* class_data = dex_file->GetClassData(class_def);
Andreas Gampe5073fed2015-08-10 11:40:25 -07001338 if (class_data == nullptr) { // empty class such as a marker interface?
1339 return;
1340 }
1341 ClassDataItemIterator it(*dex_file, class_data);
1342 while (it.HasNextStaticField()) {
1343 it.Next();
1344 }
1345 while (it.HasNextInstanceField()) {
1346 it.Next();
1347 }
1348 while (it.HasNextDirectMethod()) {
1349 dumpCfg(dex_file,
1350 it.GetMemberIndex(),
1351 it.GetMethodCodeItem());
1352 it.Next();
1353 }
1354 while (it.HasNextVirtualMethod()) {
1355 dumpCfg(dex_file,
1356 it.GetMemberIndex(),
1357 it.GetMethodCodeItem());
1358 it.Next();
1359 }
1360}
1361
1362/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001363 * Dumps the class.
1364 *
1365 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1366 *
1367 * If "*pLastPackage" is nullptr or does not match the current class' package,
1368 * the value will be replaced with a newly-allocated string.
1369 */
1370static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1371 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1372
1373 // Omitting non-public class.
1374 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1375 return;
1376 }
1377
Aart Bikdce50862016-06-10 16:04:03 -07001378 if (gOptions.showSectionHeaders) {
1379 dumpClassDef(pDexFile, idx);
1380 }
1381
1382 if (gOptions.showAnnotations) {
1383 dumpClassAnnotations(pDexFile, idx);
1384 }
1385
1386 if (gOptions.showCfg) {
Andreas Gampe5073fed2015-08-10 11:40:25 -07001387 dumpCfg(pDexFile, idx);
1388 return;
1389 }
1390
Aart Bik69ae54a2015-07-01 14:52:26 -07001391 // For the XML output, show the package name. Ideally we'd gather
1392 // up the classes, sort them, and dump them alphabetically so the
1393 // package name wouldn't jump around, but that's not a great plan
1394 // for something that needs to run on the device.
1395 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1396 if (!(classDescriptor[0] == 'L' &&
1397 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1398 // Arrays and primitives should not be defined explicitly. Keep going?
1399 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1400 } else if (gOptions.outputFormat == OUTPUT_XML) {
1401 char* mangle = strdup(classDescriptor + 1);
1402 mangle[strlen(mangle)-1] = '\0';
1403
1404 // Reduce to just the package name.
1405 char* lastSlash = strrchr(mangle, '/');
1406 if (lastSlash != nullptr) {
1407 *lastSlash = '\0';
1408 } else {
1409 *mangle = '\0';
1410 }
1411
1412 for (char* cp = mangle; *cp != '\0'; cp++) {
1413 if (*cp == '/') {
1414 *cp = '.';
1415 }
1416 } // for
1417
1418 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1419 // Start of a new package.
1420 if (*pLastPackage != nullptr) {
1421 fprintf(gOutFile, "</package>\n");
1422 }
1423 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1424 free(*pLastPackage);
1425 *pLastPackage = mangle;
1426 } else {
1427 free(mangle);
1428 }
1429 }
1430
1431 // General class information.
1432 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1433 const char* superclassDescriptor;
1434 if (pClassDef.superclass_idx_ == DexFile::kDexNoIndex16) {
1435 superclassDescriptor = nullptr;
1436 } else {
1437 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1438 }
1439 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1440 fprintf(gOutFile, "Class #%d -\n", idx);
1441 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1442 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1443 if (superclassDescriptor != nullptr) {
1444 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1445 }
1446 fprintf(gOutFile, " Interfaces -\n");
1447 } else {
Aart Bikc05e2f22016-07-12 15:53:13 -07001448 std::unique_ptr<char[]> dot(descriptorClassToDot(classDescriptor));
1449 fprintf(gOutFile, "<class name=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001450 if (superclassDescriptor != nullptr) {
Aart Bikc05e2f22016-07-12 15:53:13 -07001451 dot = descriptorToDot(superclassDescriptor);
1452 fprintf(gOutFile, " extends=\"%s\"\n", dot.get());
Aart Bik69ae54a2015-07-01 14:52:26 -07001453 }
Alex Light1f12e282015-12-10 16:49:47 -08001454 fprintf(gOutFile, " interface=%s\n",
1455 quotedBool((pClassDef.access_flags_ & kAccInterface) != 0));
Aart Bik69ae54a2015-07-01 14:52:26 -07001456 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1457 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1458 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1459 // The "deprecated=" not knowable w/o parsing annotations.
1460 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1461 fprintf(gOutFile, ">\n");
1462 }
1463
1464 // Interfaces.
1465 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1466 if (pInterfaces != nullptr) {
1467 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1468 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1469 } // for
1470 }
1471
1472 // Fields and methods.
1473 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
1474 if (pEncodedData == nullptr) {
1475 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1476 fprintf(gOutFile, " Static fields -\n");
1477 fprintf(gOutFile, " Instance fields -\n");
1478 fprintf(gOutFile, " Direct methods -\n");
1479 fprintf(gOutFile, " Virtual methods -\n");
1480 }
1481 } else {
1482 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
Aart Bikdce50862016-06-10 16:04:03 -07001483
1484 // Prepare data for static fields.
1485 const u1* sData = pDexFile->GetEncodedStaticFieldValuesArray(pClassDef);
1486 const u4 sSize = sData != nullptr ? DecodeUnsignedLeb128(&sData) : 0;
1487
1488 // Static fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001489 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1490 fprintf(gOutFile, " Static fields -\n");
1491 }
Aart Bikdce50862016-06-10 16:04:03 -07001492 for (u4 i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) {
1493 dumpSField(pDexFile,
1494 pClassData.GetMemberIndex(),
1495 pClassData.GetRawMemberAccessFlags(),
1496 i,
1497 i < sSize ? &sData : nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001498 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001499
1500 // Instance fields.
Aart Bik69ae54a2015-07-01 14:52:26 -07001501 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1502 fprintf(gOutFile, " Instance fields -\n");
1503 }
Aart Bikdce50862016-06-10 16:04:03 -07001504 for (u4 i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) {
1505 dumpIField(pDexFile,
1506 pClassData.GetMemberIndex(),
1507 pClassData.GetRawMemberAccessFlags(),
1508 i);
Aart Bik69ae54a2015-07-01 14:52:26 -07001509 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001510
1511 // Direct methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001512 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1513 fprintf(gOutFile, " Direct methods -\n");
1514 }
1515 for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) {
1516 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1517 pClassData.GetRawMemberAccessFlags(),
1518 pClassData.GetMethodCodeItem(),
1519 pClassData.GetMethodCodeItemOffset(), i);
1520 } // for
Aart Bikdce50862016-06-10 16:04:03 -07001521
1522 // Virtual methods.
Aart Bik69ae54a2015-07-01 14:52:26 -07001523 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1524 fprintf(gOutFile, " Virtual methods -\n");
1525 }
1526 for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) {
1527 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1528 pClassData.GetRawMemberAccessFlags(),
1529 pClassData.GetMethodCodeItem(),
1530 pClassData.GetMethodCodeItemOffset(), i);
1531 } // for
1532 }
1533
1534 // End of class.
1535 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1536 const char* fileName;
1537 if (pClassDef.source_file_idx_ != DexFile::kDexNoIndex) {
1538 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1539 } else {
1540 fileName = "unknown";
1541 }
1542 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
1543 pClassDef.source_file_idx_, fileName);
1544 } else if (gOptions.outputFormat == OUTPUT_XML) {
1545 fprintf(gOutFile, "</class>\n");
1546 }
1547
1548 free(accessStr);
1549}
1550
1551/*
1552 * Dumps the requested sections of the file.
1553 */
1554static void processDexFile(const char* fileName, const DexFile* pDexFile) {
1555 if (gOptions.verbose) {
1556 fprintf(gOutFile, "Opened '%s', DEX version '%.3s'\n",
1557 fileName, pDexFile->GetHeader().magic_ + 4);
1558 }
1559
1560 // Headers.
1561 if (gOptions.showFileHeaders) {
1562 dumpFileHeader(pDexFile);
1563 }
1564
1565 // Open XML context.
1566 if (gOptions.outputFormat == OUTPUT_XML) {
1567 fprintf(gOutFile, "<api>\n");
1568 }
1569
1570 // Iterate over all classes.
1571 char* package = nullptr;
1572 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
1573 for (u4 i = 0; i < classDefsSize; i++) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001574 dumpClass(pDexFile, i, &package);
1575 } // for
1576
1577 // Free the last package allocated.
1578 if (package != nullptr) {
1579 fprintf(gOutFile, "</package>\n");
1580 free(package);
1581 }
1582
1583 // Close XML context.
1584 if (gOptions.outputFormat == OUTPUT_XML) {
1585 fprintf(gOutFile, "</api>\n");
1586 }
1587}
1588
1589/*
1590 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1591 */
1592int processFile(const char* fileName) {
1593 if (gOptions.verbose) {
1594 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1595 }
1596
1597 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
Aart Bikdce50862016-06-10 16:04:03 -07001598 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -07001599 const bool kVerifyChecksum = !gOptions.ignoreBadChecksum;
Aart Bik69ae54a2015-07-01 14:52:26 -07001600 std::string error_msg;
1601 std::vector<std::unique_ptr<const DexFile>> dex_files;
Aart Bik37d6a3b2016-06-21 18:30:10 -07001602 if (!DexFile::Open(fileName, fileName, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001603 // Display returned error message to user. Note that this error behavior
1604 // differs from the error messages shown by the original Dalvik dexdump.
1605 fputs(error_msg.c_str(), stderr);
1606 fputc('\n', stderr);
1607 return -1;
1608 }
1609
Aart Bik4e149602015-07-09 11:45:28 -07001610 // Success. Either report checksum verification or process
1611 // all dex files found in given file.
Aart Bik69ae54a2015-07-01 14:52:26 -07001612 if (gOptions.checksumOnly) {
1613 fprintf(gOutFile, "Checksum verified\n");
1614 } else {
Aart Bik4e149602015-07-09 11:45:28 -07001615 for (size_t i = 0; i < dex_files.size(); i++) {
1616 processDexFile(fileName, dex_files[i].get());
1617 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001618 }
1619 return 0;
1620}
1621
1622} // namespace art