blob: 81fb33cab26c6c43e90273af7f38e5edb6dee155 [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
30 * - no "value" on fields
31 * - no parameter names
32 * - no generic signatures on parameters, e.g. type="java.lang.Class<?>"
33 * - class shows declared fields and methods; does not show inherited fields
34 */
35
36#include "dexdump.h"
37
38#include <inttypes.h>
39#include <stdio.h>
40
Andreas Gampe5073fed2015-08-10 11:40:25 -070041#include <iostream>
Aart Bik69ae54a2015-07-01 14:52:26 -070042#include <memory>
Andreas Gampe5073fed2015-08-10 11:40:25 -070043#include <sstream>
Aart Bik69ae54a2015-07-01 14:52:26 -070044#include <vector>
45
46#include "dex_file-inl.h"
47#include "dex_instruction-inl.h"
Andreas Gampe5073fed2015-08-10 11:40:25 -070048#include "utils.h"
Aart Bik69ae54a2015-07-01 14:52:26 -070049
50namespace art {
51
52/*
53 * Options parsed in main driver.
54 */
55struct Options gOptions;
56
57/*
Aart Bik4e149602015-07-09 11:45:28 -070058 * Output file. Defaults to stdout.
Aart Bik69ae54a2015-07-01 14:52:26 -070059 */
60FILE* gOutFile = stdout;
61
62/*
63 * Data types that match the definitions in the VM specification.
64 */
65typedef uint8_t u1;
66typedef uint16_t u2;
67typedef uint32_t u4;
68typedef uint64_t u8;
Aart Bik69ae54a2015-07-01 14:52:26 -070069typedef int32_t s4;
70typedef int64_t s8;
71
72/*
73 * Basic information about a field or a method.
74 */
75struct FieldMethodInfo {
76 const char* classDescriptor;
77 const char* name;
78 const char* signature;
79};
80
81/*
82 * Flags for use with createAccessFlagStr().
83 */
84enum AccessFor {
85 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
86};
87const int kNumFlags = 18;
88
89/*
90 * Gets 2 little-endian bytes.
91 */
92static inline u2 get2LE(unsigned char const* pSrc) {
93 return pSrc[0] | (pSrc[1] << 8);
94}
95
96/*
97 * Converts a single-character primitive type into human-readable form.
98 */
99static const char* primitiveTypeLabel(char typeChar) {
100 switch (typeChar) {
101 case 'B': return "byte";
102 case 'C': return "char";
103 case 'D': return "double";
104 case 'F': return "float";
105 case 'I': return "int";
106 case 'J': return "long";
107 case 'S': return "short";
108 case 'V': return "void";
109 case 'Z': return "boolean";
110 default: return "UNKNOWN";
111 } // switch
112}
113
114/*
115 * Converts a type descriptor to human-readable "dotted" form. For
116 * example, "Ljava/lang/String;" becomes "java.lang.String", and
117 * "[I" becomes "int[]". Also converts '$' to '.', which means this
118 * form can't be converted back to a descriptor.
119 */
120static char* descriptorToDot(const char* str) {
121 int targetLen = strlen(str);
122 int offset = 0;
123
124 // Strip leading [s; will be added to end.
125 while (targetLen > 1 && str[offset] == '[') {
126 offset++;
127 targetLen--;
128 } // while
129
130 const int arrayDepth = offset;
131
132 if (targetLen == 1) {
133 // Primitive type.
134 str = primitiveTypeLabel(str[offset]);
135 offset = 0;
136 targetLen = strlen(str);
137 } else {
138 // Account for leading 'L' and trailing ';'.
139 if (targetLen >= 2 && str[offset] == 'L' &&
140 str[offset + targetLen - 1] == ';') {
141 targetLen -= 2;
142 offset++;
143 }
144 }
145
146 // Copy class name over.
147 char* newStr = reinterpret_cast<char*>(
148 malloc(targetLen + arrayDepth * 2 + 1));
149 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
167 * "dotted" form.
168 *
169 * Returns a newly-allocated string.
170 */
171static char* descriptorClassToDot(const char* str) {
172 // Reduce to just the class name, trimming trailing ';'.
173 const char* lastSlash = strrchr(str, '/');
174 if (lastSlash == nullptr) {
175 lastSlash = str + 1; // start past 'L'
176 } else {
177 lastSlash++; // start past '/'
178 }
179
180 char* newStr = strdup(lastSlash);
181 newStr[strlen(lastSlash) - 1] = '\0';
182 for (char* cp = newStr; *cp != '\0'; cp++) {
183 if (*cp == '$') {
184 *cp = '.';
185 }
186 } // for
187 return newStr;
188}
189
190/*
191 * Returns a quoted string representing the boolean value.
192 */
193static const char* quotedBool(bool val) {
194 return val ? "\"true\"" : "\"false\"";
195}
196
197/*
198 * Returns a quoted string representing the access flags.
199 */
200static const char* quotedVisibility(u4 accessFlags) {
201 if (accessFlags & kAccPublic) {
202 return "\"public\"";
203 } else if (accessFlags & kAccProtected) {
204 return "\"protected\"";
205 } else if (accessFlags & kAccPrivate) {
206 return "\"private\"";
207 } else {
208 return "\"package\"";
209 }
210}
211
212/*
213 * Counts the number of '1' bits in a word.
214 */
215static int countOnes(u4 val) {
216 val = val - ((val >> 1) & 0x55555555);
217 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
218 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
219}
220
221/*
222 * Creates a new string with human-readable access flags.
223 *
224 * In the base language the access_flags fields are type u2; in Dalvik
225 * they're u4.
226 */
227static char* createAccessFlagStr(u4 flags, AccessFor forWhat) {
228 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
229 {
230 "PUBLIC", /* 0x00001 */
231 "PRIVATE", /* 0x00002 */
232 "PROTECTED", /* 0x00004 */
233 "STATIC", /* 0x00008 */
234 "FINAL", /* 0x00010 */
235 "?", /* 0x00020 */
236 "?", /* 0x00040 */
237 "?", /* 0x00080 */
238 "?", /* 0x00100 */
239 "INTERFACE", /* 0x00200 */
240 "ABSTRACT", /* 0x00400 */
241 "?", /* 0x00800 */
242 "SYNTHETIC", /* 0x01000 */
243 "ANNOTATION", /* 0x02000 */
244 "ENUM", /* 0x04000 */
245 "?", /* 0x08000 */
246 "VERIFIED", /* 0x10000 */
247 "OPTIMIZED", /* 0x20000 */
248 }, {
249 "PUBLIC", /* 0x00001 */
250 "PRIVATE", /* 0x00002 */
251 "PROTECTED", /* 0x00004 */
252 "STATIC", /* 0x00008 */
253 "FINAL", /* 0x00010 */
254 "SYNCHRONIZED", /* 0x00020 */
255 "BRIDGE", /* 0x00040 */
256 "VARARGS", /* 0x00080 */
257 "NATIVE", /* 0x00100 */
258 "?", /* 0x00200 */
259 "ABSTRACT", /* 0x00400 */
260 "STRICT", /* 0x00800 */
261 "SYNTHETIC", /* 0x01000 */
262 "?", /* 0x02000 */
263 "?", /* 0x04000 */
264 "MIRANDA", /* 0x08000 */
265 "CONSTRUCTOR", /* 0x10000 */
266 "DECLARED_SYNCHRONIZED", /* 0x20000 */
267 }, {
268 "PUBLIC", /* 0x00001 */
269 "PRIVATE", /* 0x00002 */
270 "PROTECTED", /* 0x00004 */
271 "STATIC", /* 0x00008 */
272 "FINAL", /* 0x00010 */
273 "?", /* 0x00020 */
274 "VOLATILE", /* 0x00040 */
275 "TRANSIENT", /* 0x00080 */
276 "?", /* 0x00100 */
277 "?", /* 0x00200 */
278 "?", /* 0x00400 */
279 "?", /* 0x00800 */
280 "SYNTHETIC", /* 0x01000 */
281 "?", /* 0x02000 */
282 "ENUM", /* 0x04000 */
283 "?", /* 0x08000 */
284 "?", /* 0x10000 */
285 "?", /* 0x20000 */
286 },
287 };
288
289 // Allocate enough storage to hold the expected number of strings,
290 // plus a space between each. We over-allocate, using the longest
291 // string above as the base metric.
292 const int kLongest = 21; // The strlen of longest string above.
293 const int count = countOnes(flags);
294 char* str;
295 char* cp;
296 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
297
298 for (int i = 0; i < kNumFlags; i++) {
299 if (flags & 0x01) {
300 const char* accessStr = kAccessStrings[forWhat][i];
301 const int len = strlen(accessStr);
302 if (cp != str) {
303 *cp++ = ' ';
304 }
305 memcpy(cp, accessStr, len);
306 cp += len;
307 }
308 flags >>= 1;
309 } // for
310
311 *cp = '\0';
312 return str;
313}
314
315/*
316 * Copies character data from "data" to "out", converting non-ASCII values
317 * to fprintf format chars or an ASCII filler ('.' or '?').
318 *
319 * The output buffer must be able to hold (2*len)+1 bytes. The result is
320 * NULL-terminated.
321 */
322static void asciify(char* out, const unsigned char* data, size_t len) {
323 while (len--) {
324 if (*data < 0x20) {
325 // Could do more here, but we don't need them yet.
326 switch (*data) {
327 case '\0':
328 *out++ = '\\';
329 *out++ = '0';
330 break;
331 case '\n':
332 *out++ = '\\';
333 *out++ = 'n';
334 break;
335 default:
336 *out++ = '.';
337 break;
338 } // switch
339 } else if (*data >= 0x80) {
340 *out++ = '?';
341 } else {
342 *out++ = *data;
343 }
344 data++;
345 } // while
346 *out = '\0';
347}
348
349/*
350 * Dumps the file header.
351 *
352 * Note that some of the : are misaligned on purpose to preserve
353 * the exact output of the original Dalvik dexdump.
354 */
355static void dumpFileHeader(const DexFile* pDexFile) {
356 const DexFile::Header& pHeader = pDexFile->GetHeader();
357 char sanitized[sizeof(pHeader.magic_) * 2 + 1];
358 fprintf(gOutFile, "DEX file header:\n");
359 asciify(sanitized, pHeader.magic_, sizeof(pHeader.magic_));
360 fprintf(gOutFile, "magic : '%s'\n", sanitized);
361 fprintf(gOutFile, "checksum : %08x\n", pHeader.checksum_);
362 fprintf(gOutFile, "signature : %02x%02x...%02x%02x\n",
363 pHeader.signature_[0], pHeader.signature_[1],
364 pHeader.signature_[DexFile::kSha1DigestSize - 2],
365 pHeader.signature_[DexFile::kSha1DigestSize - 1]);
366 fprintf(gOutFile, "file_size : %d\n", pHeader.file_size_);
367 fprintf(gOutFile, "header_size : %d\n", pHeader.header_size_);
368 fprintf(gOutFile, "link_size : %d\n", pHeader.link_size_);
369 fprintf(gOutFile, "link_off : %d (0x%06x)\n",
370 pHeader.link_off_, pHeader.link_off_);
371 fprintf(gOutFile, "string_ids_size : %d\n", pHeader.string_ids_size_);
372 fprintf(gOutFile, "string_ids_off : %d (0x%06x)\n",
373 pHeader.string_ids_off_, pHeader.string_ids_off_);
374 fprintf(gOutFile, "type_ids_size : %d\n", pHeader.type_ids_size_);
375 fprintf(gOutFile, "type_ids_off : %d (0x%06x)\n",
376 pHeader.type_ids_off_, pHeader.type_ids_off_);
377 fprintf(gOutFile, "proto_ids_size : %d\n", pHeader.proto_ids_size_);
378 fprintf(gOutFile, "proto_ids_off : %d (0x%06x)\n",
379 pHeader.proto_ids_off_, pHeader.proto_ids_off_);
380 fprintf(gOutFile, "field_ids_size : %d\n", pHeader.field_ids_size_);
381 fprintf(gOutFile, "field_ids_off : %d (0x%06x)\n",
382 pHeader.field_ids_off_, pHeader.field_ids_off_);
383 fprintf(gOutFile, "method_ids_size : %d\n", pHeader.method_ids_size_);
384 fprintf(gOutFile, "method_ids_off : %d (0x%06x)\n",
385 pHeader.method_ids_off_, pHeader.method_ids_off_);
386 fprintf(gOutFile, "class_defs_size : %d\n", pHeader.class_defs_size_);
387 fprintf(gOutFile, "class_defs_off : %d (0x%06x)\n",
388 pHeader.class_defs_off_, pHeader.class_defs_off_);
389 fprintf(gOutFile, "data_size : %d\n", pHeader.data_size_);
390 fprintf(gOutFile, "data_off : %d (0x%06x)\n\n",
391 pHeader.data_off_, pHeader.data_off_);
392}
393
394/*
395 * Dumps a class_def_item.
396 */
397static void dumpClassDef(const DexFile* pDexFile, int idx) {
398 // General class information.
399 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
400 fprintf(gOutFile, "Class #%d header:\n", idx);
401 fprintf(gOutFile, "class_idx : %d\n", pClassDef.class_idx_);
402 fprintf(gOutFile, "access_flags : %d (0x%04x)\n",
403 pClassDef.access_flags_, pClassDef.access_flags_);
404 fprintf(gOutFile, "superclass_idx : %d\n", pClassDef.superclass_idx_);
405 fprintf(gOutFile, "interfaces_off : %d (0x%06x)\n",
406 pClassDef.interfaces_off_, pClassDef.interfaces_off_);
407 fprintf(gOutFile, "source_file_idx : %d\n", pClassDef.source_file_idx_);
408 fprintf(gOutFile, "annotations_off : %d (0x%06x)\n",
409 pClassDef.annotations_off_, pClassDef.annotations_off_);
410 fprintf(gOutFile, "class_data_off : %d (0x%06x)\n",
411 pClassDef.class_data_off_, pClassDef.class_data_off_);
412
413 // Fields and methods.
414 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
415 if (pEncodedData != nullptr) {
416 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
417 fprintf(gOutFile, "static_fields_size : %d\n", pClassData.NumStaticFields());
418 fprintf(gOutFile, "instance_fields_size: %d\n", pClassData.NumInstanceFields());
419 fprintf(gOutFile, "direct_methods_size : %d\n", pClassData.NumDirectMethods());
420 fprintf(gOutFile, "virtual_methods_size: %d\n", pClassData.NumVirtualMethods());
421 } else {
422 fprintf(gOutFile, "static_fields_size : 0\n");
423 fprintf(gOutFile, "instance_fields_size: 0\n");
424 fprintf(gOutFile, "direct_methods_size : 0\n");
425 fprintf(gOutFile, "virtual_methods_size: 0\n");
426 }
427 fprintf(gOutFile, "\n");
428}
429
430/*
431 * Dumps an interface that a class declares to implement.
432 */
433static void dumpInterface(const DexFile* pDexFile, const DexFile::TypeItem& pTypeItem, int i) {
434 const char* interfaceName = pDexFile->StringByTypeIdx(pTypeItem.type_idx_);
435 if (gOptions.outputFormat == OUTPUT_PLAIN) {
436 fprintf(gOutFile, " #%d : '%s'\n", i, interfaceName);
437 } else {
438 char* dotted = descriptorToDot(interfaceName);
439 fprintf(gOutFile, "<implements name=\"%s\">\n</implements>\n", dotted);
440 free(dotted);
441 }
442}
443
444/*
445 * Dumps the catches table associated with the code.
446 */
447static void dumpCatches(const DexFile* pDexFile, const DexFile::CodeItem* pCode) {
448 const u4 triesSize = pCode->tries_size_;
449
450 // No catch table.
451 if (triesSize == 0) {
452 fprintf(gOutFile, " catches : (none)\n");
453 return;
454 }
455
456 // Dump all table entries.
457 fprintf(gOutFile, " catches : %d\n", triesSize);
458 for (u4 i = 0; i < triesSize; i++) {
459 const DexFile::TryItem* pTry = pDexFile->GetTryItems(*pCode, i);
460 const u4 start = pTry->start_addr_;
461 const u4 end = start + pTry->insn_count_;
462 fprintf(gOutFile, " 0x%04x - 0x%04x\n", start, end);
463 for (CatchHandlerIterator it(*pCode, *pTry); it.HasNext(); it.Next()) {
464 const u2 tidx = it.GetHandlerTypeIndex();
465 const char* descriptor =
466 (tidx == DexFile::kDexNoIndex16) ? "<any>" : pDexFile->StringByTypeIdx(tidx);
467 fprintf(gOutFile, " %s -> 0x%04x\n", descriptor, it.GetHandlerAddress());
468 } // for
469 } // for
470}
471
472/*
473 * Callback for dumping each positions table entry.
474 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000475static bool dumpPositionsCb(void* /*context*/, const DexFile::PositionInfo& entry) {
476 fprintf(gOutFile, " 0x%04x line=%d\n", entry.address_, entry.line_);
Aart Bik69ae54a2015-07-01 14:52:26 -0700477 return false;
478}
479
480/*
481 * Callback for dumping locals table entry.
482 */
David Srbeckyb06e28e2015-12-10 13:15:00 +0000483static void dumpLocalsCb(void* /*context*/, const DexFile::LocalInfo& entry) {
484 const char* signature = entry.signature_ != nullptr ? entry.signature_ : "";
Aart Bik69ae54a2015-07-01 14:52:26 -0700485 fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
David Srbeckyb06e28e2015-12-10 13:15:00 +0000486 entry.start_address_, entry.end_address_, entry.reg_,
487 entry.name_, entry.descriptor_, signature);
Aart Bik69ae54a2015-07-01 14:52:26 -0700488}
489
490/*
491 * Helper for dumpInstruction(), which builds the string
492 * representation for the index in the given instruction. This will
493 * first try to use the given buffer, but if the result won't fit,
494 * then this will allocate a new buffer to hold the result. A pointer
495 * to the buffer which holds the full result is always returned, and
496 * this can be compared with the one passed in, to see if the result
497 * needs to be free()d.
498 */
499static char* indexString(const DexFile* pDexFile,
500 const Instruction* pDecInsn, char* buf, size_t bufSize) {
501 // Determine index and width of the string.
502 u4 index = 0;
503 u4 width = 4;
504 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
505 // SOME NOT SUPPORTED:
506 // case Instruction::k20bc:
507 case Instruction::k21c:
508 case Instruction::k35c:
509 // case Instruction::k35ms:
510 case Instruction::k3rc:
511 // case Instruction::k3rms:
512 // case Instruction::k35mi:
513 // case Instruction::k3rmi:
514 index = pDecInsn->VRegB();
515 width = 4;
516 break;
517 case Instruction::k31c:
518 index = pDecInsn->VRegB();
519 width = 8;
520 break;
521 case Instruction::k22c:
522 // case Instruction::k22cs:
523 index = pDecInsn->VRegC();
524 width = 4;
525 break;
526 default:
527 break;
528 } // switch
529
530 // Determine index type.
531 size_t outSize = 0;
532 switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) {
533 case Instruction::kIndexUnknown:
534 // This function should never get called for this type, but do
535 // something sensible here, just to help with debugging.
536 outSize = snprintf(buf, bufSize, "<unknown-index>");
537 break;
538 case Instruction::kIndexNone:
539 // This function should never get called for this type, but do
540 // something sensible here, just to help with debugging.
541 outSize = snprintf(buf, bufSize, "<no-index>");
542 break;
543 case Instruction::kIndexTypeRef:
544 if (index < pDexFile->GetHeader().type_ids_size_) {
545 const char* tp = pDexFile->StringByTypeIdx(index);
546 outSize = snprintf(buf, bufSize, "%s // type@%0*x", tp, width, index);
547 } else {
548 outSize = snprintf(buf, bufSize, "<type?> // type@%0*x", width, index);
549 }
550 break;
551 case Instruction::kIndexStringRef:
552 if (index < pDexFile->GetHeader().string_ids_size_) {
553 const char* st = pDexFile->StringDataByIdx(index);
554 outSize = snprintf(buf, bufSize, "\"%s\" // string@%0*x", st, width, index);
555 } else {
556 outSize = snprintf(buf, bufSize, "<string?> // string@%0*x", width, index);
557 }
558 break;
559 case Instruction::kIndexMethodRef:
560 if (index < pDexFile->GetHeader().method_ids_size_) {
561 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index);
562 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
563 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
564 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
565 outSize = snprintf(buf, bufSize, "%s.%s:%s // method@%0*x",
566 backDescriptor, name, signature.ToString().c_str(), width, index);
567 } else {
568 outSize = snprintf(buf, bufSize, "<method?> // method@%0*x", width, index);
569 }
570 break;
571 case Instruction::kIndexFieldRef:
572 if (index < pDexFile->GetHeader().field_ids_size_) {
573 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index);
574 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
575 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
576 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
577 outSize = snprintf(buf, bufSize, "%s.%s:%s // field@%0*x",
578 backDescriptor, name, typeDescriptor, width, index);
579 } else {
580 outSize = snprintf(buf, bufSize, "<field?> // field@%0*x", width, index);
581 }
582 break;
583 case Instruction::kIndexVtableOffset:
584 outSize = snprintf(buf, bufSize, "[%0*x] // vtable #%0*x",
585 width, index, width, index);
586 break;
587 case Instruction::kIndexFieldOffset:
588 outSize = snprintf(buf, bufSize, "[obj+%0*x]", width, index);
589 break;
590 // SOME NOT SUPPORTED:
591 // case Instruction::kIndexVaries:
592 // case Instruction::kIndexInlineMethod:
593 default:
594 outSize = snprintf(buf, bufSize, "<?>");
595 break;
596 } // switch
597
598 // Determine success of string construction.
599 if (outSize >= bufSize) {
600 // The buffer wasn't big enough; allocate and retry. Note:
601 // snprintf() doesn't count the '\0' as part of its returned
602 // size, so we add explicit space for it here.
603 outSize++;
604 buf = reinterpret_cast<char*>(malloc(outSize));
605 if (buf == nullptr) {
606 return nullptr;
607 }
608 return indexString(pDexFile, pDecInsn, buf, outSize);
609 }
610 return buf;
611}
612
613/*
614 * Dumps a single instruction.
615 */
616static void dumpInstruction(const DexFile* pDexFile,
617 const DexFile::CodeItem* pCode,
618 u4 codeOffset, u4 insnIdx, u4 insnWidth,
619 const Instruction* pDecInsn) {
620 // Address of instruction (expressed as byte offset).
621 fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2);
622
623 // Dump (part of) raw bytes.
624 const u2* insns = pCode->insns_;
625 for (u4 i = 0; i < 8; i++) {
626 if (i < insnWidth) {
627 if (i == 7) {
628 fprintf(gOutFile, " ... ");
629 } else {
630 // Print 16-bit value in little-endian order.
631 const u1* bytePtr = (const u1*) &insns[insnIdx + i];
632 fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]);
633 }
634 } else {
635 fputs(" ", gOutFile);
636 }
637 } // for
638
639 // Dump pseudo-instruction or opcode.
640 if (pDecInsn->Opcode() == Instruction::NOP) {
641 const u2 instr = get2LE((const u1*) &insns[insnIdx]);
642 if (instr == Instruction::kPackedSwitchSignature) {
643 fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth);
644 } else if (instr == Instruction::kSparseSwitchSignature) {
645 fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth);
646 } else if (instr == Instruction::kArrayDataSignature) {
647 fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth);
648 } else {
649 fprintf(gOutFile, "|%04x: nop // spacer", insnIdx);
650 }
651 } else {
652 fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name());
653 }
654
655 // Set up additional argument.
656 char indexBufChars[200];
657 char *indexBuf = indexBufChars;
658 if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) {
659 indexBuf = indexString(pDexFile, pDecInsn,
660 indexBufChars, sizeof(indexBufChars));
661 }
662
663 // Dump the instruction.
664 //
665 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
666 //
667 switch (Instruction::FormatOf(pDecInsn->Opcode())) {
668 case Instruction::k10x: // op
669 break;
670 case Instruction::k12x: // op vA, vB
671 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
672 break;
673 case Instruction::k11n: // op vA, #+B
674 fprintf(gOutFile, " v%d, #int %d // #%x",
675 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB());
676 break;
677 case Instruction::k11x: // op vAA
678 fprintf(gOutFile, " v%d", pDecInsn->VRegA());
679 break;
680 case Instruction::k10t: // op +AA
681 case Instruction::k20t: // op +AAAA
682 {
683 const s4 targ = (s4) pDecInsn->VRegA();
684 fprintf(gOutFile, " %04x // %c%04x",
685 insnIdx + targ,
686 (targ < 0) ? '-' : '+',
687 (targ < 0) ? -targ : targ);
688 }
689 break;
690 case Instruction::k22x: // op vAA, vBBBB
691 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
692 break;
693 case Instruction::k21t: // op vAA, +BBBB
694 {
695 const s4 targ = (s4) pDecInsn->VRegB();
696 fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(),
697 insnIdx + targ,
698 (targ < 0) ? '-' : '+',
699 (targ < 0) ? -targ : targ);
700 }
701 break;
702 case Instruction::k21s: // op vAA, #+BBBB
703 fprintf(gOutFile, " v%d, #int %d // #%x",
704 pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB());
705 break;
706 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
707 // The printed format varies a bit based on the actual opcode.
708 if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) {
709 const s4 value = pDecInsn->VRegB() << 16;
710 fprintf(gOutFile, " v%d, #int %d // #%x",
711 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
712 } else {
713 const s8 value = ((s8) pDecInsn->VRegB()) << 48;
714 fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x",
715 pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB());
716 }
717 break;
718 case Instruction::k21c: // op vAA, thing@BBBB
719 case Instruction::k31c: // op vAA, thing@BBBBBBBB
720 fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf);
721 break;
722 case Instruction::k23x: // op vAA, vBB, vCC
723 fprintf(gOutFile, " v%d, v%d, v%d",
724 pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC());
725 break;
726 case Instruction::k22b: // op vAA, vBB, #+CC
727 fprintf(gOutFile, " v%d, v%d, #int %d // #%02x",
728 pDecInsn->VRegA(), pDecInsn->VRegB(),
729 (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC());
730 break;
731 case Instruction::k22t: // op vA, vB, +CCCC
732 {
733 const s4 targ = (s4) pDecInsn->VRegC();
734 fprintf(gOutFile, " v%d, v%d, %04x // %c%04x",
735 pDecInsn->VRegA(), pDecInsn->VRegB(),
736 insnIdx + targ,
737 (targ < 0) ? '-' : '+',
738 (targ < 0) ? -targ : targ);
739 }
740 break;
741 case Instruction::k22s: // op vA, vB, #+CCCC
742 fprintf(gOutFile, " v%d, v%d, #int %d // #%04x",
743 pDecInsn->VRegA(), pDecInsn->VRegB(),
744 (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC());
745 break;
746 case Instruction::k22c: // op vA, vB, thing@CCCC
747 // NOT SUPPORTED:
748 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
749 fprintf(gOutFile, " v%d, v%d, %s",
750 pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf);
751 break;
752 case Instruction::k30t:
753 fprintf(gOutFile, " #%08x", pDecInsn->VRegA());
754 break;
755 case Instruction::k31i: // op vAA, #+BBBBBBBB
756 {
757 // This is often, but not always, a float.
758 union {
759 float f;
760 u4 i;
761 } conv;
762 conv.i = pDecInsn->VRegB();
763 fprintf(gOutFile, " v%d, #float %f // #%08x",
764 pDecInsn->VRegA(), conv.f, pDecInsn->VRegB());
765 }
766 break;
767 case Instruction::k31t: // op vAA, offset +BBBBBBBB
768 fprintf(gOutFile, " v%d, %08x // +%08x",
769 pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB());
770 break;
771 case Instruction::k32x: // op vAAAA, vBBBB
772 fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB());
773 break;
774 case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB
775 // NOT SUPPORTED:
776 // case Instruction::k35ms: // [opt] invoke-virtual+super
777 // case Instruction::k35mi: // [opt] inline invoke
778 {
Aart Bika3bb7202015-10-26 17:24:09 -0700779 u4 arg[Instruction::kMaxVarArgRegs];
Aart Bik69ae54a2015-07-01 14:52:26 -0700780 pDecInsn->GetVarArgs(arg);
781 fputs(" {", gOutFile);
782 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
783 if (i == 0) {
784 fprintf(gOutFile, "v%d", arg[i]);
785 } else {
786 fprintf(gOutFile, ", v%d", arg[i]);
787 }
788 } // for
789 fprintf(gOutFile, "}, %s", indexBuf);
790 }
791 break;
Aart Bika3bb7202015-10-26 17:24:09 -0700792 case Instruction::k25x: // op vC, {vD, vE, vF, vG} (B: count)
793 {
794 u4 arg[Instruction::kMaxVarArgRegs25x];
795 pDecInsn->GetAllArgs25x(arg);
796 fprintf(gOutFile, " v%d, {", arg[0]);
797 for (int i = 0, n = pDecInsn->VRegB(); i < n; i++) {
798 if (i == 0) {
799 fprintf(gOutFile, "v%d", arg[Instruction::kLambdaVirtualRegisterWidth + i]);
800 } else {
801 fprintf(gOutFile, ", v%d", arg[Instruction::kLambdaVirtualRegisterWidth + i]);
802 }
803 } // for
804 fputc('}', gOutFile);
805 }
806 break;
Aart Bik69ae54a2015-07-01 14:52:26 -0700807 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
808 // NOT SUPPORTED:
809 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
810 // case Instruction::k3rmi: // [opt] execute-inline/range
811 {
812 // This doesn't match the "dx" output when some of the args are
813 // 64-bit values -- dx only shows the first register.
814 fputs(" {", gOutFile);
815 for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) {
816 if (i == 0) {
817 fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i);
818 } else {
819 fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i);
820 }
821 } // for
822 fprintf(gOutFile, "}, %s", indexBuf);
823 }
824 break;
825 case Instruction::k51l: // op vAA, #+BBBBBBBBBBBBBBBB
826 {
827 // This is often, but not always, a double.
828 union {
829 double d;
830 u8 j;
831 } conv;
832 conv.j = pDecInsn->WideVRegB();
833 fprintf(gOutFile, " v%d, #double %f // #%016" PRIx64,
834 pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB());
835 }
836 break;
837 // NOT SUPPORTED:
838 // case Instruction::k00x: // unknown op or breakpoint
839 // break;
840 default:
841 fprintf(gOutFile, " ???");
842 break;
843 } // switch
844
845 fputc('\n', gOutFile);
846
847 if (indexBuf != indexBufChars) {
848 free(indexBuf);
849 }
850}
851
852/*
853 * Dumps a bytecode disassembly.
854 */
855static void dumpBytecodes(const DexFile* pDexFile, u4 idx,
856 const DexFile::CodeItem* pCode, u4 codeOffset) {
857 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
858 const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_);
859 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
860 const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
861
862 // Generate header.
863 char* tmp = descriptorToDot(backDescriptor);
864 fprintf(gOutFile, "%06x: "
865 "|[%06x] %s.%s:%s\n",
866 codeOffset, codeOffset, tmp, name, signature.ToString().c_str());
867 free(tmp);
868
869 // Iterate over all instructions.
870 const u2* insns = pCode->insns_;
871 for (u4 insnIdx = 0; insnIdx < pCode->insns_size_in_code_units_;) {
872 const Instruction* instruction = Instruction::At(&insns[insnIdx]);
873 const u4 insnWidth = instruction->SizeInCodeUnits();
874 if (insnWidth == 0) {
875 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx);
876 break;
877 }
878 dumpInstruction(pDexFile, pCode, codeOffset, insnIdx, insnWidth, instruction);
879 insnIdx += insnWidth;
880 } // for
881}
882
883/*
884 * Dumps code of a method.
885 */
886static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags,
887 const DexFile::CodeItem* pCode, u4 codeOffset) {
888 fprintf(gOutFile, " registers : %d\n", pCode->registers_size_);
889 fprintf(gOutFile, " ins : %d\n", pCode->ins_size_);
890 fprintf(gOutFile, " outs : %d\n", pCode->outs_size_);
891 fprintf(gOutFile, " insns size : %d 16-bit code units\n",
892 pCode->insns_size_in_code_units_);
893
894 // Bytecode disassembly, if requested.
895 if (gOptions.disassemble) {
896 dumpBytecodes(pDexFile, idx, pCode, codeOffset);
897 }
898
899 // Try-catch blocks.
900 dumpCatches(pDexFile, pCode);
901
902 // Positions and locals table in the debug info.
903 bool is_static = (flags & kAccStatic) != 0;
904 fprintf(gOutFile, " positions : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +0000905 pDexFile->DecodeDebugPositionInfo(pCode, dumpPositionsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -0700906 fprintf(gOutFile, " locals : \n");
David Srbeckyb06e28e2015-12-10 13:15:00 +0000907 pDexFile->DecodeDebugLocalInfo(pCode, is_static, idx, dumpLocalsCb, nullptr);
Aart Bik69ae54a2015-07-01 14:52:26 -0700908}
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/*
1021 * Dumps a static (class) field.
1022 */
1023static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
1024 // Bail for anything private if export only requested.
1025 if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) {
1026 return;
1027 }
1028
1029 const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(idx);
1030 const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_);
1031 const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_);
1032 const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_);
1033 char* accessStr = createAccessFlagStr(flags, kAccessForField);
1034
1035 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1036 fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor);
1037 fprintf(gOutFile, " name : '%s'\n", name);
1038 fprintf(gOutFile, " type : '%s'\n", typeDescriptor);
1039 fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr);
1040 } else if (gOptions.outputFormat == OUTPUT_XML) {
1041 fprintf(gOutFile, "<field name=\"%s\"\n", name);
1042 char *tmp = descriptorToDot(typeDescriptor);
1043 fprintf(gOutFile, " type=\"%s\"\n", tmp);
1044 free(tmp);
1045 fprintf(gOutFile, " transient=%s\n", quotedBool((flags & kAccTransient) != 0));
1046 fprintf(gOutFile, " volatile=%s\n", quotedBool((flags & kAccVolatile) != 0));
1047 // The "value=" is not knowable w/o parsing annotations.
1048 fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0));
1049 fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0));
1050 // The "deprecated=" is not knowable w/o parsing annotations.
1051 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(flags));
1052 fprintf(gOutFile, ">\n</field>\n");
1053 }
1054
1055 free(accessStr);
1056}
1057
1058/*
1059 * Dumps an instance field.
1060 */
1061static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) {
1062 dumpSField(pDexFile, idx, flags, i);
1063}
1064
1065/*
Andreas Gampe5073fed2015-08-10 11:40:25 -07001066 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1067 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1068 * tool, so this is not performance-critical.
1069 */
1070
1071static void dumpCfg(const DexFile* dex_file,
1072 uint32_t dex_method_idx,
1073 const DexFile::CodeItem* code_item) {
1074 if (code_item != nullptr) {
1075 std::ostringstream oss;
1076 DumpMethodCFG(dex_file, dex_method_idx, oss);
1077 fprintf(gOutFile, "%s", oss.str().c_str());
1078 }
1079}
1080
1081static void dumpCfg(const DexFile* dex_file, int idx) {
1082 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
1083 const uint8_t* class_data = dex_file->GetClassData(class_def);
1084 if (class_data == nullptr) { // empty class such as a marker interface?
1085 return;
1086 }
1087 ClassDataItemIterator it(*dex_file, class_data);
1088 while (it.HasNextStaticField()) {
1089 it.Next();
1090 }
1091 while (it.HasNextInstanceField()) {
1092 it.Next();
1093 }
1094 while (it.HasNextDirectMethod()) {
1095 dumpCfg(dex_file,
1096 it.GetMemberIndex(),
1097 it.GetMethodCodeItem());
1098 it.Next();
1099 }
1100 while (it.HasNextVirtualMethod()) {
1101 dumpCfg(dex_file,
1102 it.GetMemberIndex(),
1103 it.GetMethodCodeItem());
1104 it.Next();
1105 }
1106}
1107
1108/*
Aart Bik69ae54a2015-07-01 14:52:26 -07001109 * Dumps the class.
1110 *
1111 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1112 *
1113 * If "*pLastPackage" is nullptr or does not match the current class' package,
1114 * the value will be replaced with a newly-allocated string.
1115 */
1116static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) {
1117 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
1118
1119 // Omitting non-public class.
1120 if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) {
1121 return;
1122 }
1123
Andreas Gampe5073fed2015-08-10 11:40:25 -07001124 if (gOptions.cfg) {
1125 dumpCfg(pDexFile, idx);
1126 return;
1127 }
1128
Aart Bik69ae54a2015-07-01 14:52:26 -07001129 // For the XML output, show the package name. Ideally we'd gather
1130 // up the classes, sort them, and dump them alphabetically so the
1131 // package name wouldn't jump around, but that's not a great plan
1132 // for something that needs to run on the device.
1133 const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_);
1134 if (!(classDescriptor[0] == 'L' &&
1135 classDescriptor[strlen(classDescriptor)-1] == ';')) {
1136 // Arrays and primitives should not be defined explicitly. Keep going?
1137 fprintf(stderr, "Malformed class name '%s'\n", classDescriptor);
1138 } else if (gOptions.outputFormat == OUTPUT_XML) {
1139 char* mangle = strdup(classDescriptor + 1);
1140 mangle[strlen(mangle)-1] = '\0';
1141
1142 // Reduce to just the package name.
1143 char* lastSlash = strrchr(mangle, '/');
1144 if (lastSlash != nullptr) {
1145 *lastSlash = '\0';
1146 } else {
1147 *mangle = '\0';
1148 }
1149
1150 for (char* cp = mangle; *cp != '\0'; cp++) {
1151 if (*cp == '/') {
1152 *cp = '.';
1153 }
1154 } // for
1155
1156 if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) {
1157 // Start of a new package.
1158 if (*pLastPackage != nullptr) {
1159 fprintf(gOutFile, "</package>\n");
1160 }
1161 fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle);
1162 free(*pLastPackage);
1163 *pLastPackage = mangle;
1164 } else {
1165 free(mangle);
1166 }
1167 }
1168
1169 // General class information.
1170 char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass);
1171 const char* superclassDescriptor;
1172 if (pClassDef.superclass_idx_ == DexFile::kDexNoIndex16) {
1173 superclassDescriptor = nullptr;
1174 } else {
1175 superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_);
1176 }
1177 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1178 fprintf(gOutFile, "Class #%d -\n", idx);
1179 fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor);
1180 fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr);
1181 if (superclassDescriptor != nullptr) {
1182 fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor);
1183 }
1184 fprintf(gOutFile, " Interfaces -\n");
1185 } else {
1186 char* tmp = descriptorClassToDot(classDescriptor);
1187 fprintf(gOutFile, "<class name=\"%s\"\n", tmp);
1188 free(tmp);
1189 if (superclassDescriptor != nullptr) {
1190 tmp = descriptorToDot(superclassDescriptor);
1191 fprintf(gOutFile, " extends=\"%s\"\n", tmp);
1192 free(tmp);
1193 }
1194 fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0));
1195 fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0));
1196 fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0));
1197 // The "deprecated=" not knowable w/o parsing annotations.
1198 fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_));
1199 fprintf(gOutFile, ">\n");
1200 }
1201
1202 // Interfaces.
1203 const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef);
1204 if (pInterfaces != nullptr) {
1205 for (u4 i = 0; i < pInterfaces->Size(); i++) {
1206 dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i);
1207 } // for
1208 }
1209
1210 // Fields and methods.
1211 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
1212 if (pEncodedData == nullptr) {
1213 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1214 fprintf(gOutFile, " Static fields -\n");
1215 fprintf(gOutFile, " Instance fields -\n");
1216 fprintf(gOutFile, " Direct methods -\n");
1217 fprintf(gOutFile, " Virtual methods -\n");
1218 }
1219 } else {
1220 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
1221 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1222 fprintf(gOutFile, " Static fields -\n");
1223 }
1224 for (int i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) {
1225 dumpSField(pDexFile, pClassData.GetMemberIndex(),
1226 pClassData.GetRawMemberAccessFlags(), i);
1227 } // for
1228 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1229 fprintf(gOutFile, " Instance fields -\n");
1230 }
1231 for (int i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) {
1232 dumpIField(pDexFile, pClassData.GetMemberIndex(),
1233 pClassData.GetRawMemberAccessFlags(), i);
1234 } // for
1235 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1236 fprintf(gOutFile, " Direct methods -\n");
1237 }
1238 for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) {
1239 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1240 pClassData.GetRawMemberAccessFlags(),
1241 pClassData.GetMethodCodeItem(),
1242 pClassData.GetMethodCodeItemOffset(), i);
1243 } // for
1244 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1245 fprintf(gOutFile, " Virtual methods -\n");
1246 }
1247 for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) {
1248 dumpMethod(pDexFile, pClassData.GetMemberIndex(),
1249 pClassData.GetRawMemberAccessFlags(),
1250 pClassData.GetMethodCodeItem(),
1251 pClassData.GetMethodCodeItemOffset(), i);
1252 } // for
1253 }
1254
1255 // End of class.
1256 if (gOptions.outputFormat == OUTPUT_PLAIN) {
1257 const char* fileName;
1258 if (pClassDef.source_file_idx_ != DexFile::kDexNoIndex) {
1259 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
1260 } else {
1261 fileName = "unknown";
1262 }
1263 fprintf(gOutFile, " source_file_idx : %d (%s)\n\n",
1264 pClassDef.source_file_idx_, fileName);
1265 } else if (gOptions.outputFormat == OUTPUT_XML) {
1266 fprintf(gOutFile, "</class>\n");
1267 }
1268
1269 free(accessStr);
1270}
1271
1272/*
1273 * Dumps the requested sections of the file.
1274 */
1275static void processDexFile(const char* fileName, const DexFile* pDexFile) {
1276 if (gOptions.verbose) {
1277 fprintf(gOutFile, "Opened '%s', DEX version '%.3s'\n",
1278 fileName, pDexFile->GetHeader().magic_ + 4);
1279 }
1280
1281 // Headers.
1282 if (gOptions.showFileHeaders) {
1283 dumpFileHeader(pDexFile);
1284 }
1285
1286 // Open XML context.
1287 if (gOptions.outputFormat == OUTPUT_XML) {
1288 fprintf(gOutFile, "<api>\n");
1289 }
1290
1291 // Iterate over all classes.
1292 char* package = nullptr;
1293 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
1294 for (u4 i = 0; i < classDefsSize; i++) {
1295 if (gOptions.showSectionHeaders) {
1296 dumpClassDef(pDexFile, i);
1297 }
1298 dumpClass(pDexFile, i, &package);
1299 } // for
1300
1301 // Free the last package allocated.
1302 if (package != nullptr) {
1303 fprintf(gOutFile, "</package>\n");
1304 free(package);
1305 }
1306
1307 // Close XML context.
1308 if (gOptions.outputFormat == OUTPUT_XML) {
1309 fprintf(gOutFile, "</api>\n");
1310 }
1311}
1312
1313/*
1314 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1315 */
1316int processFile(const char* fileName) {
1317 if (gOptions.verbose) {
1318 fprintf(gOutFile, "Processing '%s'...\n", fileName);
1319 }
1320
1321 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1322 // all of which are Zip archives with "classes.dex" inside. The compressed
1323 // data needs to be extracted to a temp file, the location of which varies.
1324 //
1325 // TODO(ajcbik): fix following issues
1326 //
1327 // (1) gOptions.tempFileName is not accounted for
1328 // (2) gOptions.ignoreBadChecksum is not accounted for
1329 //
1330 std::string error_msg;
1331 std::vector<std::unique_ptr<const DexFile>> dex_files;
1332 if (!DexFile::Open(fileName, fileName, &error_msg, &dex_files)) {
1333 // Display returned error message to user. Note that this error behavior
1334 // differs from the error messages shown by the original Dalvik dexdump.
1335 fputs(error_msg.c_str(), stderr);
1336 fputc('\n', stderr);
1337 return -1;
1338 }
1339
Aart Bik4e149602015-07-09 11:45:28 -07001340 // Success. Either report checksum verification or process
1341 // all dex files found in given file.
Aart Bik69ae54a2015-07-01 14:52:26 -07001342 if (gOptions.checksumOnly) {
1343 fprintf(gOutFile, "Checksum verified\n");
1344 } else {
Aart Bik4e149602015-07-09 11:45:28 -07001345 for (size_t i = 0; i < dex_files.size(); i++) {
1346 processDexFile(fileName, dex_files[i].get());
1347 }
Aart Bik69ae54a2015-07-01 14:52:26 -07001348 }
1349 return 0;
1350}
1351
1352} // namespace art