Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame^] | 41 | #include <iostream> |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 42 | #include <memory> |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame^] | 43 | #include <sstream> |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 44 | #include <vector> |
| 45 | |
| 46 | #include "dex_file-inl.h" |
| 47 | #include "dex_instruction-inl.h" |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame^] | 48 | #include "utils.h" |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 49 | |
| 50 | namespace art { |
| 51 | |
| 52 | /* |
| 53 | * Options parsed in main driver. |
| 54 | */ |
| 55 | struct Options gOptions; |
| 56 | |
| 57 | /* |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 58 | * Output file. Defaults to stdout. |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 59 | */ |
| 60 | FILE* gOutFile = stdout; |
| 61 | |
| 62 | /* |
| 63 | * Data types that match the definitions in the VM specification. |
| 64 | */ |
| 65 | typedef uint8_t u1; |
| 66 | typedef uint16_t u2; |
| 67 | typedef uint32_t u4; |
| 68 | typedef uint64_t u8; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 69 | typedef int32_t s4; |
| 70 | typedef int64_t s8; |
| 71 | |
| 72 | /* |
| 73 | * Basic information about a field or a method. |
| 74 | */ |
| 75 | struct FieldMethodInfo { |
| 76 | const char* classDescriptor; |
| 77 | const char* name; |
| 78 | const char* signature; |
| 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * Flags for use with createAccessFlagStr(). |
| 83 | */ |
| 84 | enum AccessFor { |
| 85 | kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX |
| 86 | }; |
| 87 | const int kNumFlags = 18; |
| 88 | |
| 89 | /* |
| 90 | * Gets 2 little-endian bytes. |
| 91 | */ |
| 92 | static 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 | */ |
| 99 | static 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 | */ |
| 120 | static 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 | */ |
| 171 | static 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 | */ |
| 193 | static const char* quotedBool(bool val) { |
| 194 | return val ? "\"true\"" : "\"false\""; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Returns a quoted string representing the access flags. |
| 199 | */ |
| 200 | static 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 | */ |
| 215 | static 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 | */ |
| 227 | static 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 | */ |
| 322 | static 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 | */ |
| 355 | static 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 | */ |
| 397 | static 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 | */ |
| 433 | static 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 | */ |
| 447 | static 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 | */ |
| 475 | static bool dumpPositionsCb(void* /*context*/, u4 address, u4 lineNum) { |
| 476 | fprintf(gOutFile, " 0x%04x line=%d\n", address, lineNum); |
| 477 | return false; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Callback for dumping locals table entry. |
| 482 | */ |
| 483 | static void dumpLocalsCb(void* /*context*/, u2 slot, u4 startAddress, u4 endAddress, |
| 484 | const char* name, const char* descriptor, const char* signature) { |
| 485 | fprintf(gOutFile, " 0x%04x - 0x%04x reg=%d %s %s %s\n", |
| 486 | startAddress, endAddress, slot, name, descriptor, signature); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Helper for dumpInstruction(), which builds the string |
| 491 | * representation for the index in the given instruction. This will |
| 492 | * first try to use the given buffer, but if the result won't fit, |
| 493 | * then this will allocate a new buffer to hold the result. A pointer |
| 494 | * to the buffer which holds the full result is always returned, and |
| 495 | * this can be compared with the one passed in, to see if the result |
| 496 | * needs to be free()d. |
| 497 | */ |
| 498 | static char* indexString(const DexFile* pDexFile, |
| 499 | const Instruction* pDecInsn, char* buf, size_t bufSize) { |
| 500 | // Determine index and width of the string. |
| 501 | u4 index = 0; |
| 502 | u4 width = 4; |
| 503 | switch (Instruction::FormatOf(pDecInsn->Opcode())) { |
| 504 | // SOME NOT SUPPORTED: |
| 505 | // case Instruction::k20bc: |
| 506 | case Instruction::k21c: |
| 507 | case Instruction::k35c: |
| 508 | // case Instruction::k35ms: |
| 509 | case Instruction::k3rc: |
| 510 | // case Instruction::k3rms: |
| 511 | // case Instruction::k35mi: |
| 512 | // case Instruction::k3rmi: |
| 513 | index = pDecInsn->VRegB(); |
| 514 | width = 4; |
| 515 | break; |
| 516 | case Instruction::k31c: |
| 517 | index = pDecInsn->VRegB(); |
| 518 | width = 8; |
| 519 | break; |
| 520 | case Instruction::k22c: |
| 521 | // case Instruction::k22cs: |
| 522 | index = pDecInsn->VRegC(); |
| 523 | width = 4; |
| 524 | break; |
| 525 | default: |
| 526 | break; |
| 527 | } // switch |
| 528 | |
| 529 | // Determine index type. |
| 530 | size_t outSize = 0; |
| 531 | switch (Instruction::IndexTypeOf(pDecInsn->Opcode())) { |
| 532 | case Instruction::kIndexUnknown: |
| 533 | // This function should never get called for this type, but do |
| 534 | // something sensible here, just to help with debugging. |
| 535 | outSize = snprintf(buf, bufSize, "<unknown-index>"); |
| 536 | break; |
| 537 | case Instruction::kIndexNone: |
| 538 | // This function should never get called for this type, but do |
| 539 | // something sensible here, just to help with debugging. |
| 540 | outSize = snprintf(buf, bufSize, "<no-index>"); |
| 541 | break; |
| 542 | case Instruction::kIndexTypeRef: |
| 543 | if (index < pDexFile->GetHeader().type_ids_size_) { |
| 544 | const char* tp = pDexFile->StringByTypeIdx(index); |
| 545 | outSize = snprintf(buf, bufSize, "%s // type@%0*x", tp, width, index); |
| 546 | } else { |
| 547 | outSize = snprintf(buf, bufSize, "<type?> // type@%0*x", width, index); |
| 548 | } |
| 549 | break; |
| 550 | case Instruction::kIndexStringRef: |
| 551 | if (index < pDexFile->GetHeader().string_ids_size_) { |
| 552 | const char* st = pDexFile->StringDataByIdx(index); |
| 553 | outSize = snprintf(buf, bufSize, "\"%s\" // string@%0*x", st, width, index); |
| 554 | } else { |
| 555 | outSize = snprintf(buf, bufSize, "<string?> // string@%0*x", width, index); |
| 556 | } |
| 557 | break; |
| 558 | case Instruction::kIndexMethodRef: |
| 559 | if (index < pDexFile->GetHeader().method_ids_size_) { |
| 560 | const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(index); |
| 561 | const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_); |
| 562 | const Signature signature = pDexFile->GetMethodSignature(pMethodId); |
| 563 | const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_); |
| 564 | outSize = snprintf(buf, bufSize, "%s.%s:%s // method@%0*x", |
| 565 | backDescriptor, name, signature.ToString().c_str(), width, index); |
| 566 | } else { |
| 567 | outSize = snprintf(buf, bufSize, "<method?> // method@%0*x", width, index); |
| 568 | } |
| 569 | break; |
| 570 | case Instruction::kIndexFieldRef: |
| 571 | if (index < pDexFile->GetHeader().field_ids_size_) { |
| 572 | const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(index); |
| 573 | const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_); |
| 574 | const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_); |
| 575 | const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_); |
| 576 | outSize = snprintf(buf, bufSize, "%s.%s:%s // field@%0*x", |
| 577 | backDescriptor, name, typeDescriptor, width, index); |
| 578 | } else { |
| 579 | outSize = snprintf(buf, bufSize, "<field?> // field@%0*x", width, index); |
| 580 | } |
| 581 | break; |
| 582 | case Instruction::kIndexVtableOffset: |
| 583 | outSize = snprintf(buf, bufSize, "[%0*x] // vtable #%0*x", |
| 584 | width, index, width, index); |
| 585 | break; |
| 586 | case Instruction::kIndexFieldOffset: |
| 587 | outSize = snprintf(buf, bufSize, "[obj+%0*x]", width, index); |
| 588 | break; |
| 589 | // SOME NOT SUPPORTED: |
| 590 | // case Instruction::kIndexVaries: |
| 591 | // case Instruction::kIndexInlineMethod: |
| 592 | default: |
| 593 | outSize = snprintf(buf, bufSize, "<?>"); |
| 594 | break; |
| 595 | } // switch |
| 596 | |
| 597 | // Determine success of string construction. |
| 598 | if (outSize >= bufSize) { |
| 599 | // The buffer wasn't big enough; allocate and retry. Note: |
| 600 | // snprintf() doesn't count the '\0' as part of its returned |
| 601 | // size, so we add explicit space for it here. |
| 602 | outSize++; |
| 603 | buf = reinterpret_cast<char*>(malloc(outSize)); |
| 604 | if (buf == nullptr) { |
| 605 | return nullptr; |
| 606 | } |
| 607 | return indexString(pDexFile, pDecInsn, buf, outSize); |
| 608 | } |
| 609 | return buf; |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Dumps a single instruction. |
| 614 | */ |
| 615 | static void dumpInstruction(const DexFile* pDexFile, |
| 616 | const DexFile::CodeItem* pCode, |
| 617 | u4 codeOffset, u4 insnIdx, u4 insnWidth, |
| 618 | const Instruction* pDecInsn) { |
| 619 | // Address of instruction (expressed as byte offset). |
| 620 | fprintf(gOutFile, "%06x:", codeOffset + 0x10 + insnIdx * 2); |
| 621 | |
| 622 | // Dump (part of) raw bytes. |
| 623 | const u2* insns = pCode->insns_; |
| 624 | for (u4 i = 0; i < 8; i++) { |
| 625 | if (i < insnWidth) { |
| 626 | if (i == 7) { |
| 627 | fprintf(gOutFile, " ... "); |
| 628 | } else { |
| 629 | // Print 16-bit value in little-endian order. |
| 630 | const u1* bytePtr = (const u1*) &insns[insnIdx + i]; |
| 631 | fprintf(gOutFile, " %02x%02x", bytePtr[0], bytePtr[1]); |
| 632 | } |
| 633 | } else { |
| 634 | fputs(" ", gOutFile); |
| 635 | } |
| 636 | } // for |
| 637 | |
| 638 | // Dump pseudo-instruction or opcode. |
| 639 | if (pDecInsn->Opcode() == Instruction::NOP) { |
| 640 | const u2 instr = get2LE((const u1*) &insns[insnIdx]); |
| 641 | if (instr == Instruction::kPackedSwitchSignature) { |
| 642 | fprintf(gOutFile, "|%04x: packed-switch-data (%d units)", insnIdx, insnWidth); |
| 643 | } else if (instr == Instruction::kSparseSwitchSignature) { |
| 644 | fprintf(gOutFile, "|%04x: sparse-switch-data (%d units)", insnIdx, insnWidth); |
| 645 | } else if (instr == Instruction::kArrayDataSignature) { |
| 646 | fprintf(gOutFile, "|%04x: array-data (%d units)", insnIdx, insnWidth); |
| 647 | } else { |
| 648 | fprintf(gOutFile, "|%04x: nop // spacer", insnIdx); |
| 649 | } |
| 650 | } else { |
| 651 | fprintf(gOutFile, "|%04x: %s", insnIdx, pDecInsn->Name()); |
| 652 | } |
| 653 | |
| 654 | // Set up additional argument. |
| 655 | char indexBufChars[200]; |
| 656 | char *indexBuf = indexBufChars; |
| 657 | if (Instruction::IndexTypeOf(pDecInsn->Opcode()) != Instruction::kIndexNone) { |
| 658 | indexBuf = indexString(pDexFile, pDecInsn, |
| 659 | indexBufChars, sizeof(indexBufChars)); |
| 660 | } |
| 661 | |
| 662 | // Dump the instruction. |
| 663 | // |
| 664 | // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original. |
| 665 | // |
| 666 | switch (Instruction::FormatOf(pDecInsn->Opcode())) { |
| 667 | case Instruction::k10x: // op |
| 668 | break; |
| 669 | case Instruction::k12x: // op vA, vB |
| 670 | fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB()); |
| 671 | break; |
| 672 | case Instruction::k11n: // op vA, #+B |
| 673 | fprintf(gOutFile, " v%d, #int %d // #%x", |
| 674 | pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u1)pDecInsn->VRegB()); |
| 675 | break; |
| 676 | case Instruction::k11x: // op vAA |
| 677 | fprintf(gOutFile, " v%d", pDecInsn->VRegA()); |
| 678 | break; |
| 679 | case Instruction::k10t: // op +AA |
| 680 | case Instruction::k20t: // op +AAAA |
| 681 | { |
| 682 | const s4 targ = (s4) pDecInsn->VRegA(); |
| 683 | fprintf(gOutFile, " %04x // %c%04x", |
| 684 | insnIdx + targ, |
| 685 | (targ < 0) ? '-' : '+', |
| 686 | (targ < 0) ? -targ : targ); |
| 687 | } |
| 688 | break; |
| 689 | case Instruction::k22x: // op vAA, vBBBB |
| 690 | fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB()); |
| 691 | break; |
| 692 | case Instruction::k21t: // op vAA, +BBBB |
| 693 | { |
| 694 | const s4 targ = (s4) pDecInsn->VRegB(); |
| 695 | fprintf(gOutFile, " v%d, %04x // %c%04x", pDecInsn->VRegA(), |
| 696 | insnIdx + targ, |
| 697 | (targ < 0) ? '-' : '+', |
| 698 | (targ < 0) ? -targ : targ); |
| 699 | } |
| 700 | break; |
| 701 | case Instruction::k21s: // op vAA, #+BBBB |
| 702 | fprintf(gOutFile, " v%d, #int %d // #%x", |
| 703 | pDecInsn->VRegA(), (s4) pDecInsn->VRegB(), (u2)pDecInsn->VRegB()); |
| 704 | break; |
| 705 | case Instruction::k21h: // op vAA, #+BBBB0000[00000000] |
| 706 | // The printed format varies a bit based on the actual opcode. |
| 707 | if (pDecInsn->Opcode() == Instruction::CONST_HIGH16) { |
| 708 | const s4 value = pDecInsn->VRegB() << 16; |
| 709 | fprintf(gOutFile, " v%d, #int %d // #%x", |
| 710 | pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB()); |
| 711 | } else { |
| 712 | const s8 value = ((s8) pDecInsn->VRegB()) << 48; |
| 713 | fprintf(gOutFile, " v%d, #long %" PRId64 " // #%x", |
| 714 | pDecInsn->VRegA(), value, (u2) pDecInsn->VRegB()); |
| 715 | } |
| 716 | break; |
| 717 | case Instruction::k21c: // op vAA, thing@BBBB |
| 718 | case Instruction::k31c: // op vAA, thing@BBBBBBBB |
| 719 | fprintf(gOutFile, " v%d, %s", pDecInsn->VRegA(), indexBuf); |
| 720 | break; |
| 721 | case Instruction::k23x: // op vAA, vBB, vCC |
| 722 | fprintf(gOutFile, " v%d, v%d, v%d", |
| 723 | pDecInsn->VRegA(), pDecInsn->VRegB(), pDecInsn->VRegC()); |
| 724 | break; |
| 725 | case Instruction::k22b: // op vAA, vBB, #+CC |
| 726 | fprintf(gOutFile, " v%d, v%d, #int %d // #%02x", |
| 727 | pDecInsn->VRegA(), pDecInsn->VRegB(), |
| 728 | (s4) pDecInsn->VRegC(), (u1) pDecInsn->VRegC()); |
| 729 | break; |
| 730 | case Instruction::k22t: // op vA, vB, +CCCC |
| 731 | { |
| 732 | const s4 targ = (s4) pDecInsn->VRegC(); |
| 733 | fprintf(gOutFile, " v%d, v%d, %04x // %c%04x", |
| 734 | pDecInsn->VRegA(), pDecInsn->VRegB(), |
| 735 | insnIdx + targ, |
| 736 | (targ < 0) ? '-' : '+', |
| 737 | (targ < 0) ? -targ : targ); |
| 738 | } |
| 739 | break; |
| 740 | case Instruction::k22s: // op vA, vB, #+CCCC |
| 741 | fprintf(gOutFile, " v%d, v%d, #int %d // #%04x", |
| 742 | pDecInsn->VRegA(), pDecInsn->VRegB(), |
| 743 | (s4) pDecInsn->VRegC(), (u2) pDecInsn->VRegC()); |
| 744 | break; |
| 745 | case Instruction::k22c: // op vA, vB, thing@CCCC |
| 746 | // NOT SUPPORTED: |
| 747 | // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC |
| 748 | fprintf(gOutFile, " v%d, v%d, %s", |
| 749 | pDecInsn->VRegA(), pDecInsn->VRegB(), indexBuf); |
| 750 | break; |
| 751 | case Instruction::k30t: |
| 752 | fprintf(gOutFile, " #%08x", pDecInsn->VRegA()); |
| 753 | break; |
| 754 | case Instruction::k31i: // op vAA, #+BBBBBBBB |
| 755 | { |
| 756 | // This is often, but not always, a float. |
| 757 | union { |
| 758 | float f; |
| 759 | u4 i; |
| 760 | } conv; |
| 761 | conv.i = pDecInsn->VRegB(); |
| 762 | fprintf(gOutFile, " v%d, #float %f // #%08x", |
| 763 | pDecInsn->VRegA(), conv.f, pDecInsn->VRegB()); |
| 764 | } |
| 765 | break; |
| 766 | case Instruction::k31t: // op vAA, offset +BBBBBBBB |
| 767 | fprintf(gOutFile, " v%d, %08x // +%08x", |
| 768 | pDecInsn->VRegA(), insnIdx + pDecInsn->VRegB(), pDecInsn->VRegB()); |
| 769 | break; |
| 770 | case Instruction::k32x: // op vAAAA, vBBBB |
| 771 | fprintf(gOutFile, " v%d, v%d", pDecInsn->VRegA(), pDecInsn->VRegB()); |
| 772 | break; |
| 773 | case Instruction::k35c: // op {vC, vD, vE, vF, vG}, thing@BBBB |
| 774 | // NOT SUPPORTED: |
| 775 | // case Instruction::k35ms: // [opt] invoke-virtual+super |
| 776 | // case Instruction::k35mi: // [opt] inline invoke |
| 777 | { |
| 778 | u4 arg[5]; |
| 779 | pDecInsn->GetVarArgs(arg); |
| 780 | fputs(" {", gOutFile); |
| 781 | for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) { |
| 782 | if (i == 0) { |
| 783 | fprintf(gOutFile, "v%d", arg[i]); |
| 784 | } else { |
| 785 | fprintf(gOutFile, ", v%d", arg[i]); |
| 786 | } |
| 787 | } // for |
| 788 | fprintf(gOutFile, "}, %s", indexBuf); |
| 789 | } |
| 790 | break; |
| 791 | case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB |
| 792 | // NOT SUPPORTED: |
| 793 | // case Instruction::k3rms: // [opt] invoke-virtual+super/range |
| 794 | // case Instruction::k3rmi: // [opt] execute-inline/range |
| 795 | { |
| 796 | // This doesn't match the "dx" output when some of the args are |
| 797 | // 64-bit values -- dx only shows the first register. |
| 798 | fputs(" {", gOutFile); |
| 799 | for (int i = 0, n = pDecInsn->VRegA(); i < n; i++) { |
| 800 | if (i == 0) { |
| 801 | fprintf(gOutFile, "v%d", pDecInsn->VRegC() + i); |
| 802 | } else { |
| 803 | fprintf(gOutFile, ", v%d", pDecInsn->VRegC() + i); |
| 804 | } |
| 805 | } // for |
| 806 | fprintf(gOutFile, "}, %s", indexBuf); |
| 807 | } |
| 808 | break; |
| 809 | case Instruction::k51l: // op vAA, #+BBBBBBBBBBBBBBBB |
| 810 | { |
| 811 | // This is often, but not always, a double. |
| 812 | union { |
| 813 | double d; |
| 814 | u8 j; |
| 815 | } conv; |
| 816 | conv.j = pDecInsn->WideVRegB(); |
| 817 | fprintf(gOutFile, " v%d, #double %f // #%016" PRIx64, |
| 818 | pDecInsn->VRegA(), conv.d, pDecInsn->WideVRegB()); |
| 819 | } |
| 820 | break; |
| 821 | // NOT SUPPORTED: |
| 822 | // case Instruction::k00x: // unknown op or breakpoint |
| 823 | // break; |
| 824 | default: |
| 825 | fprintf(gOutFile, " ???"); |
| 826 | break; |
| 827 | } // switch |
| 828 | |
| 829 | fputc('\n', gOutFile); |
| 830 | |
| 831 | if (indexBuf != indexBufChars) { |
| 832 | free(indexBuf); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Dumps a bytecode disassembly. |
| 838 | */ |
| 839 | static void dumpBytecodes(const DexFile* pDexFile, u4 idx, |
| 840 | const DexFile::CodeItem* pCode, u4 codeOffset) { |
| 841 | const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx); |
| 842 | const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_); |
| 843 | const Signature signature = pDexFile->GetMethodSignature(pMethodId); |
| 844 | const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_); |
| 845 | |
| 846 | // Generate header. |
| 847 | char* tmp = descriptorToDot(backDescriptor); |
| 848 | fprintf(gOutFile, "%06x: " |
| 849 | "|[%06x] %s.%s:%s\n", |
| 850 | codeOffset, codeOffset, tmp, name, signature.ToString().c_str()); |
| 851 | free(tmp); |
| 852 | |
| 853 | // Iterate over all instructions. |
| 854 | const u2* insns = pCode->insns_; |
| 855 | for (u4 insnIdx = 0; insnIdx < pCode->insns_size_in_code_units_;) { |
| 856 | const Instruction* instruction = Instruction::At(&insns[insnIdx]); |
| 857 | const u4 insnWidth = instruction->SizeInCodeUnits(); |
| 858 | if (insnWidth == 0) { |
| 859 | fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insnIdx); |
| 860 | break; |
| 861 | } |
| 862 | dumpInstruction(pDexFile, pCode, codeOffset, insnIdx, insnWidth, instruction); |
| 863 | insnIdx += insnWidth; |
| 864 | } // for |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * Dumps code of a method. |
| 869 | */ |
| 870 | static void dumpCode(const DexFile* pDexFile, u4 idx, u4 flags, |
| 871 | const DexFile::CodeItem* pCode, u4 codeOffset) { |
| 872 | fprintf(gOutFile, " registers : %d\n", pCode->registers_size_); |
| 873 | fprintf(gOutFile, " ins : %d\n", pCode->ins_size_); |
| 874 | fprintf(gOutFile, " outs : %d\n", pCode->outs_size_); |
| 875 | fprintf(gOutFile, " insns size : %d 16-bit code units\n", |
| 876 | pCode->insns_size_in_code_units_); |
| 877 | |
| 878 | // Bytecode disassembly, if requested. |
| 879 | if (gOptions.disassemble) { |
| 880 | dumpBytecodes(pDexFile, idx, pCode, codeOffset); |
| 881 | } |
| 882 | |
| 883 | // Try-catch blocks. |
| 884 | dumpCatches(pDexFile, pCode); |
| 885 | |
| 886 | // Positions and locals table in the debug info. |
| 887 | bool is_static = (flags & kAccStatic) != 0; |
| 888 | fprintf(gOutFile, " positions : \n"); |
| 889 | pDexFile->DecodeDebugInfo( |
| 890 | pCode, is_static, idx, dumpPositionsCb, nullptr, nullptr); |
| 891 | fprintf(gOutFile, " locals : \n"); |
| 892 | pDexFile->DecodeDebugInfo( |
| 893 | pCode, is_static, idx, nullptr, dumpLocalsCb, nullptr); |
| 894 | } |
| 895 | |
| 896 | /* |
| 897 | * Dumps a method. |
| 898 | */ |
| 899 | static void dumpMethod(const DexFile* pDexFile, u4 idx, u4 flags, |
| 900 | const DexFile::CodeItem* pCode, u4 codeOffset, int i) { |
| 901 | // Bail for anything private if export only requested. |
| 902 | if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) { |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx); |
| 907 | const char* name = pDexFile->StringDataByIdx(pMethodId.name_idx_); |
| 908 | const Signature signature = pDexFile->GetMethodSignature(pMethodId); |
| 909 | char* typeDescriptor = strdup(signature.ToString().c_str()); |
| 910 | const char* backDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_); |
| 911 | char* accessStr = createAccessFlagStr(flags, kAccessForMethod); |
| 912 | |
| 913 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 914 | fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor); |
| 915 | fprintf(gOutFile, " name : '%s'\n", name); |
| 916 | fprintf(gOutFile, " type : '%s'\n", typeDescriptor); |
| 917 | fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr); |
| 918 | if (pCode == nullptr) { |
| 919 | fprintf(gOutFile, " code : (none)\n"); |
| 920 | } else { |
| 921 | fprintf(gOutFile, " code -\n"); |
| 922 | dumpCode(pDexFile, idx, flags, pCode, codeOffset); |
| 923 | } |
| 924 | if (gOptions.disassemble) { |
| 925 | fputc('\n', gOutFile); |
| 926 | } |
| 927 | } else if (gOptions.outputFormat == OUTPUT_XML) { |
| 928 | const bool constructor = (name[0] == '<'); |
| 929 | |
| 930 | // Method name and prototype. |
| 931 | if (constructor) { |
| 932 | char* tmp = descriptorClassToDot(backDescriptor); |
| 933 | fprintf(gOutFile, "<constructor name=\"%s\"\n", tmp); |
| 934 | free(tmp); |
| 935 | tmp = descriptorToDot(backDescriptor); |
| 936 | fprintf(gOutFile, " type=\"%s\"\n", tmp); |
| 937 | free(tmp); |
| 938 | } else { |
| 939 | fprintf(gOutFile, "<method name=\"%s\"\n", name); |
| 940 | const char* returnType = strrchr(typeDescriptor, ')'); |
| 941 | if (returnType == nullptr) { |
| 942 | fprintf(stderr, "bad method type descriptor '%s'\n", typeDescriptor); |
| 943 | goto bail; |
| 944 | } |
| 945 | char* tmp = descriptorToDot(returnType+1); |
| 946 | fprintf(gOutFile, " return=\"%s\"\n", tmp); |
| 947 | free(tmp); |
| 948 | fprintf(gOutFile, " abstract=%s\n", quotedBool((flags & kAccAbstract) != 0)); |
| 949 | fprintf(gOutFile, " native=%s\n", quotedBool((flags & kAccNative) != 0)); |
| 950 | fprintf(gOutFile, " synchronized=%s\n", quotedBool( |
| 951 | (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0)); |
| 952 | } |
| 953 | |
| 954 | // Additional method flags. |
| 955 | fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0)); |
| 956 | fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0)); |
| 957 | // The "deprecated=" not knowable w/o parsing annotations. |
| 958 | fprintf(gOutFile, " visibility=%s\n>\n", quotedVisibility(flags)); |
| 959 | |
| 960 | // Parameters. |
| 961 | if (typeDescriptor[0] != '(') { |
| 962 | fprintf(stderr, "ERROR: bad descriptor '%s'\n", typeDescriptor); |
| 963 | goto bail; |
| 964 | } |
| 965 | char* tmpBuf = reinterpret_cast<char*>(malloc(strlen(typeDescriptor) + 1)); |
| 966 | const char* base = typeDescriptor + 1; |
| 967 | int argNum = 0; |
| 968 | while (*base != ')') { |
| 969 | char* cp = tmpBuf; |
| 970 | while (*base == '[') { |
| 971 | *cp++ = *base++; |
| 972 | } |
| 973 | if (*base == 'L') { |
| 974 | // Copy through ';'. |
| 975 | do { |
| 976 | *cp = *base++; |
| 977 | } while (*cp++ != ';'); |
| 978 | } else { |
| 979 | // Primitive char, copy it. |
| 980 | if (strchr("ZBCSIFJD", *base) == NULL) { |
| 981 | fprintf(stderr, "ERROR: bad method signature '%s'\n", base); |
| 982 | goto bail; |
| 983 | } |
| 984 | *cp++ = *base++; |
| 985 | } |
| 986 | // Null terminate and display. |
| 987 | *cp++ = '\0'; |
| 988 | char* tmp = descriptorToDot(tmpBuf); |
| 989 | fprintf(gOutFile, "<parameter name=\"arg%d\" type=\"%s\">\n" |
| 990 | "</parameter>\n", argNum++, tmp); |
| 991 | free(tmp); |
| 992 | } // while |
| 993 | free(tmpBuf); |
| 994 | if (constructor) { |
| 995 | fprintf(gOutFile, "</constructor>\n"); |
| 996 | } else { |
| 997 | fprintf(gOutFile, "</method>\n"); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | bail: |
| 1002 | free(typeDescriptor); |
| 1003 | free(accessStr); |
| 1004 | } |
| 1005 | |
| 1006 | /* |
| 1007 | * Dumps a static (class) field. |
| 1008 | */ |
| 1009 | static void dumpSField(const DexFile* pDexFile, u4 idx, u4 flags, int i) { |
| 1010 | // Bail for anything private if export only requested. |
| 1011 | if (gOptions.exportsOnly && (flags & (kAccPublic | kAccProtected)) == 0) { |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | const DexFile::FieldId& pFieldId = pDexFile->GetFieldId(idx); |
| 1016 | const char* name = pDexFile->StringDataByIdx(pFieldId.name_idx_); |
| 1017 | const char* typeDescriptor = pDexFile->StringByTypeIdx(pFieldId.type_idx_); |
| 1018 | const char* backDescriptor = pDexFile->StringByTypeIdx(pFieldId.class_idx_); |
| 1019 | char* accessStr = createAccessFlagStr(flags, kAccessForField); |
| 1020 | |
| 1021 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 1022 | fprintf(gOutFile, " #%d : (in %s)\n", i, backDescriptor); |
| 1023 | fprintf(gOutFile, " name : '%s'\n", name); |
| 1024 | fprintf(gOutFile, " type : '%s'\n", typeDescriptor); |
| 1025 | fprintf(gOutFile, " access : 0x%04x (%s)\n", flags, accessStr); |
| 1026 | } else if (gOptions.outputFormat == OUTPUT_XML) { |
| 1027 | fprintf(gOutFile, "<field name=\"%s\"\n", name); |
| 1028 | char *tmp = descriptorToDot(typeDescriptor); |
| 1029 | fprintf(gOutFile, " type=\"%s\"\n", tmp); |
| 1030 | free(tmp); |
| 1031 | fprintf(gOutFile, " transient=%s\n", quotedBool((flags & kAccTransient) != 0)); |
| 1032 | fprintf(gOutFile, " volatile=%s\n", quotedBool((flags & kAccVolatile) != 0)); |
| 1033 | // The "value=" is not knowable w/o parsing annotations. |
| 1034 | fprintf(gOutFile, " static=%s\n", quotedBool((flags & kAccStatic) != 0)); |
| 1035 | fprintf(gOutFile, " final=%s\n", quotedBool((flags & kAccFinal) != 0)); |
| 1036 | // The "deprecated=" is not knowable w/o parsing annotations. |
| 1037 | fprintf(gOutFile, " visibility=%s\n", quotedVisibility(flags)); |
| 1038 | fprintf(gOutFile, ">\n</field>\n"); |
| 1039 | } |
| 1040 | |
| 1041 | free(accessStr); |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | * Dumps an instance field. |
| 1046 | */ |
| 1047 | static void dumpIField(const DexFile* pDexFile, u4 idx, u4 flags, int i) { |
| 1048 | dumpSField(pDexFile, idx, flags, i); |
| 1049 | } |
| 1050 | |
| 1051 | /* |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame^] | 1052 | * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item |
| 1053 | * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a |
| 1054 | * tool, so this is not performance-critical. |
| 1055 | */ |
| 1056 | |
| 1057 | static void dumpCfg(const DexFile* dex_file, |
| 1058 | uint32_t dex_method_idx, |
| 1059 | const DexFile::CodeItem* code_item) { |
| 1060 | if (code_item != nullptr) { |
| 1061 | std::ostringstream oss; |
| 1062 | DumpMethodCFG(dex_file, dex_method_idx, oss); |
| 1063 | fprintf(gOutFile, "%s", oss.str().c_str()); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | static void dumpCfg(const DexFile* dex_file, int idx) { |
| 1068 | const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx); |
| 1069 | const uint8_t* class_data = dex_file->GetClassData(class_def); |
| 1070 | if (class_data == nullptr) { // empty class such as a marker interface? |
| 1071 | return; |
| 1072 | } |
| 1073 | ClassDataItemIterator it(*dex_file, class_data); |
| 1074 | while (it.HasNextStaticField()) { |
| 1075 | it.Next(); |
| 1076 | } |
| 1077 | while (it.HasNextInstanceField()) { |
| 1078 | it.Next(); |
| 1079 | } |
| 1080 | while (it.HasNextDirectMethod()) { |
| 1081 | dumpCfg(dex_file, |
| 1082 | it.GetMemberIndex(), |
| 1083 | it.GetMethodCodeItem()); |
| 1084 | it.Next(); |
| 1085 | } |
| 1086 | while (it.HasNextVirtualMethod()) { |
| 1087 | dumpCfg(dex_file, |
| 1088 | it.GetMemberIndex(), |
| 1089 | it.GetMethodCodeItem()); |
| 1090 | it.Next(); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | /* |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 1095 | * Dumps the class. |
| 1096 | * |
| 1097 | * Note "idx" is a DexClassDef index, not a DexTypeId index. |
| 1098 | * |
| 1099 | * If "*pLastPackage" is nullptr or does not match the current class' package, |
| 1100 | * the value will be replaced with a newly-allocated string. |
| 1101 | */ |
| 1102 | static void dumpClass(const DexFile* pDexFile, int idx, char** pLastPackage) { |
| 1103 | const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx); |
| 1104 | |
| 1105 | // Omitting non-public class. |
| 1106 | if (gOptions.exportsOnly && (pClassDef.access_flags_ & kAccPublic) == 0) { |
| 1107 | return; |
| 1108 | } |
| 1109 | |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame^] | 1110 | if (gOptions.cfg) { |
| 1111 | dumpCfg(pDexFile, idx); |
| 1112 | return; |
| 1113 | } |
| 1114 | |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 1115 | // For the XML output, show the package name. Ideally we'd gather |
| 1116 | // up the classes, sort them, and dump them alphabetically so the |
| 1117 | // package name wouldn't jump around, but that's not a great plan |
| 1118 | // for something that needs to run on the device. |
| 1119 | const char* classDescriptor = pDexFile->StringByTypeIdx(pClassDef.class_idx_); |
| 1120 | if (!(classDescriptor[0] == 'L' && |
| 1121 | classDescriptor[strlen(classDescriptor)-1] == ';')) { |
| 1122 | // Arrays and primitives should not be defined explicitly. Keep going? |
| 1123 | fprintf(stderr, "Malformed class name '%s'\n", classDescriptor); |
| 1124 | } else if (gOptions.outputFormat == OUTPUT_XML) { |
| 1125 | char* mangle = strdup(classDescriptor + 1); |
| 1126 | mangle[strlen(mangle)-1] = '\0'; |
| 1127 | |
| 1128 | // Reduce to just the package name. |
| 1129 | char* lastSlash = strrchr(mangle, '/'); |
| 1130 | if (lastSlash != nullptr) { |
| 1131 | *lastSlash = '\0'; |
| 1132 | } else { |
| 1133 | *mangle = '\0'; |
| 1134 | } |
| 1135 | |
| 1136 | for (char* cp = mangle; *cp != '\0'; cp++) { |
| 1137 | if (*cp == '/') { |
| 1138 | *cp = '.'; |
| 1139 | } |
| 1140 | } // for |
| 1141 | |
| 1142 | if (*pLastPackage == nullptr || strcmp(mangle, *pLastPackage) != 0) { |
| 1143 | // Start of a new package. |
| 1144 | if (*pLastPackage != nullptr) { |
| 1145 | fprintf(gOutFile, "</package>\n"); |
| 1146 | } |
| 1147 | fprintf(gOutFile, "<package name=\"%s\"\n>\n", mangle); |
| 1148 | free(*pLastPackage); |
| 1149 | *pLastPackage = mangle; |
| 1150 | } else { |
| 1151 | free(mangle); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | // General class information. |
| 1156 | char* accessStr = createAccessFlagStr(pClassDef.access_flags_, kAccessForClass); |
| 1157 | const char* superclassDescriptor; |
| 1158 | if (pClassDef.superclass_idx_ == DexFile::kDexNoIndex16) { |
| 1159 | superclassDescriptor = nullptr; |
| 1160 | } else { |
| 1161 | superclassDescriptor = pDexFile->StringByTypeIdx(pClassDef.superclass_idx_); |
| 1162 | } |
| 1163 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 1164 | fprintf(gOutFile, "Class #%d -\n", idx); |
| 1165 | fprintf(gOutFile, " Class descriptor : '%s'\n", classDescriptor); |
| 1166 | fprintf(gOutFile, " Access flags : 0x%04x (%s)\n", pClassDef.access_flags_, accessStr); |
| 1167 | if (superclassDescriptor != nullptr) { |
| 1168 | fprintf(gOutFile, " Superclass : '%s'\n", superclassDescriptor); |
| 1169 | } |
| 1170 | fprintf(gOutFile, " Interfaces -\n"); |
| 1171 | } else { |
| 1172 | char* tmp = descriptorClassToDot(classDescriptor); |
| 1173 | fprintf(gOutFile, "<class name=\"%s\"\n", tmp); |
| 1174 | free(tmp); |
| 1175 | if (superclassDescriptor != nullptr) { |
| 1176 | tmp = descriptorToDot(superclassDescriptor); |
| 1177 | fprintf(gOutFile, " extends=\"%s\"\n", tmp); |
| 1178 | free(tmp); |
| 1179 | } |
| 1180 | fprintf(gOutFile, " abstract=%s\n", quotedBool((pClassDef.access_flags_ & kAccAbstract) != 0)); |
| 1181 | fprintf(gOutFile, " static=%s\n", quotedBool((pClassDef.access_flags_ & kAccStatic) != 0)); |
| 1182 | fprintf(gOutFile, " final=%s\n", quotedBool((pClassDef.access_flags_ & kAccFinal) != 0)); |
| 1183 | // The "deprecated=" not knowable w/o parsing annotations. |
| 1184 | fprintf(gOutFile, " visibility=%s\n", quotedVisibility(pClassDef.access_flags_)); |
| 1185 | fprintf(gOutFile, ">\n"); |
| 1186 | } |
| 1187 | |
| 1188 | // Interfaces. |
| 1189 | const DexFile::TypeList* pInterfaces = pDexFile->GetInterfacesList(pClassDef); |
| 1190 | if (pInterfaces != nullptr) { |
| 1191 | for (u4 i = 0; i < pInterfaces->Size(); i++) { |
| 1192 | dumpInterface(pDexFile, pInterfaces->GetTypeItem(i), i); |
| 1193 | } // for |
| 1194 | } |
| 1195 | |
| 1196 | // Fields and methods. |
| 1197 | const u1* pEncodedData = pDexFile->GetClassData(pClassDef); |
| 1198 | if (pEncodedData == nullptr) { |
| 1199 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 1200 | fprintf(gOutFile, " Static fields -\n"); |
| 1201 | fprintf(gOutFile, " Instance fields -\n"); |
| 1202 | fprintf(gOutFile, " Direct methods -\n"); |
| 1203 | fprintf(gOutFile, " Virtual methods -\n"); |
| 1204 | } |
| 1205 | } else { |
| 1206 | ClassDataItemIterator pClassData(*pDexFile, pEncodedData); |
| 1207 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 1208 | fprintf(gOutFile, " Static fields -\n"); |
| 1209 | } |
| 1210 | for (int i = 0; pClassData.HasNextStaticField(); i++, pClassData.Next()) { |
| 1211 | dumpSField(pDexFile, pClassData.GetMemberIndex(), |
| 1212 | pClassData.GetRawMemberAccessFlags(), i); |
| 1213 | } // for |
| 1214 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 1215 | fprintf(gOutFile, " Instance fields -\n"); |
| 1216 | } |
| 1217 | for (int i = 0; pClassData.HasNextInstanceField(); i++, pClassData.Next()) { |
| 1218 | dumpIField(pDexFile, pClassData.GetMemberIndex(), |
| 1219 | pClassData.GetRawMemberAccessFlags(), i); |
| 1220 | } // for |
| 1221 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 1222 | fprintf(gOutFile, " Direct methods -\n"); |
| 1223 | } |
| 1224 | for (int i = 0; pClassData.HasNextDirectMethod(); i++, pClassData.Next()) { |
| 1225 | dumpMethod(pDexFile, pClassData.GetMemberIndex(), |
| 1226 | pClassData.GetRawMemberAccessFlags(), |
| 1227 | pClassData.GetMethodCodeItem(), |
| 1228 | pClassData.GetMethodCodeItemOffset(), i); |
| 1229 | } // for |
| 1230 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 1231 | fprintf(gOutFile, " Virtual methods -\n"); |
| 1232 | } |
| 1233 | for (int i = 0; pClassData.HasNextVirtualMethod(); i++, pClassData.Next()) { |
| 1234 | dumpMethod(pDexFile, pClassData.GetMemberIndex(), |
| 1235 | pClassData.GetRawMemberAccessFlags(), |
| 1236 | pClassData.GetMethodCodeItem(), |
| 1237 | pClassData.GetMethodCodeItemOffset(), i); |
| 1238 | } // for |
| 1239 | } |
| 1240 | |
| 1241 | // End of class. |
| 1242 | if (gOptions.outputFormat == OUTPUT_PLAIN) { |
| 1243 | const char* fileName; |
| 1244 | if (pClassDef.source_file_idx_ != DexFile::kDexNoIndex) { |
| 1245 | fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_); |
| 1246 | } else { |
| 1247 | fileName = "unknown"; |
| 1248 | } |
| 1249 | fprintf(gOutFile, " source_file_idx : %d (%s)\n\n", |
| 1250 | pClassDef.source_file_idx_, fileName); |
| 1251 | } else if (gOptions.outputFormat == OUTPUT_XML) { |
| 1252 | fprintf(gOutFile, "</class>\n"); |
| 1253 | } |
| 1254 | |
| 1255 | free(accessStr); |
| 1256 | } |
| 1257 | |
| 1258 | /* |
| 1259 | * Dumps the requested sections of the file. |
| 1260 | */ |
| 1261 | static void processDexFile(const char* fileName, const DexFile* pDexFile) { |
| 1262 | if (gOptions.verbose) { |
| 1263 | fprintf(gOutFile, "Opened '%s', DEX version '%.3s'\n", |
| 1264 | fileName, pDexFile->GetHeader().magic_ + 4); |
| 1265 | } |
| 1266 | |
| 1267 | // Headers. |
| 1268 | if (gOptions.showFileHeaders) { |
| 1269 | dumpFileHeader(pDexFile); |
| 1270 | } |
| 1271 | |
| 1272 | // Open XML context. |
| 1273 | if (gOptions.outputFormat == OUTPUT_XML) { |
| 1274 | fprintf(gOutFile, "<api>\n"); |
| 1275 | } |
| 1276 | |
| 1277 | // Iterate over all classes. |
| 1278 | char* package = nullptr; |
| 1279 | const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_; |
| 1280 | for (u4 i = 0; i < classDefsSize; i++) { |
| 1281 | if (gOptions.showSectionHeaders) { |
| 1282 | dumpClassDef(pDexFile, i); |
| 1283 | } |
| 1284 | dumpClass(pDexFile, i, &package); |
| 1285 | } // for |
| 1286 | |
| 1287 | // Free the last package allocated. |
| 1288 | if (package != nullptr) { |
| 1289 | fprintf(gOutFile, "</package>\n"); |
| 1290 | free(package); |
| 1291 | } |
| 1292 | |
| 1293 | // Close XML context. |
| 1294 | if (gOptions.outputFormat == OUTPUT_XML) { |
| 1295 | fprintf(gOutFile, "</api>\n"); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * Processes a single file (either direct .dex or indirect .zip/.jar/.apk). |
| 1301 | */ |
| 1302 | int processFile(const char* fileName) { |
| 1303 | if (gOptions.verbose) { |
| 1304 | fprintf(gOutFile, "Processing '%s'...\n", fileName); |
| 1305 | } |
| 1306 | |
| 1307 | // If the file is not a .dex file, the function tries .zip/.jar/.apk files, |
| 1308 | // all of which are Zip archives with "classes.dex" inside. The compressed |
| 1309 | // data needs to be extracted to a temp file, the location of which varies. |
| 1310 | // |
| 1311 | // TODO(ajcbik): fix following issues |
| 1312 | // |
| 1313 | // (1) gOptions.tempFileName is not accounted for |
| 1314 | // (2) gOptions.ignoreBadChecksum is not accounted for |
| 1315 | // |
| 1316 | std::string error_msg; |
| 1317 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1318 | if (!DexFile::Open(fileName, fileName, &error_msg, &dex_files)) { |
| 1319 | // Display returned error message to user. Note that this error behavior |
| 1320 | // differs from the error messages shown by the original Dalvik dexdump. |
| 1321 | fputs(error_msg.c_str(), stderr); |
| 1322 | fputc('\n', stderr); |
| 1323 | return -1; |
| 1324 | } |
| 1325 | |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 1326 | // Success. Either report checksum verification or process |
| 1327 | // all dex files found in given file. |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 1328 | if (gOptions.checksumOnly) { |
| 1329 | fprintf(gOutFile, "Checksum verified\n"); |
| 1330 | } else { |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 1331 | for (size_t i = 0; i < dex_files.size(); i++) { |
| 1332 | processDexFile(fileName, dex_files[i].get()); |
| 1333 | } |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 1334 | } |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | } // namespace art |