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