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