blob: 9a186350664fa2536f44d28462a975a84cb5b921 [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
20 * based on Art functions in libart instead. The output is identical to
21 * the original for correct DEX files. Error messages may differ, however.
22 * 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 Bik69ae54a2015-07-01 14:52:26 -070068typedef int32_t s4;
69typedef int64_t s8;
70
71/*
72 * Basic information about a field or a method.
73 */
74struct FieldMethodInfo {
75 const char* classDescriptor;
76 const char* name;
77 const char* signature;
78};
79
80/*
81 * Flags for use with createAccessFlagStr().
82 */
83enum AccessFor {
84 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
85};
86const int kNumFlags = 18;
87
88/*
89 * Gets 2 little-endian bytes.
90 */
91static inline u2 get2LE(unsigned char const* pSrc) {
92 return pSrc[0] | (pSrc[1] << 8);
93}
94
95/*
96 * Converts a single-character primitive type into human-readable form.
97 */
98static const char* primitiveTypeLabel(char typeChar) {
99 switch (typeChar) {
100 case 'B': return "byte";
101 case 'C': return "char";
102 case 'D': return "double";
103 case 'F': return "float";
104 case 'I': return "int";
105 case 'J': return "long";
106 case 'S': return "short";
107 case 'V': return "void";
108 case 'Z': return "boolean";
109 default: return "UNKNOWN";
110 } // switch
111}
112
113/*
114 * Converts a type descriptor to human-readable "dotted" form. For
115 * example, "Ljava/lang/String;" becomes "java.lang.String", and
116 * "[I" becomes "int[]". Also converts '$' to '.', which means this
117 * form can't be converted back to a descriptor.
118 */
119static char* descriptorToDot(const char* str) {
120 int targetLen = strlen(str);
121 int offset = 0;
122
123 // Strip leading [s; will be added to end.
124 while (targetLen > 1 && str[offset] == '[') {
125 offset++;
126 targetLen--;
127 } // while
128
129 const int arrayDepth = offset;
130
131 if (targetLen == 1) {
132 // Primitive type.
133 str = primitiveTypeLabel(str[offset]);
134 offset = 0;
135 targetLen = strlen(str);
136 } else {
137 // Account for leading 'L' and trailing ';'.
138 if (targetLen >= 2 && str[offset] == 'L' &&
139 str[offset + targetLen - 1] == ';') {
140 targetLen -= 2;
141 offset++;
142 }
143 }
144
145 // Copy class name over.
146 char* newStr = reinterpret_cast<char*>(
147 malloc(targetLen + arrayDepth * 2 + 1));
148 int i = 0;
149 for (; i < targetLen; i++) {
150 const char ch = str[offset + i];
151 newStr[i] = (ch == '/' || ch == '$') ? '.' : ch;
152 } // for
153
154 // Add the appropriate number of brackets for arrays.
155 for (int j = 0; j < arrayDepth; j++) {
156 newStr[i++] = '[';
157 newStr[i++] = ']';
158 } // for
159
160 newStr[i] = '\0';
161 return newStr;
162}
163
164/*
165 * Converts the class name portion of a type descriptor to human-readable
166 * "dotted" form.
167 *
168 * Returns a newly-allocated string.
169 */
170static char* descriptorClassToDot(const char* str) {
171 // Reduce to just the class name, trimming trailing ';'.
172 const char* lastSlash = strrchr(str, '/');
173 if (lastSlash == nullptr) {
174 lastSlash = str + 1; // start past 'L'
175 } else {
176 lastSlash++; // start past '/'
177 }
178
179 char* newStr = strdup(lastSlash);
180 newStr[strlen(lastSlash) - 1] = '\0';
181 for (char* cp = newStr; *cp != '\0'; cp++) {
182 if (*cp == '$') {
183 *cp = '.';
184 }
185 } // for
186 return newStr;
187}
188
189/*
190 * Returns a quoted string representing the boolean value.
191 */
192static const char* quotedBool(bool val) {
193 return val ? "\"true\"" : "\"false\"";
194}
195
196/*
197 * Returns a quoted string representing the access flags.
198 */
199static const char* quotedVisibility(u4 accessFlags) {
200 if (accessFlags & kAccPublic) {
201 return "\"public\"";
202 } else if (accessFlags & kAccProtected) {
203 return "\"protected\"";
204 } else if (accessFlags & kAccPrivate) {
205 return "\"private\"";
206 } else {
207 return "\"package\"";
208 }
209}
210
211/*
212 * Counts the number of '1' bits in a word.
213 */
214static int countOnes(u4 val) {
215 val = val - ((val >> 1) & 0x55555555);
216 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
217 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
218}
219
220/*
221 * Creates a new string with human-readable access flags.
222 *
223 * In the base language the access_flags fields are type u2; in Dalvik
224 * they're u4.
225 */
226static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
227 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
228 {
229 "PUBLIC", /* 0x00001 */
230 "PRIVATE", /* 0x00002 */
231 "PROTECTED", /* 0x00004 */
232 "STATIC", /* 0x00008 */
233 "FINAL", /* 0x00010 */
234 "?", /* 0x00020 */
235 "?", /* 0x00040 */
236 "?", /* 0x00080 */
237 "?", /* 0x00100 */
238 "INTERFACE", /* 0x00200 */
239 "ABSTRACT", /* 0x00400 */
240 "?", /* 0x00800 */
241 "SYNTHETIC", /* 0x01000 */
242 "ANNOTATION", /* 0x02000 */
243 "ENUM", /* 0x04000 */
244 "?", /* 0x08000 */
245 "VERIFIED", /* 0x10000 */
246 "OPTIMIZED", /* 0x20000 */
247 }, {
248 "PUBLIC", /* 0x00001 */
249 "PRIVATE", /* 0x00002 */
250 "PROTECTED", /* 0x00004 */
251 "STATIC", /* 0x00008 */
252 "FINAL", /* 0x00010 */
253 "SYNCHRONIZED", /* 0x00020 */
254 "BRIDGE", /* 0x00040 */
255 "VARARGS", /* 0x00080 */
256 "NATIVE", /* 0x00100 */
257 "?", /* 0x00200 */
258 "ABSTRACT", /* 0x00400 */
259 "STRICT", /* 0x00800 */
260 "SYNTHETIC", /* 0x01000 */
261 "?", /* 0x02000 */
262 "?", /* 0x04000 */
263 "MIRANDA", /* 0x08000 */
264 "CONSTRUCTOR", /* 0x10000 */
265 "DECLARED_SYNCHRONIZED", /* 0x20000 */
266 }, {
267 "PUBLIC", /* 0x00001 */
268 "PRIVATE", /* 0x00002 */
269 "PROTECTED", /* 0x00004 */
270 "STATIC", /* 0x00008 */
271 "FINAL", /* 0x00010 */
272 "?", /* 0x00020 */
273 "VOLATILE", /* 0x00040 */
274 "TRANSIENT", /* 0x00080 */
275 "?", /* 0x00100 */
276 "?", /* 0x00200 */
277 "?", /* 0x00400 */
278 "?", /* 0x00800 */
279 "SYNTHETIC", /* 0x01000 */
280 "?", /* 0x02000 */
281 "ENUM", /* 0x04000 */
282 "?", /* 0x08000 */
283 "?", /* 0x10000 */
284 "?", /* 0x20000 */
285 },
286 };
287
288 // Allocate enough storage to hold the expected number of strings,
289 // plus a space between each. We over-allocate, using the longest
290 // string above as the base metric.
291 const int kLongest = 21; // The strlen of longest string above.
292 const int count = countOnes(flags);
293 char* str;
294 char* cp;
295 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
296
297 for (int i = 0; i < kNumFlags; i++) {
298 if (flags & 0x01) {
299 const char* accessStr = kAccessStrings[forWhat][i];
300 const int len = strlen(accessStr);
301 if (cp != str) {
302 *cp++ = ' ';
303 }
304 memcpy(cp, accessStr, len);
305 cp += len;
306 }
307 flags >>= 1;
308 } // for
309
310 *cp = '\0';
311 return str;
312}
313
314/*
315 * Copies character data from "data" to "out", converting non-ASCII values
316 * to fprintf format chars or an ASCII filler ('.' or '?').
317 *
318 * The output buffer must be able to hold (2*len)+1 bytes. The result is
319 * NULL-terminated.
320 */
321static void asciify(char* out, const unsigned char* data, size_t len) {
322 while (len--) {
323 if (*data < 0x20) {
324 // Could do more here, but we don't need them yet.
325 switch (*data) {
326 case '\0':
327 *out++ = '\\';
328 *out++ = '0';
329 break;
330 case '\n':
331 *out++ = '\\';
332 *out++ = 'n';
333 break;
334 default:
335 *out++ = '.';
336 break;
337 } // switch
338 } else if (*data >= 0x80) {
339 *out++ = '?';
340 } else {
341 *out++ = *data;
342 }
343 data++;
344 } // while
345 *out = '\0';
346}
347
348/*
349 * Dumps the file header.
350 *
351 * Note that some of the : are misaligned on purpose to preserve
352 * the exact output of the original Dalvik dexdump.
353 */
354static void dumpFileHeader(const DexFile* pDexFile) {
355 const DexFile::Header& pHeader = pDexFile->GetHeader();
356 char sanitized[sizeof(pHeader.magic_) * 2 + 1];
357 fprintf(gOutFile, "DEX file header:\n");
358 asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
359 fprintf(gOutFile, "magic : '%s'\n", sanitized);
360 fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
361 fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
362 pHeader.signature_[0], pHeader.signature_[1],
363 pHeader.signature_[DexFile::kSha1DigestSize - 2],
364 pHeader.signature_[DexFile::kSha1DigestSize - 1]);
365 fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
366 fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
367 fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
368 fprintf(gOutFile, "link_off : %d (0x%06x)\n",
369 pHeader.link_off_, pHeader.link_off_);
370 fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
371 fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
372 pHeader.string_ids_off_, pHeader.string_ids_off_);
373 fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
374 fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
375 pHeader.type_ids_off_, pHeader.type_ids_off_);
376 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
377 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
378 pHeader.proto_ids_off_, pHeader.proto_ids_off_);
379 fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
380 fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
381 pHeader.field_ids_off_, pHeader.field_ids_off_);
382 fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
383 fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
384 pHeader.method_ids_off_, pHeader.method_ids_off_);
385 fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
386 fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
387 pHeader.class_defs_off_, pHeader.class_defs_off_);
388 fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
389 fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
390 pHeader.data_off_, pHeader.data_off_);
391}
392
393/*
394 * Dumps a class_def_item.
395 */
396static void dumpClassDef(const DexFile* pDexFile, int idx) {
397 // General class information.
398 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
399 fprintf(gOutFile, "Class #%d header:\n", idx);
400 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_);
401 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
402 pClassDef.access_flags_, pClassDef.access_flags_);
403 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_);
404 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
405 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
406 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_);
407 fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
408 pClassDef.annotations_off_, pClassDef.annotations_off_);
409 fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
410 pClassDef.class_data_off_, pClassDef.class_data_off_);
411
412 // Fields and methods.
413 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
414 if (pEncodedData != nullptr) {
415 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
416 fprintf(gOutFile, "static_fields_size : %d\n", pClassData.NumStaticFields());
417 fprintf(gOutFile, "instance_fields_size: %d\n", pClassData.NumInstanceFields());
418 fprintf(gOutFile, "direct_methods_size : %d\n", pClassData.NumDirectMethods());
419 fprintf(gOutFile, "virtual_methods_size: %d\n", pClassData.NumVirtualMethods());
420 } else {
421 fprintf(gOutFile, "static_fields_size : 0\n");
422 fprintf(gOutFile, "instance_fields_size: 0\n");
423 fprintf(gOutFile, "direct_methods_size : 0\n");
424 fprintf(gOutFile, "virtual_methods_size: 0\n");
425 }
426 fprintf(gOutFile, "\n");
427}
428
429/*
430 * Dumps an interface that a class declares to implement.
431 */
432static void dumpInterface(const DexFile* pDexFile, const DexFile::TypeItem& pTypeItem, int i) {
433 const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
434 if (gOptions.outputFormat == OUTPUT_PLAIN) {
435 fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
436 } else {
437 char* dotted = descriptorToDot(interfaceName);
438 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dotted);
439 free(dotted);
440 }
441}
442
443/*
444 * Dumps the catches table associated with the code.
445 */
446static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
447 const u4 triesSize = pCode->tries_size_;
448
449 // No catch table.
450 if (triesSize == 0) {
451 fprintf(gOutFile, " catches : (none)\n");
452 return;
453 }
454
455 // Dump all table entries.
456 fprintf(gOutFile, " catches : %d\n", triesSize);
457 for (u4 i = 0; i < triesSize; i++) {
458 const DexFile::TryItem* pTry = pDexFile->GetTryItems(*pCode, i);
459 const u4 start = pTry->start_addr_;
460 const u4 end = start + pTry->insn_count_;
461 fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
462 for (CatchHandlerIterator it(*pCode, *pTry); it.HasNext(); it.Next()) {
463 const u2 tidx = it.GetHandlerTypeIndex();
464 const char* descriptor =
465 (tidx == DexFile::kDexNoIndex16) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
466 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
467 } // for
468 } // for
469}
470
471/*
472 * Callback for dumping each positions table entry.
473 */
474static bool dumpPositionsCb(void* /*context*/, u4 address, u4 lineNum) {
475 fprintf(gOutFile, " 0x%04x line=%d\n", address, lineNum);
476 return false;
477}
478
479/*
480 * Callback for dumping locals table entry.
481 */
482static void dumpLocalsCb(void* /*context*/, u2 slot, u4 startAddress, u4 endAddress,
483 const char* name, const char* descriptor, const char* signature) {
484 fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
485 startAddress, endAddress, slot, name, descriptor, signature);
486}
487
488/*
489 * Helper for dumpInstruction(), which builds the string
490 * representation for the index in the given instruction. This will
491 * first try to use the given buffer, but if the result won't fit,
492 * then this will allocate a new buffer to hold the result. A pointer
493 * to the buffer which holds the full result is always returned, and
494 * this can be compared with the one passed in, to see if the result
495 * needs to be free()d.
496 */
497static char* indexString(const DexFile* pDexFile,
498 const Instruction* pDecInsn, char* buf, size_t bufSize) {
499 // Determine index and width of the string.
500 u4 index = 0;
501 u4 width = 4;
502 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
503 // SOME NOT SUPPORTED:
504 // case Instruction::k20bc:
505 case Instruction::k21c:
506 case Instruction::k35c:
507 // case Instruction::k35ms:
508 case Instruction::k3rc:
509 // case Instruction::k3rms:
510 // case Instruction::k35mi:
511 // case Instruction::k3rmi:
512 index = pDecInsn->VRegB();
513 width = 4;
514 break;
515 case Instruction::k31c:
516 index = pDecInsn->VRegB();
517 width = 8;
518 break;
519 case Instruction::k22c:
520 // case Instruction::k22cs:
521 index = pDecInsn->VRegC();
522 width = 4;
523 break;
524 default:
525 break;
526 } // switch
527
528 // Determine index type.
529 size_t outSize = 0;
530 switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
531 case Instruction::kIndexUnknown:
532 // This function should never get called for this type, but do
533 // something sensible here, just to help with debugging.
534 outSize = snprintf(buf, bufSize, "<unknown-index>");
535 break;
536 case Instruction::kIndexNone:
537 // This function should never get called for this type, but do
538 // something sensible here, just to help with debugging.
539 outSize = snprintf(buf, bufSize, "<no-index>");
540 break;
541 case Instruction::kIndexTypeRef:
542 if (index < pDexFile->GetHeader().type_ids_size_) {
543 const char* tp = pDexFile->StringByTypeIdx(index);
544 outSize = snprintf(buf, bufSize, "%s // type@%0*x", tp, width, index);
545 } else {
546 outSize = snprintf(buf, bufSize, "<type?> // type@%0*x", width, index);
547 }
548 break;
549 case Instruction::kIndexStringRef:
550 if (index < pDexFile->GetHeader().string_ids_size_) {
551 const char* st = pDexFile->StringDataByIdx(index);
552 outSize = snprintf(buf, bufSize, "\"%s\" // string@%0*x", st, width, index);
553 } else {
554 outSize = snprintf(buf, bufSize, "<string?> // string@%0*x", width, index);
555 }
556 break;
557 case Instruction::kIndexMethodRef:
558 if (index < pDexFile->GetHeader().method_ids_size_) {
559 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
560 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
561 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
562 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
563 outSize = snprintf(buf, bufSize, "%s.%s:%s // method@%0*x",
564 backDescriptor, name, signature.ToString().c_str(), width, index);
565 } else {
566 outSize = snprintf(buf, bufSize, "<method?> // method@%0*x", width, index);
567 }
568 break;
569 case Instruction::kIndexFieldRef:
570 if (index < pDexFile->GetHeader().field_ids_size_) {
571 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index);
572 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
573 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
574 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
575 outSize = snprintf(buf, bufSize, "%s.%s:%s // field@%0*x",
576 backDescriptor, name, typeDescriptor, width, index);
577 } else {
578 outSize = snprintf(buf, bufSize, "<field?> // field@%0*x", width, index);
579 }
580 break;
581 case Instruction::kIndexVtableOffset:
582 outSize = snprintf(buf, bufSize, "[%0*x] // vtable #%0*x",
583 width, index, width, index);
584 break;
585 case Instruction::kIndexFieldOffset:
586 outSize = snprintf(buf, bufSize, "[obj+%0*x]", width, index);
587 break;
588 // SOME NOT SUPPORTED:
589 // case Instruction::kIndexVaries:
590 // case Instruction::kIndexInlineMethod:
591 default:
592 outSize = snprintf(buf, bufSize, "<?>");
593 break;
594 } // switch
595
596 // Determine success of string construction.
597 if (outSize >= bufSize) {
598 // The buffer wasn't big enough; allocate and retry. Note:
599 // snprintf() doesn't count the '\0' as part of its returned
600 // size, so we add explicit space for it here.
601 outSize++;
602 buf = reinterpret_cast<char*>(malloc(outSize));
603 if (buf == nullptr) {
604 return nullptr;
605 }
606 return indexString(pDexFile, pDecInsn, buf, outSize);
607 }
608 return buf;
609}
610
611/*
612 * Dumps a single instruction.
613 */
614static void dumpInstruction(const DexFile* pDexFile,
615 const DexFile::CodeItem* pCode,
616 u4 codeOffset, u4 insnIdx, u4 insnWidth,
617 const Instruction* pDecInsn) {
618 // Address of instruction (expressed as byte offset).
619 fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
620
621 // Dump (part of) raw bytes.
622 const u2* insns = pCode->insns_;
623 for (u4 i = 0; i < 8; i++) {
624 if (i < insnWidth) {
625 if (i == 7) {
626 fprintf(gOutFile, " ... ");
627 } else {
628 // Print 16-bit value in little-endian order.
629 const u1* bytePtr = (const u1*) &insns[insnIdx + i];
630 fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
631 }
632 } else {
633 fputs(" ", gOutFile);
634 }
635 } // for
636
637 // Dump pseudo-instruction or opcode.
638 if (pDecInsn->Opcode() == Instruction::NOP) {
639 const u2 instr = get2LE((const u1*) &insns[insnIdx]);
640 if (instr == Instruction::kPackedSwitchSignature) {
641 fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
642 } else if (instr == Instruction::kSparseSwitchSignature) {
643 fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
644 } else if (instr == Instruction::kArrayDataSignature) {
645 fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
646 } else {
647 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
648 }
649 } else {
650 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
651 }
652
653 // Set up additional argument.
654 char indexBufChars[200];
655 char *indexBuf = indexBufChars;
656 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
657 indexBuf = indexString(pDexFile, pDecInsn,
658 indexBufChars, sizeof(indexBufChars));
659 }
660
661 // Dump the instruction.
662 //
663 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
664 //
665 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
666 case Instruction::k10x: // op
667 break;
668 case Instruction::k12x: // op vA, vB
669 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
670 break;
671 case Instruction::k11n: // op vA, #+B
672 fprintf(gOutFile, " v%d, #int %d // #%x",
673 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
674 break;
675 case Instruction::k11x: // op vAA
676 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
677 break;
678 case Instruction::k10t: // op +AA
679 case Instruction::k20t: // op +AAAA
680 {
681 const s4 targ = (s4) pDecInsn->VRegA();
682 fprintf(gOutFile, " %04x // %c%04x",
683 insnIdx + targ,
684 (targ < 0) ? '-' : '+',
685 (targ < 0) ? -targ : targ);
686 }
687 break;
688 case Instruction::k22x: // op vAA, vBBBB
689 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
690 break;
691 case Instruction::k21t: // op vAA, +BBBB
692 {
693 const s4 targ = (s4) pDecInsn->VRegB();
694 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
695 insnIdx + targ,
696 (targ < 0) ? '-' : '+',
697 (targ < 0) ? -targ : targ);
698 }
699 break;
700 case Instruction::k21s: // op vAA, #+BBBB
701 fprintf(gOutFile, " v%d, #int %d // #%x",
702 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
703 break;
704 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
705 // The printed format varies a bit based on the actual opcode.
706 if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) {
707 const s4 value = pDecInsn->VRegB() << 16;
708 fprintf(gOutFile, " v%d, #int %d // #%x",
709 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
710 } else {
711 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
712 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
713 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
714 }
715 break;
716 case Instruction::k21c: // op vAA, thing@BBBB
717 case Instruction::k31c: // op vAA, thing@BBBBBBBB
718 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf);
719 break;
720 case Instruction::k23x: // op vAA, vBB, vCC
721 fprintf(gOutFile, " v%d, v%d, v%d",
722 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
723 break;
724 case Instruction::k22b: // op vAA, vBB, #+CC
725 fprintf(gOutFile, " v%d, v%d, #int %d // #%02x",
726 pDecInsn->VRegA(), pDecInsn->VRegB(),
727 (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC());
728 break;
729 case Instruction::k22t: // op vA, vB, +CCCC
730 {
731 const s4 targ = (s4) pDecInsn->VRegC();
732 fprintf(gOutFile, " v%d, v%d, %04x // %c%04x",
733 pDecInsn->VRegA(), pDecInsn->VRegB(),
734 insnIdx + targ,
735 (targ < 0) ? '-' : '+',
736 (targ < 0) ? -targ : targ);
737 }
738 break;
739 case Instruction::k22s: // op vA, vB, #+CCCC
740 fprintf(gOutFile, " v%d, v%d, #int %d // #%04x",
741 pDecInsn->VRegA(), pDecInsn->VRegB(),
742 (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC());
743 break;
744 case Instruction::k22c: // op vA, vB, thing@CCCC
745 // NOT SUPPORTED:
746 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
747 fprintf(gOutFile, " v%d, v%d, %s",
748 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf);
749 break;
750 case Instruction::k30t:
751 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
752 break;
753 case Instruction::k31i: // op vAA, #+BBBBBBBB
754 {
755 // This is often, but not always, a float.
756 union {
757 float f;
758 u4 i;
759 } conv;
760 conv.i = pDecInsn->VRegB();
761 fprintf(gOutFile, " v%d, #float %f // #%08x",
762 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
763 }
764 break;
765 case Instruction::k31t: // op vAA, offset +BBBBBBBB
766 fprintf(gOutFile, " v%d, %08x // +%08x",
767 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
768 break;
769 case Instruction::k32x: // op vAAAA, vBBBB
770 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
771 break;
772 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
773 // NOT SUPPORTED:
774 // case Instruction::k35ms: // [opt] invoke-virtual+super
775 // case Instruction::k35mi: // [opt] inline invoke
776 {
Aart Bika3bb7202015-10-26 17:24:09 -0700777 u4 arg[Instruction::kMaxVarArgRegs];
Aart Bik69ae54a2015-07-01 14:52:26 -0700778 pDecInsn->GetVarArgs(arg);
779 fputs(" {", gOutFile);
780 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
781 if (i == 0) {
782 fprintf(gOutFile, "v%d", arg[i]);
783 } else {
784 fprintf(gOutFile, ", v%d", arg[i]);
785 }
786 } // for
787 fprintf(gOutFile, "}, %s", indexBuf);
788 }
789 break;
Aart Bika3bb7202015-10-26 17:24:09 -0700790 case Instruction::k25x: // op vC, {vD, vE, vF, vG} (B: count)
791 {
792 u4 arg[Instruction::kMaxVarArgRegs25x];
793 pDecInsn->GetAllArgs25x(arg);
794 fprintf(gOutFile, " v%d, {", arg[0]);
795 for (int i = 0, n = pDecInsn->VRegB(); i < n; i++) {
796 if (i == 0) {
797 fprintf(gOutFile, "v%d", arg[Instruction::kLambdaVirtualRegisterWidth + i]);
798 } else {
799 fprintf(gOutFile, ", v%d", arg[Instruction::kLambdaVirtualRegisterWidth + i]);
800 }
801 } // for
802 fputc('}', gOutFile);
803 }
804 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700805 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
806 // NOT SUPPORTED:
807 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
808 // case Instruction::k3rmi: // [opt] execute-inline/range
809 {
810 // This doesn't match the "dx" output when some of the args are
811 // 64-bit values -- dx only shows the first register.
812 fputs(" {", gOutFile);
813 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
814 if (i == 0) {
815 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
816 } else {
817 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
818 }
819 } // for
820 fprintf(gOutFile, "}, %s", indexBuf);
821 }
822 break;
823 case Instruction::k51l: // op vAA, #+BBBBBBBBBBBBBBBB
824 {
825 // This is often, but not always, a double.
826 union {
827 double d;
828 u8 j;
829 } conv;
830 conv.j = pDecInsn->WideVRegB();
831 fprintf(gOutFile, " v%d, #double %f // #%016" PRIx64,
832 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
833 }
834 break;
835 // NOT SUPPORTED:
836 // case Instruction::k00x: // unknown op or breakpoint
837 // break;
838 default:
839 fprintf(gOutFile, " ???");
840 break;
841 } // switch
842
843 fputc('\n', gOutFile);
844
845 if (indexBuf != indexBufChars) {
846 free(indexBuf);
847 }
848}
849
850/*
851 * Dumps a bytecode disassembly.
852 */
853static void dumpBytecodes(const DexFile* pDexFile, u4 idx,
854 const DexFile::CodeItem* pCode, u4 codeOffset) {
855 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
856 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
857 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
858 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
859
860 // Generate header.
861 char* tmp = descriptorToDot(backDescriptor);
862 fprintf(gOutFile, "%06x: "
863 "|[%06x] %s.%s:%s\n",
864 codeOffset, codeOffset, tmp, name, signature.ToString().c_str());
865 free(tmp);
866
867 // Iterate over all instructions.
868 const u2* insns = pCode->insns_;
869 for (u4 insnIdx = 0; insnIdx < pCode->insns_size_in_code_units_;) {
870 const Instruction* instruction = Instruction::At(&insns[insnIdx]);
871 const u4 insnWidth = instruction->SizeInCodeUnits();
872 if (insnWidth == 0) {
873 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx);
874 break;
875 }
876 dumpInstruction(pDexFile, pCode, codeOffset, insnIdx, insnWidth, instruction);
877 insnIdx += insnWidth;
878 } // for
879}
880
881/*
882 * Dumps code of a method.
883 */
884static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags,
885 const DexFile::CodeItem* pCode, u4 codeOffset) {
886 fprintf(gOutFile, " registers : %d\n", pCode->registers_size_);
887 fprintf(gOutFile, " ins : %d\n", pCode->ins_size_);
888 fprintf(gOutFile, " outs : %d\n", pCode->outs_size_);
889 fprintf(gOutFile, " insns size : %d 16-bit code units\n",
890 pCode->insns_size_in_code_units_);
891
892 // Bytecode disassembly, if requested.
893 if (gOptions.disassemble) {
894 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
895 }
896
897 // Try-catch blocks.
898 dumpCatches(pDexFile, pCode);
899
900 // Positions and locals table in the debug info.
901 bool is_static = (flags & kAccStatic) != 0;
902 fprintf(gOutFile, " positions : \n");
903 pDexFile->DecodeDebugInfo(
904 pCode, is_static, idx, dumpPositionsCb, nullptr, nullptr);
905 fprintf(gOutFile, " locals : \n");
906 pDexFile->DecodeDebugInfo(
907 pCode, is_static, idx, nullptr, dumpLocalsCb, nullptr);
908}
909
910/*
911 * Dumps a method.
912 */
913static void dumpMethod(const DexFile* pDexFile, u4 idx, u4 flags,
914 const DexFile::CodeItem* pCode, u4 codeOffset, int i) {
915 // Bail for anything private if export only requested.
916 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
917 return;
918 }
919
920 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
921 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
922 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
923 char* typeDescriptor = strdup(signature.ToString().c_str());
924 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
925 char* accessStr = createAccessFlagStr(flags, kAccessForMethod);
926
927 if (gOptions.outputFormat == OUTPUT_PLAIN) {
928 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
929 fprintf(gOutFile, " name : '%s'\n", name);
930 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
931 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
932 if (pCode == nullptr) {
933 fprintf(gOutFile, " code : (none)\n");
934 } else {
935 fprintf(gOutFile, " code -\n");
936 dumpCode(pDexFile, idx, flags, pCode, codeOffset);
937 }
938 if (gOptions.disassemble) {
939 fputc('\n', gOutFile);
940 }
941 } else if (gOptions.outputFormat == OUTPUT_XML) {
942 const bool constructor = (name[0] == '<');
943
944 // Method name and prototype.
945 if (constructor) {
946 char* tmp = descriptorClassToDot(backDescriptor);
947 fprintf(gOutFile, "<constructor name=\"%s\"\n", tmp);
948 free(tmp);
949 tmp = descriptorToDot(backDescriptor);
950 fprintf(gOutFile, " type=\"%s\"\n", tmp);
951 free(tmp);
952 } else {
953 fprintf(gOutFile, "<method name=\"%s\"\n", name);
954 const char* returnType = strrchr(typeDescriptor, ')');
955 if (returnType == nullptr) {
956 fprintf(stderr, "bad method type descriptor '%s'\n", typeDescriptor);
957 goto bail;
958 }
959 char* tmp = descriptorToDot(returnType+1);
960 fprintf(gOutFile, " return=\"%s\"\n", tmp);
961 free(tmp);
962 fprintf(gOutFile, " abstract=%s\n", quotedBool((flags & kAccAbstract) != 0));
963 fprintf(gOutFile, " native=%s\n", quotedBool((flags & kAccNative) != 0));
964 fprintf(gOutFile, " synchronized=%s\n", quotedBool(
965 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
966 }
967
968 // Additional method flags.
969 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
970 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
971 // The "deprecated=" not knowable w/o parsing annotations.
972 fprintf(gOutFile, " visibility=%s\n>\n", quotedVisibility(flags));
973
974 // Parameters.
975 if (typeDescriptor[0] != '(') {
976 fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor);
977 goto bail;
978 }
979 char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1));
980 const char* base = typeDescriptor + 1;
981 int argNum = 0;
982 while (*base != ')') {
983 char* cp = tmpBuf;
984 while (*base == '[') {
985 *cp++ = *base++;
986 }
987 if (*base == 'L') {
988 // Copy through ';'.
989 do {
990 *cp = *base++;
991 } while (*cp++ != ';');
992 } else {
993 // Primitive char, copy it.
994 if (strchr("ZBCSIFJD", *base) == NULL) {
995 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
996 goto bail;
997 }
998 *cp++ = *base++;
999 }
1000 // Null terminate and display.
1001 *cp++ = '\0';
1002 char* tmp = descriptorToDot(tmpBuf);
1003 fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n"
1004 "</parameter>\n", argNum++, tmp);
1005 free(tmp);
1006 } // while
1007 free(tmpBuf);
1008 if (constructor) {
1009 fprintf(gOutFile, "</constructor>\n");
1010 } else {
1011 fprintf(gOutFile, "</method>\n");
1012 }
1013 }
1014
1015 bail:
1016 free(typeDescriptor);
1017 free(accessStr);
1018}
1019
1020/*
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001021 * Dumps a string value with some escape characters.
1022 */
1023static void dumpEscapedString(const char* p) {
1024 for (; *p; p++) {
1025 switch (*p) {
1026 case '\\':
1027 fputs("\\\\", gOutFile);
1028 break;
1029 case '\"':
1030 fputs("\\\"", gOutFile);
1031 break;
1032 case '\t':
1033 fputs("\\t", gOutFile);
1034 break;
1035 case '\n':
1036 fputs("\\n", gOutFile);
1037 break;
1038 case '\r':
1039 fputs("\\r", gOutFile);
1040 break;
1041 default:
1042 putc(*p, gOutFile);
1043 }
1044 }
1045}
1046
1047/*
1048 * Dumps an XML attribute value between double-quotes.
1049 */
1050static void dumpXmlAttribute(const char* p) {
1051 for (; *p; p++) {
1052 switch (*p) {
1053 case '&':
1054 fputs("&amp;", gOutFile);
1055 break;
1056 case '<':
1057 fputs("&lt;", gOutFile);
1058 break;
1059 case '"':
1060 fputs("&quot;", gOutFile);
1061 break;
1062 case '\t':
1063 fputs("&#x9;", gOutFile);
1064 break;
1065 case '\n':
1066 fputs("&#xA;", gOutFile);
1067 break;
1068 case '\r':
1069 fputs("&#xD;", gOutFile);
1070 break;
1071 default:
1072 putc(*p, gOutFile);
1073 }
1074 }
1075}
1076
1077/*
1078 * Dumps a value of static (class) field.
1079 */
1080static void dumpSFieldValue(const DexFile* pDexFile,
1081 EncodedStaticFieldValueIterator::ValueType valueType,
1082 const jvalue* pValue) {
1083 switch (valueType) {
1084 case EncodedStaticFieldValueIterator::kByte:
1085 fprintf(gOutFile, "%" PRIu8, pValue->b);
1086 break;
1087 case EncodedStaticFieldValueIterator::kShort:
1088 fprintf(gOutFile, "%" PRId16, pValue->s);
1089 break;
1090 case EncodedStaticFieldValueIterator::kChar:
1091 fprintf(gOutFile, "%" PRIu16, pValue->c);
1092 break;
1093 case EncodedStaticFieldValueIterator::kInt:
1094 fprintf(gOutFile, "%" PRId32, pValue->i);
1095 break;
1096 case EncodedStaticFieldValueIterator::kLong:
1097 fprintf(gOutFile, "%" PRId64, pValue->j);
1098 break;
1099 case EncodedStaticFieldValueIterator::kFloat:
1100 fprintf(gOutFile, "%f", pValue->f);
1101 break;
1102 case EncodedStaticFieldValueIterator::kDouble:
1103 fprintf(gOutFile, "%f", pValue->d);
1104 break;
1105 case EncodedStaticFieldValueIterator::kString: {
1106 const char* str =
1107 pDexFile->GetStringData(pDexFile->GetStringId(pValue->i));
1108 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1109 fputs("\"", gOutFile);
1110 dumpEscapedString(str);
1111 fputs("\"", gOutFile);
1112 } else {
1113 dumpXmlAttribute(str);
1114 }
1115 break;
1116 }
1117 case EncodedStaticFieldValueIterator::kNull:
1118 fputs("null", gOutFile);
1119 break;
1120 case EncodedStaticFieldValueIterator::kBoolean:
1121 fputs(pValue->z ? "true" : "false", gOutFile);
1122 break;
1123
1124 case EncodedStaticFieldValueIterator::kAnnotation:
1125 case EncodedStaticFieldValueIterator::kArray:
1126 case EncodedStaticFieldValueIterator::kEnum:
1127 case EncodedStaticFieldValueIterator::kField:
1128 case EncodedStaticFieldValueIterator::kMethod:
1129 case EncodedStaticFieldValueIterator::kType:
1130 default:
1131 fprintf(gOutFile, "Unexpected static field type: %d", valueType);
1132 }
1133}
1134
1135/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001136 * Dumps a static (class) field.
1137 */
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001138static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i,
1139 EncodedStaticFieldValueIterator::ValueType valueType,
1140 const jvalue* pValue) {
Aart Bik69ae54a2015-07-01 14:52:26 -07001141 // Bail for anything private if export only requested.
1142 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1143 return;
1144 }
1145
1146 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(idx);
1147 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
1148 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
1149 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
1150 char* accessStr = createAccessFlagStr(flags, kAccessForField);
1151
1152 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1153 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1154 fprintf(gOutFile, " name : '%s'\n", name);
1155 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1156 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001157 if (pValue != nullptr) {
1158 fputs(" value : ", gOutFile);
1159 dumpSFieldValue(pDexFile, valueType, pValue);
1160 fputs("\n", gOutFile);
1161 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001162 } else if (gOptions.outputFormat == OUTPUT_XML) {
1163 fprintf(gOutFile, "<field name=\"%s\"\n", name);
1164 char *tmp = descriptorToDot(typeDescriptor);
1165 fprintf(gOutFile, " type=\"%s\"\n", tmp);
1166 free(tmp);
1167 fprintf(gOutFile, " transient=%s\n", quotedBool((flags & kAccTransient) != 0));
1168 fprintf(gOutFile, " volatile=%s\n", quotedBool((flags & kAccVolatile) != 0));
1169 // The "value=" is not knowable w/o parsing annotations.
1170 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1171 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1172 // The "deprecated=" is not knowable w/o parsing annotations.
1173 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(flags));
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001174 if (pValue != nullptr) {
1175 fputs(" value=\"", gOutFile);
1176 dumpSFieldValue(pDexFile, valueType, pValue);
1177 fputs("\"\n", gOutFile);
1178 }
1179 fputs(">\n</field>\n", gOutFile);
Aart Bik69ae54a2015-07-01 14:52:26 -07001180 }
1181
1182 free(accessStr);
1183}
1184
1185/*
1186 * Dumps an instance field.
1187 */
1188static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001189 dumpSField(pDexFile, idx, flags, i,
1190 EncodedStaticFieldValueIterator::kByte, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -07001191}
1192
1193/*
Andreas Gampe5073fed2015-08-10 11:40:25 -07001194 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1195 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1196 * tool, so this is not performance-critical.
1197 */
1198
1199static void dumpCfg(const DexFile* dex_file,
1200 uint32_t dex_method_idx,
1201 const DexFile::CodeItem* code_item) {
1202 if (code_item != nullptr) {
1203 std::ostringstream oss;
1204 DumpMethodCFG(dex_file, dex_method_idx, oss);
1205 fprintf(gOutFile, "%s", oss.str().c_str());
1206 }
1207}
1208
1209static void dumpCfg(const DexFile* dex_file, int idx) {
1210 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
1211 const uint8_t* class_data = dex_file->GetClassData(class_def);
1212 if (class_data == nullptr) { // empty class such as a marker interface?
1213 return;
1214 }
1215 ClassDataItemIterator it(*dex_file, class_data);
1216 while (it.HasNextStaticField()) {
1217 it.Next();
1218 }
1219 while (it.HasNextInstanceField()) {
1220 it.Next();
1221 }
1222 while (it.HasNextDirectMethod()) {
1223 dumpCfg(dex_file,
1224 it.GetMemberIndex(),
1225 it.GetMethodCodeItem());
1226 it.Next();
1227 }
1228 while (it.HasNextVirtualMethod()) {
1229 dumpCfg(dex_file,
1230 it.GetMemberIndex(),
1231 it.GetMethodCodeItem());
1232 it.Next();
1233 }
1234}
1235
1236/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001237 * Dumps the class.
1238 *
1239 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1240 *
1241 * If "*pLastPackage" is nullptr or does not match the current class' package,
1242 * the value will be replaced with a newly-allocated string.
1243 */
1244static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1245 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1246
1247 // Omitting non-public class.
1248 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1249 return;
1250 }
1251
Andreas Gampe5073fed2015-08-10 11:40:25 -07001252 if (gOptions.cfg) {
1253 dumpCfg(pDexFile, idx);
1254 return;
1255 }
1256
Aart Bik69ae54a2015-07-01 14:52:26 -07001257 // For the XML output, show the package name. Ideally we'd gather
1258 // up the classes, sort them, and dump them alphabetically so the
1259 // package name wouldn't jump around, but that's not a great plan
1260 // for something that needs to run on the device.
1261 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1262 if (!(classDescriptor[0] == 'L' &&
1263 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1264 // Arrays and primitives should not be defined explicitly. Keep going?
1265 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1266 } else if (gOptions.outputFormat == OUTPUT_XML) {
1267 char* mangle = strdup(classDescriptor + 1);
1268 mangle[strlen(mangle)-1] = '\0';
1269
1270 // Reduce to just the package name.
1271 char* lastSlash = strrchr(mangle, '/');
1272 if (lastSlash != nullptr) {
1273 *lastSlash = '\0';
1274 } else {
1275 *mangle = '\0';
1276 }
1277
1278 for (char* cp = mangle; *cp != '\0'; cp++) {
1279 if (*cp == '/') {
1280 *cp = '.';
1281 }
1282 } // for
1283
1284 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1285 // Start of a new package.
1286 if (*pLastPackage != nullptr) {
1287 fprintf(gOutFile, "</package>\n");
1288 }
1289 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1290 free(*pLastPackage);
1291 *pLastPackage = mangle;
1292 } else {
1293 free(mangle);
1294 }
1295 }
1296
1297 // General class information.
1298 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1299 const char* superclassDescriptor;
1300 if (pClassDef.superclass_idx_ == DexFile::kDexNoIndex16) {
1301 superclassDescriptor = nullptr;
1302 } else {
1303 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1304 }
1305 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1306 fprintf(gOutFile, "Class #%d -\n", idx);
1307 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1308 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1309 if (superclassDescriptor != nullptr) {
1310 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1311 }
1312 fprintf(gOutFile, " Interfaces -\n");
1313 } else {
1314 char* tmp = descriptorClassToDot(classDescriptor);
1315 fprintf(gOutFile, "<class name=\"%s\"\n", tmp);
1316 free(tmp);
1317 if (superclassDescriptor != nullptr) {
1318 tmp = descriptorToDot(superclassDescriptor);
1319 fprintf(gOutFile, " extends=\"%s\"\n", tmp);
1320 free(tmp);
1321 }
Alex Light1f12e282015-12-10 16:49:47 -08001322 fprintf(gOutFile, " interface=%s\n",
1323 quotedBool((pClassDef.access_flags_ & kAccInterface) != 0));
Aart Bik69ae54a2015-07-01 14:52:26 -07001324 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1325 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1326 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1327 // The "deprecated=" not knowable w/o parsing annotations.
1328 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1329 fprintf(gOutFile, ">\n");
1330 }
1331
1332 // Interfaces.
1333 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1334 if (pInterfaces != nullptr) {
1335 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1336 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1337 } // for
1338 }
1339
1340 // Fields and methods.
1341 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
1342 if (pEncodedData == nullptr) {
1343 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1344 fprintf(gOutFile, " Static fields -\n");
1345 fprintf(gOutFile, " Instance fields -\n");
1346 fprintf(gOutFile, " Direct methods -\n");
1347 fprintf(gOutFile, " Virtual methods -\n");
1348 }
1349 } else {
1350 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
1351 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1352 fprintf(gOutFile, " Static fields -\n");
1353 }
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001354 EncodedStaticFieldValueIterator staticFieldValues(*pDexFile, pClassDef);
Aart Bik69ae54a2015-07-01 14:52:26 -07001355 for (int i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) {
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001356 EncodedStaticFieldValueIterator::ValueType valueType =
1357 EncodedStaticFieldValueIterator::kByte;
1358 const jvalue* pValue = nullptr;
1359 if (staticFieldValues.HasNext()) {
1360 valueType = staticFieldValues.GetValueType();
1361 pValue = &staticFieldValues.GetJavaValue();
1362 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001363 dumpSField(pDexFile, pClassData.GetMemberIndex(),
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001364 pClassData.GetRawMemberAccessFlags(), i,
1365 valueType, pValue);
1366 if (staticFieldValues.HasNext()) {
1367 staticFieldValues.Next();
1368 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001369 } // for
Shinichiro Hamaji82863f02015-11-05 16:51:33 +09001370 DCHECK(!staticFieldValues.HasNext());
Aart Bik69ae54a2015-07-01 14:52:26 -07001371 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1372 fprintf(gOutFile, " Instance fields -\n");
1373 }
1374 for (int i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) {
1375 dumpIField(pDexFile, pClassData.GetMemberIndex(),
1376 pClassData.GetRawMemberAccessFlags(), i);
1377 } // for
1378 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1379 fprintf(gOutFile, " Direct methods -\n");
1380 }
1381 for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) {
1382 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1383 pClassData.GetRawMemberAccessFlags(),
1384 pClassData.GetMethodCodeItem(),
1385 pClassData.GetMethodCodeItemOffset(), i);
1386 } // for
1387 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1388 fprintf(gOutFile, " Virtual methods -\n");
1389 }
1390 for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) {
1391 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1392 pClassData.GetRawMemberAccessFlags(),
1393 pClassData.GetMethodCodeItem(),
1394 pClassData.GetMethodCodeItemOffset(), i);
1395 } // for
1396 }
1397
1398 // End of class.
1399 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1400 const char* fileName;
1401 if (pClassDef.source_file_idx_ != DexFile::kDexNoIndex) {
1402 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1403 } else {
1404 fileName = "unknown";
1405 }
1406 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
1407 pClassDef.source_file_idx_, fileName);
1408 } else if (gOptions.outputFormat == OUTPUT_XML) {
1409 fprintf(gOutFile, "</class>\n");
1410 }
1411
1412 free(accessStr);
1413}
1414
1415/*
1416 * Dumps the requested sections of the file.
1417 */
1418static void processDexFile(const char* fileName, const DexFile* pDexFile) {
1419 if (gOptions.verbose) {
1420 fprintf(gOutFile, "Opened '%s', DEX version '%.3s'\n",
1421 fileName, pDexFile->GetHeader().magic_ + 4);
1422 }
1423
1424 // Headers.
1425 if (gOptions.showFileHeaders) {
1426 dumpFileHeader(pDexFile);
1427 }
1428
1429 // Open XML context.
1430 if (gOptions.outputFormat == OUTPUT_XML) {
1431 fprintf(gOutFile, "<api>\n");
1432 }
1433
1434 // Iterate over all classes.
1435 char* package = nullptr;
1436 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
1437 for (u4 i = 0; i < classDefsSize; i++) {
1438 if (gOptions.showSectionHeaders) {
1439 dumpClassDef(pDexFile, i);
1440 }
1441 dumpClass(pDexFile, i, &package);
1442 } // for
1443
1444 // Free the last package allocated.
1445 if (package != nullptr) {
1446 fprintf(gOutFile, "</package>\n");
1447 free(package);
1448 }
1449
1450 // Close XML context.
1451 if (gOptions.outputFormat == OUTPUT_XML) {
1452 fprintf(gOutFile, "</api>\n");
1453 }
1454}
1455
1456/*
1457 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1458 */
1459int processFile(const char* fileName) {
1460 if (gOptions.verbose) {
1461 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1462 }
1463
1464 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1465 // all of which are Zip archives with "classes.dex" inside. The compressed
1466 // data needs to be extracted to a temp file, the location of which varies.
1467 //
1468 // TODO(ajcbik): fix following issues
1469 //
1470 // (1) gOptions.tempFileName is not accounted for
1471 // (2) gOptions.ignoreBadChecksum is not accounted for
1472 //
1473 std::string error_msg;
1474 std::vector<std::unique_ptr<const DexFile>> dex_files;
1475 if (!DexFile::Open(fileName, fileName, &error_msg, &dex_files)) {
1476 // Display returned error message to user. Note that this error behavior
1477 // differs from the error messages shown by the original Dalvik dexdump.
1478 fputs(error_msg.c_str(), stderr);
1479 fputc('\n', stderr);
1480 return -1;
1481 }
1482
Aart Bik4e149602015-07-09 11:45:28 -07001483 // Success. Either report checksum verification or process
1484 // all dex files found in given file.
Aart Bik69ae54a2015-07-01 14:52:26 -07001485 if (gOptions.checksumOnly) {
1486 fprintf(gOutFile, "Checksum verified\n");
1487 } else {
Aart Bik4e149602015-07-09 11:45:28 -07001488 for (size_t i = 0; i < dex_files.size(); i++) {
1489 processDexFile(fileName, dex_files[i].get());
1490 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001491 }
1492 return 0;
1493}
1494
1495} // namespace art