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