oatdump.cc revision 131980fc9aeb2b4d03480443e0fb494c76ce03a2
1/* 2 * Copyright (C) 2011 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 17#include <stdio.h> 18#include <stdlib.h> 19 20#include <fstream> 21#include <iostream> 22#include <map> 23#include <set> 24#include <string> 25#include <unordered_map> 26#include <vector> 27 28#include "arch/instruction_set_features.h" 29#include "art_field-inl.h" 30#include "art_method-inl.h" 31#include "base/stl_util.h" 32#include "base/unix_file/fd_file.h" 33#include "class_linker.h" 34#include "class_linker-inl.h" 35#include "dex_file-inl.h" 36#include "dex_instruction.h" 37#include "disassembler.h" 38#include "elf_builder.h" 39#include "gc_map.h" 40#include "gc/space/image_space.h" 41#include "gc/space/large_object_space.h" 42#include "gc/space/space-inl.h" 43#include "image.h" 44#include "indenter.h" 45#include "linker/buffered_output_stream.h" 46#include "linker/file_output_stream.h" 47#include "mapping_table.h" 48#include "mirror/array-inl.h" 49#include "mirror/class-inl.h" 50#include "mirror/dex_cache-inl.h" 51#include "mirror/object-inl.h" 52#include "mirror/object_array-inl.h" 53#include "oat.h" 54#include "oat_file-inl.h" 55#include "oat_file_manager.h" 56#include "os.h" 57#include "safe_map.h" 58#include "scoped_thread_state_change.h" 59#include "stack_map.h" 60#include "ScopedLocalRef.h" 61#include "thread_list.h" 62#include "verifier/dex_gc_map.h" 63#include "verifier/method_verifier.h" 64#include "vmap_table.h" 65#include "well_known_classes.h" 66 67#include <sys/stat.h> 68#include "cmdline.h" 69 70namespace art { 71 72const char* image_methods_descriptions_[] = { 73 "kResolutionMethod", 74 "kImtConflictMethod", 75 "kImtUnimplementedMethod", 76 "kCalleeSaveMethod", 77 "kRefsOnlySaveMethod", 78 "kRefsAndArgsSaveMethod", 79}; 80 81const char* image_roots_descriptions_[] = { 82 "kDexCaches", 83 "kClassRoots", 84}; 85 86// Map is so that we don't allocate multiple dex files for the same OatDexFile. 87static std::map<const OatFile::OatDexFile*, 88 std::unique_ptr<const DexFile>> opened_dex_files; 89 90const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) { 91 DCHECK(oat_dex_file != nullptr); 92 auto it = opened_dex_files.find(oat_dex_file); 93 if (it != opened_dex_files.end()) { 94 return it->second.get(); 95 } 96 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release(); 97 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret)); 98 return ret; 99} 100 101class OatSymbolizer FINAL { 102 public: 103 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) : 104 oat_file_(oat_file), builder_(nullptr), 105 output_name_(output_name.empty() ? "symbolized.oat" : output_name) { 106 } 107 108 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&, 109 uint32_t, 110 const OatFile::OatMethod&, 111 const DexFile&, 112 uint32_t, 113 const DexFile::CodeItem*, 114 uint32_t); 115 116 bool Symbolize() { 117 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet(); 118 119 File* elf_file = OS::CreateEmptyFile(output_name_.c_str()); 120 std::unique_ptr<BufferedOutputStream> output_stream( 121 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file))); 122 builder_.reset(new ElfBuilder<ElfTypes32>(isa, output_stream.get())); 123 124 builder_->Start(); 125 126 auto* rodata = builder_->GetRoData(); 127 auto* text = builder_->GetText(); 128 auto* bss = builder_->GetBss(); 129 auto* strtab = builder_->GetStrTab(); 130 auto* symtab = builder_->GetSymTab(); 131 132 rodata->Start(); 133 const uint8_t* rodata_begin = oat_file_->Begin(); 134 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset(); 135 rodata->WriteFully(rodata_begin, rodata_size); 136 rodata->End(); 137 138 text->Start(); 139 const uint8_t* text_begin = oat_file_->Begin() + rodata_size; 140 const size_t text_size = oat_file_->End() - text_begin; 141 text->WriteFully(text_begin, text_size); 142 text->End(); 143 144 if (oat_file_->BssSize() != 0) { 145 bss->Start(); 146 bss->SetSize(oat_file_->BssSize()); 147 bss->End(); 148 } 149 150 builder_->WriteDynamicSection(elf_file->GetPath()); 151 152 Walk(&art::OatSymbolizer::RegisterForDedup); 153 154 NormalizeState(); 155 156 strtab->Start(); 157 strtab->Write(""); // strtab should start with empty string. 158 Walk(&art::OatSymbolizer::AddSymbol); 159 strtab->End(); 160 161 symtab->Start(); 162 symtab->Write(); 163 symtab->End(); 164 165 builder_->End(); 166 167 return builder_->Good(); 168 } 169 170 void Walk(Callback callback) { 171 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles(); 172 for (size_t i = 0; i < oat_dex_files.size(); i++) { 173 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i]; 174 CHECK(oat_dex_file != nullptr); 175 WalkOatDexFile(oat_dex_file, callback); 176 } 177 } 178 179 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) { 180 std::string error_msg; 181 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); 182 if (dex_file == nullptr) { 183 return; 184 } 185 for (size_t class_def_index = 0; 186 class_def_index < dex_file->NumClassDefs(); 187 class_def_index++) { 188 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); 189 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); 190 OatClassType type = oat_class.GetType(); 191 switch (type) { 192 case kOatClassAllCompiled: 193 case kOatClassSomeCompiled: 194 WalkOatClass(oat_class, *dex_file, class_def, callback); 195 break; 196 197 case kOatClassNoneCompiled: 198 case kOatClassMax: 199 // Ignore. 200 break; 201 } 202 } 203 } 204 205 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file, 206 const DexFile::ClassDef& class_def, Callback callback) { 207 const uint8_t* class_data = dex_file.GetClassData(class_def); 208 if (class_data == nullptr) { // empty class such as a marker interface? 209 return; 210 } 211 // Note: even if this is an interface or a native class, we still have to walk it, as there 212 // might be a static initializer. 213 ClassDataItemIterator it(dex_file, class_data); 214 SkipAllFields(&it); 215 uint32_t class_method_idx = 0; 216 while (it.HasNextDirectMethod()) { 217 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx); 218 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(), 219 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback); 220 class_method_idx++; 221 it.Next(); 222 } 223 while (it.HasNextVirtualMethod()) { 224 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx); 225 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(), 226 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback); 227 class_method_idx++; 228 it.Next(); 229 } 230 DCHECK(!it.HasNext()); 231 } 232 233 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index, 234 const OatFile::OatMethod& oat_method, const DexFile& dex_file, 235 uint32_t dex_method_idx, const DexFile::CodeItem* code_item, 236 uint32_t method_access_flags, Callback callback) { 237 if ((method_access_flags & kAccAbstract) != 0) { 238 // Abstract method, no code. 239 return; 240 } 241 if (oat_method.GetCodeOffset() == 0) { 242 // No code. 243 return; 244 } 245 246 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item, 247 method_access_flags); 248 } 249 250 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED, 251 uint32_t class_method_index ATTRIBUTE_UNUSED, 252 const OatFile::OatMethod& oat_method, 253 const DexFile& dex_file ATTRIBUTE_UNUSED, 254 uint32_t dex_method_idx ATTRIBUTE_UNUSED, 255 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED, 256 uint32_t method_access_flags ATTRIBUTE_UNUSED) { 257 state_[oat_method.GetCodeOffset()]++; 258 } 259 260 void NormalizeState() { 261 for (auto& x : state_) { 262 if (x.second == 1) { 263 state_[x.first] = 0; 264 } 265 } 266 } 267 268 enum class DedupState { // private 269 kNotDeduplicated, 270 kDeduplicatedFirst, 271 kDeduplicatedOther 272 }; 273 DedupState IsDuplicated(uint32_t offset) { 274 if (state_[offset] == 0) { 275 return DedupState::kNotDeduplicated; 276 } 277 if (state_[offset] == 1) { 278 return DedupState::kDeduplicatedOther; 279 } 280 state_[offset] = 1; 281 return DedupState::kDeduplicatedFirst; 282 } 283 284 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED, 285 uint32_t class_method_index ATTRIBUTE_UNUSED, 286 const OatFile::OatMethod& oat_method, 287 const DexFile& dex_file, 288 uint32_t dex_method_idx, 289 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED, 290 uint32_t method_access_flags ATTRIBUTE_UNUSED) { 291 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset()); 292 if (dedup != DedupState::kDeduplicatedOther) { 293 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true); 294 295 if (dedup == DedupState::kDeduplicatedFirst) { 296 pretty_name = "[Dedup]" + pretty_name; 297 } 298 299 int name_offset = builder_->GetStrTab()->Write(pretty_name); 300 builder_->GetSymTab()->Add(name_offset, builder_->GetText(), 301 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(), 302 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC); 303 } 304 } 305 306 private: 307 static void SkipAllFields(ClassDataItemIterator* it) { 308 while (it->HasNextStaticField()) { 309 it->Next(); 310 } 311 while (it->HasNextInstanceField()) { 312 it->Next(); 313 } 314 } 315 316 const OatFile* oat_file_; 317 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_; 318 std::unordered_map<uint32_t, uint32_t> state_; 319 const std::string output_name_; 320}; 321 322class OatDumperOptions { 323 public: 324 OatDumperOptions(bool dump_raw_mapping_table, 325 bool dump_raw_gc_map, 326 bool dump_vmap, 327 bool dump_code_info_stack_maps, 328 bool disassemble_code, 329 bool absolute_addresses, 330 const char* class_filter, 331 const char* method_filter, 332 bool list_classes, 333 bool list_methods, 334 const char* export_dex_location, 335 uint32_t addr2instr) 336 : dump_raw_mapping_table_(dump_raw_mapping_table), 337 dump_raw_gc_map_(dump_raw_gc_map), 338 dump_vmap_(dump_vmap), 339 dump_code_info_stack_maps_(dump_code_info_stack_maps), 340 disassemble_code_(disassemble_code), 341 absolute_addresses_(absolute_addresses), 342 class_filter_(class_filter), 343 method_filter_(method_filter), 344 list_classes_(list_classes), 345 list_methods_(list_methods), 346 export_dex_location_(export_dex_location), 347 addr2instr_(addr2instr), 348 class_loader_(nullptr) {} 349 350 const bool dump_raw_mapping_table_; 351 const bool dump_raw_gc_map_; 352 const bool dump_vmap_; 353 const bool dump_code_info_stack_maps_; 354 const bool disassemble_code_; 355 const bool absolute_addresses_; 356 const char* const class_filter_; 357 const char* const method_filter_; 358 const bool list_classes_; 359 const bool list_methods_; 360 const char* const export_dex_location_; 361 uint32_t addr2instr_; 362 Handle<mirror::ClassLoader>* class_loader_; 363}; 364 365class OatDumper { 366 public: 367 OatDumper(const OatFile& oat_file, const OatDumperOptions& options) 368 : oat_file_(oat_file), 369 oat_dex_files_(oat_file.GetOatDexFiles()), 370 options_(options), 371 resolved_addr2instr_(0), 372 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()), 373 disassembler_(Disassembler::Create(instruction_set_, 374 new DisassemblerOptions(options_.absolute_addresses_, 375 oat_file.Begin(), 376 true /* can_read_literals_ */))) { 377 CHECK(options_.class_loader_ != nullptr); 378 CHECK(options_.class_filter_ != nullptr); 379 CHECK(options_.method_filter_ != nullptr); 380 AddAllOffsets(); 381 } 382 383 ~OatDumper() { 384 delete disassembler_; 385 } 386 387 InstructionSet GetInstructionSet() { 388 return instruction_set_; 389 } 390 391 bool Dump(std::ostream& os) { 392 bool success = true; 393 const OatHeader& oat_header = oat_file_.GetOatHeader(); 394 395 os << "MAGIC:\n"; 396 os << oat_header.GetMagic() << "\n\n"; 397 398 os << "CHECKSUM:\n"; 399 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum()); 400 401 os << "INSTRUCTION SET:\n"; 402 os << oat_header.GetInstructionSet() << "\n\n"; 403 404 { 405 std::unique_ptr<const InstructionSetFeatures> features( 406 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(), 407 oat_header.GetInstructionSetFeaturesBitmap())); 408 os << "INSTRUCTION SET FEATURES:\n"; 409 os << features->GetFeatureString() << "\n\n"; 410 } 411 412 os << "DEX FILE COUNT:\n"; 413 os << oat_header.GetDexFileCount() << "\n\n"; 414 415#define DUMP_OAT_HEADER_OFFSET(label, offset) \ 416 os << label " OFFSET:\n"; \ 417 os << StringPrintf("0x%08x", oat_header.offset()); \ 418 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \ 419 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \ 420 } \ 421 os << StringPrintf("\n\n"); 422 423 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset); 424 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE", 425 GetInterpreterToInterpreterBridgeOffset); 426 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE", 427 GetInterpreterToCompiledCodeBridgeOffset); 428 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP", 429 GetJniDlsymLookupOffset); 430 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE", 431 GetQuickGenericJniTrampolineOffset); 432 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE", 433 GetQuickImtConflictTrampolineOffset); 434 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE", 435 GetQuickResolutionTrampolineOffset); 436 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE", 437 GetQuickToInterpreterBridgeOffset); 438#undef DUMP_OAT_HEADER_OFFSET 439 440 os << "IMAGE PATCH DELTA:\n"; 441 os << StringPrintf("%d (0x%08x)\n\n", 442 oat_header.GetImagePatchDelta(), 443 oat_header.GetImagePatchDelta()); 444 445 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n"; 446 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum()); 447 448 os << "IMAGE FILE LOCATION OAT BEGIN:\n"; 449 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin()); 450 451 // Print the key-value store. 452 { 453 os << "KEY VALUE STORE:\n"; 454 size_t index = 0; 455 const char* key; 456 const char* value; 457 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) { 458 os << key << " = " << value << "\n"; 459 index++; 460 } 461 os << "\n"; 462 } 463 464 if (options_.absolute_addresses_) { 465 os << "BEGIN:\n"; 466 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n"; 467 468 os << "END:\n"; 469 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n"; 470 } 471 472 os << "SIZE:\n"; 473 os << oat_file_.Size() << "\n\n"; 474 475 os << std::flush; 476 477 // If set, adjust relative address to be searched 478 if (options_.addr2instr_ != 0) { 479 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset(); 480 os << "SEARCH ADDRESS (executable offset + input):\n"; 481 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_); 482 } 483 484 for (size_t i = 0; i < oat_dex_files_.size(); i++) { 485 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; 486 CHECK(oat_dex_file != nullptr); 487 488 // If file export selected skip file analysis 489 if (options_.export_dex_location_) { 490 if (!ExportDexFile(os, *oat_dex_file)) { 491 success = false; 492 } 493 } else { 494 if (!DumpOatDexFile(os, *oat_dex_file)) { 495 success = false; 496 } 497 } 498 } 499 os << std::flush; 500 return success; 501 } 502 503 size_t ComputeSize(const void* oat_data) { 504 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() || 505 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) { 506 return 0; // Address not in oat file 507 } 508 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) - 509 reinterpret_cast<uintptr_t>(oat_file_.Begin()); 510 auto it = offsets_.upper_bound(begin_offset); 511 CHECK(it != offsets_.end()); 512 uintptr_t end_offset = *it; 513 return end_offset - begin_offset; 514 } 515 516 InstructionSet GetOatInstructionSet() { 517 return oat_file_.GetOatHeader().GetInstructionSet(); 518 } 519 520 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) { 521 for (size_t i = 0; i < oat_dex_files_.size(); i++) { 522 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; 523 CHECK(oat_dex_file != nullptr); 524 std::string error_msg; 525 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); 526 if (dex_file == nullptr) { 527 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() 528 << "': " << error_msg; 529 } else { 530 const char* descriptor = m->GetDeclaringClassDescriptor(); 531 const DexFile::ClassDef* class_def = 532 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor)); 533 if (class_def != nullptr) { 534 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def); 535 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); 536 size_t method_index = m->GetMethodIndex(); 537 return oat_class.GetOatMethod(method_index).GetQuickCode(); 538 } 539 } 540 } 541 return nullptr; 542 } 543 544 private: 545 void AddAllOffsets() { 546 // We don't know the length of the code for each method, but we need to know where to stop 547 // when disassembling. What we do know is that a region of code will be followed by some other 548 // region, so if we keep a sorted sequence of the start of each region, we can infer the length 549 // of a piece of code by using upper_bound to find the start of the next region. 550 for (size_t i = 0; i < oat_dex_files_.size(); i++) { 551 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; 552 CHECK(oat_dex_file != nullptr); 553 std::string error_msg; 554 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg); 555 if (dex_file == nullptr) { 556 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() 557 << "': " << error_msg; 558 continue; 559 } 560 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader())); 561 for (size_t class_def_index = 0; 562 class_def_index < dex_file->NumClassDefs(); 563 class_def_index++) { 564 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); 565 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index); 566 const uint8_t* class_data = dex_file->GetClassData(class_def); 567 if (class_data != nullptr) { 568 ClassDataItemIterator it(*dex_file, class_data); 569 SkipAllFields(it); 570 uint32_t class_method_index = 0; 571 while (it.HasNextDirectMethod()) { 572 AddOffsets(oat_class.GetOatMethod(class_method_index++)); 573 it.Next(); 574 } 575 while (it.HasNextVirtualMethod()) { 576 AddOffsets(oat_class.GetOatMethod(class_method_index++)); 577 it.Next(); 578 } 579 } 580 } 581 } 582 583 // If the last thing in the file is code for a method, there won't be an offset for the "next" 584 // thing. Instead of having a special case in the upper_bound code, let's just add an entry 585 // for the end of the file. 586 offsets_.insert(oat_file_.Size()); 587 } 588 589 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) { 590 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific. 591 } 592 593 void AddOffsets(const OatFile::OatMethod& oat_method) { 594 uint32_t code_offset = oat_method.GetCodeOffset(); 595 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) { 596 code_offset &= ~0x1; 597 } 598 offsets_.insert(code_offset); 599 offsets_.insert(oat_method.GetMappingTableOffset()); 600 offsets_.insert(oat_method.GetVmapTableOffset()); 601 offsets_.insert(oat_method.GetGcMapOffset()); 602 } 603 604 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) { 605 bool success = true; 606 bool stop_analysis = false; 607 os << "OatDexFile:\n"; 608 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str()); 609 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum()); 610 611 // Create the verifier early. 612 613 std::string error_msg; 614 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg); 615 if (dex_file == nullptr) { 616 os << "NOT FOUND: " << error_msg << "\n\n"; 617 os << std::flush; 618 return false; 619 } 620 621 VariableIndentationOutputStream vios(&os); 622 ScopedIndentation indent1(&vios); 623 for (size_t class_def_index = 0; 624 class_def_index < dex_file->NumClassDefs(); 625 class_def_index++) { 626 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); 627 const char* descriptor = dex_file->GetClassDescriptor(class_def); 628 629 // TODO: Support regex 630 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) { 631 continue; 632 } 633 634 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index); 635 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index); 636 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)", 637 class_def_index, descriptor, oat_class_offset, class_def.class_idx_) 638 << " (" << oat_class.GetStatus() << ")" 639 << " (" << oat_class.GetType() << ")\n"; 640 // TODO: include bitmap here if type is kOatClassSomeCompiled? 641 if (options_.list_classes_) continue; 642 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) { 643 success = false; 644 } 645 if (stop_analysis) { 646 os << std::flush; 647 return success; 648 } 649 } 650 651 os << std::flush; 652 return success; 653 } 654 655 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) { 656 std::string error_msg; 657 std::string dex_file_location = oat_dex_file.GetDexFileLocation(); 658 659 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg); 660 if (dex_file == nullptr) { 661 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg; 662 return false; 663 } 664 size_t fsize = oat_dex_file.FileSize(); 665 666 // Some quick checks just in case 667 if (fsize == 0 || fsize < sizeof(DexFile::Header)) { 668 os << "Invalid dex file\n"; 669 return false; 670 } 671 672 // Verify output directory exists 673 if (!OS::DirectoryExists(options_.export_dex_location_)) { 674 // TODO: Extend OS::DirectoryExists if symlink support is required 675 os << options_.export_dex_location_ << " output directory not found or symlink\n"; 676 return false; 677 } 678 679 // Beautify path names 680 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) { 681 return false; 682 } 683 684 std::string dex_orig_name; 685 size_t dex_orig_pos = dex_file_location.rfind('/'); 686 if (dex_orig_pos == std::string::npos) 687 dex_orig_name = dex_file_location; 688 else 689 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1); 690 691 // A more elegant approach to efficiently name user installed apps is welcome 692 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) { 693 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1); 694 size_t apk_orig_pos = dex_file_location.rfind('/'); 695 if (apk_orig_pos != std::string::npos) { 696 dex_orig_name = dex_file_location.substr(++apk_orig_pos); 697 } 698 } 699 700 std::string out_dex_path(options_.export_dex_location_); 701 if (out_dex_path.back() != '/') { 702 out_dex_path.append("/"); 703 } 704 out_dex_path.append(dex_orig_name); 705 out_dex_path.append("_export.dex"); 706 if (out_dex_path.length() > PATH_MAX) { 707 return false; 708 } 709 710 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str())); 711 if (file.get() == nullptr) { 712 os << "Failed to open output dex file " << out_dex_path; 713 return false; 714 } 715 716 if (!file->WriteFully(dex_file->Begin(), fsize)) { 717 os << "Failed to write dex file"; 718 file->Erase(); 719 return false; 720 } 721 722 if (file->FlushCloseOrErase() != 0) { 723 os << "Flush and close failed"; 724 return false; 725 } 726 727 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize); 728 os << std::flush; 729 730 return true; 731 } 732 733 static void SkipAllFields(ClassDataItemIterator& it) { 734 while (it.HasNextStaticField()) { 735 it.Next(); 736 } 737 while (it.HasNextInstanceField()) { 738 it.Next(); 739 } 740 } 741 742 bool DumpOatClass(VariableIndentationOutputStream* vios, 743 const OatFile::OatClass& oat_class, const DexFile& dex_file, 744 const DexFile::ClassDef& class_def, bool* stop_analysis) { 745 bool success = true; 746 bool addr_found = false; 747 const uint8_t* class_data = dex_file.GetClassData(class_def); 748 if (class_data == nullptr) { // empty class such as a marker interface? 749 vios->Stream() << std::flush; 750 return success; 751 } 752 ClassDataItemIterator it(dex_file, class_data); 753 SkipAllFields(it); 754 uint32_t class_method_index = 0; 755 while (it.HasNextDirectMethod()) { 756 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file, 757 it.GetMemberIndex(), it.GetMethodCodeItem(), 758 it.GetRawMemberAccessFlags(), &addr_found)) { 759 success = false; 760 } 761 if (addr_found) { 762 *stop_analysis = true; 763 return success; 764 } 765 class_method_index++; 766 it.Next(); 767 } 768 while (it.HasNextVirtualMethod()) { 769 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file, 770 it.GetMemberIndex(), it.GetMethodCodeItem(), 771 it.GetRawMemberAccessFlags(), &addr_found)) { 772 success = false; 773 } 774 if (addr_found) { 775 *stop_analysis = true; 776 return success; 777 } 778 class_method_index++; 779 it.Next(); 780 } 781 DCHECK(!it.HasNext()); 782 vios->Stream() << std::flush; 783 return success; 784 } 785 786 static constexpr uint32_t kPrologueBytes = 16; 787 788 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes. 789 static constexpr uint32_t kMaxCodeSize = 100 * 1000; 790 791 bool DumpOatMethod(VariableIndentationOutputStream* vios, 792 const DexFile::ClassDef& class_def, 793 uint32_t class_method_index, 794 const OatFile::OatClass& oat_class, const DexFile& dex_file, 795 uint32_t dex_method_idx, const DexFile::CodeItem* code_item, 796 uint32_t method_access_flags, bool* addr_found) { 797 bool success = true; 798 799 // TODO: Support regex 800 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx)); 801 if (method_name.find(options_.method_filter_) == std::string::npos) { 802 return success; 803 } 804 805 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true); 806 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n", 807 class_method_index, pretty_method.c_str(), 808 dex_method_idx); 809 if (options_.list_methods_) return success; 810 811 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index); 812 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index); 813 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index); 814 uint32_t code_offset = oat_method.GetCodeOffset(); 815 uint32_t code_size = oat_method.GetQuickCodeSize(); 816 if (resolved_addr2instr_ != 0) { 817 if (resolved_addr2instr_ > code_offset + code_size) { 818 return success; 819 } else { 820 *addr_found = true; // stop analyzing file at next iteration 821 } 822 } 823 824 // Everything below is indented at least once. 825 ScopedIndentation indent1(vios); 826 827 { 828 vios->Stream() << "DEX CODE:\n"; 829 ScopedIndentation indent2(vios); 830 DumpDexCode(vios->Stream(), dex_file, code_item); 831 } 832 833 std::unique_ptr<StackHandleScope<1>> hs; 834 std::unique_ptr<verifier::MethodVerifier> verifier; 835 if (Runtime::Current() != nullptr) { 836 // We need to have the handle scope stay live until after the verifier since the verifier has 837 // a handle to the dex cache from hs. 838 hs.reset(new StackHandleScope<1>(Thread::Current())); 839 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n"; 840 ScopedIndentation indent2(vios); 841 verifier.reset(DumpVerifier(vios, hs.get(), 842 dex_method_idx, &dex_file, class_def, code_item, 843 method_access_flags)); 844 } 845 { 846 vios->Stream() << "OatMethodOffsets "; 847 if (options_.absolute_addresses_) { 848 vios->Stream() << StringPrintf("%p ", oat_method_offsets); 849 } 850 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset); 851 if (oat_method_offsets_offset > oat_file_.Size()) { 852 vios->Stream() << StringPrintf( 853 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n", 854 oat_method_offsets_offset, oat_file_.Size()); 855 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read. 856 vios->Stream() << std::flush; 857 return false; 858 } 859 860 ScopedIndentation indent2(vios); 861 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset); 862 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset()); 863 if (aligned_code_begin > oat_file_.Size()) { 864 vios->Stream() << StringPrintf("WARNING: " 865 "code offset 0x%08x is past end of file 0x%08zx.\n", 866 aligned_code_begin, oat_file_.Size()); 867 success = false; 868 } 869 vios->Stream() << "\n"; 870 871 vios->Stream() << "gc_map: "; 872 if (options_.absolute_addresses_) { 873 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap()); 874 } 875 uint32_t gc_map_offset = oat_method.GetGcMapOffset(); 876 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset); 877 if (gc_map_offset > oat_file_.Size()) { 878 vios->Stream() << StringPrintf("WARNING: " 879 "gc map table offset 0x%08x is past end of file 0x%08zx.\n", 880 gc_map_offset, oat_file_.Size()); 881 success = false; 882 } else if (options_.dump_raw_gc_map_) { 883 ScopedIndentation indent3(vios); 884 DumpGcMap(vios->Stream(), oat_method, code_item); 885 } 886 } 887 { 888 vios->Stream() << "OatQuickMethodHeader "; 889 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset(); 890 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader(); 891 892 if (options_.absolute_addresses_) { 893 vios->Stream() << StringPrintf("%p ", method_header); 894 } 895 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset); 896 if (method_header_offset > oat_file_.Size()) { 897 vios->Stream() << StringPrintf( 898 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n", 899 method_header_offset, oat_file_.Size()); 900 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read. 901 vios->Stream() << std::flush; 902 return false; 903 } 904 905 ScopedIndentation indent2(vios); 906 vios->Stream() << "mapping_table: "; 907 if (options_.absolute_addresses_) { 908 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable()); 909 } 910 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset(); 911 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset()); 912 if (mapping_table_offset > oat_file_.Size()) { 913 vios->Stream() << StringPrintf("WARNING: " 914 "mapping table offset 0x%08x is past end of file 0x%08zx. " 915 "mapping table offset was loaded from offset 0x%08x.\n", 916 mapping_table_offset, oat_file_.Size(), 917 oat_method.GetMappingTableOffsetOffset()); 918 success = false; 919 } else if (options_.dump_raw_mapping_table_) { 920 ScopedIndentation indent3(vios); 921 DumpMappingTable(vios, oat_method); 922 } 923 924 vios->Stream() << "vmap_table: "; 925 if (options_.absolute_addresses_) { 926 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable()); 927 } 928 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset(); 929 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset); 930 if (vmap_table_offset > oat_file_.Size()) { 931 vios->Stream() << StringPrintf("WARNING: " 932 "vmap table offset 0x%08x is past end of file 0x%08zx. " 933 "vmap table offset was loaded from offset 0x%08x.\n", 934 vmap_table_offset, oat_file_.Size(), 935 oat_method.GetVmapTableOffsetOffset()); 936 success = false; 937 } else if (options_.dump_vmap_) { 938 DumpVmapData(vios, oat_method, code_item); 939 } 940 } 941 { 942 vios->Stream() << "QuickMethodFrameInfo\n"; 943 944 ScopedIndentation indent2(vios); 945 vios->Stream() 946 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes()); 947 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask()); 948 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false); 949 vios->Stream() << "\n"; 950 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask()); 951 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true); 952 vios->Stream() << "\n"; 953 } 954 { 955 // Based on spill masks from QuickMethodFrameInfo so placed 956 // after it is dumped, but useful for understanding quick 957 // code, so dumped here. 958 ScopedIndentation indent2(vios); 959 DumpVregLocations(vios->Stream(), oat_method, code_item); 960 } 961 { 962 vios->Stream() << "CODE: "; 963 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset(); 964 if (code_size_offset > oat_file_.Size()) { 965 ScopedIndentation indent2(vios); 966 vios->Stream() << StringPrintf("WARNING: " 967 "code size offset 0x%08x is past end of file 0x%08zx.", 968 code_size_offset, oat_file_.Size()); 969 success = false; 970 } else { 971 const void* code = oat_method.GetQuickCode(); 972 uint32_t aligned_code_begin = AlignCodeOffset(code_offset); 973 uint64_t aligned_code_end = aligned_code_begin + code_size; 974 975 if (options_.absolute_addresses_) { 976 vios->Stream() << StringPrintf("%p ", code); 977 } 978 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n", 979 code_offset, 980 code_size_offset, 981 code_size, 982 code != nullptr ? "..." : ""); 983 984 ScopedIndentation indent2(vios); 985 if (aligned_code_begin > oat_file_.Size()) { 986 vios->Stream() << StringPrintf("WARNING: " 987 "start of code at 0x%08x is past end of file 0x%08zx.", 988 aligned_code_begin, oat_file_.Size()); 989 success = false; 990 } else if (aligned_code_end > oat_file_.Size()) { 991 vios->Stream() << StringPrintf( 992 "WARNING: " 993 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. " 994 "code size is 0x%08x loaded from offset 0x%08x.\n", 995 aligned_code_end, oat_file_.Size(), 996 code_size, code_size_offset); 997 success = false; 998 if (options_.disassemble_code_) { 999 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) { 1000 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes); 1001 } 1002 } 1003 } else if (code_size > kMaxCodeSize) { 1004 vios->Stream() << StringPrintf( 1005 "WARNING: " 1006 "code size %d is bigger than max expected threshold of %d. " 1007 "code size is 0x%08x loaded from offset 0x%08x.\n", 1008 code_size, kMaxCodeSize, 1009 code_size, code_size_offset); 1010 success = false; 1011 if (options_.disassemble_code_) { 1012 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) { 1013 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes); 1014 } 1015 } 1016 } else if (options_.disassemble_code_) { 1017 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0); 1018 } 1019 } 1020 } 1021 vios->Stream() << std::flush; 1022 return success; 1023 } 1024 1025 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) { 1026 if (spill_mask == 0) { 1027 return; 1028 } 1029 os << "("; 1030 for (size_t i = 0; i < 32; i++) { 1031 if ((spill_mask & (1 << i)) != 0) { 1032 if (is_float) { 1033 os << "fr" << i; 1034 } else { 1035 os << "r" << i; 1036 } 1037 spill_mask ^= 1 << i; // clear bit 1038 if (spill_mask != 0) { 1039 os << ", "; 1040 } else { 1041 break; 1042 } 1043 } 1044 } 1045 os << ")"; 1046 } 1047 1048 // Display data stored at the the vmap offset of an oat method. 1049 void DumpVmapData(VariableIndentationOutputStream* vios, 1050 const OatFile::OatMethod& oat_method, 1051 const DexFile::CodeItem* code_item) { 1052 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) { 1053 // The optimizing compiler outputs its CodeInfo data in the vmap table. 1054 const void* raw_code_info = oat_method.GetVmapTable(); 1055 if (raw_code_info != nullptr) { 1056 CodeInfo code_info(raw_code_info); 1057 DCHECK(code_item != nullptr); 1058 ScopedIndentation indent1(vios); 1059 DumpCodeInfo(vios, code_info, oat_method, *code_item); 1060 } 1061 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) { 1062 // We don't encode the size in the table, so just emit that we have quickened 1063 // information. 1064 ScopedIndentation indent(vios); 1065 vios->Stream() << "quickened data\n"; 1066 } else { 1067 // Otherwise, display the vmap table. 1068 const uint8_t* raw_table = oat_method.GetVmapTable(); 1069 if (raw_table != nullptr) { 1070 VmapTable vmap_table(raw_table); 1071 DumpVmapTable(vios->Stream(), oat_method, vmap_table); 1072 } 1073 } 1074 } 1075 1076 // Display a CodeInfo object emitted by the optimizing compiler. 1077 void DumpCodeInfo(VariableIndentationOutputStream* vios, 1078 const CodeInfo& code_info, 1079 const OatFile::OatMethod& oat_method, 1080 const DexFile::CodeItem& code_item) { 1081 code_info.Dump(vios, 1082 oat_method.GetCodeOffset(), 1083 code_item.registers_size_, 1084 options_.dump_code_info_stack_maps_); 1085 } 1086 1087 // Display a vmap table. 1088 void DumpVmapTable(std::ostream& os, 1089 const OatFile::OatMethod& oat_method, 1090 const VmapTable& vmap_table) { 1091 bool first = true; 1092 bool processing_fp = false; 1093 uint32_t spill_mask = oat_method.GetCoreSpillMask(); 1094 for (size_t i = 0; i < vmap_table.Size(); i++) { 1095 uint16_t dex_reg = vmap_table[i]; 1096 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i, 1097 processing_fp ? kFloatVReg : kIntVReg); 1098 os << (first ? "v" : ", v") << dex_reg; 1099 if (!processing_fp) { 1100 os << "/r" << cpu_reg; 1101 } else { 1102 os << "/fr" << cpu_reg; 1103 } 1104 first = false; 1105 if (!processing_fp && dex_reg == 0xFFFF) { 1106 processing_fp = true; 1107 spill_mask = oat_method.GetFpSpillMask(); 1108 } 1109 } 1110 os << "\n"; 1111 } 1112 1113 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method, 1114 const DexFile::CodeItem* code_item) { 1115 if (code_item != nullptr) { 1116 size_t num_locals_ins = code_item->registers_size_; 1117 size_t num_ins = code_item->ins_size_; 1118 size_t num_locals = num_locals_ins - num_ins; 1119 size_t num_outs = code_item->outs_size_; 1120 1121 os << "vr_stack_locations:"; 1122 for (size_t reg = 0; reg <= num_locals_ins; reg++) { 1123 // For readability, delimit the different kinds of VRs. 1124 if (reg == num_locals_ins) { 1125 os << "\n\tmethod*:"; 1126 } else if (reg == num_locals && num_ins > 0) { 1127 os << "\n\tins:"; 1128 } else if (reg == 0 && num_locals > 0) { 1129 os << "\n\tlocals:"; 1130 } 1131 1132 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode( 1133 code_item, 1134 oat_method.GetCoreSpillMask(), 1135 oat_method.GetFpSpillMask(), 1136 oat_method.GetFrameSizeInBytes(), 1137 reg, 1138 GetInstructionSet()); 1139 os << " v" << reg << "[sp + #" << offset << "]"; 1140 } 1141 1142 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) { 1143 if (out_reg == 0) { 1144 os << "\n\touts:"; 1145 } 1146 1147 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet()); 1148 os << " v" << out_reg << "[sp + #" << offset << "]"; 1149 } 1150 1151 os << "\n"; 1152 } 1153 } 1154 1155 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method, 1156 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) { 1157 const uint8_t* raw_table = oat_method.GetVmapTable(); 1158 if (raw_table != nullptr) { 1159 const VmapTable vmap_table(raw_table); 1160 uint32_t vmap_offset; 1161 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) { 1162 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); 1163 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask() 1164 : oat_method.GetCoreSpillMask(); 1165 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind); 1166 } else { 1167 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode( 1168 code_item, 1169 oat_method.GetCoreSpillMask(), 1170 oat_method.GetFpSpillMask(), 1171 oat_method.GetFrameSizeInBytes(), 1172 reg, 1173 GetInstructionSet()); 1174 os << "[sp + #" << offset << "]"; 1175 } 1176 } 1177 } 1178 1179 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method, 1180 const DexFile::CodeItem* code_item, 1181 size_t num_regs, const uint8_t* reg_bitmap) { 1182 bool first = true; 1183 for (size_t reg = 0; reg < num_regs; reg++) { 1184 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { 1185 if (first) { 1186 os << " v" << reg << " ("; 1187 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); 1188 os << ")"; 1189 first = false; 1190 } else { 1191 os << ", v" << reg << " ("; 1192 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); 1193 os << ")"; 1194 } 1195 } 1196 } 1197 if (first) { 1198 os << "No registers in GC map\n"; 1199 } else { 1200 os << "\n"; 1201 } 1202 } 1203 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method, 1204 const DexFile::CodeItem* code_item) { 1205 const uint8_t* gc_map_raw = oat_method.GetGcMap(); 1206 if (gc_map_raw == nullptr) { 1207 return; // No GC map. 1208 } 1209 const void* quick_code = oat_method.GetQuickCode(); 1210 NativePcOffsetToReferenceMap map(gc_map_raw); 1211 for (size_t entry = 0; entry < map.NumEntries(); entry++) { 1212 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) + 1213 map.GetNativePcOffset(entry); 1214 os << StringPrintf("%p", native_pc); 1215 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry)); 1216 } 1217 } 1218 1219 void DumpMappingTable(VariableIndentationOutputStream* vios, 1220 const OatFile::OatMethod& oat_method) { 1221 const void* quick_code = oat_method.GetQuickCode(); 1222 if (quick_code == nullptr) { 1223 return; 1224 } 1225 MappingTable table(oat_method.GetMappingTable()); 1226 if (table.TotalSize() != 0) { 1227 if (table.PcToDexSize() != 0) { 1228 typedef MappingTable::PcToDexIterator It; 1229 vios->Stream() << "suspend point mappings {\n"; 1230 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { 1231 ScopedIndentation indent1(vios); 1232 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc()); 1233 } 1234 vios->Stream() << "}\n"; 1235 } 1236 if (table.DexToPcSize() != 0) { 1237 typedef MappingTable::DexToPcIterator It; 1238 vios->Stream() << "catch entry mappings {\n"; 1239 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { 1240 ScopedIndentation indent1(vios); 1241 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc()); 1242 } 1243 vios->Stream() << "}\n"; 1244 } 1245 } 1246 } 1247 1248 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios, 1249 const OatFile::OatMethod& oat_method, 1250 const DexFile::CodeItem* code_item, 1251 size_t offset, 1252 bool suspend_point_mapping) { 1253 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) { 1254 if (suspend_point_mapping) { 1255 ScopedIndentation indent1(vios); 1256 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset); 1257 } 1258 // The return value is not used in the case of a method compiled 1259 // with the optimizing compiler. 1260 return DexFile::kDexNoIndex; 1261 } else { 1262 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping); 1263 } 1264 } 1265 1266 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, 1267 size_t offset, bool suspend_point_mapping) { 1268 MappingTable table(oat_method.GetMappingTable()); 1269 if (suspend_point_mapping && table.PcToDexSize() > 0) { 1270 typedef MappingTable::PcToDexIterator It; 1271 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { 1272 if (offset == cur.NativePcOffset()) { 1273 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc()); 1274 return cur.DexPc(); 1275 } 1276 } 1277 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) { 1278 typedef MappingTable::DexToPcIterator It; 1279 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { 1280 if (offset == cur.NativePcOffset()) { 1281 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc()); 1282 return cur.DexPc(); 1283 } 1284 } 1285 } 1286 return DexFile::kDexNoIndex; 1287 } 1288 1289 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method, 1290 const DexFile::CodeItem* code_item, size_t native_pc_offset) { 1291 const uint8_t* gc_map_raw = oat_method.GetGcMap(); 1292 if (gc_map_raw != nullptr) { 1293 NativePcOffsetToReferenceMap map(gc_map_raw); 1294 if (map.HasEntry(native_pc_offset)) { 1295 size_t num_regs = map.RegWidth() * 8; 1296 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset); 1297 bool first = true; 1298 for (size_t reg = 0; reg < num_regs; reg++) { 1299 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { 1300 if (first) { 1301 os << "GC map objects: v" << reg << " ("; 1302 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); 1303 os << ")"; 1304 first = false; 1305 } else { 1306 os << ", v" << reg << " ("; 1307 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); 1308 os << ")"; 1309 } 1310 } 1311 } 1312 if (!first) { 1313 os << "\n"; 1314 } 1315 } 1316 } 1317 } 1318 1319 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier, 1320 const OatFile::OatMethod& oat_method, 1321 const DexFile::CodeItem* code_item, uint32_t dex_pc) { 1322 DCHECK(verifier != nullptr); 1323 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc); 1324 bool first = true; 1325 for (size_t reg = 0; reg < code_item->registers_size_; reg++) { 1326 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2)); 1327 if (kind != kUndefined) { 1328 if (first) { 1329 os << "VRegs: v"; 1330 first = false; 1331 } else { 1332 os << ", v"; 1333 } 1334 os << reg << " ("; 1335 switch (kind) { 1336 case kImpreciseConstant: 1337 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", "; 1338 DescribeVReg(os, oat_method, code_item, reg, kind); 1339 break; 1340 case kConstant: 1341 os << "Constant: " << kinds.at((reg * 2) + 1); 1342 break; 1343 default: 1344 DescribeVReg(os, oat_method, code_item, reg, kind); 1345 break; 1346 } 1347 os << ")"; 1348 } 1349 } 1350 if (!first) { 1351 os << "\n"; 1352 } 1353 } 1354 1355 1356 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) { 1357 if (code_item != nullptr) { 1358 size_t i = 0; 1359 while (i < code_item->insns_size_in_code_units_) { 1360 const Instruction* instruction = Instruction::At(&code_item->insns_[i]); 1361 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5) 1362 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str()); 1363 i += instruction->SizeInCodeUnits(); 1364 } 1365 } 1366 } 1367 1368 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by 1369 // the optimizing compiler? 1370 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method, 1371 const DexFile::CodeItem* code_item) { 1372 // If the native GC map is null and the Dex `code_item` is not 1373 // null, then this method has been compiled with the optimizing 1374 // compiler. 1375 return oat_method.GetQuickCode() != nullptr && 1376 oat_method.GetGcMap() == nullptr && 1377 code_item != nullptr; 1378 } 1379 1380 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by 1381 // the dextodex compiler? 1382 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method, 1383 const DexFile::CodeItem* code_item) { 1384 // If the quick code is null, the Dex `code_item` is not 1385 // null, and the vmap table is not null, then this method has been compiled 1386 // with the dextodex compiler. 1387 return oat_method.GetQuickCode() == nullptr && 1388 oat_method.GetVmapTable() != nullptr && 1389 code_item != nullptr; 1390 } 1391 1392 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios, 1393 const OatFile::OatMethod& oat_method, 1394 const DexFile::CodeItem* code_item, 1395 size_t offset) { 1396 // This method is only relevant for oat methods compiled with the 1397 // optimizing compiler. 1398 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)); 1399 1400 // The optimizing compiler outputs its CodeInfo data in the vmap table. 1401 const void* raw_code_info = oat_method.GetVmapTable(); 1402 if (raw_code_info != nullptr) { 1403 CodeInfo code_info(raw_code_info); 1404 StackMapEncoding encoding = code_info.ExtractEncoding(); 1405 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding); 1406 if (stack_map.IsValid()) { 1407 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(), 1408 code_item->registers_size_); 1409 } 1410 } 1411 } 1412 1413 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios, 1414 StackHandleScope<1>* hs, 1415 uint32_t dex_method_idx, 1416 const DexFile* dex_file, 1417 const DexFile::ClassDef& class_def, 1418 const DexFile::CodeItem* code_item, 1419 uint32_t method_access_flags) { 1420 if ((method_access_flags & kAccNative) == 0) { 1421 ScopedObjectAccess soa(Thread::Current()); 1422 Runtime* const runtime = Runtime::Current(); 1423 Handle<mirror::DexCache> dex_cache( 1424 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, 1425 runtime->GetLinearAlloc()))); 1426 DCHECK(options_.class_loader_ != nullptr); 1427 return verifier::MethodVerifier::VerifyMethodAndDump( 1428 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_, 1429 &class_def, code_item, nullptr, method_access_flags); 1430 } 1431 1432 return nullptr; 1433 } 1434 1435 void DumpCode(VariableIndentationOutputStream* vios, 1436 verifier::MethodVerifier* verifier, 1437 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item, 1438 bool bad_input, size_t code_size) { 1439 const void* quick_code = oat_method.GetQuickCode(); 1440 1441 if (code_size == 0) { 1442 code_size = oat_method.GetQuickCodeSize(); 1443 } 1444 if (code_size == 0 || quick_code == nullptr) { 1445 vios->Stream() << "NO CODE!\n"; 1446 return; 1447 } else { 1448 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code); 1449 size_t offset = 0; 1450 while (offset < code_size) { 1451 if (!bad_input) { 1452 DumpInformationAtOffset(vios, oat_method, code_item, offset, false); 1453 } 1454 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset); 1455 if (!bad_input) { 1456 uint32_t dex_pc = 1457 DumpInformationAtOffset(vios, oat_method, code_item, offset, true); 1458 if (dex_pc != DexFile::kDexNoIndex) { 1459 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset); 1460 if (verifier != nullptr) { 1461 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc); 1462 } 1463 } 1464 } 1465 } 1466 } 1467 } 1468 1469 const OatFile& oat_file_; 1470 const std::vector<const OatFile::OatDexFile*> oat_dex_files_; 1471 const OatDumperOptions& options_; 1472 uint32_t resolved_addr2instr_; 1473 InstructionSet instruction_set_; 1474 std::set<uintptr_t> offsets_; 1475 Disassembler* disassembler_; 1476}; 1477 1478class ImageDumper { 1479 public: 1480 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space, 1481 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options) 1482 : os_(os), 1483 vios_(os), 1484 indent1_(&vios_), 1485 image_space_(image_space), 1486 image_header_(image_header), 1487 oat_dumper_options_(oat_dumper_options) {} 1488 1489 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) { 1490 std::ostream& os = *os_; 1491 std::ostream& indent_os = vios_.Stream(); 1492 1493 os << "MAGIC: " << image_header_.GetMagic() << "\n\n"; 1494 1495 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; 1496 1497 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n"; 1498 1499 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) { 1500 auto section = static_cast<ImageHeader::ImageSections>(i); 1501 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n"; 1502 } 1503 1504 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()); 1505 1506 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n"; 1507 1508 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n"; 1509 1510 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n"; 1511 1512 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n"; 1513 1514 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n"; 1515 1516 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n"; 1517 1518 { 1519 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n"; 1520 static_assert(arraysize(image_roots_descriptions_) == 1521 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match"); 1522 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { 1523 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); 1524 const char* image_root_description = image_roots_descriptions_[i]; 1525 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root); 1526 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object); 1527 if (image_root_object->IsObjectArray()) { 1528 mirror::ObjectArray<mirror::Object>* image_root_object_array 1529 = image_root_object->AsObjectArray<mirror::Object>(); 1530 ScopedIndentation indent2(&vios_); 1531 for (int j = 0; j < image_root_object_array->GetLength(); j++) { 1532 mirror::Object* value = image_root_object_array->Get(j); 1533 size_t run = 0; 1534 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) { 1535 if (value == image_root_object_array->Get(k)) { 1536 run++; 1537 } else { 1538 break; 1539 } 1540 } 1541 if (run == 0) { 1542 indent_os << StringPrintf("%d: ", j); 1543 } else { 1544 indent_os << StringPrintf("%d to %zd: ", j, j + run); 1545 j = j + run; 1546 } 1547 if (value != nullptr) { 1548 PrettyObjectValue(indent_os, value->GetClass(), value); 1549 } else { 1550 indent_os << j << ": null\n"; 1551 } 1552 } 1553 } 1554 } 1555 } 1556 1557 { 1558 os << "METHOD ROOTS\n"; 1559 static_assert(arraysize(image_methods_descriptions_) == 1560 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match"); 1561 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) { 1562 auto image_root = static_cast<ImageHeader::ImageMethod>(i); 1563 const char* description = image_methods_descriptions_[i]; 1564 auto* image_method = image_header_.GetImageMethod(image_root); 1565 indent_os << StringPrintf("%s: %p\n", description, image_method); 1566 } 1567 } 1568 os << "\n"; 1569 1570 Runtime* const runtime = Runtime::Current(); 1571 ClassLinker* class_linker = runtime->GetClassLinker(); 1572 std::string image_filename = image_space_.GetImageFilename(); 1573 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename); 1574 os << "OAT LOCATION: " << oat_location; 1575 os << "\n"; 1576 std::string error_msg; 1577 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation( 1578 oat_location); 1579 if (oat_file == nullptr) { 1580 oat_file = OatFile::Open(oat_location, oat_location, 1581 nullptr, nullptr, false, nullptr, 1582 &error_msg); 1583 if (oat_file == nullptr) { 1584 os << "NOT FOUND: " << error_msg << "\n"; 1585 return false; 1586 } 1587 } 1588 os << "\n"; 1589 1590 stats_.oat_file_bytes = oat_file->Size(); 1591 1592 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_)); 1593 1594 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { 1595 CHECK(oat_dex_file != nullptr); 1596 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(), 1597 oat_dex_file->FileSize())); 1598 } 1599 1600 os << "OBJECTS:\n" << std::flush; 1601 1602 // Loop through all the image spaces and dump their objects. 1603 gc::Heap* heap = runtime->GetHeap(); 1604 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces(); 1605 Thread* self = Thread::Current(); 1606 { 1607 { 1608 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); 1609 heap->FlushAllocStack(); 1610 } 1611 // Since FlushAllocStack() above resets the (active) allocation 1612 // stack. Need to revoke the thread-local allocation stacks that 1613 // point into it. 1614 ScopedThreadSuspension sts(self, kNative); 1615 ScopedSuspendAll ssa(__FUNCTION__); 1616 heap->RevokeAllThreadLocalAllocationStacks(self); 1617 } 1618 { 1619 // Mark dex caches. 1620 dex_caches_.clear(); 1621 { 1622 ReaderMutexLock mu(self, *class_linker->DexLock()); 1623 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) { 1624 mirror::DexCache* dex_cache = 1625 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root)); 1626 if (dex_cache != nullptr) { 1627 dex_caches_.insert(dex_cache); 1628 } 1629 } 1630 } 1631 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); 1632 for (const auto& space : spaces) { 1633 if (space->IsImageSpace()) { 1634 auto* image_space = space->AsImageSpace(); 1635 // Dump the normal objects before ArtMethods. 1636 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this); 1637 indent_os << "\n"; 1638 // TODO: Dump fields. 1639 // Dump methods after. 1640 const auto& methods_section = image_header_.GetMethodsSection(); 1641 const size_t pointer_size = 1642 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()); 1643 DumpArtMethodVisitor visitor(this); 1644 methods_section.VisitPackedArtMethods(&visitor, image_space->Begin(), pointer_size); 1645 } 1646 } 1647 // Dump the large objects separately. 1648 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this); 1649 indent_os << "\n"; 1650 } 1651 os << "STATS:\n" << std::flush; 1652 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str())); 1653 if (file.get() == nullptr) { 1654 LOG(WARNING) << "Failed to find image in " << image_filename; 1655 } 1656 if (file.get() != nullptr) { 1657 stats_.file_bytes = file->GetLength(); 1658 } 1659 size_t header_bytes = sizeof(ImageHeader); 1660 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap); 1661 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields); 1662 const auto& method_section = image_header_.GetMethodsSection(); 1663 const auto& dex_cache_arrays_section = image_header_.GetImageSection( 1664 ImageHeader::kSectionDexCacheArrays); 1665 const auto& intern_section = image_header_.GetImageSection( 1666 ImageHeader::kSectionInternedStrings); 1667 stats_.header_bytes = header_bytes; 1668 stats_.alignment_bytes += RoundUp(header_bytes, kObjectAlignment) - header_bytes; 1669 // Add padding between the field and method section. 1670 // (Field section is 4-byte aligned, method section is 8-byte aligned on 64-bit targets.) 1671 stats_.alignment_bytes += method_section.Offset() - 1672 (field_section.Offset() + field_section.Size()); 1673 // Add padding between the dex cache arrays section and the intern table. (Dex cache 1674 // arrays section is 4-byte aligned on 32-bit targets, intern table is 8-byte aligned.) 1675 stats_.alignment_bytes += intern_section.Offset() - 1676 (dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size()); 1677 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize(); 1678 stats_.bitmap_bytes += bitmap_section.Size(); 1679 stats_.art_field_bytes += field_section.Size(); 1680 stats_.art_method_bytes += method_section.Size(); 1681 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size(); 1682 stats_.interned_strings_bytes += intern_section.Size(); 1683 stats_.Dump(os, indent_os); 1684 os << "\n"; 1685 1686 os << std::flush; 1687 1688 return oat_dumper_->Dump(os); 1689 } 1690 1691 private: 1692 class DumpArtMethodVisitor : public ArtMethodVisitor { 1693 public: 1694 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {} 1695 1696 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) { 1697 std::ostream& indent_os = image_dumper_->vios_.Stream(); 1698 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n"; 1699 image_dumper_->DumpMethod(method, image_dumper_, indent_os); 1700 indent_os << "\n"; 1701 } 1702 1703 private: 1704 ImageDumper* const image_dumper_; 1705 }; 1706 1707 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value) 1708 SHARED_REQUIRES(Locks::mutator_lock_) { 1709 CHECK(type != nullptr); 1710 if (value == nullptr) { 1711 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str()); 1712 } else if (type->IsStringClass()) { 1713 mirror::String* string = value->AsString(); 1714 os << StringPrintf("%p String: %s\n", string, 1715 PrintableString(string->ToModifiedUtf8().c_str()).c_str()); 1716 } else if (type->IsClassClass()) { 1717 mirror::Class* klass = value->AsClass(); 1718 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str()); 1719 } else { 1720 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str()); 1721 } 1722 } 1723 1724 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj) 1725 SHARED_REQUIRES(Locks::mutator_lock_) { 1726 os << StringPrintf("%s: ", field->GetName()); 1727 switch (field->GetTypeAsPrimitiveType()) { 1728 case Primitive::kPrimLong: 1729 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj)); 1730 break; 1731 case Primitive::kPrimDouble: 1732 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj)); 1733 break; 1734 case Primitive::kPrimFloat: 1735 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj)); 1736 break; 1737 case Primitive::kPrimInt: 1738 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj)); 1739 break; 1740 case Primitive::kPrimChar: 1741 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj)); 1742 break; 1743 case Primitive::kPrimShort: 1744 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj)); 1745 break; 1746 case Primitive::kPrimBoolean: 1747 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false", 1748 field->GetBoolean(obj)); 1749 break; 1750 case Primitive::kPrimByte: 1751 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj)); 1752 break; 1753 case Primitive::kPrimNot: { 1754 // Get the value, don't compute the type unless it is non-null as we don't want 1755 // to cause class loading. 1756 mirror::Object* value = field->GetObj(obj); 1757 if (value == nullptr) { 1758 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str()); 1759 } else { 1760 // Grab the field type without causing resolution. 1761 mirror::Class* field_type = field->GetType<false>(); 1762 if (field_type != nullptr) { 1763 PrettyObjectValue(os, field_type, value); 1764 } else { 1765 os << StringPrintf("%p %s\n", value, 1766 PrettyDescriptor(field->GetTypeDescriptor()).c_str()); 1767 } 1768 } 1769 break; 1770 } 1771 default: 1772 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n"; 1773 break; 1774 } 1775 } 1776 1777 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass) 1778 SHARED_REQUIRES(Locks::mutator_lock_) { 1779 mirror::Class* super = klass->GetSuperClass(); 1780 if (super != nullptr) { 1781 DumpFields(os, obj, super); 1782 } 1783 for (ArtField& field : klass->GetIFields()) { 1784 PrintField(os, &field, obj); 1785 } 1786 } 1787 1788 bool InDumpSpace(const mirror::Object* object) { 1789 return image_space_.Contains(object); 1790 } 1791 1792 const void* GetQuickOatCodeBegin(ArtMethod* m) 1793 SHARED_REQUIRES(Locks::mutator_lock_) { 1794 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize( 1795 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet())); 1796 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) { 1797 quick_code = oat_dumper_->GetQuickOatCode(m); 1798 } 1799 if (oat_dumper_->GetInstructionSet() == kThumb2) { 1800 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1); 1801 } 1802 return quick_code; 1803 } 1804 1805 uint32_t GetQuickOatCodeSize(ArtMethod* m) 1806 SHARED_REQUIRES(Locks::mutator_lock_) { 1807 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m)); 1808 if (oat_code_begin == nullptr) { 1809 return 0; 1810 } 1811 return oat_code_begin[-1]; 1812 } 1813 1814 const void* GetQuickOatCodeEnd(ArtMethod* m) 1815 SHARED_REQUIRES(Locks::mutator_lock_) { 1816 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m)); 1817 if (oat_code_begin == nullptr) { 1818 return nullptr; 1819 } 1820 return oat_code_begin + GetQuickOatCodeSize(m); 1821 } 1822 1823 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) { 1824 DCHECK(obj != nullptr); 1825 DCHECK(arg != nullptr); 1826 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg); 1827 if (!state->InDumpSpace(obj)) { 1828 return; 1829 } 1830 1831 size_t object_bytes = obj->SizeOf(); 1832 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; 1833 state->stats_.object_bytes += object_bytes; 1834 state->stats_.alignment_bytes += alignment_bytes; 1835 1836 std::ostream& os = state->vios_.Stream(); 1837 1838 mirror::Class* obj_class = obj->GetClass(); 1839 if (obj_class->IsArrayClass()) { 1840 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(), 1841 obj->AsArray()->GetLength()); 1842 } else if (obj->IsClass()) { 1843 mirror::Class* klass = obj->AsClass(); 1844 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str()) 1845 << klass->GetStatus() << ")\n"; 1846 } else if (obj_class->IsStringClass()) { 1847 os << StringPrintf("%p: java.lang.String %s\n", obj, 1848 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str()); 1849 } else { 1850 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str()); 1851 } 1852 ScopedIndentation indent1(&state->vios_); 1853 DumpFields(os, obj, obj_class); 1854 const auto image_pointer_size = 1855 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet()); 1856 if (obj->IsObjectArray()) { 1857 auto* obj_array = obj->AsObjectArray<mirror::Object>(); 1858 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) { 1859 mirror::Object* value = obj_array->Get(i); 1860 size_t run = 0; 1861 for (int32_t j = i + 1; j < length; j++) { 1862 if (value == obj_array->Get(j)) { 1863 run++; 1864 } else { 1865 break; 1866 } 1867 } 1868 if (run == 0) { 1869 os << StringPrintf("%d: ", i); 1870 } else { 1871 os << StringPrintf("%d to %zd: ", i, i + run); 1872 i = i + run; 1873 } 1874 mirror::Class* value_class = 1875 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass(); 1876 PrettyObjectValue(os, value_class, value); 1877 } 1878 } else if (obj->IsClass()) { 1879 mirror::Class* klass = obj->AsClass(); 1880 if (klass->NumStaticFields() != 0) { 1881 os << "STATICS:\n"; 1882 ScopedIndentation indent2(&state->vios_); 1883 for (ArtField& field : klass->GetSFields()) { 1884 PrintField(os, &field, field.GetDeclaringClass()); 1885 } 1886 } 1887 } else { 1888 auto it = state->dex_caches_.find(obj); 1889 if (it != state->dex_caches_.end()) { 1890 auto* dex_cache = down_cast<mirror::DexCache*>(obj); 1891 const auto& field_section = state->image_header_.GetImageSection( 1892 ImageHeader::kSectionArtFields); 1893 const auto& method_section = state->image_header_.GetMethodsSection(); 1894 size_t num_methods = dex_cache->NumResolvedMethods(); 1895 if (num_methods != 0u) { 1896 os << "Methods (size=" << num_methods << "):"; 1897 ScopedIndentation indent2(&state->vios_); 1898 auto* resolved_methods = dex_cache->GetResolvedMethods(); 1899 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) { 1900 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size); 1901 size_t run = 0; 1902 for (size_t j = i + 1; 1903 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods, 1904 j, 1905 image_pointer_size); 1906 ++j, ++run) {} 1907 if (run == 0) { 1908 os << StringPrintf("%zd: ", i); 1909 } else { 1910 os << StringPrintf("%zd to %zd: ", i, i + run); 1911 i = i + run; 1912 } 1913 std::string msg; 1914 if (elem == nullptr) { 1915 msg = "null"; 1916 } else if (method_section.Contains( 1917 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) { 1918 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem)); 1919 } else { 1920 msg = "<not in method section>"; 1921 } 1922 os << StringPrintf("%p %s\n", elem, msg.c_str()); 1923 } 1924 } 1925 size_t num_fields = dex_cache->NumResolvedFields(); 1926 if (num_fields != 0u) { 1927 os << "Fields (size=" << num_fields << "):"; 1928 ScopedIndentation indent2(&state->vios_); 1929 auto* resolved_fields = dex_cache->GetResolvedFields(); 1930 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) { 1931 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size); 1932 size_t run = 0; 1933 for (size_t j = i + 1; 1934 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields, 1935 j, 1936 image_pointer_size); 1937 ++j, ++run) {} 1938 if (run == 0) { 1939 os << StringPrintf("%zd: ", i); 1940 } else { 1941 os << StringPrintf("%zd to %zd: ", i, i + run); 1942 i = i + run; 1943 } 1944 std::string msg; 1945 if (elem == nullptr) { 1946 msg = "null"; 1947 } else if (field_section.Contains( 1948 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) { 1949 msg = PrettyField(reinterpret_cast<ArtField*>(elem)); 1950 } else { 1951 msg = "<not in field section>"; 1952 } 1953 os << StringPrintf("%p %s\n", elem, msg.c_str()); 1954 } 1955 } 1956 } 1957 } 1958 std::string temp; 1959 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes); 1960 } 1961 1962 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os) 1963 SHARED_REQUIRES(Locks::mutator_lock_) { 1964 DCHECK(method != nullptr); 1965 const auto image_pointer_size = 1966 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet()); 1967 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method); 1968 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method); 1969 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>( 1970 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader)); 1971 if (method->IsNative()) { 1972 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) { 1973 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method); 1974 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method); 1975 } 1976 bool first_occurrence; 1977 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method); 1978 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence); 1979 if (first_occurrence) { 1980 state->stats_.native_to_managed_code_bytes += quick_oat_code_size; 1981 } 1982 if (quick_oat_code_begin != 1983 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) { 1984 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin); 1985 } 1986 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() || 1987 method->IsResolutionMethod() || method->IsImtConflictMethod() || 1988 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) { 1989 } else { 1990 const DexFile::CodeItem* code_item = method->GetCodeItem(); 1991 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2; 1992 state->stats_.dex_instruction_bytes += dex_instruction_bytes; 1993 1994 bool first_occurrence; 1995 size_t gc_map_bytes = state->ComputeOatSize( 1996 method_header->GetNativeGcMap(), &first_occurrence); 1997 if (first_occurrence) { 1998 state->stats_.gc_map_bytes += gc_map_bytes; 1999 } 2000 2001 size_t pc_mapping_table_bytes = state->ComputeOatSize( 2002 method_header->GetMappingTable(), &first_occurrence); 2003 if (first_occurrence) { 2004 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; 2005 } 2006 2007 size_t vmap_table_bytes = 0u; 2008 if (!method_header->IsOptimized()) { 2009 // Method compiled with the optimizing compiler have no vmap table. 2010 vmap_table_bytes = state->ComputeOatSize( 2011 method_header->GetVmapTable(), &first_occurrence); 2012 if (first_occurrence) { 2013 state->stats_.vmap_table_bytes += vmap_table_bytes; 2014 } 2015 } 2016 2017 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method); 2018 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence); 2019 if (first_occurrence) { 2020 state->stats_.managed_code_bytes += quick_oat_code_size; 2021 if (method->IsConstructor()) { 2022 if (method->IsStatic()) { 2023 state->stats_.class_initializer_code_bytes += quick_oat_code_size; 2024 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) { 2025 state->stats_.large_initializer_code_bytes += quick_oat_code_size; 2026 } 2027 } else if (dex_instruction_bytes > kLargeMethodDexBytes) { 2028 state->stats_.large_method_code_bytes += quick_oat_code_size; 2029 } 2030 } 2031 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size; 2032 2033 uint32_t method_access_flags = method->GetAccessFlags(); 2034 2035 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end); 2036 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n", 2037 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes, 2038 method_access_flags); 2039 2040 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes + 2041 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size); 2042 2043 double expansion = 2044 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes); 2045 state->stats_.ComputeOutliers(total_size, expansion, method); 2046 } 2047 } 2048 2049 std::set<const void*> already_seen_; 2050 // Compute the size of the given data within the oat file and whether this is the first time 2051 // this data has been requested 2052 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) { 2053 if (already_seen_.count(oat_data) == 0) { 2054 *first_occurrence = true; 2055 already_seen_.insert(oat_data); 2056 } else { 2057 *first_occurrence = false; 2058 } 2059 return oat_dumper_->ComputeSize(oat_data); 2060 } 2061 2062 public: 2063 struct Stats { 2064 size_t oat_file_bytes; 2065 size_t file_bytes; 2066 2067 size_t header_bytes; 2068 size_t object_bytes; 2069 size_t art_field_bytes; 2070 size_t art_method_bytes; 2071 size_t dex_cache_arrays_bytes; 2072 size_t interned_strings_bytes; 2073 size_t bitmap_bytes; 2074 size_t alignment_bytes; 2075 2076 size_t managed_code_bytes; 2077 size_t managed_code_bytes_ignoring_deduplication; 2078 size_t managed_to_native_code_bytes; 2079 size_t native_to_managed_code_bytes; 2080 size_t class_initializer_code_bytes; 2081 size_t large_initializer_code_bytes; 2082 size_t large_method_code_bytes; 2083 2084 size_t gc_map_bytes; 2085 size_t pc_mapping_table_bytes; 2086 size_t vmap_table_bytes; 2087 2088 size_t dex_instruction_bytes; 2089 2090 std::vector<ArtMethod*> method_outlier; 2091 std::vector<size_t> method_outlier_size; 2092 std::vector<double> method_outlier_expansion; 2093 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes; 2094 2095 Stats() 2096 : oat_file_bytes(0), 2097 file_bytes(0), 2098 header_bytes(0), 2099 object_bytes(0), 2100 art_field_bytes(0), 2101 art_method_bytes(0), 2102 dex_cache_arrays_bytes(0), 2103 interned_strings_bytes(0), 2104 bitmap_bytes(0), 2105 alignment_bytes(0), 2106 managed_code_bytes(0), 2107 managed_code_bytes_ignoring_deduplication(0), 2108 managed_to_native_code_bytes(0), 2109 native_to_managed_code_bytes(0), 2110 class_initializer_code_bytes(0), 2111 large_initializer_code_bytes(0), 2112 large_method_code_bytes(0), 2113 gc_map_bytes(0), 2114 pc_mapping_table_bytes(0), 2115 vmap_table_bytes(0), 2116 dex_instruction_bytes(0) {} 2117 2118 struct SizeAndCount { 2119 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {} 2120 size_t bytes; 2121 size_t count; 2122 }; 2123 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable; 2124 SizeAndCountTable sizes_and_counts; 2125 2126 void Update(const char* descriptor, size_t object_bytes_in) { 2127 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor); 2128 if (it != sizes_and_counts.end()) { 2129 it->second.bytes += object_bytes_in; 2130 it->second.count += 1; 2131 } else { 2132 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1)); 2133 } 2134 } 2135 2136 double PercentOfOatBytes(size_t size) { 2137 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100; 2138 } 2139 2140 double PercentOfFileBytes(size_t size) { 2141 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; 2142 } 2143 2144 double PercentOfObjectBytes(size_t size) { 2145 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; 2146 } 2147 2148 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) { 2149 method_outlier_size.push_back(total_size); 2150 method_outlier_expansion.push_back(expansion); 2151 method_outlier.push_back(method); 2152 } 2153 2154 void DumpOutliers(std::ostream& os) 2155 SHARED_REQUIRES(Locks::mutator_lock_) { 2156 size_t sum_of_sizes = 0; 2157 size_t sum_of_sizes_squared = 0; 2158 size_t sum_of_expansion = 0; 2159 size_t sum_of_expansion_squared = 0; 2160 size_t n = method_outlier_size.size(); 2161 for (size_t i = 0; i < n; i++) { 2162 size_t cur_size = method_outlier_size[i]; 2163 sum_of_sizes += cur_size; 2164 sum_of_sizes_squared += cur_size * cur_size; 2165 double cur_expansion = method_outlier_expansion[i]; 2166 sum_of_expansion += cur_expansion; 2167 sum_of_expansion_squared += cur_expansion * cur_expansion; 2168 } 2169 size_t size_mean = sum_of_sizes / n; 2170 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1); 2171 double expansion_mean = sum_of_expansion / n; 2172 double expansion_variance = 2173 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1); 2174 2175 // Dump methods whose size is a certain number of standard deviations from the mean 2176 size_t dumped_values = 0; 2177 size_t skipped_values = 0; 2178 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations 2179 size_t cur_size_variance = i * i * size_variance; 2180 bool first = true; 2181 for (size_t j = 0; j < n; j++) { 2182 size_t cur_size = method_outlier_size[j]; 2183 if (cur_size > size_mean) { 2184 size_t cur_var = cur_size - size_mean; 2185 cur_var = cur_var * cur_var; 2186 if (cur_var > cur_size_variance) { 2187 if (dumped_values > 20) { 2188 if (i == 1) { 2189 skipped_values++; 2190 } else { 2191 i = 2; // jump to counting for 1 standard deviation 2192 break; 2193 } 2194 } else { 2195 if (first) { 2196 os << "\nBig methods (size > " << i << " standard deviations the norm):\n"; 2197 first = false; 2198 } 2199 os << PrettyMethod(method_outlier[j]) << " requires storage of " 2200 << PrettySize(cur_size) << "\n"; 2201 method_outlier_size[j] = 0; // don't consider this method again 2202 dumped_values++; 2203 } 2204 } 2205 } 2206 } 2207 } 2208 if (skipped_values > 0) { 2209 os << "... skipped " << skipped_values 2210 << " methods with size > 1 standard deviation from the norm\n"; 2211 } 2212 os << std::flush; 2213 2214 // Dump methods whose expansion is a certain number of standard deviations from the mean 2215 dumped_values = 0; 2216 skipped_values = 0; 2217 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations 2218 double cur_expansion_variance = i * i * expansion_variance; 2219 bool first = true; 2220 for (size_t j = 0; j < n; j++) { 2221 double cur_expansion = method_outlier_expansion[j]; 2222 if (cur_expansion > expansion_mean) { 2223 size_t cur_var = cur_expansion - expansion_mean; 2224 cur_var = cur_var * cur_var; 2225 if (cur_var > cur_expansion_variance) { 2226 if (dumped_values > 20) { 2227 if (i == 1) { 2228 skipped_values++; 2229 } else { 2230 i = 2; // jump to counting for 1 standard deviation 2231 break; 2232 } 2233 } else { 2234 if (first) { 2235 os << "\nLarge expansion methods (size > " << i 2236 << " standard deviations the norm):\n"; 2237 first = false; 2238 } 2239 os << PrettyMethod(method_outlier[j]) << " expanded code by " 2240 << cur_expansion << "\n"; 2241 method_outlier_expansion[j] = 0.0; // don't consider this method again 2242 dumped_values++; 2243 } 2244 } 2245 } 2246 } 2247 } 2248 if (skipped_values > 0) { 2249 os << "... skipped " << skipped_values 2250 << " methods with expansion > 1 standard deviation from the norm\n"; 2251 } 2252 os << "\n" << std::flush; 2253 } 2254 2255 void Dump(std::ostream& os, std::ostream& indent_os) 2256 SHARED_REQUIRES(Locks::mutator_lock_) { 2257 { 2258 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n" 2259 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n"; 2260 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n" 2261 "object_bytes = %8zd (%2.0f%% of art file bytes)\n" 2262 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n" 2263 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n" 2264 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n" 2265 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n" 2266 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n" 2267 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n", 2268 header_bytes, PercentOfFileBytes(header_bytes), 2269 object_bytes, PercentOfFileBytes(object_bytes), 2270 art_field_bytes, PercentOfFileBytes(art_field_bytes), 2271 art_method_bytes, PercentOfFileBytes(art_method_bytes), 2272 dex_cache_arrays_bytes, 2273 PercentOfFileBytes(dex_cache_arrays_bytes), 2274 interned_strings_bytes, 2275 PercentOfFileBytes(interned_strings_bytes), 2276 bitmap_bytes, PercentOfFileBytes(bitmap_bytes), 2277 alignment_bytes, PercentOfFileBytes(alignment_bytes)) 2278 << std::flush; 2279 CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes + 2280 dex_cache_arrays_bytes + interned_strings_bytes + bitmap_bytes + alignment_bytes); 2281 } 2282 2283 os << "object_bytes breakdown:\n"; 2284 size_t object_bytes_total = 0; 2285 for (const auto& sizes_and_count : sizes_and_counts) { 2286 const std::string& descriptor(sizes_and_count.first); 2287 double average = static_cast<double>(sizes_and_count.second.bytes) / 2288 static_cast<double>(sizes_and_count.second.count); 2289 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes); 2290 os << StringPrintf("%32s %8zd bytes %6zd instances " 2291 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n", 2292 descriptor.c_str(), sizes_and_count.second.bytes, 2293 sizes_and_count.second.count, average, percent); 2294 object_bytes_total += sizes_and_count.second.bytes; 2295 } 2296 os << "\n" << std::flush; 2297 CHECK_EQ(object_bytes, object_bytes_total); 2298 2299 os << StringPrintf("oat_file_bytes = %8zd\n" 2300 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" 2301 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" 2302 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n" 2303 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" 2304 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" 2305 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n", 2306 oat_file_bytes, 2307 managed_code_bytes, 2308 PercentOfOatBytes(managed_code_bytes), 2309 managed_to_native_code_bytes, 2310 PercentOfOatBytes(managed_to_native_code_bytes), 2311 native_to_managed_code_bytes, 2312 PercentOfOatBytes(native_to_managed_code_bytes), 2313 class_initializer_code_bytes, 2314 PercentOfOatBytes(class_initializer_code_bytes), 2315 large_initializer_code_bytes, 2316 PercentOfOatBytes(large_initializer_code_bytes), 2317 large_method_code_bytes, 2318 PercentOfOatBytes(large_method_code_bytes)) 2319 << "DexFile sizes:\n"; 2320 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) { 2321 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n", 2322 oat_dex_file_size.first.c_str(), oat_dex_file_size.second, 2323 PercentOfOatBytes(oat_dex_file_size.second)); 2324 } 2325 2326 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n" 2327 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n" 2328 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n", 2329 gc_map_bytes, PercentOfOatBytes(gc_map_bytes), 2330 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes), 2331 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes)) 2332 << std::flush; 2333 2334 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes) 2335 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n", 2336 static_cast<double>(managed_code_bytes) / 2337 static_cast<double>(dex_instruction_bytes), 2338 static_cast<double>(managed_code_bytes_ignoring_deduplication) / 2339 static_cast<double>(dex_instruction_bytes)) 2340 << std::flush; 2341 2342 DumpOutliers(os); 2343 } 2344 } stats_; 2345 2346 private: 2347 enum { 2348 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block 2349 // threshold, we assume 2 bytes per instruction and 2 instructions per block. 2350 kLargeConstructorDexBytes = 4000, 2351 // Number of bytes for a method to be considered large. Based on the 4000 basic block 2352 // threshold, we assume 2 bytes per instruction and 2 instructions per block. 2353 kLargeMethodDexBytes = 16000 2354 }; 2355 2356 // For performance, use the *os_ directly for anything that doesn't need indentation 2357 // and prepare an indentation stream with default indentation 1. 2358 std::ostream* os_; 2359 VariableIndentationOutputStream vios_; 2360 ScopedIndentation indent1_; 2361 2362 gc::space::ImageSpace& image_space_; 2363 const ImageHeader& image_header_; 2364 std::unique_ptr<OatDumper> oat_dumper_; 2365 OatDumperOptions* oat_dumper_options_; 2366 std::set<mirror::Object*> dex_caches_; 2367 2368 DISALLOW_COPY_AND_ASSIGN(ImageDumper); 2369}; 2370 2371static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options, 2372 std::ostream* os) { 2373 // Dumping the image, no explicit class loader. 2374 NullHandle<mirror::ClassLoader> null_class_loader; 2375 options->class_loader_ = &null_class_loader; 2376 2377 ScopedObjectAccess soa(Thread::Current()); 2378 gc::Heap* heap = runtime->GetHeap(); 2379 gc::space::ImageSpace* image_space = heap->GetBootImageSpace(); 2380 CHECK(image_space != nullptr); 2381 const ImageHeader& image_header = image_space->GetImageHeader(); 2382 if (!image_header.IsValid()) { 2383 fprintf(stderr, "Invalid image header %s\n", image_location); 2384 return EXIT_FAILURE; 2385 } 2386 2387 ImageDumper image_dumper(os, *image_space, image_header, options); 2388 2389 bool success = image_dumper.Dump(); 2390 return (success) ? EXIT_SUCCESS : EXIT_FAILURE; 2391} 2392 2393static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options, 2394 std::ostream* os) { 2395 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr); 2396 2397 Thread* self = Thread::Current(); 2398 CHECK(self != nullptr); 2399 // Need well-known-classes. 2400 WellKnownClasses::Init(self->GetJniEnv()); 2401 2402 // Need to register dex files to get a working dex cache. 2403 ScopedObjectAccess soa(self); 2404 ClassLinker* class_linker = runtime->GetClassLinker(); 2405 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file)); 2406 std::vector<const DexFile*> class_path; 2407 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) { 2408 std::string error_msg; 2409 const DexFile* const dex_file = OpenDexFile(odf, &error_msg); 2410 CHECK(dex_file != nullptr) << error_msg; 2411 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc()); 2412 class_path.push_back(dex_file); 2413 } 2414 2415 // Need a class loader. 2416 // Fake that we're a compiler. 2417 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path, /*parent*/nullptr); 2418 2419 // Use the class loader while dumping. 2420 StackHandleScope<1> scope(self); 2421 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle( 2422 soa.Decode<mirror::ClassLoader*>(class_loader)); 2423 options->class_loader_ = &loader_handle; 2424 2425 OatDumper oat_dumper(*oat_file, *options); 2426 bool success = oat_dumper.Dump(*os); 2427 return (success) ? EXIT_SUCCESS : EXIT_FAILURE; 2428} 2429 2430static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) { 2431 CHECK(oat_file != nullptr && options != nullptr); 2432 // No image = no class loader. 2433 NullHandle<mirror::ClassLoader> null_class_loader; 2434 options->class_loader_ = &null_class_loader; 2435 2436 OatDumper oat_dumper(*oat_file, *options); 2437 bool success = oat_dumper.Dump(*os); 2438 return (success) ? EXIT_SUCCESS : EXIT_FAILURE; 2439} 2440 2441static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options, 2442 std::ostream* os) { 2443 std::string error_msg; 2444 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, 2445 nullptr, &error_msg); 2446 if (oat_file == nullptr) { 2447 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str()); 2448 return EXIT_FAILURE; 2449 } 2450 2451 if (runtime != nullptr) { 2452 return DumpOatWithRuntime(runtime, oat_file, options, os); 2453 } else { 2454 return DumpOatWithoutRuntime(oat_file, options, os); 2455 } 2456} 2457 2458static int SymbolizeOat(const char* oat_filename, std::string& output_name) { 2459 std::string error_msg; 2460 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, 2461 nullptr, &error_msg); 2462 if (oat_file == nullptr) { 2463 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str()); 2464 return EXIT_FAILURE; 2465 } 2466 2467 OatSymbolizer oat_symbolizer(oat_file, output_name); 2468 if (!oat_symbolizer.Symbolize()) { 2469 fprintf(stderr, "Failed to symbolize\n"); 2470 return EXIT_FAILURE; 2471 } 2472 2473 return EXIT_SUCCESS; 2474} 2475 2476struct OatdumpArgs : public CmdlineArgs { 2477 protected: 2478 using Base = CmdlineArgs; 2479 2480 virtual ParseStatus ParseCustom(const StringPiece& option, 2481 std::string* error_msg) OVERRIDE { 2482 { 2483 ParseStatus base_parse = Base::ParseCustom(option, error_msg); 2484 if (base_parse != kParseUnknownArgument) { 2485 return base_parse; 2486 } 2487 } 2488 2489 if (option.starts_with("--oat-file=")) { 2490 oat_filename_ = option.substr(strlen("--oat-file=")).data(); 2491 } else if (option.starts_with("--image=")) { 2492 image_location_ = option.substr(strlen("--image=")).data(); 2493 } else if (option =="--dump:raw_mapping_table") { 2494 dump_raw_mapping_table_ = true; 2495 } else if (option == "--dump:raw_gc_map") { 2496 dump_raw_gc_map_ = true; 2497 } else if (option == "--no-dump:vmap") { 2498 dump_vmap_ = false; 2499 } else if (option =="--dump:code_info_stack_maps") { 2500 dump_code_info_stack_maps_ = true; 2501 } else if (option == "--no-disassemble") { 2502 disassemble_code_ = false; 2503 } else if (option.starts_with("--symbolize=")) { 2504 oat_filename_ = option.substr(strlen("--symbolize=")).data(); 2505 symbolize_ = true; 2506 } else if (option.starts_with("--class-filter=")) { 2507 class_filter_ = option.substr(strlen("--class-filter=")).data(); 2508 } else if (option.starts_with("--method-filter=")) { 2509 method_filter_ = option.substr(strlen("--method-filter=")).data(); 2510 } else if (option.starts_with("--list-classes")) { 2511 list_classes_ = true; 2512 } else if (option.starts_with("--list-methods")) { 2513 list_methods_ = true; 2514 } else if (option.starts_with("--export-dex-to=")) { 2515 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data(); 2516 } else if (option.starts_with("--addr2instr=")) { 2517 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) { 2518 *error_msg = "Address conversion failed"; 2519 return kParseError; 2520 } 2521 } else { 2522 return kParseUnknownArgument; 2523 } 2524 2525 return kParseOk; 2526 } 2527 2528 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE { 2529 // Infer boot image location from the image location if possible. 2530 if (boot_image_location_ == nullptr) { 2531 boot_image_location_ = image_location_; 2532 } 2533 2534 // Perform the parent checks. 2535 ParseStatus parent_checks = Base::ParseChecks(error_msg); 2536 if (parent_checks != kParseOk) { 2537 return parent_checks; 2538 } 2539 2540 // Perform our own checks. 2541 if (image_location_ == nullptr && oat_filename_ == nullptr) { 2542 *error_msg = "Either --image or --oat-file must be specified"; 2543 return kParseError; 2544 } else if (image_location_ != nullptr && oat_filename_ != nullptr) { 2545 *error_msg = "Either --image or --oat-file must be specified but not both"; 2546 return kParseError; 2547 } 2548 2549 return kParseOk; 2550 } 2551 2552 virtual std::string GetUsage() const { 2553 std::string usage; 2554 2555 usage += 2556 "Usage: oatdump [options] ...\n" 2557 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n" 2558 " Example: adb shell oatdump --image=/system/framework/boot.art\n" 2559 "\n" 2560 // Either oat-file or image is required. 2561 " --oat-file=<file.oat>: specifies an input oat filename.\n" 2562 " Example: --oat-file=/system/framework/boot.oat\n" 2563 "\n" 2564 " --image=<file.art>: specifies an input image location.\n" 2565 " Example: --image=/system/framework/boot.art\n" 2566 "\n"; 2567 2568 usage += Base::GetUsage(); 2569 2570 usage += // Optional. 2571 " --dump:raw_mapping_table enables dumping of the mapping table.\n" 2572 " Example: --dump:raw_mapping_table\n" 2573 "\n" 2574 " --dump:raw_gc_map enables dumping of the GC map.\n" 2575 " Example: --dump:raw_gc_map\n" 2576 "\n" 2577 " --no-dump:vmap may be used to disable vmap dumping.\n" 2578 " Example: --no-dump:vmap\n" 2579 "\n" 2580 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n" 2581 " Example: --dump:code_info_stack_maps\n" 2582 "\n" 2583 " --no-disassemble may be used to disable disassembly.\n" 2584 " Example: --no-disassemble\n" 2585 "\n" 2586 " --list-classes may be used to list target file classes (can be used with filters).\n" 2587 " Example: --list-classes\n" 2588 " Example: --list-classes --class-filter=com.example.foo\n" 2589 "\n" 2590 " --list-methods may be used to list target file methods (can be used with filters).\n" 2591 " Example: --list-methods\n" 2592 " Example: --list-methods --class-filter=com.example --method-filter=foo\n" 2593 "\n" 2594 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n" 2595 " Example: --symbolize=/system/framework/boot.oat\n" 2596 "\n" 2597 " --class-filter=<class name>: only dumps classes that contain the filter.\n" 2598 " Example: --class-filter=com.example.foo\n" 2599 "\n" 2600 " --method-filter=<method name>: only dumps methods that contain the filter.\n" 2601 " Example: --method-filter=foo\n" 2602 "\n" 2603 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n" 2604 " Example: --export-dex-to=/data/local/tmp\n" 2605 "\n" 2606 " --addr2instr=<address>: output matching method disassembled code from relative\n" 2607 " address (e.g. PC from crash dump)\n" 2608 " Example: --addr2instr=0x00001a3b\n" 2609 "\n"; 2610 2611 return usage; 2612 } 2613 2614 public: 2615 const char* oat_filename_ = nullptr; 2616 const char* class_filter_ = ""; 2617 const char* method_filter_ = ""; 2618 const char* image_location_ = nullptr; 2619 std::string elf_filename_prefix_; 2620 bool dump_raw_mapping_table_ = false; 2621 bool dump_raw_gc_map_ = false; 2622 bool dump_vmap_ = true; 2623 bool dump_code_info_stack_maps_ = false; 2624 bool disassemble_code_ = true; 2625 bool symbolize_ = false; 2626 bool list_classes_ = false; 2627 bool list_methods_ = false; 2628 uint32_t addr2instr_ = 0; 2629 const char* export_dex_location_ = nullptr; 2630}; 2631 2632struct OatdumpMain : public CmdlineMain<OatdumpArgs> { 2633 virtual bool NeedsRuntime() OVERRIDE { 2634 CHECK(args_ != nullptr); 2635 2636 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping. 2637 bool absolute_addresses = (args_->oat_filename_ == nullptr); 2638 2639 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions( 2640 args_->dump_raw_mapping_table_, 2641 args_->dump_raw_gc_map_, 2642 args_->dump_vmap_, 2643 args_->dump_code_info_stack_maps_, 2644 args_->disassemble_code_, 2645 absolute_addresses, 2646 args_->class_filter_, 2647 args_->method_filter_, 2648 args_->list_classes_, 2649 args_->list_methods_, 2650 args_->export_dex_location_, 2651 args_->addr2instr_)); 2652 2653 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) && 2654 !args_->symbolize_; 2655 } 2656 2657 virtual bool ExecuteWithoutRuntime() OVERRIDE { 2658 CHECK(args_ != nullptr); 2659 CHECK(args_->oat_filename_ != nullptr); 2660 2661 MemMap::Init(); 2662 2663 if (args_->symbolize_) { 2664 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS; 2665 } else { 2666 return DumpOat(nullptr, 2667 args_->oat_filename_, 2668 oat_dumper_options_.get(), 2669 args_->os_) == EXIT_SUCCESS; 2670 } 2671 } 2672 2673 virtual bool ExecuteWithRuntime(Runtime* runtime) { 2674 CHECK(args_ != nullptr); 2675 2676 if (args_->oat_filename_ != nullptr) { 2677 return DumpOat(runtime, 2678 args_->oat_filename_, 2679 oat_dumper_options_.get(), 2680 args_->os_) == EXIT_SUCCESS; 2681 } 2682 2683 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_) 2684 == EXIT_SUCCESS; 2685 } 2686 2687 std::unique_ptr<OatDumperOptions> oat_dumper_options_; 2688}; 2689 2690} // namespace art 2691 2692int main(int argc, char** argv) { 2693 art::OatdumpMain main; 2694 return main.Main(argc, argv); 2695} 2696