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