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