2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * The "dexdump" tool is intended to mimic "objdump". When possible, use
18 * similar command-line arguments.
20 * TODO: rework the output format to be more regexp-friendly
22 #include "libdex/DexFile.h"
23 #include "libdex/DexCatch.h"
24 #include "libdex/DexClass.h"
25 #include "libdex/DexProto.h"
26 #include "libdex/InstrUtils.h"
27 #include "libdex/SysUtil.h"
28 #include "libdex/CmdUtils.h"
30 #include "dexdump/OpCodeNames.h"
41 static const char* gProgName = "dexdump";
43 static InstructionWidth* gInstrWidth;
44 static InstructionFormat* gInstrFormat;
46 /* command-line options */
50 bool showSectionHeaders;
51 const char* tempFileName;
54 /* basic info about a field or method */
55 typedef struct FieldMethodInfo {
56 const char* classDescriptor;
58 const char* signature;
62 * Get 2 little-endian bytes.
64 static inline u2 get2LE(unsigned char const* pSrc)
66 return pSrc[0] | (pSrc[1] << 8);
70 * Return a newly-allocated string for the "dot version" of the class
71 * name for the given type descriptor. That is, The initial "L" and
72 * final ";" (if any) have been removed and all occurrences of '/'
73 * have been changed to '.'.
75 static char* descriptorToDot(const char* str)
77 size_t at = strlen(str);
81 assert(str[at - 1] == ';');
82 at -= 2; /* Two fewer chars to copy. */
83 str++; /* Skip the 'L'. */
86 newStr = malloc(at + 1); /* Add one for the '\0'. */
91 newStr[at] = (str[at] == '/') ? '.' : str[at];
98 * Count the number of '1' bits in a word.
100 * Having completed this, I'm ready for an interview at Google.
102 * TODO? there's a parallel version w/o loops. Performance not currently
105 static int countOnes(u4 val)
119 * Flag for use with createAccessFlagStr().
121 typedef enum AccessFor {
122 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2,
127 * Create a new string with human-readable access flags.
129 * In the base language the access_flags fields are type u2; in Dalvik
132 static char* createAccessFlagStr(u4 flags, AccessFor forWhat)
135 static const char* kAccessStrings[kAccessForMAX][NUM_FLAGS] = {
137 /* class, inner class */
138 "PUBLIC", /* 0x0001 */
139 "PRIVATE", /* 0x0002 */
140 "PROTECTED", /* 0x0004 */
141 "STATIC", /* 0x0008 */
142 "FINAL", /* 0x0010 */
147 "INTERFACE", /* 0x0200 */
148 "ABSTRACT", /* 0x0400 */
150 "SYNTHETIC", /* 0x1000 */
151 "ANNOTATION", /* 0x2000 */
154 "VERIFIED", /* 0x10000 */
155 "OPTIMIZED", /* 0x20000 */
159 "PUBLIC", /* 0x0001 */
160 "PRIVATE", /* 0x0002 */
161 "PROTECTED", /* 0x0004 */
162 "STATIC", /* 0x0008 */
163 "FINAL", /* 0x0010 */
164 "SYNCHRONIZED", /* 0x0020 */
165 "BRIDGE", /* 0x0040 */
166 "VARARGS", /* 0x0080 */
167 "NATIVE", /* 0x0100 */
169 "ABSTRACT", /* 0x0400 */
170 "STRICT", /* 0x0800 */
171 "SYNTHETIC", /* 0x1000 */
174 "MIRANDA", /* 0x8000 */
175 "CONSTRUCTOR", /* 0x10000 */
176 "DECLARED_SYNCHRONIZED", /* 0x20000 */
180 "PUBLIC", /* 0x0001 */
181 "PRIVATE", /* 0x0002 */
182 "PROTECTED", /* 0x0004 */
183 "STATIC", /* 0x0008 */
184 "FINAL", /* 0x0010 */
186 "VOLATILE", /* 0x0040 */
187 "TRANSIENT", /* 0x0080 */
192 "SYNTHETIC", /* 0x1000 */
200 const int kLongest = 21; /* strlen of longest string above */
206 * Allocate enough storage to hold the expected number of strings,
207 * plus a space between each. We over-allocate, using the longest
208 * string above as the base metric.
210 count = countOnes(flags);
211 cp = str = (char*) malloc(count * (kLongest+1) +1);
213 for (i = 0; i < NUM_FLAGS; i++) {
215 const char* accessStr = kAccessStrings[forWhat][i];
216 int len = strlen(accessStr);
220 memcpy(cp, accessStr, len);
232 * Dump the file header.
234 void dumpFileHeader(const DexFile* pDexFile)
236 const DexHeader* pHeader = pDexFile->pHeader;
238 printf("DEX file header:\n");
239 printf("magic : '%.8s'\n", pHeader->magic);
240 printf("checksum : %08x\n", pHeader->checksum);
241 printf("signature : %02x%02x...%02x%02x\n",
242 pHeader->signature[0], pHeader->signature[1],
243 pHeader->signature[kSHA1DigestLen-2],
244 pHeader->signature[kSHA1DigestLen-1]);
245 printf("file_size : %d\n", pHeader->fileSize);
246 printf("header_size : %d\n", pHeader->headerSize);
247 printf("link_size : %d\n", pHeader->linkSize);
248 printf("link_off : %d (0x%06x)\n",
249 pHeader->linkOff, pHeader->linkOff);
250 printf("string_ids_size : %d\n", pHeader->stringIdsSize);
251 printf("string_ids_off : %d (0x%06x)\n",
252 pHeader->stringIdsOff, pHeader->stringIdsOff);
253 printf("type_ids_size : %d\n", pHeader->typeIdsSize);
254 printf("type_ids_off : %d (0x%06x)\n",
255 pHeader->typeIdsOff, pHeader->typeIdsOff);
256 printf("field_ids_size : %d\n", pHeader->fieldIdsSize);
257 printf("field_ids_off : %d (0x%06x)\n",
258 pHeader->fieldIdsOff, pHeader->fieldIdsOff);
259 printf("method_ids_size : %d\n", pHeader->methodIdsSize);
260 printf("method_ids_off : %d (0x%06x)\n",
261 pHeader->methodIdsOff, pHeader->methodIdsOff);
262 printf("class_defs_size : %d\n", pHeader->classDefsSize);
263 printf("class_defs_off : %d (0x%06x)\n",
264 pHeader->classDefsOff, pHeader->classDefsOff);
265 printf("data_size : %d\n", pHeader->dataSize);
266 printf("data_off : %d (0x%06x)\n",
267 pHeader->dataOff, pHeader->dataOff);
272 * Dump a class_def_item.
274 void dumpClassDef(DexFile* pDexFile, int idx)
276 const DexClassDef* pClassDef;
277 const u1* pEncodedData;
278 DexClassData* pClassData;
280 pClassDef = dexGetClassDef(pDexFile, idx);
281 pEncodedData = dexGetClassData(pDexFile, pClassDef);
282 pClassData = dexReadAndVerifyClassData(&pEncodedData, NULL);
284 if (pClassData == NULL) {
285 fprintf(stderr, "Trouble reading class data\n");
289 printf("Class #%d header:\n", idx);
290 printf("class_idx : %d\n", pClassDef->classIdx);
291 printf("access_flags : %d (0x%04x)\n",
292 pClassDef->accessFlags, pClassDef->accessFlags);
293 printf("superclass_idx : %d\n", pClassDef->superclassIdx);
294 printf("interfaces_off : %d (0x%06x)\n",
295 pClassDef->interfacesOff, pClassDef->interfacesOff);
296 printf("source_file_idx : %d\n", pClassDef->sourceFileIdx);
297 printf("annotations_off : %d (0x%06x)\n",
298 pClassDef->annotationsOff, pClassDef->annotationsOff);
299 printf("class_data_off : %d (0x%06x)\n",
300 pClassDef->classDataOff, pClassDef->classDataOff);
301 printf("static_fields_size : %d\n", pClassData->header.staticFieldsSize);
302 printf("instance_fields_size: %d\n",
303 pClassData->header.instanceFieldsSize);
304 printf("direct_methods_size : %d\n", pClassData->header.directMethodsSize);
305 printf("virtual_methods_size: %d\n",
306 pClassData->header.virtualMethodsSize);
315 void dumpInterface(const DexFile* pDexFile, const DexTypeItem* pTypeItem,
318 const char* interfaceName =
319 dexStringByTypeIdx(pDexFile, pTypeItem->typeIdx);
321 printf(" #%d : '%s'\n", i, interfaceName);
325 * Dump the catches table associated with the code.
327 void dumpCatches(DexFile* pDexFile, const DexCode* pCode)
329 u4 triesSize = pCode->triesSize;
331 if (triesSize == 0) {
332 printf(" catches : (none)\n");
336 printf(" catches : %d\n", triesSize);
338 const DexTry* pTries = dexGetTries(pCode);
341 for (i = 0; i < triesSize; i++) {
342 const DexTry* pTry = &pTries[i];
343 u4 start = pTry->startAddr;
344 u4 end = start + pTry->insnCount;
345 DexCatchIterator iterator;
347 printf(" 0x%04x - 0x%04x\n", start, end);
349 dexCatchIteratorInit(&iterator, pCode, pTry->handlerOff);
352 DexCatchHandler* handler = dexCatchIteratorNext(&iterator);
353 const char* descriptor;
355 if (handler == NULL) {
359 descriptor = (handler->typeIdx == kDexNoIndex) ? "<any>" :
360 dexStringByTypeIdx(pDexFile, handler->typeIdx);
362 printf(" %s -> 0x%04x\n", descriptor,
368 static int dumpPositionsCb(void *cnxt, u4 address, u4 lineNum)
370 printf(" 0x%04x line=%d\n", address, lineNum);
375 * Dump the positions list.
377 void dumpPositions(DexFile* pDexFile, const DexCode* pCode,
378 const DexMethod *pDexMethod)
380 printf(" positions : \n");
381 const DexMethodId *pMethodId
382 = dexGetMethodId(pDexFile, pDexMethod->methodIdx);
383 const char *classDescriptor
384 = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
386 dexDecodeDebugInfo(pDexFile, pCode, classDescriptor, pMethodId->protoIdx,
387 pDexMethod->accessFlags, dumpPositionsCb, NULL, NULL);
390 static void dumpLocalsCb(void *cnxt, u2 reg, u4 startAddress,
391 u4 endAddress, const char *name, const char *descriptor,
392 const char *signature)
394 printf(" 0x%04x - 0x%04x reg=%d %s %s %s\n",
395 startAddress, endAddress, reg, name, descriptor,
400 * Dump the locals list.
402 void dumpLocals(DexFile* pDexFile, const DexCode* pCode,
403 const DexMethod *pDexMethod)
405 printf(" locals : \n");
407 const DexMethodId *pMethodId
408 = dexGetMethodId(pDexFile, pDexMethod->methodIdx);
409 const char *classDescriptor
410 = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
412 dexDecodeDebugInfo(pDexFile, pCode, classDescriptor, pMethodId->protoIdx,
413 pDexMethod->accessFlags, NULL, dumpLocalsCb, NULL);
417 * Get information about a method.
419 bool getMethodInfo(DexFile* pDexFile, u4 methodIdx, FieldMethodInfo* pMethInfo)
421 const DexMethodId* pMethodId;
423 if (methodIdx >= pDexFile->pHeader->methodIdsSize)
426 pMethodId = dexGetMethodId(pDexFile, methodIdx);
427 pMethInfo->name = dexStringById(pDexFile, pMethodId->nameIdx);
428 pMethInfo->signature = dexCopyDescriptorFromMethodId(pDexFile, pMethodId);
430 pMethInfo->classDescriptor =
431 dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
436 * Get information about a field.
438 bool getFieldInfo(DexFile* pDexFile, u4 fieldIdx, FieldMethodInfo* pFieldInfo)
440 const DexFieldId* pFieldId;
442 if (fieldIdx >= pDexFile->pHeader->fieldIdsSize)
445 pFieldId = dexGetFieldId(pDexFile, fieldIdx);
446 pFieldInfo->name = dexStringById(pDexFile, pFieldId->nameIdx);
447 pFieldInfo->signature = dexStringByTypeIdx(pDexFile, pFieldId->typeIdx);
448 pFieldInfo->classDescriptor =
449 dexStringByTypeIdx(pDexFile, pFieldId->classIdx);
455 * Look up a class' descriptor.
457 const char* getClassDescriptor(DexFile* pDexFile, u4 classIdx)
459 return dexStringByTypeIdx(pDexFile, classIdx);
463 * Dump a single instruction.
465 void dumpInstruction(DexFile* pDexFile, const DexCode* pCode, int insnIdx,
466 int insnWidth, const DecodedInstruction* pDecInsn)
468 static const float gSpecialTab[16] = {
469 -2.0f, -1.0f, -0.5f, -0.25f, -0.1f, 0.1f, 0.25f, 0.5f,
470 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 10.0f, 100.0f, 1000.0f
472 const u2* insns = pCode->insns;
475 printf("%06x:", ((u1*)insns - pDexFile->baseAddr) + insnIdx*2);
476 for (i = 0; i < 8; i++) {
481 /* print 16-bit value in little-endian order */
482 const u1* bytePtr = (const u1*) &insns[insnIdx+i];
483 printf(" %02x%02x", bytePtr[0], bytePtr[1]);
490 if (pDecInsn->opCode == OP_NOP) {
491 u2 instr = get2LE((const u1*) &insns[insnIdx]);
492 if (instr == kPackedSwitchSignature) {
493 printf("|%04x: packed-switch-data (%d units)",
495 } else if (instr == kSparseSwitchSignature) {
496 printf("|%04x: sparse-switch-data (%d units)",
498 } else if (instr == kArrayDataSignature) {
499 printf("|%04x: array-data (%d units)",
502 printf("|%04x: nop // spacer", insnIdx);
505 printf("|%04x: %s", insnIdx, getOpcodeName(pDecInsn->opCode));
508 switch (dexGetInstrFormat(gInstrFormat, pDecInsn->opCode)) {
511 case kFmt12x: // op vA, vB
512 printf(" v%d, v%d", pDecInsn->vA, pDecInsn->vB);
514 case kFmt11n: // op vA, #+B
515 printf(" v%d, #int %d // #%x",
516 pDecInsn->vA, (s4)pDecInsn->vB, (u1)pDecInsn->vB);
518 case kFmt11x: // op vAA
519 printf(" v%d", pDecInsn->vA);
521 case kFmt10t: // op +AA
522 case kFmt20t: // op +AAAA
524 s4 targ = (s4) pDecInsn->vA;
525 printf(" %04x // %c%04x",
527 (targ < 0) ? '-' : '+',
528 (targ < 0) ? -targ : targ);
531 case kFmt22x: // op vAA, vBBBB
532 printf(" v%d, v%d", pDecInsn->vA, pDecInsn->vB);
534 case kFmt21t: // op vAA, +BBBB
536 s4 targ = (s4) pDecInsn->vB;
537 printf(" v%d, %04x // %c%04x", pDecInsn->vA,
539 (targ < 0) ? '-' : '+',
540 (targ < 0) ? -targ : targ);
543 case kFmt21s: // op vAA, #+BBBB
544 printf(" v%d, #int %d // #%x",
545 pDecInsn->vA, (s4)pDecInsn->vB, (u2)pDecInsn->vB);
547 case kFmt21h: // op vAA, #+BBBB0000[00000000]
548 // The printed format varies a bit based on the actual opcode.
549 if (pDecInsn->opCode == OP_CONST_HIGH16) {
550 s4 value = pDecInsn->vB << 16;
551 printf(" v%d, #int %d // #%x",
552 pDecInsn->vA, value, (u2)pDecInsn->vB);
554 s8 value = ((s8) pDecInsn->vB) << 48;
555 printf(" v%d, #long %lld // #%x",
556 pDecInsn->vA, value, (u2)pDecInsn->vB);
559 case kFmt21c: // op vAA, thing@BBBB
560 if (pDecInsn->opCode == OP_CONST_STRING) {
561 printf(" v%d, \"%s\" // string@%04x", pDecInsn->vA,
562 dexStringById(pDexFile, pDecInsn->vB), pDecInsn->vB);
563 } else if (pDecInsn->opCode == OP_CHECK_CAST ||
564 pDecInsn->opCode == OP_NEW_INSTANCE ||
565 pDecInsn->opCode == OP_CONST_CLASS)
567 printf(" v%d, %s // class@%04x", pDecInsn->vA,
568 getClassDescriptor(pDexFile, pDecInsn->vB), pDecInsn->vB);
569 } else /* OP_SGET* */ {
570 FieldMethodInfo fieldInfo;
571 if (getFieldInfo(pDexFile, pDecInsn->vB, &fieldInfo)) {
572 printf(" v%d, %s.%s:%s // field@%04x", pDecInsn->vA,
573 fieldInfo.classDescriptor, fieldInfo.name,
574 fieldInfo.signature, pDecInsn->vB);
576 printf(" v%d, ??? // field@%04x", pDecInsn->vA, pDecInsn->vB);
580 case kFmt23x: // op vAA, vBB, vCC
581 printf(" v%d, v%d, v%d", pDecInsn->vA, pDecInsn->vB, pDecInsn->vC);
583 case kFmt22b: // op vAA, vBB, #+CC
584 printf(" v%d, v%d, #int %d // #%02x",
585 pDecInsn->vA, pDecInsn->vB, (s4)pDecInsn->vC, (u1)pDecInsn->vC);
587 case kFmt22t: // op vA, vB, +CCCC
589 s4 targ = (s4) pDecInsn->vC;
590 printf(" v%d, v%d, %04x // %c%04x", pDecInsn->vA, pDecInsn->vB,
592 (targ < 0) ? '-' : '+',
593 (targ < 0) ? -targ : targ);
596 case kFmt22s: // op vA, vB, #+CCCC
597 printf(" v%d, v%d, #int %d // #%04x",
598 pDecInsn->vA, pDecInsn->vB, (s4)pDecInsn->vC, (u2)pDecInsn->vC);
600 case kFmt22c: // op vA, vB, thing@CCCC
601 if (pDecInsn->opCode >= OP_IGET && pDecInsn->opCode <= OP_IPUT_SHORT) {
602 FieldMethodInfo fieldInfo;
603 if (getFieldInfo(pDexFile, pDecInsn->vC, &fieldInfo)) {
604 printf(" v%d, v%d, %s.%s:%s // field@%04x", pDecInsn->vA,
605 pDecInsn->vB, fieldInfo.classDescriptor, fieldInfo.name,
606 fieldInfo.signature, pDecInsn->vC);
608 printf(" v%d, v%d, ??? // field@%04x", pDecInsn->vA,
609 pDecInsn->vB, pDecInsn->vC);
612 printf(" v%d, v%d, %s // class@%04x",
613 pDecInsn->vA, pDecInsn->vB,
614 getClassDescriptor(pDexFile, pDecInsn->vC), pDecInsn->vC);
617 case kFmt22cs: // [opt] op vA, vB, field offset CCCC
618 printf(" v%d, v%d, [obj+%04x]",
619 pDecInsn->vA, pDecInsn->vB, pDecInsn->vC);
622 printf(" #%08x", pDecInsn->vA);
624 case kFmt31i: // op vAA, #+BBBBBBBB
626 /* this is often, but not always, a float */
631 conv.i = pDecInsn->vB;
632 printf(" v%d, #float %f // #%08x",
633 pDecInsn->vA, conv.f, pDecInsn->vB);
636 case kFmt31c: // op vAA, thing@BBBBBBBB
637 printf(" v%d, \"%s\" // string@%08x", pDecInsn->vA,
638 dexStringById(pDexFile, pDecInsn->vB), pDecInsn->vB);
640 case kFmt31t: // op vAA, offset +BBBBBBBB
641 printf(" v%d, %08x // +%08x",
642 pDecInsn->vA, insnIdx + pDecInsn->vB, pDecInsn->vB);
644 case kFmt32x: // op vAAAA, vBBBB
645 printf(" v%d, v%d", pDecInsn->vA, pDecInsn->vB);
647 case kFmt35c: // op vB, {vD, vE, vF, vG, vA}, thing@CCCC
649 /* NOTE: decoding of 35c doesn't quite match spec */
651 for (i = 0; i < (int) pDecInsn->vA; i++) {
653 printf("v%d", pDecInsn->arg[i]);
655 printf(", v%d", pDecInsn->arg[i]);
657 if (pDecInsn->opCode == OP_FILLED_NEW_ARRAY) {
658 printf("}, %s // class@%04x",
659 getClassDescriptor(pDexFile, pDecInsn->vB), pDecInsn->vB);
661 FieldMethodInfo methInfo;
662 if (getMethodInfo(pDexFile, pDecInsn->vB, &methInfo)) {
663 printf("}, %s.%s:%s // method@%04x",
664 methInfo.classDescriptor, methInfo.name,
665 methInfo.signature, pDecInsn->vB);
667 printf("}, ??? // method@%04x", pDecInsn->vB);
672 case kFmt35ms: // [opt] invoke-virtual+super
673 case kFmt35fs: // [opt] invoke-interface
676 for (i = 0; i < (int) pDecInsn->vA; i++) {
678 printf("v%d", pDecInsn->arg[i]);
680 printf(", v%d", pDecInsn->arg[i]);
682 printf("}, [%04x] // vtable #%04x", pDecInsn->vB, pDecInsn->vB);
685 case kFmt3rc: // op {vCCCC .. v(CCCC+AA-1)}, meth@BBBB
688 * This doesn't match the "dx" output when some of the args are
689 * 64-bit values -- dx only shows the first register.
692 for (i = 0; i < (int) pDecInsn->vA; i++) {
694 printf("v%d", pDecInsn->vC + i);
696 printf(", v%d", pDecInsn->vC + i);
698 if (pDecInsn->opCode == OP_FILLED_NEW_ARRAY_RANGE) {
699 printf("}, %s // class@%04x",
700 getClassDescriptor(pDexFile, pDecInsn->vB), pDecInsn->vB);
702 FieldMethodInfo methInfo;
703 if (getMethodInfo(pDexFile, pDecInsn->vB, &methInfo)) {
704 printf("}, %s.%s:%s // method@%04x",
705 methInfo.classDescriptor, methInfo.name,
706 methInfo.signature, pDecInsn->vB);
708 printf("}, ??? // method@%04x", pDecInsn->vB);
713 case kFmt3rms: // [opt] invoke-virtual+super/range
714 case kFmt3rfs: // [opt] invoke-interface/range
717 * This doesn't match the "dx" output when some of the args are
718 * 64-bit values -- dx only shows the first register.
721 for (i = 0; i < (int) pDecInsn->vA; i++) {
723 printf("v%d", pDecInsn->vC + i);
725 printf(", v%d", pDecInsn->vC + i);
727 printf("}, [%04x] // vtable #%04x", pDecInsn->vB, pDecInsn->vB);
730 case kFmt3inline: // [opt] inline invoke
733 const InlineOperation* inlineOpsTable = dvmGetInlineOpsTable();
734 u4 tableLen = dvmGetInlineOpsTableLength();
738 for (i = 0; i < (int) pDecInsn->vA; i++) {
740 printf("v%d", pDecInsn->arg[i]);
742 printf(", v%d", pDecInsn->arg[i]);
745 if (pDecInsn->vB < tableLen) {
746 printf("}, %s.%s:%s // inline #%04x",
747 inlineOpsTable[pDecInsn->vB].classDescriptor,
748 inlineOpsTable[pDecInsn->vB].methodName,
749 inlineOpsTable[pDecInsn->vB].methodSignature,
753 printf("}, [%04x] // inline #%04x", pDecInsn->vB, pDecInsn->vB);
759 case kFmt51l: // op vAA, #+BBBBBBBBBBBBBBBB
761 /* this is often, but not always, a double */
766 conv.j = pDecInsn->vB_wide;
767 printf(" v%d, #double %f // #%016llx",
768 pDecInsn->vA, conv.d, pDecInsn->vB_wide);
784 * Dump a bytecode disassembly.
786 void dumpBytecodes(DexFile* pDexFile, const DexMethod* pDexMethod)
788 const DexCode* pCode = dexGetCode(pDexFile, pDexMethod);
791 FieldMethodInfo methInfo;
793 char* className = NULL;
795 assert(pCode->insnsSize > 0);
796 insns = pCode->insns;
798 getMethodInfo(pDexFile, pDexMethod->methodIdx, &methInfo);
799 startAddr = ((u1*)pCode - pDexFile->baseAddr);
800 className = descriptorToDot(methInfo.classDescriptor);
802 printf("%06x: |[%06x] %s.%s:%s\n",
803 startAddr, startAddr,
804 className, methInfo.name, methInfo.signature);
807 while (insnIdx < (int) pCode->insnsSize) {
810 DecodedInstruction decInsn;
813 instr = get2LE((const u1*)insns);
814 if (instr == kPackedSwitchSignature) {
815 insnWidth = 4 + get2LE((const u1*)(insns+1)) * 2;
816 } else if (instr == kSparseSwitchSignature) {
817 insnWidth = 2 + get2LE((const u1*)(insns+1)) * 4;
818 } else if (instr == kArrayDataSignature) {
819 int width = get2LE((const u1*)(insns+1));
820 int size = get2LE((const u1*)(insns+2)) |
821 (get2LE((const u1*)(insns+3))<<16);
822 // The plus 1 is to round up for odd size and width
823 insnWidth = 4 + ((size * width) + 1) / 2;
825 opCode = instr & 0xff;
826 insnWidth = dexGetInstrWidthAbs(gInstrWidth, opCode);
827 if (insnWidth == 0) {
829 "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx);
834 dexDecodeInstruction(gInstrFormat, insns, &decInsn);
835 dumpInstruction(pDexFile, pCode, insnIdx, insnWidth, &decInsn);
838 insnIdx += insnWidth;
845 * Dump a "code" struct.
847 void dumpCode(DexFile* pDexFile, const DexMethod* pDexMethod)
849 const DexCode* pCode = dexGetCode(pDexFile, pDexMethod);
851 printf(" registers : %d\n", pCode->registersSize);
852 printf(" ins : %d\n", pCode->insSize);
853 printf(" outs : %d\n", pCode->outsSize);
854 printf(" insns size : %d 16-bit code units\n", pCode->insnsSize);
856 if (gOptions.disassemble)
857 dumpBytecodes(pDexFile, pDexMethod);
859 dumpCatches(pDexFile, pCode);
860 /* both of these are encoded in debug info */
861 dumpPositions(pDexFile, pCode, pDexMethod);
862 dumpLocals(pDexFile, pCode, pDexMethod);
868 void dumpMethod(DexFile* pDexFile, const DexMethod* pDexMethod, int i)
870 const DexMethodId* pMethodId;
871 const char* backDescriptor;
873 char* typeDescriptor;
876 pMethodId = dexGetMethodId(pDexFile, pDexMethod->methodIdx);
877 name = dexStringById(pDexFile, pMethodId->nameIdx);
878 typeDescriptor = dexCopyDescriptorFromMethodId(pDexFile, pMethodId);
880 backDescriptor = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
882 accessStr = createAccessFlagStr(pDexMethod->accessFlags,
885 printf(" #%d : (in %s)\n", i, backDescriptor);
886 printf(" name : '%s'\n", name);
887 printf(" type : '%s'\n", typeDescriptor);
888 printf(" access : 0x%04x (%s)\n",
889 pDexMethod->accessFlags, accessStr);
891 if (pDexMethod->codeOff == 0) {
892 printf(" code : (none)\n");
895 dumpCode(pDexFile, pDexMethod);
898 if (gOptions.disassemble)
901 free(typeDescriptor);
906 * Dump a static (class) field.
908 void dumpSField(const DexFile* pDexFile, const DexField* pSField, int i)
910 const DexFieldId* pFieldId;
911 const char* backDescriptor;
913 const char* typeDescriptor;
916 pFieldId = dexGetFieldId(pDexFile, pSField->fieldIdx);
917 name = dexStringById(pDexFile, pFieldId->nameIdx);
918 typeDescriptor = dexStringByTypeIdx(pDexFile, pFieldId->typeIdx);
919 backDescriptor = dexStringByTypeIdx(pDexFile, pFieldId->classIdx);
921 accessStr = createAccessFlagStr(pSField->accessFlags, kAccessForField);
923 printf(" #%d : (in %s)\n", i, backDescriptor);
924 printf(" name : '%s'\n", name);
925 printf(" type : '%s'\n", typeDescriptor);
926 printf(" access : 0x%04x (%s)\n",
927 pSField->accessFlags, accessStr);
933 * Dump an instance field.
935 void dumpIField(const DexFile* pDexFile, const DexField* pIField, int i)
937 const DexFieldId* pFieldId;
938 const char* backDescriptor;
940 const char* typeDescriptor;
943 pFieldId = dexGetFieldId(pDexFile, pIField->fieldIdx);
944 name = dexStringById(pDexFile, pFieldId->nameIdx);
945 typeDescriptor = dexStringByTypeIdx(pDexFile, pFieldId->typeIdx);
946 backDescriptor = dexStringByTypeIdx(pDexFile, pFieldId->classIdx);
948 accessStr = createAccessFlagStr(pIField->accessFlags, kAccessForField);
950 printf(" #%d : (in %s)\n", i, backDescriptor);
951 printf(" name : '%s'\n", name);
952 printf(" type : '%s'\n", typeDescriptor);
953 printf(" access : 0x%04x (%s)\n",
954 pIField->accessFlags, accessStr);
962 void dumpClass(DexFile* pDexFile, int idx)
964 const DexTypeList* pInterfaces;
965 const DexClassDef* pClassDef;
966 DexClassData* pClassData;
967 const u1* pEncodedData;
968 const char* fileName;
969 const char* classDescriptor;
970 const char* superclassDescriptor;
974 pClassDef = dexGetClassDef(pDexFile, idx);
975 printf("Class #%d -\n", idx);
977 pEncodedData = dexGetClassData(pDexFile, pClassDef);
978 pClassData = dexReadAndVerifyClassData(&pEncodedData, NULL);
980 if (pClassData == NULL) {
981 printf("Trouble reading class data\n");
985 classDescriptor = dexStringByTypeIdx(pDexFile, pClassDef->classIdx);
986 printf(" Class descriptor : '%s'\n", classDescriptor);
988 accessStr = createAccessFlagStr(pClassDef->accessFlags, kAccessForClass);
989 printf(" Access flags : 0x%04x (%s)\n",
990 pClassDef->accessFlags, accessStr);
992 if (pClassDef->superclassIdx == kDexNoIndex)
993 superclassDescriptor = "(none)";
995 superclassDescriptor =
996 dexStringByTypeIdx(pDexFile, pClassDef->superclassIdx);
997 printf(" Superclass : '%s'\n", superclassDescriptor);
1000 printf(" Interfaces -\n");
1001 pInterfaces = dexGetInterfacesList(pDexFile, pClassDef);
1002 if (pInterfaces != NULL) {
1003 for (i = 0; i < (int) pInterfaces->size; i++)
1004 dumpInterface(pDexFile, dexGetTypeItem(pInterfaces, i), i);
1007 printf(" Static fields -\n");
1008 for (i = 0; i < (int) pClassData->header.staticFieldsSize; i++) {
1009 dumpSField(pDexFile, &pClassData->staticFields[i], i);
1012 printf(" Instance fields -\n");
1013 for (i = 0; i < (int) pClassData->header.instanceFieldsSize; i++) {
1014 dumpIField(pDexFile, &pClassData->instanceFields[i], i);
1017 printf(" Direct methods -\n");
1018 for (i = 0; i < (int) pClassData->header.directMethodsSize; i++) {
1019 dumpMethod(pDexFile, &pClassData->directMethods[i], i);
1022 printf(" Virtual methods -\n");
1023 for (i = 0; i < (int) pClassData->header.virtualMethodsSize; i++) {
1024 dumpMethod(pDexFile, &pClassData->virtualMethods[i], i);
1027 // TODO: Annotations.
1029 if (pClassDef->sourceFileIdx != kDexNoIndex)
1030 fileName = dexStringById(pDexFile, pClassDef->sourceFileIdx);
1032 fileName = "unknown";
1033 printf(" source_file_idx : %d (%s)\n",
1034 pClassDef->sourceFileIdx, fileName);
1043 * Dump the requested sections of the file.
1045 void processDexFile(const char* fileName, DexFile* pDexFile)
1049 printf("Opened '%s', DEX version '%.3s'\n", fileName,
1050 pDexFile->pHeader->magic +4);
1052 if (gOptions.showFileHeaders)
1053 dumpFileHeader(pDexFile);
1055 for (i = 0; i < (int) pDexFile->pHeader->classDefsSize; i++) {
1056 if (gOptions.showSectionHeaders)
1057 dumpClassDef(pDexFile, i);
1059 dumpClass(pDexFile, i);
1067 int process(const char* fileName)
1069 DexFile* pDexFile = NULL;
1071 bool mapped = false;
1074 printf("Processing '%s'...\n", fileName);
1076 if (dexOpenAndMap(fileName, gOptions.tempFileName, &map, false) != 0)
1080 pDexFile = dexFileParse(map.addr, map.length,
1081 kDexParseVerifyChecksum | kDexParseContinueOnError);
1082 if (pDexFile == NULL) {
1083 fprintf(stderr, "ERROR: DEX parse failed\n");
1087 processDexFile(fileName, pDexFile);
1093 sysReleaseShmem(&map);
1094 if (pDexFile != NULL)
1095 dexFileFree(pDexFile);
1105 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
1106 fprintf(stderr, "%s: [-d] [-f] [-h] [-t tempfile] dexfile...\n", gProgName);
1107 fprintf(stderr, "\n");
1108 fprintf(stderr, " -d : disassemble code sections\n");
1109 fprintf(stderr, " -f : display summary information from file header\n");
1110 fprintf(stderr, " -h : display file header details\n");
1111 fprintf(stderr, " -t : temp file name (defaults to /sdcard/dex-temp-*)\n");
1117 * I'm not using getopt_long() because we may not have it in libc.
1119 int main(int argc, char* const argv[])
1121 bool wantUsage = false;
1124 memset(&gOptions, 0, sizeof(gOptions));
1127 ic = getopt(argc, argv, "dfht:");
1132 case 'd': // disassemble Dalvik instructions
1133 gOptions.disassemble = true;
1135 case 'f': // dump outer file header
1136 gOptions.showFileHeaders = true;
1138 case 'h': // dump section headers, i.e. all meta-data
1139 gOptions.showSectionHeaders = true;
1141 case 't': // temp file, used when opening compressed Jar
1142 gOptions.tempFileName = argv[optind];
1150 if (optind == argc) {
1151 fprintf(stderr, "%s: no file specified\n", gProgName);
1155 /* initialize some VM tables */
1156 gInstrWidth = dexCreateInstrWidthTable();
1157 gInstrFormat = dexCreateInstrFormatTable();
1164 while (optind < argc)
1165 process(argv[optind++]);