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