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