oatdump.cc revision fa82427c68b09f4aedbee319dc71579afbfc66f5
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 <string> 23#include <vector> 24 25#include "base/stringpiece.h" 26#include "base/unix_file/fd_file.h" 27#include "class_linker.h" 28#include "class_linker-inl.h" 29#include "dex_file-inl.h" 30#include "dex_instruction.h" 31#include "disassembler.h" 32#include "gc_map.h" 33#include "gc/space/image_space.h" 34#include "gc/space/large_object_space.h" 35#include "gc/space/space-inl.h" 36#include "image.h" 37#include "indenter.h" 38#include "mapping_table.h" 39#include "mirror/art_field-inl.h" 40#include "mirror/art_method-inl.h" 41#include "mirror/array-inl.h" 42#include "mirror/class-inl.h" 43#include "mirror/object-inl.h" 44#include "mirror/object_array-inl.h" 45#include "oat.h" 46#include "object_utils.h" 47#include "os.h" 48#include "runtime.h" 49#include "safe_map.h" 50#include "scoped_thread_state_change.h" 51#include "verifier/method_verifier.h" 52#include "vmap_table.h" 53 54namespace art { 55 56static void usage() { 57 fprintf(stderr, 58 "Usage: oatdump [options] ...\n" 59 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n" 60 " Example: adb shell oatdump --image=/system/framework/boot.art\n" 61 "\n"); 62 fprintf(stderr, 63 " --oat-file=<file.oat>: specifies an input oat filename.\n" 64 " Example: --oat-file=/system/framework/boot.oat\n" 65 "\n"); 66 fprintf(stderr, 67 " --image=<file.art>: specifies an input image filename.\n" 68 " Example: --image=/system/framework/boot.art\n" 69 "\n"); 70 fprintf(stderr, 71 " --boot-image=<file.art>: provide the image file for the boot class path.\n" 72 " Example: --boot-image=/system/framework/boot.art\n" 73 "\n"); 74 fprintf(stderr, 75 " --host-prefix may be used to translate host paths to target paths during\n" 76 " cross compilation.\n" 77 " Example: --host-prefix=out/target/product/crespo\n" 78 " Default: $ANDROID_PRODUCT_OUT\n" 79 "\n"); 80 fprintf(stderr, 81 " --output=<file> may be used to send the output to a file.\n" 82 " Example: --output=/tmp/oatdump.txt\n" 83 "\n"); 84 exit(EXIT_FAILURE); 85} 86 87const char* image_roots_descriptions_[] = { 88 "kResolutionMethod", 89 "kImtConflictMethod", 90 "kDefaultImt", 91 "kCalleeSaveMethod", 92 "kRefsOnlySaveMethod", 93 "kRefsAndArgsSaveMethod", 94 "kOatLocation", 95 "kDexCaches", 96 "kClassRoots", 97}; 98 99class OatDumper { 100 public: 101 explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file) 102 : host_prefix_(host_prefix), 103 oat_file_(oat_file), 104 oat_dex_files_(oat_file.GetOatDexFiles()), 105 disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) { 106 AddAllOffsets(); 107 } 108 109 void Dump(std::ostream& os) { 110 const OatHeader& oat_header = oat_file_.GetOatHeader(); 111 112 os << "MAGIC:\n"; 113 os << oat_header.GetMagic() << "\n\n"; 114 115 os << "CHECKSUM:\n"; 116 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum()); 117 118 os << "INSTRUCTION SET:\n"; 119 os << oat_header.GetInstructionSet() << "\n\n"; 120 121 os << "DEX FILE COUNT:\n"; 122 os << oat_header.GetDexFileCount() << "\n\n"; 123 124 os << "EXECUTABLE OFFSET:\n"; 125 os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset()); 126 127 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n"; 128 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum()); 129 130 os << "IMAGE FILE LOCATION OAT BEGIN:\n"; 131 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin()); 132 133 os << "IMAGE FILE LOCATION:\n"; 134 const std::string image_file_location(oat_header.GetImageFileLocation()); 135 os << image_file_location; 136 if (!image_file_location.empty() && !host_prefix_.empty()) { 137 os << " (" << host_prefix_ << image_file_location << ")"; 138 } 139 os << "\n\n"; 140 141 os << "BEGIN:\n"; 142 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n"; 143 144 os << "END:\n"; 145 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n"; 146 147 os << std::flush; 148 149 for (size_t i = 0; i < oat_dex_files_.size(); i++) { 150 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; 151 CHECK(oat_dex_file != NULL); 152 DumpOatDexFile(os, *oat_dex_file); 153 } 154 } 155 156 size_t ComputeSize(const void* oat_data) { 157 if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() || 158 reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) { 159 return 0; // Address not in oat file 160 } 161 uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) - 162 reinterpret_cast<size_t>(oat_file_.Begin()); 163 typedef std::set<uint32_t>::iterator It; 164 It it = offsets_.upper_bound(begin_offset); 165 CHECK(it != offsets_.end()); 166 uint32_t end_offset = *it; 167 return end_offset - begin_offset; 168 } 169 170 InstructionSet GetInstructionSet() { 171 return oat_file_.GetOatHeader().GetInstructionSet(); 172 } 173 174 const void* GetOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 175 MethodHelper mh(m); 176 for (size_t i = 0; i < oat_dex_files_.size(); i++) { 177 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; 178 CHECK(oat_dex_file != nullptr); 179 std::string error_msg; 180 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg)); 181 if (dex_file.get() == nullptr) { 182 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() 183 << "': " << error_msg; 184 } else { 185 const DexFile::ClassDef* class_def = 186 dex_file->FindClassDef(mh.GetDeclaringClassDescriptor()); 187 if (class_def != NULL) { 188 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def); 189 const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index); 190 CHECK(oat_class != NULL); 191 size_t method_index = m->GetMethodIndex(); 192 return oat_class->GetOatMethod(method_index).GetCode(); 193 } 194 } 195 } 196 return NULL; 197 } 198 199 private: 200 void AddAllOffsets() { 201 // We don't know the length of the code for each method, but we need to know where to stop 202 // when disassembling. What we do know is that a region of code will be followed by some other 203 // region, so if we keep a sorted sequence of the start of each region, we can infer the length 204 // of a piece of code by using upper_bound to find the start of the next region. 205 for (size_t i = 0; i < oat_dex_files_.size(); i++) { 206 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i]; 207 CHECK(oat_dex_file != NULL); 208 std::string error_msg; 209 UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg)); 210 if (dex_file.get() == nullptr) { 211 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() 212 << "': " << error_msg; 213 continue; 214 } 215 offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader())); 216 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) { 217 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); 218 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index)); 219 const byte* class_data = dex_file->GetClassData(class_def); 220 if (class_data != NULL) { 221 ClassDataItemIterator it(*dex_file, class_data); 222 SkipAllFields(it); 223 uint32_t class_method_index = 0; 224 while (it.HasNextDirectMethod()) { 225 AddOffsets(oat_class->GetOatMethod(class_method_index++)); 226 it.Next(); 227 } 228 while (it.HasNextVirtualMethod()) { 229 AddOffsets(oat_class->GetOatMethod(class_method_index++)); 230 it.Next(); 231 } 232 } 233 } 234 } 235 236 // If the last thing in the file is code for a method, there won't be an offset for the "next" 237 // thing. Instead of having a special case in the upper_bound code, let's just add an entry 238 // for the end of the file. 239 offsets_.insert(static_cast<uint32_t>(oat_file_.Size())); 240 } 241 242 void AddOffsets(const OatFile::OatMethod& oat_method) { 243 uint32_t code_offset = oat_method.GetCodeOffset(); 244 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) { 245 code_offset &= ~0x1; 246 } 247 offsets_.insert(code_offset); 248 offsets_.insert(oat_method.GetMappingTableOffset()); 249 offsets_.insert(oat_method.GetVmapTableOffset()); 250 offsets_.insert(oat_method.GetNativeGcMapOffset()); 251 } 252 253 void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) { 254 os << "OAT DEX FILE:\n"; 255 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str()); 256 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum()); 257 std::string error_msg; 258 UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg)); 259 if (dex_file.get() == NULL) { 260 os << "NOT FOUND: " << error_msg << "\n\n"; 261 return; 262 } 263 for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) { 264 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index); 265 const char* descriptor = dex_file->GetClassDescriptor(class_def); 266 UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index)); 267 CHECK(oat_class.get() != NULL); 268 os << StringPrintf("%zd: %s (type_idx=%d)", class_def_index, descriptor, class_def.class_idx_) 269 << " (" << oat_class->GetStatus() << ")" 270 << " (" << oat_class->GetType() << ")\n"; 271 // TODO: include bitmap here if type is kOatClassBitmap? 272 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 273 std::ostream indented_os(&indent_filter); 274 DumpOatClass(indented_os, *oat_class.get(), *(dex_file.get()), class_def); 275 } 276 277 os << std::flush; 278 } 279 280 static void SkipAllFields(ClassDataItemIterator& it) { 281 while (it.HasNextStaticField()) { 282 it.Next(); 283 } 284 while (it.HasNextInstanceField()) { 285 it.Next(); 286 } 287 } 288 289 void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file, 290 const DexFile::ClassDef& class_def) { 291 const byte* class_data = dex_file.GetClassData(class_def); 292 if (class_data == NULL) { // empty class such as a marker interface? 293 return; 294 } 295 ClassDataItemIterator it(dex_file, class_data); 296 SkipAllFields(it); 297 uint32_t class_method_idx = 0; 298 while (it.HasNextDirectMethod()) { 299 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx); 300 DumpOatMethod(os, class_def, class_method_idx, oat_method, dex_file, 301 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags()); 302 class_method_idx++; 303 it.Next(); 304 } 305 while (it.HasNextVirtualMethod()) { 306 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx); 307 DumpOatMethod(os, class_def, class_method_idx, oat_method, dex_file, 308 it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags()); 309 class_method_idx++; 310 it.Next(); 311 } 312 DCHECK(!it.HasNext()); 313 os << std::flush; 314 } 315 316 void DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def, 317 uint32_t class_method_index, 318 const OatFile::OatMethod& oat_method, const DexFile& dex_file, 319 uint32_t dex_method_idx, const DexFile::CodeItem* code_item, 320 uint32_t method_access_flags) { 321 os << StringPrintf("%d: %s (dex_method_idx=%d)\n", 322 class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(), 323 dex_method_idx); 324 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 325 std::ostream indent1_os(&indent1_filter); 326 { 327 indent1_os << "DEX CODE:\n"; 328 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count); 329 std::ostream indent2_os(&indent2_filter); 330 DumpDexCode(indent2_os, dex_file, code_item); 331 } 332 if (Runtime::Current() != NULL) { 333 indent1_os << "VERIFIER TYPE ANALYSIS:\n"; 334 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count); 335 std::ostream indent2_os(&indent2_filter); 336 DumpVerifier(indent2_os, dex_method_idx, &dex_file, class_def, code_item, 337 method_access_flags); 338 } 339 { 340 indent1_os << "OAT DATA:\n"; 341 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count); 342 std::ostream indent2_os(&indent2_filter); 343 344 indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes()); 345 indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask()); 346 DumpSpillMask(indent2_os, oat_method.GetCoreSpillMask(), false); 347 indent2_os << StringPrintf("\nfp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask()); 348 DumpSpillMask(indent2_os, oat_method.GetFpSpillMask(), true); 349 indent2_os << StringPrintf("\nvmap_table: %p (offset=0x%08x)\n", 350 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset()); 351 DumpVmap(indent2_os, oat_method); 352 indent2_os << StringPrintf("mapping_table: %p (offset=0x%08x)\n", 353 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset()); 354 const bool kDumpRawMappingTable = false; 355 if (kDumpRawMappingTable) { 356 Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count); 357 std::ostream indent3_os(&indent3_filter); 358 DumpMappingTable(indent3_os, oat_method); 359 } 360 indent2_os << StringPrintf("gc_map: %p (offset=0x%08x)\n", 361 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset()); 362 const bool kDumpRawGcMap = false; 363 if (kDumpRawGcMap) { 364 Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count); 365 std::ostream indent3_os(&indent3_filter); 366 DumpGcMap(indent3_os, oat_method, code_item); 367 } 368 } 369 { 370 indent1_os << StringPrintf("CODE: %p (offset=0x%08x size=%d)%s\n", 371 oat_method.GetCode(), 372 oat_method.GetCodeOffset(), 373 oat_method.GetCodeSize(), 374 oat_method.GetCode() != NULL ? "..." : ""); 375 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count); 376 std::ostream indent2_os(&indent2_filter); 377 DumpCode(indent2_os, oat_method, dex_method_idx, &dex_file, class_def, code_item, 378 method_access_flags); 379 } 380 } 381 382 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) { 383 if (spill_mask == 0) { 384 return; 385 } 386 os << "("; 387 for (size_t i = 0; i < 32; i++) { 388 if ((spill_mask & (1 << i)) != 0) { 389 if (is_float) { 390 os << "fr" << i; 391 } else { 392 os << "r" << i; 393 } 394 spill_mask ^= 1 << i; // clear bit 395 if (spill_mask != 0) { 396 os << ", "; 397 } else { 398 break; 399 } 400 } 401 } 402 os << ")"; 403 } 404 405 void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) { 406 const uint8_t* raw_table = oat_method.GetVmapTable(); 407 if (raw_table != NULL) { 408 const VmapTable vmap_table(raw_table); 409 bool first = true; 410 bool processing_fp = false; 411 uint32_t spill_mask = oat_method.GetCoreSpillMask(); 412 for (size_t i = 0; i < vmap_table.Size(); i++) { 413 uint16_t dex_reg = vmap_table[i]; 414 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i, 415 processing_fp ? kFloatVReg : kIntVReg); 416 os << (first ? "v" : ", v") << dex_reg; 417 if (!processing_fp) { 418 os << "/r" << cpu_reg; 419 } else { 420 os << "/fr" << cpu_reg; 421 } 422 first = false; 423 if (!processing_fp && dex_reg == 0xFFFF) { 424 processing_fp = true; 425 spill_mask = oat_method.GetFpSpillMask(); 426 } 427 } 428 os << "\n"; 429 } 430 } 431 432 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method, 433 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) { 434 const uint8_t* raw_table = oat_method.GetVmapTable(); 435 if (raw_table != NULL) { 436 const VmapTable vmap_table(raw_table); 437 uint32_t vmap_offset; 438 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) { 439 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg); 440 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask() 441 : oat_method.GetCoreSpillMask(); 442 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind); 443 } else { 444 uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(), 445 oat_method.GetFpSpillMask(), 446 oat_method.GetFrameSizeInBytes(), reg); 447 os << "[sp + #" << offset << "]"; 448 } 449 } 450 } 451 452 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method, 453 const DexFile::CodeItem* code_item) { 454 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap(); 455 if (gc_map_raw == NULL) { 456 return; 457 } 458 NativePcOffsetToReferenceMap map(gc_map_raw); 459 const void* code = oat_method.GetCode(); 460 for (size_t entry = 0; entry < map.NumEntries(); entry++) { 461 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) + 462 map.GetNativePcOffset(entry); 463 os << StringPrintf("%p", native_pc); 464 size_t num_regs = map.RegWidth() * 8; 465 const uint8_t* reg_bitmap = map.GetBitMap(entry); 466 bool first = true; 467 for (size_t reg = 0; reg < num_regs; reg++) { 468 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { 469 if (first) { 470 os << " v" << reg << " ("; 471 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); 472 os << ")"; 473 first = false; 474 } else { 475 os << ", v" << reg << " ("; 476 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); 477 os << ")"; 478 } 479 } 480 } 481 os << "\n"; 482 } 483 } 484 485 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) { 486 const void* code = oat_method.GetCode(); 487 if (code == NULL) { 488 return; 489 } 490 MappingTable table(oat_method.GetMappingTable()); 491 if (table.TotalSize() != 0) { 492 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 493 std::ostream indent_os(&indent_filter); 494 if (table.PcToDexSize() != 0) { 495 typedef MappingTable::PcToDexIterator It; 496 os << "suspend point mappings {\n"; 497 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { 498 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc()); 499 } 500 os << "}\n"; 501 } 502 if (table.DexToPcSize() != 0) { 503 typedef MappingTable::DexToPcIterator It; 504 os << "catch entry mappings {\n"; 505 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { 506 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc()); 507 } 508 os << "}\n"; 509 } 510 } 511 } 512 513 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method, 514 size_t offset, bool suspend_point_mapping) { 515 MappingTable table(oat_method.GetMappingTable()); 516 if (suspend_point_mapping && table.PcToDexSize() > 0) { 517 typedef MappingTable::PcToDexIterator It; 518 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) { 519 if (offset == cur.NativePcOffset()) { 520 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc()); 521 return cur.DexPc(); 522 } 523 } 524 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) { 525 typedef MappingTable::DexToPcIterator It; 526 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) { 527 if (offset == cur.NativePcOffset()) { 528 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc()); 529 return cur.DexPc(); 530 } 531 } 532 } 533 return DexFile::kDexNoIndex; 534 } 535 536 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method, 537 const DexFile::CodeItem* code_item, size_t native_pc_offset) { 538 const uint8_t* gc_map_raw = oat_method.GetNativeGcMap(); 539 if (gc_map_raw != NULL) { 540 NativePcOffsetToReferenceMap map(gc_map_raw); 541 if (map.HasEntry(native_pc_offset)) { 542 size_t num_regs = map.RegWidth() * 8; 543 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset); 544 bool first = true; 545 for (size_t reg = 0; reg < num_regs; reg++) { 546 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) { 547 if (first) { 548 os << "GC map objects: v" << reg << " ("; 549 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); 550 os << ")"; 551 first = false; 552 } else { 553 os << ", v" << reg << " ("; 554 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg); 555 os << ")"; 556 } 557 } 558 } 559 if (!first) { 560 os << "\n"; 561 } 562 } 563 } 564 } 565 566 void DumpVRegsAtDexPc(std::ostream& os, const OatFile::OatMethod& oat_method, 567 uint32_t dex_method_idx, const DexFile* dex_file, 568 const DexFile::ClassDef& class_def, const DexFile::CodeItem* code_item, 569 uint32_t method_access_flags, uint32_t dex_pc) { 570 static UniquePtr<verifier::MethodVerifier> verifier; 571 static const DexFile* verified_dex_file = NULL; 572 static uint32_t verified_dex_method_idx = DexFile::kDexNoIndex; 573 if (dex_file != verified_dex_file || verified_dex_method_idx != dex_method_idx) { 574 ScopedObjectAccess soa(Thread::Current()); 575 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file); 576 mirror::ClassLoader* class_loader = NULL; 577 verifier.reset(new verifier::MethodVerifier(dex_file, dex_cache, class_loader, &class_def, 578 code_item, dex_method_idx, NULL, 579 method_access_flags, true, true)); 580 verifier->Verify(); 581 verified_dex_file = dex_file; 582 verified_dex_method_idx = dex_method_idx; 583 } 584 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc); 585 bool first = true; 586 for (size_t reg = 0; reg < code_item->registers_size_; reg++) { 587 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2)); 588 if (kind != kUndefined) { 589 if (first) { 590 os << "VRegs: v"; 591 first = false; 592 } else { 593 os << ", v"; 594 } 595 os << reg << " ("; 596 switch (kind) { 597 case kImpreciseConstant: 598 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", "; 599 DescribeVReg(os, oat_method, code_item, reg, kind); 600 break; 601 case kConstant: 602 os << "Constant: " << kinds.at((reg * 2) + 1); 603 break; 604 default: 605 DescribeVReg(os, oat_method, code_item, reg, kind); 606 break; 607 } 608 os << ")"; 609 } 610 } 611 if (!first) { 612 os << "\n"; 613 } 614 } 615 616 617 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) { 618 if (code_item != NULL) { 619 size_t i = 0; 620 while (i < code_item->insns_size_in_code_units_) { 621 const Instruction* instruction = Instruction::At(&code_item->insns_[i]); 622 os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str()); 623 i += instruction->SizeInCodeUnits(); 624 } 625 } 626 } 627 628 void DumpVerifier(std::ostream& os, uint32_t dex_method_idx, const DexFile* dex_file, 629 const DexFile::ClassDef& class_def, const DexFile::CodeItem* code_item, 630 uint32_t method_access_flags) { 631 if ((method_access_flags & kAccNative) == 0) { 632 ScopedObjectAccess soa(Thread::Current()); 633 mirror::DexCache* dex_cache = Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file); 634 mirror::ClassLoader* class_loader = NULL; 635 verifier::MethodVerifier::VerifyMethodAndDump(os, dex_method_idx, dex_file, dex_cache, 636 class_loader, &class_def, code_item, NULL, 637 method_access_flags); 638 } 639 } 640 641 void DumpCode(std::ostream& os, const OatFile::OatMethod& oat_method, 642 uint32_t dex_method_idx, const DexFile* dex_file, 643 const DexFile::ClassDef& class_def, const DexFile::CodeItem* code_item, 644 uint32_t method_access_flags) { 645 const void* code = oat_method.GetCode(); 646 size_t code_size = oat_method.GetCodeSize(); 647 if (code == NULL || code_size == 0) { 648 os << "NO CODE!\n"; 649 return; 650 } 651 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code); 652 size_t offset = 0; 653 const bool kDumpVRegs = (Runtime::Current() != NULL); 654 while (offset < code_size) { 655 DumpMappingAtOffset(os, oat_method, offset, false); 656 offset += disassembler_->Dump(os, native_pc + offset); 657 uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true); 658 if (dex_pc != DexFile::kDexNoIndex) { 659 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset); 660 if (kDumpVRegs) { 661 DumpVRegsAtDexPc(os, oat_method, dex_method_idx, dex_file, class_def, code_item, 662 method_access_flags, dex_pc); 663 } 664 } 665 } 666 } 667 668 const std::string host_prefix_; 669 const OatFile& oat_file_; 670 std::vector<const OatFile::OatDexFile*> oat_dex_files_; 671 std::set<uint32_t> offsets_; 672 UniquePtr<Disassembler> disassembler_; 673}; 674 675class ImageDumper { 676 public: 677 explicit ImageDumper(std::ostream* os, const std::string& image_filename, 678 const std::string& host_prefix, gc::space::ImageSpace& image_space, 679 const ImageHeader& image_header) 680 : os_(os), image_filename_(image_filename), host_prefix_(host_prefix), 681 image_space_(image_space), image_header_(image_header) {} 682 683 void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 684 std::ostream& os = *os_; 685 os << "MAGIC: " << image_header_.GetMagic() << "\n\n"; 686 687 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n"; 688 689 os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset()) 690 << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n"; 691 692 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()); 693 694 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n"; 695 696 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n"; 697 698 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n"; 699 700 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n"; 701 702 { 703 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n"; 704 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 705 std::ostream indent1_os(&indent1_filter); 706 CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax)); 707 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { 708 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); 709 const char* image_root_description = image_roots_descriptions_[i]; 710 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root); 711 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object); 712 if (image_root_object->IsObjectArray()) { 713 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count); 714 std::ostream indent2_os(&indent2_filter); 715 mirror::ObjectArray<mirror::Object>* image_root_object_array 716 = image_root_object->AsObjectArray<mirror::Object>(); 717 for (int i = 0; i < image_root_object_array->GetLength(); i++) { 718 mirror::Object* value = image_root_object_array->Get(i); 719 size_t run = 0; 720 for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) { 721 if (value == image_root_object_array->Get(j)) { 722 run++; 723 } else { 724 break; 725 } 726 } 727 if (run == 0) { 728 indent2_os << StringPrintf("%d: ", i); 729 } else { 730 indent2_os << StringPrintf("%d to %zd: ", i, i + run); 731 i = i + run; 732 } 733 if (value != NULL) { 734 PrettyObjectValue(indent2_os, value->GetClass(), value); 735 } else { 736 indent2_os << i << ": null\n"; 737 } 738 } 739 } 740 } 741 } 742 os << "\n"; 743 744 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 745 mirror::Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation); 746 std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8()); 747 os << "OAT LOCATION: " << oat_location; 748 if (!host_prefix_.empty()) { 749 oat_location = host_prefix_ + oat_location; 750 os << " (" << oat_location << ")"; 751 } 752 os << "\n"; 753 std::string error_msg; 754 const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location, &error_msg); 755 if (oat_file == NULL) { 756 os << "NOT FOUND: " << error_msg << "\n"; 757 return; 758 } 759 os << "\n"; 760 761 stats_.oat_file_bytes = oat_file->Size(); 762 763 oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file)); 764 765 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) { 766 CHECK(oat_dex_file != NULL); 767 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(), 768 oat_dex_file->FileSize())); 769 } 770 771 os << "OBJECTS:\n" << std::flush; 772 773 // Loop through all the image spaces and dump their objects. 774 gc::Heap* heap = Runtime::Current()->GetHeap(); 775 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces(); 776 Thread* self = Thread::Current(); 777 { 778 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); 779 heap->FlushAllocStack(); 780 } 781 { 782 std::ostream* saved_os = os_; 783 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 784 std::ostream indent_os(&indent_filter); 785 os_ = &indent_os; 786 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); 787 for (const auto& space : spaces) { 788 if (space->IsImageSpace()) { 789 gc::space::ImageSpace* image_space = space->AsImageSpace(); 790 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this); 791 indent_os << "\n"; 792 } 793 } 794 // Dump the large objects separately. 795 heap->GetLargeObjectsSpace()->GetLiveObjects()->Walk(ImageDumper::Callback, this); 796 indent_os << "\n"; 797 os_ = saved_os; 798 } 799 os << "STATS:\n" << std::flush; 800 UniquePtr<File> file(OS::OpenFileForReading(image_filename_.c_str())); 801 if (file.get() == NULL) { 802 std::string cache_location(GetDalvikCacheFilenameOrDie(image_filename_.c_str())); 803 file.reset(OS::OpenFileForReading(cache_location.c_str())); 804 if (file.get() == NULL) { 805 LOG(WARNING) << "Failed to find image in " << image_filename_ 806 << " and " << cache_location; 807 } 808 } 809 if (file.get() != NULL) { 810 stats_.file_bytes = file->GetLength(); 811 } 812 size_t header_bytes = sizeof(ImageHeader); 813 stats_.header_bytes = header_bytes; 814 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes; 815 stats_.alignment_bytes += alignment_bytes; 816 stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize(); 817 stats_.bitmap_bytes += image_header_.GetImageBitmapSize(); 818 stats_.Dump(os); 819 os << "\n"; 820 821 os << std::flush; 822 823 oat_dumper_->Dump(os); 824 } 825 826 private: 827 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value) 828 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 829 CHECK(type != NULL); 830 if (value == NULL) { 831 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str()); 832 } else if (type->IsStringClass()) { 833 mirror::String* string = value->AsString(); 834 os << StringPrintf("%p String: %s\n", string, 835 PrintableString(string->ToModifiedUtf8()).c_str()); 836 } else if (type->IsClassClass()) { 837 mirror::Class* klass = value->AsClass(); 838 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str()); 839 } else if (type->IsArtFieldClass()) { 840 mirror::ArtField* field = value->AsArtField(); 841 os << StringPrintf("%p Field: %s\n", field, PrettyField(field).c_str()); 842 } else if (type->IsArtMethodClass()) { 843 mirror::ArtMethod* method = value->AsArtMethod(); 844 os << StringPrintf("%p Method: %s\n", method, PrettyMethod(method).c_str()); 845 } else { 846 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str()); 847 } 848 } 849 850 static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj) 851 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 852 FieldHelper fh(field); 853 const char* descriptor = fh.GetTypeDescriptor(); 854 os << StringPrintf("%s: ", fh.GetName()); 855 if (descriptor[0] != 'L' && descriptor[0] != '[') { 856 mirror::Class* type = fh.GetType(); 857 if (type->IsPrimitiveLong()) { 858 os << StringPrintf("%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj)); 859 } else if (type->IsPrimitiveDouble()) { 860 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj)); 861 } else if (type->IsPrimitiveFloat()) { 862 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj)); 863 } else { 864 DCHECK(type->IsPrimitive()); 865 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj)); 866 } 867 } else { 868 // Get the value, don't compute the type unless it is non-null as we don't want 869 // to cause class loading. 870 mirror::Object* value = field->GetObj(obj); 871 if (value == NULL) { 872 os << StringPrintf("null %s\n", PrettyDescriptor(descriptor).c_str()); 873 } else { 874 // Grab the field type without causing resolution. 875 mirror::Class* field_type = fh.GetType(false); 876 if (field_type != NULL) { 877 PrettyObjectValue(os, field_type, value); 878 } else { 879 os << StringPrintf("%p %s\n", value, PrettyDescriptor(descriptor).c_str()); 880 } 881 } 882 } 883 } 884 885 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass) 886 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 887 mirror::Class* super = klass->GetSuperClass(); 888 if (super != NULL) { 889 DumpFields(os, obj, super); 890 } 891 mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields(); 892 if (fields != NULL) { 893 for (int32_t i = 0; i < fields->GetLength(); i++) { 894 mirror::ArtField* field = fields->Get(i); 895 PrintField(os, field, obj); 896 } 897 } 898 } 899 900 bool InDumpSpace(const mirror::Object* object) { 901 return image_space_.Contains(object); 902 } 903 904 const void* GetOatCodeBegin(mirror::ArtMethod* m) 905 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 906 const void* code = m->GetEntryPointFromCompiledCode(); 907 if (code == GetResolutionTrampoline(Runtime::Current()->GetClassLinker())) { 908 code = oat_dumper_->GetOatCode(m); 909 } 910 if (oat_dumper_->GetInstructionSet() == kThumb2) { 911 code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1); 912 } 913 return code; 914 } 915 916 uint32_t GetOatCodeSize(mirror::ArtMethod* m) 917 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 918 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m)); 919 if (oat_code_begin == NULL) { 920 return 0; 921 } 922 return oat_code_begin[-1]; 923 } 924 925 const void* GetOatCodeEnd(mirror::ArtMethod* m) 926 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 927 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m)); 928 if (oat_code_begin == NULL) { 929 return NULL; 930 } 931 return oat_code_begin + GetOatCodeSize(m); 932 } 933 934 static void Callback(mirror::Object* obj, void* arg) 935 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 936 DCHECK(obj != NULL); 937 DCHECK(arg != NULL); 938 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg); 939 if (!state->InDumpSpace(obj)) { 940 return; 941 } 942 943 size_t object_bytes = obj->SizeOf(); 944 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; 945 state->stats_.object_bytes += object_bytes; 946 state->stats_.alignment_bytes += alignment_bytes; 947 948 std::ostream& os = *state->os_; 949 mirror::Class* obj_class = obj->GetClass(); 950 if (obj_class->IsArrayClass()) { 951 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(), 952 obj->AsArray()->GetLength()); 953 } else if (obj->IsClass()) { 954 mirror::Class* klass = obj->AsClass(); 955 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str()) 956 << klass->GetStatus() << ")\n"; 957 } else if (obj->IsArtField()) { 958 os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj, 959 PrettyField(obj->AsArtField()).c_str()); 960 } else if (obj->IsArtMethod()) { 961 os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj, 962 PrettyMethod(obj->AsArtMethod()).c_str()); 963 } else if (obj_class->IsStringClass()) { 964 os << StringPrintf("%p: java.lang.String %s\n", obj, 965 PrintableString(obj->AsString()->ToModifiedUtf8()).c_str()); 966 } else { 967 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str()); 968 } 969 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 970 std::ostream indent_os(&indent_filter); 971 DumpFields(indent_os, obj, obj_class); 972 if (obj->IsObjectArray()) { 973 mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>(); 974 int32_t length = obj_array->GetLength(); 975 for (int32_t i = 0; i < length; i++) { 976 mirror::Object* value = obj_array->Get(i); 977 size_t run = 0; 978 for (int32_t j = i + 1; j < length; j++) { 979 if (value == obj_array->Get(j)) { 980 run++; 981 } else { 982 break; 983 } 984 } 985 if (run == 0) { 986 indent_os << StringPrintf("%d: ", i); 987 } else { 988 indent_os << StringPrintf("%d to %zd: ", i, i + run); 989 i = i + run; 990 } 991 mirror::Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass(); 992 PrettyObjectValue(indent_os, value_class, value); 993 } 994 } else if (obj->IsClass()) { 995 mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields(); 996 if (sfields != NULL) { 997 indent_os << "STATICS:\n"; 998 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count); 999 std::ostream indent2_os(&indent2_filter); 1000 for (int32_t i = 0; i < sfields->GetLength(); i++) { 1001 mirror::ArtField* field = sfields->Get(i); 1002 PrintField(indent2_os, field, field->GetDeclaringClass()); 1003 } 1004 } 1005 } else if (obj->IsArtMethod()) { 1006 mirror::ArtMethod* method = obj->AsArtMethod(); 1007 if (method->IsNative()) { 1008 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method); 1009 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); 1010 bool first_occurrence; 1011 const void* oat_code = state->GetOatCodeBegin(method); 1012 uint32_t oat_code_size = state->GetOatCodeSize(method); 1013 state->ComputeOatSize(oat_code, &first_occurrence); 1014 if (first_occurrence) { 1015 state->stats_.native_to_managed_code_bytes += oat_code_size; 1016 } 1017 if (oat_code != method->GetEntryPointFromCompiledCode()) { 1018 indent_os << StringPrintf("OAT CODE: %p\n", oat_code); 1019 } 1020 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() || 1021 method->IsResolutionMethod() || method->IsImtConflictMethod() || 1022 MethodHelper(method).IsClassInitializer()) { 1023 DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method); 1024 DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method); 1025 } else { 1026 // TODO: we check there is a GC map here, we may not have a GC map if the code is pointing 1027 // to the quick/portable to interpreter bridge. 1028 CHECK(method->GetNativeGcMap() != NULL) << PrettyMethod(method); 1029 1030 const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem(); 1031 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2; 1032 state->stats_.dex_instruction_bytes += dex_instruction_bytes; 1033 1034 bool first_occurrence; 1035 size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence); 1036 if (first_occurrence) { 1037 state->stats_.gc_map_bytes += gc_map_bytes; 1038 } 1039 1040 size_t pc_mapping_table_bytes = 1041 state->ComputeOatSize(method->GetMappingTable(), &first_occurrence); 1042 if (first_occurrence) { 1043 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; 1044 } 1045 1046 size_t vmap_table_bytes = 1047 state->ComputeOatSize(method->GetVmapTable(), &first_occurrence); 1048 if (first_occurrence) { 1049 state->stats_.vmap_table_bytes += vmap_table_bytes; 1050 } 1051 1052 const void* oat_code_begin = state->GetOatCodeBegin(method); 1053 const void* oat_code_end = state->GetOatCodeEnd(method); 1054 uint32_t oat_code_size = state->GetOatCodeSize(method); 1055 state->ComputeOatSize(oat_code_begin, &first_occurrence); 1056 if (first_occurrence) { 1057 state->stats_.managed_code_bytes += oat_code_size; 1058 if (method->IsConstructor()) { 1059 if (method->IsStatic()) { 1060 state->stats_.class_initializer_code_bytes += oat_code_size; 1061 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) { 1062 state->stats_.large_initializer_code_bytes += oat_code_size; 1063 } 1064 } else if (dex_instruction_bytes > kLargeMethodDexBytes) { 1065 state->stats_.large_method_code_bytes += oat_code_size; 1066 } 1067 } 1068 state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size; 1069 1070 indent_os << StringPrintf("OAT CODE: %p-%p\n", oat_code_begin, oat_code_end); 1071 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n", 1072 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes); 1073 1074 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes + 1075 vmap_table_bytes + oat_code_size + object_bytes; 1076 1077 double expansion = 1078 static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes); 1079 state->stats_.ComputeOutliers(total_size, expansion, method); 1080 } 1081 } 1082 state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes); 1083 } 1084 1085 std::set<const void*> already_seen_; 1086 // Compute the size of the given data within the oat file and whether this is the first time 1087 // this data has been requested 1088 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) { 1089 if (already_seen_.count(oat_data) == 0) { 1090 *first_occurrence = true; 1091 already_seen_.insert(oat_data); 1092 } else { 1093 *first_occurrence = false; 1094 } 1095 return oat_dumper_->ComputeSize(oat_data); 1096 } 1097 1098 public: 1099 struct Stats { 1100 size_t oat_file_bytes; 1101 size_t file_bytes; 1102 1103 size_t header_bytes; 1104 size_t object_bytes; 1105 size_t bitmap_bytes; 1106 size_t alignment_bytes; 1107 1108 size_t managed_code_bytes; 1109 size_t managed_code_bytes_ignoring_deduplication; 1110 size_t managed_to_native_code_bytes; 1111 size_t native_to_managed_code_bytes; 1112 size_t class_initializer_code_bytes; 1113 size_t large_initializer_code_bytes; 1114 size_t large_method_code_bytes; 1115 1116 size_t gc_map_bytes; 1117 size_t pc_mapping_table_bytes; 1118 size_t vmap_table_bytes; 1119 1120 size_t dex_instruction_bytes; 1121 1122 std::vector<mirror::ArtMethod*> method_outlier; 1123 std::vector<size_t> method_outlier_size; 1124 std::vector<double> method_outlier_expansion; 1125 std::vector<std::pair<std::string, size_t> > oat_dex_file_sizes; 1126 1127 explicit Stats() 1128 : oat_file_bytes(0), 1129 file_bytes(0), 1130 header_bytes(0), 1131 object_bytes(0), 1132 bitmap_bytes(0), 1133 alignment_bytes(0), 1134 managed_code_bytes(0), 1135 managed_code_bytes_ignoring_deduplication(0), 1136 managed_to_native_code_bytes(0), 1137 native_to_managed_code_bytes(0), 1138 class_initializer_code_bytes(0), 1139 large_initializer_code_bytes(0), 1140 large_method_code_bytes(0), 1141 gc_map_bytes(0), 1142 pc_mapping_table_bytes(0), 1143 vmap_table_bytes(0), 1144 dex_instruction_bytes(0) {} 1145 1146 struct SizeAndCount { 1147 SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {} 1148 size_t bytes; 1149 size_t count; 1150 }; 1151 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable; 1152 SizeAndCountTable sizes_and_counts; 1153 1154 void Update(const char* descriptor, size_t object_bytes) { 1155 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor); 1156 if (it != sizes_and_counts.end()) { 1157 it->second.bytes += object_bytes; 1158 it->second.count += 1; 1159 } else { 1160 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1)); 1161 } 1162 } 1163 1164 double PercentOfOatBytes(size_t size) { 1165 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100; 1166 } 1167 1168 double PercentOfFileBytes(size_t size) { 1169 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; 1170 } 1171 1172 double PercentOfObjectBytes(size_t size) { 1173 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; 1174 } 1175 1176 void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) { 1177 method_outlier_size.push_back(total_size); 1178 method_outlier_expansion.push_back(expansion); 1179 method_outlier.push_back(method); 1180 } 1181 1182 void DumpOutliers(std::ostream& os) 1183 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1184 size_t sum_of_sizes = 0; 1185 size_t sum_of_sizes_squared = 0; 1186 size_t sum_of_expansion = 0; 1187 size_t sum_of_expansion_squared = 0; 1188 size_t n = method_outlier_size.size(); 1189 for (size_t i = 0; i < n; i++) { 1190 size_t cur_size = method_outlier_size[i]; 1191 sum_of_sizes += cur_size; 1192 sum_of_sizes_squared += cur_size * cur_size; 1193 double cur_expansion = method_outlier_expansion[i]; 1194 sum_of_expansion += cur_expansion; 1195 sum_of_expansion_squared += cur_expansion * cur_expansion; 1196 } 1197 size_t size_mean = sum_of_sizes / n; 1198 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1); 1199 double expansion_mean = sum_of_expansion / n; 1200 double expansion_variance = 1201 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1); 1202 1203 // Dump methods whose size is a certain number of standard deviations from the mean 1204 size_t dumped_values = 0; 1205 size_t skipped_values = 0; 1206 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations 1207 size_t cur_size_variance = i * i * size_variance; 1208 bool first = true; 1209 for (size_t j = 0; j < n; j++) { 1210 size_t cur_size = method_outlier_size[j]; 1211 if (cur_size > size_mean) { 1212 size_t cur_var = cur_size - size_mean; 1213 cur_var = cur_var * cur_var; 1214 if (cur_var > cur_size_variance) { 1215 if (dumped_values > 20) { 1216 if (i == 1) { 1217 skipped_values++; 1218 } else { 1219 i = 2; // jump to counting for 1 standard deviation 1220 break; 1221 } 1222 } else { 1223 if (first) { 1224 os << "\nBig methods (size > " << i << " standard deviations the norm):\n"; 1225 first = false; 1226 } 1227 os << PrettyMethod(method_outlier[j]) << " requires storage of " 1228 << PrettySize(cur_size) << "\n"; 1229 method_outlier_size[j] = 0; // don't consider this method again 1230 dumped_values++; 1231 } 1232 } 1233 } 1234 } 1235 } 1236 if (skipped_values > 0) { 1237 os << "... skipped " << skipped_values 1238 << " methods with size > 1 standard deviation from the norm\n"; 1239 } 1240 os << std::flush; 1241 1242 // Dump methods whose expansion is a certain number of standard deviations from the mean 1243 dumped_values = 0; 1244 skipped_values = 0; 1245 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations 1246 double cur_expansion_variance = i * i * expansion_variance; 1247 bool first = true; 1248 for (size_t j = 0; j < n; j++) { 1249 double cur_expansion = method_outlier_expansion[j]; 1250 if (cur_expansion > expansion_mean) { 1251 size_t cur_var = cur_expansion - expansion_mean; 1252 cur_var = cur_var * cur_var; 1253 if (cur_var > cur_expansion_variance) { 1254 if (dumped_values > 20) { 1255 if (i == 1) { 1256 skipped_values++; 1257 } else { 1258 i = 2; // jump to counting for 1 standard deviation 1259 break; 1260 } 1261 } else { 1262 if (first) { 1263 os << "\nLarge expansion methods (size > " << i 1264 << " standard deviations the norm):\n"; 1265 first = false; 1266 } 1267 os << PrettyMethod(method_outlier[j]) << " expanded code by " 1268 << cur_expansion << "\n"; 1269 method_outlier_expansion[j] = 0.0; // don't consider this method again 1270 dumped_values++; 1271 } 1272 } 1273 } 1274 } 1275 } 1276 if (skipped_values > 0) { 1277 os << "... skipped " << skipped_values 1278 << " methods with expansion > 1 standard deviation from the norm\n"; 1279 } 1280 os << "\n" << std::flush; 1281 } 1282 1283 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 1284 { 1285 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n" 1286 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n"; 1287 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count); 1288 std::ostream indent_os(&indent_filter); 1289 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n" 1290 "object_bytes = %8zd (%2.0f%% of art file bytes)\n" 1291 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n" 1292 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n", 1293 header_bytes, PercentOfFileBytes(header_bytes), 1294 object_bytes, PercentOfFileBytes(object_bytes), 1295 bitmap_bytes, PercentOfFileBytes(bitmap_bytes), 1296 alignment_bytes, PercentOfFileBytes(alignment_bytes)) 1297 << std::flush; 1298 CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes); 1299 } 1300 1301 os << "object_bytes breakdown:\n"; 1302 size_t object_bytes_total = 0; 1303 for (const auto& sizes_and_count : sizes_and_counts) { 1304 const std::string& descriptor(sizes_and_count.first); 1305 double average = static_cast<double>(sizes_and_count.second.bytes) / 1306 static_cast<double>(sizes_and_count.second.count); 1307 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes); 1308 os << StringPrintf("%32s %8zd bytes %6zd instances " 1309 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n", 1310 descriptor.c_str(), sizes_and_count.second.bytes, 1311 sizes_and_count.second.count, average, percent); 1312 object_bytes_total += sizes_and_count.second.bytes; 1313 } 1314 os << "\n" << std::flush; 1315 CHECK_EQ(object_bytes, object_bytes_total); 1316 1317 os << StringPrintf("oat_file_bytes = %8zd\n" 1318 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" 1319 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" 1320 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n" 1321 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" 1322 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n" 1323 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n", 1324 oat_file_bytes, 1325 managed_code_bytes, PercentOfOatBytes(managed_code_bytes), 1326 managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes), 1327 native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes), 1328 class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes), 1329 large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes), 1330 large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes)) 1331 << "DexFile sizes:\n"; 1332 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) { 1333 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n", 1334 oat_dex_file_size.first.c_str(), oat_dex_file_size.second, 1335 PercentOfOatBytes(oat_dex_file_size.second)); 1336 } 1337 1338 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n" 1339 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n" 1340 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n", 1341 gc_map_bytes, PercentOfOatBytes(gc_map_bytes), 1342 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes), 1343 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes)) 1344 << std::flush; 1345 1346 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes) 1347 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n", 1348 static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes), 1349 static_cast<double>(managed_code_bytes_ignoring_deduplication) / 1350 static_cast<double>(dex_instruction_bytes)) 1351 << std::flush; 1352 1353 DumpOutliers(os); 1354 } 1355 } stats_; 1356 1357 private: 1358 enum { 1359 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block 1360 // threshold, we assume 2 bytes per instruction and 2 instructions per block. 1361 kLargeConstructorDexBytes = 4000, 1362 // Number of bytes for a method to be considered large. Based on the 4000 basic block 1363 // threshold, we assume 2 bytes per instruction and 2 instructions per block. 1364 kLargeMethodDexBytes = 16000 1365 }; 1366 UniquePtr<OatDumper> oat_dumper_; 1367 std::ostream* os_; 1368 const std::string image_filename_; 1369 const std::string host_prefix_; 1370 gc::space::ImageSpace& image_space_; 1371 const ImageHeader& image_header_; 1372 1373 DISALLOW_COPY_AND_ASSIGN(ImageDumper); 1374}; 1375 1376static int oatdump(int argc, char** argv) { 1377 InitLogging(argv); 1378 1379 // Skip over argv[0]. 1380 argv++; 1381 argc--; 1382 1383 if (argc == 0) { 1384 fprintf(stderr, "No arguments specified\n"); 1385 usage(); 1386 } 1387 1388 const char* oat_filename = NULL; 1389 const char* image_filename = NULL; 1390 const char* boot_image_filename = NULL; 1391 std::string elf_filename_prefix; 1392 UniquePtr<std::string> host_prefix; 1393 std::ostream* os = &std::cout; 1394 UniquePtr<std::ofstream> out; 1395 1396 for (int i = 0; i < argc; i++) { 1397 const StringPiece option(argv[i]); 1398 if (option.starts_with("--oat-file=")) { 1399 oat_filename = option.substr(strlen("--oat-file=")).data(); 1400 } else if (option.starts_with("--image=")) { 1401 image_filename = option.substr(strlen("--image=")).data(); 1402 } else if (option.starts_with("--boot-image=")) { 1403 boot_image_filename = option.substr(strlen("--boot-image=")).data(); 1404 } else if (option.starts_with("--host-prefix=")) { 1405 host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data())); 1406 } else if (option.starts_with("--output=")) { 1407 const char* filename = option.substr(strlen("--output=")).data(); 1408 out.reset(new std::ofstream(filename)); 1409 if (!out->good()) { 1410 fprintf(stderr, "Failed to open output filename %s\n", filename); 1411 usage(); 1412 } 1413 os = out.get(); 1414 } else { 1415 fprintf(stderr, "Unknown argument %s\n", option.data()); 1416 usage(); 1417 } 1418 } 1419 1420 if (image_filename == NULL && oat_filename == NULL) { 1421 fprintf(stderr, "Either --image or --oat must be specified\n"); 1422 return EXIT_FAILURE; 1423 } 1424 1425 if (image_filename != NULL && oat_filename != NULL) { 1426 fprintf(stderr, "Either --image or --oat must be specified but not both\n"); 1427 return EXIT_FAILURE; 1428 } 1429 1430 if (host_prefix.get() == NULL) { 1431 const char* android_product_out = getenv("ANDROID_PRODUCT_OUT"); 1432 if (android_product_out != NULL) { 1433 host_prefix.reset(new std::string(android_product_out)); 1434 } else { 1435 host_prefix.reset(new std::string("")); 1436 } 1437 } 1438 1439 if (oat_filename != NULL) { 1440 std::string error_msg; 1441 OatFile* oat_file = 1442 OatFile::Open(oat_filename, oat_filename, NULL, false, &error_msg); 1443 if (oat_file == NULL) { 1444 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str()); 1445 return EXIT_FAILURE; 1446 } 1447 OatDumper oat_dumper(*host_prefix.get(), *oat_file); 1448 oat_dumper.Dump(*os); 1449 return EXIT_SUCCESS; 1450 } 1451 1452 Runtime::Options options; 1453 std::string image_option; 1454 std::string oat_option; 1455 std::string boot_image_option; 1456 std::string boot_oat_option; 1457 1458 // We are more like a compiler than a run-time. We don't want to execute code. 1459 options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL))); 1460 1461 if (boot_image_filename != NULL) { 1462 boot_image_option += "-Ximage:"; 1463 boot_image_option += boot_image_filename; 1464 options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); 1465 } 1466 if (image_filename != NULL) { 1467 image_option += "-Ximage:"; 1468 image_option += image_filename; 1469 options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL))); 1470 } 1471 1472 if (!host_prefix->empty()) { 1473 options.push_back(std::make_pair("host-prefix", host_prefix->c_str())); 1474 } 1475 1476 if (!Runtime::Create(options, false)) { 1477 fprintf(stderr, "Failed to create runtime\n"); 1478 return EXIT_FAILURE; 1479 } 1480 UniquePtr<Runtime> runtime(Runtime::Current()); 1481 // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start, 1482 // give it away now and then switch to a more managable ScopedObjectAccess. 1483 Thread::Current()->TransitionFromRunnableToSuspended(kNative); 1484 ScopedObjectAccess soa(Thread::Current()); 1485 1486 gc::Heap* heap = Runtime::Current()->GetHeap(); 1487 gc::space::ImageSpace* image_space = heap->GetImageSpace(); 1488 CHECK(image_space != NULL); 1489 const ImageHeader& image_header = image_space->GetImageHeader(); 1490 if (!image_header.IsValid()) { 1491 fprintf(stderr, "Invalid image header %s\n", image_filename); 1492 return EXIT_FAILURE; 1493 } 1494 ImageDumper image_dumper(os, image_filename, *host_prefix.get(), *image_space, image_header); 1495 image_dumper.Dump(); 1496 return EXIT_SUCCESS; 1497} 1498 1499} // namespace art 1500 1501int main(int argc, char** argv) { 1502 return art::oatdump(argc, argv); 1503} 1504