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