oatdump.cc revision a1b4c5f562c67398e14756bd127ed806937ff35a
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
1678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
1778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include <stdio.h>
1878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include <stdlib.h>
1978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
2027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom#include <fstream>
2127ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom#include <iostream>
223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <map>
233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <set>
2478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include <string>
2554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include <unordered_map>
2678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include <vector>
2778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
28d582fa4ea62083a7598dded5b82dc2198b3daac7Ian Rogers#include "arch/instruction_set_features.h"
29c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier#include "art_field-inl.h"
30e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
3110c13565474de2786aad7c2e79757ea250747a15Vladimir Marko#include "base/stl_util.h"
32761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h"
3378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include "class_linker.h"
342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h"
355d950769b607b4f76413212db640a32d796911deDavid Srbecky#include "debug/elf_debug_writer.h"
365d950769b607b4f76413212db640a32d796911deDavid Srbecky#include "debug/method_debug_info.h"
374f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
38e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes#include "dex_instruction.h"
393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers#include "disassembler.h"
4054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include "elf_builder.h"
412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "gc_map.h"
421d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h"
431d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/large_object_space.h"
441d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
454a26f17b055cadc949c3e9fdfa637fe5656339d9Mathieu Chartier#include "image-inl.h"
462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "indenter.h"
47131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko#include "linker/buffered_output_stream.h"
48131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko#include "linker/file_output_stream.h"
491809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "mapping_table.h"
502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/array-inl.h"
512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
5205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko#include "mirror/dex_cache-inl.h"
532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
55700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "oat.h"
568a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko#include "oat_file-inl.h"
57f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "oat_file_manager.h"
58e5448b5a12003b405b22cde3b94f962ab4888a87Elliott Hughes#include "os.h"
59a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
6000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
616bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray#include "stack_map.h"
6200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "ScopedLocalRef.h"
63c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier#include "thread_list.h"
64ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "verifier/dex_gc_map.h"
652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "verifier/method_verifier.h"
661809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "vmap_table.h"
6700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "well_known_classes.h"
6878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <sys/stat.h>
703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include "cmdline.h"
7178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
723774335b08076117d6950cd472cdd59a167470b5Igor Murashkinnamespace art {
7378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
74e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_methods_descriptions_[] = {
751984651929744dd603fd082e23eacd877b9bc177Ian Rogers  "kResolutionMethod",
7688474b416eb257078e590bf9bc7957cee604a186Jeff Hao  "kImtConflictMethod",
772d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier  "kImtUnimplementedMethod",
78e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  "kCalleeSaveMethod",
79aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsOnlySaveMethod",
80aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsAndArgsSaveMethod",
81e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier};
82e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
83e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_roots_descriptions_[] = {
8458ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  "kDexCaches",
8534f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom  "kClassRoots",
8678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
8778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
88ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier// Map is so that we don't allocate multiple dex files for the same OatDexFile.
89ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartierstatic std::map<const OatFile::OatDexFile*,
90ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier                std::unique_ptr<const DexFile>> opened_dex_files;
91ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier
92ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartierconst DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
93ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  DCHECK(oat_dex_file != nullptr);
94ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  auto it = opened_dex_files.find(oat_dex_file);
95ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  if (it != opened_dex_files.end()) {
96ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    return it->second.get();
97ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  }
98ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
99ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
100ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  return ret;
101ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier}
102ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier
1032d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampetemplate <typename ElfTypes>
104bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbeckyclass OatSymbolizer FINAL {
10554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe public:
1062fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
1072fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      oat_file_(oat_file),
1082fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      builder_(nullptr),
1092fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      output_name_(output_name.empty() ? "symbolized.oat" : output_name),
1102fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      no_bits_(no_bits) {
11154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
11254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Symbolize() {
1146d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
1155d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky    const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
1165d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky        isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
1176d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1186d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
1196d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    std::unique_ptr<BufferedOutputStream> output_stream(
12010c13565474de2786aad7c2e79757ea250747a15Vladimir Marko        MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
1212d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
1226d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1236d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    builder_->Start();
1246d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1256d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* rodata = builder_->GetRoData();
1266d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* text = builder_->GetText();
1276d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* bss = builder_->GetBss();
1286d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1296d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const uint8_t* rodata_begin = oat_file_->Begin();
1306d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
1312fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    if (no_bits_) {
1322fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->WriteNoBitsSection(rodata_size);
1332fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else {
1342fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->Start();
1352fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->WriteFully(rodata_begin, rodata_size);
1362fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->End();
1372fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    }
1386d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1396d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
1406d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const size_t text_size = oat_file_->End() - text_begin;
1412fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    if (no_bits_) {
1422fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->WriteNoBitsSection(text_size);
1432fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else {
1442fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->Start();
1452fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->WriteFully(text_begin, text_size);
1462fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->End();
1472fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    }
1486d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1496d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    if (oat_file_->BssSize() != 0) {
1505b1c2ca30dad519be285f0a1e839c23cc4e3a51dDavid Srbecky      bss->WriteNoBitsSection(oat_file_->BssSize());
1516d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    }
1526d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
153316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung    if (isa == kMips || isa == kMips64) {
154316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung      builder_->WriteMIPSabiflagsSection();
155316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung    }
156944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko    builder_->PrepareDynamicSection(
157944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko        elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
158944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko    builder_->WriteDynamicSection();
15954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1605d950769b607b4f76413212db640a32d796911deDavid Srbecky    Walk();
1615d950769b607b4f76413212db640a32d796911deDavid Srbecky    for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
1625d950769b607b4f76413212db640a32d796911deDavid Srbecky      method_debug_infos_.push_back(trampoline);
1635d950769b607b4f76413212db640a32d796911deDavid Srbecky    }
16454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1655d950769b607b4f76413212db640a32d796911deDavid Srbecky    debug::WriteDebugInfo(builder_.get(),
1665d950769b607b4f76413212db640a32d796911deDavid Srbecky                          ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
1675d950769b607b4f76413212db640a32d796911deDavid Srbecky                          dwarf::DW_DEBUG_FRAME_FORMAT,
1685d950769b607b4f76413212db640a32d796911deDavid Srbecky                          true /* write_oat_patches */);
16954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1706d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    builder_->End();
17154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17210c13565474de2786aad7c2e79757ea250747a15Vladimir Marko    return builder_->Good();
17354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
17454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1755d950769b607b4f76413212db640a32d796911deDavid Srbecky  void Walk() {
17654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
17754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t i = 0; i < oat_dex_files.size(); i++) {
17854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
1792cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      CHECK(oat_dex_file != nullptr);
1805d950769b607b4f76413212db640a32d796911deDavid Srbecky      WalkOatDexFile(oat_dex_file);
18154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
18254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
18354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1845d950769b607b4f76413212db640a32d796911deDavid Srbecky  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
18554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::string error_msg;
186ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
187ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    if (dex_file == nullptr) {
18854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
18954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
19054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t class_def_index = 0;
19154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index < dex_file->NumClassDefs();
19254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index++) {
19354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
19454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      OatClassType type = oat_class.GetType();
19554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      switch (type) {
19654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassAllCompiled:
19754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassSomeCompiled:
1985d950769b607b4f76413212db640a32d796911deDavid Srbecky          WalkOatClass(oat_class, *dex_file, class_def_index);
19954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
20054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
20154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassNoneCompiled:
20254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassMax:
20354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          // Ignore.
20454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
20554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
20654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
20854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2095d950769b607b4f76413212db640a32d796911deDavid Srbecky  void WalkOatClass(const OatFile::OatClass& oat_class,
2105d950769b607b4f76413212db640a32d796911deDavid Srbecky                    const DexFile& dex_file,
2115d950769b607b4f76413212db640a32d796911deDavid Srbecky                    uint32_t class_def_index) {
2125d950769b607b4f76413212db640a32d796911deDavid Srbecky    const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
21313735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
21454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
21554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
21654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
21754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    // Note: even if this is an interface or a native class, we still have to walk it, as there
21854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    //       might be a static initializer.
21954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    ClassDataItemIterator it(dex_file, class_data);
22054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t class_method_idx = 0;
2215d950769b607b4f76413212db640a32d796911deDavid Srbecky    for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
2225d950769b607b4f76413212db640a32d796911deDavid Srbecky    for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
2235d950769b607b4f76413212db640a32d796911deDavid Srbecky    for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
2245d950769b607b4f76413212db640a32d796911deDavid Srbecky      WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
2255d950769b607b4f76413212db640a32d796911deDavid Srbecky                    dex_file,
2265d950769b607b4f76413212db640a32d796911deDavid Srbecky                    class_def_index,
2275d950769b607b4f76413212db640a32d796911deDavid Srbecky                    it.GetMemberIndex(),
2285d950769b607b4f76413212db640a32d796911deDavid Srbecky                    it.GetMethodCodeItem(),
2295d950769b607b4f76413212db640a32d796911deDavid Srbecky                    it.GetMethodAccessFlags());
23054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
23154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DCHECK(!it.HasNext());
23254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
23354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2345d950769b607b4f76413212db640a32d796911deDavid Srbecky  void WalkOatMethod(const OatFile::OatMethod& oat_method,
2355d950769b607b4f76413212db640a32d796911deDavid Srbecky                     const DexFile& dex_file,
2365d950769b607b4f76413212db640a32d796911deDavid Srbecky                     uint32_t class_def_index,
2375d950769b607b4f76413212db640a32d796911deDavid Srbecky                     uint32_t dex_method_index,
2385d950769b607b4f76413212db640a32d796911deDavid Srbecky                     const DexFile::CodeItem* code_item,
2395d950769b607b4f76413212db640a32d796911deDavid Srbecky                     uint32_t method_access_flags) {
24054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if ((method_access_flags & kAccAbstract) != 0) {
24154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // Abstract method, no code.
24254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
24354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
2445d950769b607b4f76413212db640a32d796911deDavid Srbecky    const OatHeader& oat_header = oat_file_->GetOatHeader();
2455d950769b607b4f76413212db640a32d796911deDavid Srbecky    const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
2465d950769b607b4f76413212db640a32d796911deDavid Srbecky    if (method_header == nullptr || method_header->GetCodeSize() == 0) {
24754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // No code.
24854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
24954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
251a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky    uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
252a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky    // Clear Thumb2 bit.
253a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky    const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
254a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky
2555d950769b607b4f76413212db640a32d796911deDavid Srbecky    debug::MethodDebugInfo info = debug::MethodDebugInfo();
2565d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.trampoline_name = nullptr;
2575d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.dex_file = &dex_file;
2585d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.class_def_index = class_def_index;
2595d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.dex_method_index = dex_method_index;
2605d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.access_flags = method_access_flags;
2615d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.code_item = code_item;
2625d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.isa = oat_header.GetInstructionSet();
2635d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
2645d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.is_native_debuggable = oat_header.IsNativeDebuggable();
2655d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.is_optimized = method_header->IsOptimized();
2665d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.is_code_address_text_relative = true;
267a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky    info.code_address = reinterpret_cast<uintptr_t>(code_address);
2685d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.code_size = method_header->GetCodeSize();
2695d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
2705d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
2715d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.cfi = ArrayRef<uint8_t>();
2725d950769b607b4f76413212db640a32d796911deDavid Srbecky    method_debug_infos_.push_back(info);
27354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
27454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
27554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe private:
27654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  const OatFile* oat_file_;
2772d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
2785d950769b607b4f76413212db640a32d796911deDavid Srbecky  std::vector<debug::MethodDebugInfo> method_debug_infos_;
2795d950769b607b4f76413212db640a32d796911deDavid Srbecky  std::unordered_set<uint32_t> seen_offsets_;
2800279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  const std::string output_name_;
2812fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  bool no_bits_;
28254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe};
28354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass OatDumperOptions {
2852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
2862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  OatDumperOptions(bool dump_raw_mapping_table,
2872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_raw_gc_map,
2882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_vmap,
289f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   bool dump_code_info_stack_maps,
2902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
29100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
29232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
29332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
29432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
29532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
296c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil                   bool dump_header_only,
29732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
2980b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                   const char* app_image,
2990b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                   const char* app_oat,
30032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
3012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    : dump_raw_mapping_table_(dump_raw_mapping_table),
3022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_raw_gc_map_(dump_raw_gc_map),
3032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_vmap_(dump_vmap),
304f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_(dump_code_info_stack_maps),
3052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
30600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
30732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
3083fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
30932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
31032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
311c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      dump_header_only_(dump_header_only),
31232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
3130b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      app_image_(app_image),
3140b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      app_oat_(app_oat),
31532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
3163774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
3172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
3182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_mapping_table_;
3192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_gc_map_;
3202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
321f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  const bool dump_code_info_stack_maps_;
3222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
3232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
32432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
3253fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
32632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
32732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
328c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil  const bool dump_header_only_;
32932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
3300b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const char* const app_image_;
3310b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const char* const app_oat_;
33232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
33300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
3342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
3352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
336e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
337aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
3383887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
3399583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
340a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
3412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
34232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
343a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
344a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
34532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
346a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
347eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                                                 true /* can_read_literals_ */))) {
34832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
34932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
35032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
3513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
3523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
3533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
3542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
3552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
3562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
3572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
358a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
359a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
360a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
361a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
3622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
3632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
3643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
365aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
366aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
367aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
368aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3694b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao    os << "LOCATION:\n";
3704b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao    os << oat_file_.GetLocation() << "\n\n";
3714b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao
372aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
373ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
374aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
375a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
376a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
377a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
3786f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
3796f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
3806f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
3816f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
3826f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
3836f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
3846f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
3857020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
386aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
387aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
388aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3892ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
3902ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
3912ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
39232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
3932ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
3942ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
3952ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
3962ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
3972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
3982ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
3992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
4002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
4012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
4022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
4032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
4042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
4052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
4062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
4072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
4102ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
4112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
4122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
413aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
4152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
4162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
4172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
418a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
41928db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
42028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
42128db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
42228db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
423700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
42481f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
42522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
42622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
42722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
42822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
42922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
43022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
43122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
43222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
43322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
43422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
43522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
43622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
43781f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
43832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
4392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
4402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
4412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
4432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
4442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
445aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
4472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
448aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
449aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
450aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
45132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
45232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
45332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
45432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
45532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
45632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
45732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
458c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil    if (!options_.dump_header_only_) {
459c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      for (size_t i = 0; i < oat_dex_files_.size(); i++) {
460c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
461c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        CHECK(oat_dex_file != nullptr);
46232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
463c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        // If file export selected skip file analysis
464c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        if (options_.export_dex_location_) {
465c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          if (!ExportDexFile(os, *oat_dex_file)) {
466c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil            success = false;
467c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          }
468c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        } else {
469c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          if (!DumpOatDexFile(os, *oat_dex_file)) {
470c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil            success = false;
471c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          }
47232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
4732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
4743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
475c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil
4762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
4772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
4783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
4793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
4803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
48113735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
48213735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
4833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
4843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
485ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
486ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
487ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
4883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
489ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
4903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
4913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
492e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
493a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
494f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
495f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
496f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
49790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
4983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5008d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
5018d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
502ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
503ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (dex_file == nullptr) {
5048d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5058d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5068d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
507e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5088b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
509e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
5102ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
5118b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
512d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
5133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
514d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
5153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
5163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
517aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5182ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
519aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
520aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
521aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
5223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
523e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
524e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
525e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
526e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
5273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5292ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
5308d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
531ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
532ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (dex_file == nullptr) {
5338d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5348d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
536e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
537ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
5382ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
5392ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
5402ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
541e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
542d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
54313735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
5442ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
545e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
546e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
547e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
548e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
549d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
550e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
551e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
552e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
553d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
554e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
555e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
556e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
557e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
558e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
559e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
560e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
561e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
562e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
563ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
564e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
565e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
5672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
5682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
5692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
570e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
57195ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
57295ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
57395ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
57495ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
57595ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
576e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetMappingTableOffset());
577e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
578957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    offsets_.insert(oat_method.GetGcMapOffset());
579e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
580e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
5822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
58332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
5842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
585a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
586ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
587590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
588590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // Create the verifier early.
589590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
5908d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
591ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
592ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    if (dex_file == nullptr) {
5938d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
5942cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
5952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
596aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5978f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
5988f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    VariableIndentationOutputStream vios(&os);
5998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&vios);
6002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
6012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
6022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
603aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
604aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
60532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
60632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
60732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
60832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
60932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
61032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
6112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
612d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
6132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
6142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
615d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
616d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
617d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
61832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
619ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
6202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
6212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
62232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
62332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
62432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
62532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
626aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
627aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
628aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
6292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
630aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
631aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
63232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
63332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
63432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
63532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
636ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
63732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
63832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
63932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
64232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
64432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
64532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
64632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
65032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
65132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
65232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
65332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
65732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
65832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
66232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
66332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
66432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
66532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
66632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
66732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
66932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
67032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
67132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
67232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
67432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
67532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
67632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
67832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
67932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
68032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
68232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
68332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
68432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
68532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
68732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
68832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
68932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
69032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
69432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
70132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
70532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
70632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
70932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
710e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
7110571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
7120571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
713aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7140571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
7150571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
716aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
717e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
718aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
7198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatClass(VariableIndentationOutputStream* vios,
7208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                    const OatFile::OatClass& oat_class, const DexFile& dex_file,
72132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
7222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
72332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
72413735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
7252ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
7268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << std::flush;
7272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
728e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
729e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
730e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
7312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
7320571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
7338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
7342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
73532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
73832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
73932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
74032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
74132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7430571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
744aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7450571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
7468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
7472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
74832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
75132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
75232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
75332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
75432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7560571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
757aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7580571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
7598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
7602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
761aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
762e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
7632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
7642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
7662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
7672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7688f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatMethod(VariableIndentationOutputStream* vios,
7698f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                     const DexFile::ClassDef& class_def,
7708b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
7712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
7722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
77332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
7742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
77532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
77632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
77732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
77832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
7793fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
7803fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
7813fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
78232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
7838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
7848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   class_method_index, pretty_method.c_str(),
7858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   dex_method_idx);
78632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
78732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
78832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
78932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
79032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
79132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
79232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
79332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
79432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
79532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
79632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
80032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8018f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    // Everything below is indented at least once.
8028f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(vios);
8038f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
8042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "DEX CODE:\n";
8068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpDexCode(vios->Stream(), dex_file, code_item);
8082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8092ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
810673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier    std::unique_ptr<StackHandleScope<1>> hs;
8112ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
8122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
813673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      // We need to have the handle scope stay live until after the verifier since the verifier has
814673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      // a handle to the dex cache from hs.
815673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      hs.reset(new StackHandleScope<1>(Thread::Current()));
8168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
8178f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
818673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      verifier.reset(DumpVerifier(vios, hs.get(),
8198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                  dex_method_idx, &dex_file, class_def, code_item,
8202ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
8212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatMethodOffsets ";
82432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method_offsets);
8262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8278f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
8282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
8298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
8302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
8312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
8322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
8338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
8342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
836a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
8378f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
8392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
8402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
8418f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code offset 0x%08x is past end of file 0x%08zx.\n",
8438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       aligned_code_begin, oat_file_.Size());
8442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
8473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
8488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "gc_map: ";
84932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8508f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
8513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
852957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
8538f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
8542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (gc_map_offset > oat_file_.Size()) {
8558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                           "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
8578f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                           gc_map_offset, oat_file_.Size());
8582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
85932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_gc_map_) {
8608f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent3(vios);
8618f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpGcMap(vios->Stream(), oat_method, code_item);
8622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
8642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
8658f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatQuickMethodHeader ";
8662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
8672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
8682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
86932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8708f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", method_header);
8712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
8732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
8748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
8752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
8762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
8772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
8788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
8792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8828f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "mapping_table: ";
88432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
8862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
8888f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
8892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (mapping_table_offset > oat_file_.Size()) {
8908f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "mapping table offset 0x%08x is past end of file 0x%08zx. "
8928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "mapping table offset was loaded from offset 0x%08x.\n",
8938f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       mapping_table_offset, oat_file_.Size(),
8948f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetMappingTableOffsetOffset());
8952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
89632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_mapping_table_) {
8978f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent3(vios);
8988f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpMappingTable(vios, oat_method);
8992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9018f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "vmap_table: ";
90232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9038f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
9042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
9068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
9072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
9088f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset 0x%08x is past end of file 0x%08zx. "
9108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset was loaded from offset 0x%08x.\n",
9118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       vmap_table_offset, oat_file_.Size(),
9128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetVmapTableOffsetOffset());
9132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
91432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
9158f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapData(vios, oat_method, code_item);
9162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
9172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
9198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "QuickMethodFrameInfo\n";
9208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
9218f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9228f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream()
9238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
9248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
9258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
9268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9278f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
9288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
9298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // Based on spill masks from QuickMethodFrameInfo so placed
9338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // after it is dumped, but useful for understanding quick
9348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // code, so dumped here.
9358f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpVregLocations(vios->Stream(), oat_method, code_item);
9372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9398f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "CODE: ";
9402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
9412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
9428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
9438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code size offset 0x%08x is past end of file 0x%08zx.",
9458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset, oat_file_.Size());
9462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
9482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
9492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
9502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
951590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
95232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
9538f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("%p ", code);
9542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
9568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_offset,
9578f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset,
9588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size,
9598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code != nullptr ? "..." : "");
9602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9618f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
9622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
9638f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("WARNING: "
9648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         "start of code at 0x%08x is past end of file 0x%08zx.",
9658f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         aligned_code_begin, oat_file_.Size());
9662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
9672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
9688f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
9698f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
9708f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
9718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
9728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              aligned_code_end, oat_file_.Size(),
9738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
9742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
97532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9778f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
9802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
9818f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
9828f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
9838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size %d is bigger than max expected threshold of %d. "
9848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
9858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, kMaxCodeSize,
9868f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
9872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
98832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9908f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9922cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
99332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
9948f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
9952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9988f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
9992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
10003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
10023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
10033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
10043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
10053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
10073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
10083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
10093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
10103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
10113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
10133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
10153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
10163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
10173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
10193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
10213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
10233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1025442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
10268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpVmapData(VariableIndentationOutputStream* vios,
1027442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1028442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1029f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1030f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The optimizing compiler outputs its CodeInfo data in the vmap table.
1031442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1032442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1033442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1034442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
10358f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
10368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpCodeInfo(vios, code_info, oat_method, *code_item);
1037442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10380a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
10390a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // We don't encode the size in the table, so just emit that we have quickened
10400a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // information.
10410a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      ScopedIndentation indent(vios);
10420a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      vios->Stream() << "quickened data\n";
1043442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
1044442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // Otherwise, display the vmap table.
1045442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const uint8_t* raw_table = oat_method.GetVmapTable();
1046442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_table != nullptr) {
1047442b46a087c389a91a0b51547ac9205058432364Roland Levillain        VmapTable vmap_table(raw_table);
10488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapTable(vios->Stream(), oat_method, vmap_table);
1049442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
105020d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1051442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1052442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1053442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
10548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCodeInfo(VariableIndentationOutputStream* vios,
1055442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1056f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                    const OatFile::OatMethod& oat_method,
1057442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
10588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    code_info.Dump(vios,
1059f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   oat_method.GetCodeOffset(),
1060f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   code_item.registers_size_,
1061f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   options_.dump_code_info_stack_maps_);
1062442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1063442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1064442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a vmap table.
1065442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapTable(std::ostream& os,
1066442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const OatFile::OatMethod& oat_method,
1067442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const VmapTable& vmap_table) {
1068442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool first = true;
1069442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool processing_fp = false;
1070442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1071442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < vmap_table.Size(); i++) {
1072442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint16_t dex_reg = vmap_table[i];
1073442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1074442b46a087c389a91a0b51547ac9205058432364Roland Levillain                                                    processing_fp ? kFloatVReg : kIntVReg);
1075442b46a087c389a91a0b51547ac9205058432364Roland Levillain      os << (first ? "v" : ", v")  << dex_reg;
1076442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp) {
1077442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/r" << cpu_reg;
1078442b46a087c389a91a0b51547ac9205058432364Roland Levillain      } else {
1079442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/fr" << cpu_reg;
1080442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1081442b46a087c389a91a0b51547ac9205058432364Roland Levillain      first = false;
1082442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp && dex_reg == 0xFFFF) {
1083442b46a087c389a91a0b51547ac9205058432364Roland Levillain        processing_fp = true;
1084442b46a087c389a91a0b51547ac9205058432364Roland Levillain        spill_mask = oat_method.GetFpSpillMask();
1085442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1087442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "\n";
10883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1090faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1091faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1092faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1093faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1094faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1095faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1096faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1097faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1098faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1099faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1100faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1101faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1102faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1103faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1104faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1105faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1106faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1107faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1108faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
110915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
111015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
111115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
111215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
111315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
111415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
111515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1116faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1117faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1118faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1119faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1120faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1121faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1122faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1123faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1124faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1125faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1126faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1127faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1128faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1129faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1130faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1131faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1132b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
11332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
11341809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* raw_table = oat_method.GetVmapTable();
11352ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (raw_table != nullptr) {
1136b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      const VmapTable vmap_table(raw_table);
1137b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      uint32_t vmap_offset;
11381809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
11392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
11402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
11412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                       : oat_method.GetCoreSpillMask();
11422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1143b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      } else {
114415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
114515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
114615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
114715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
114815b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
114915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
115015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1151b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        os << "[sp + #" << offset << "]";
1152b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
1153b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1154b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1155b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1156ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1157ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          const DexFile::CodeItem* code_item,
1158ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          size_t num_regs, const uint8_t* reg_bitmap) {
1159ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    bool first = true;
1160ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t reg = 0; reg < num_regs; reg++) {
1161ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1162ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        if (first) {
1163ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << "  v" << reg << " (";
1164ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1165ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1166ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          first = false;
1167ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else {
1168ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ", v" << reg << " (";
1169ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1170ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1171ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        }
1172ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      }
1173ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1174ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (first) {
1175ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "No registers in GC map\n";
1176ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    } else {
1177ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "\n";
1178ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1179ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
1180b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1181b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers                 const DexFile::CodeItem* code_item) {
1182957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1183ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (gc_map_raw == nullptr) {
1184ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return;  // No GC map.
11853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1186ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1187956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    NativePcOffsetToReferenceMap map(gc_map_raw);
1188956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1189956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1190956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes          map.GetNativePcOffset(entry);
1191956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      os << StringPrintf("%p", native_pc);
1192956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
11933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1194aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
1195e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
11968f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpMappingTable(VariableIndentationOutputStream* vios,
11978f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                        const OatFile::OatMethod& oat_method) {
1198ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1199ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (quick_code == nullptr) {
12002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      return;
12012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12021809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12031809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (table.TotalSize() != 0) {
12041809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.PcToDexSize() != 0) {
12051809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::PcToDexIterator It;
12068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "suspend point mappings {\n";
12071809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12088f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent1(vios);
12098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12101809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "}\n";
12121809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      }
12131809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.DexToPcSize() != 0) {
12141809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::DexToPcIterator It;
12158f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "catch entry mappings {\n";
12161809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12178f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent1(vios);
12188f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12191809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "}\n";
12213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
12223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
12243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
1226f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const OatFile::OatMethod& oat_method,
1227f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const DexFile::CodeItem* code_item,
1228f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   size_t offset,
1229f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   bool suspend_point_mapping) {
1230f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1231f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (suspend_point_mapping) {
12328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
12338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
1234f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1235f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The return value is not used in the case of a method compiled
1236f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // with the optimizing compiler.
1237f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      return DexFile::kDexNoIndex;
1238f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else {
12398f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
1240f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1241f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1242f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
12431809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12441809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
12451809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12461809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
12471809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
12481809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12491809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12504b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
12511809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
12521809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1253b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12541809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
12551809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
12561809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12571809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12584b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
12591809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1260b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1261b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1264b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1268957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
12692ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1270b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
12712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1272b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
12732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1274b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1275b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1276b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1277b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
12782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
12792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1280b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1281b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1282b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1283b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
12842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1285b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1286b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1287b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1288b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1289b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1290b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1291b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1292b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1294b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1296590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1297590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1298590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1299590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
13007b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
13012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
13022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
13032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
13042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
13052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
13062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
13072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
13082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
13092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
13102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
13122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
13132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
13142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
13152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
13182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
13192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
13212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
13252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
13262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
13282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
13292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
13322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1333b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
13342ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1335b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1336b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1337b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
133832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
133932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1340b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1341e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1342e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1343b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1344b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1345f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1346f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // the optimizing compiler?
1347f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1348f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                                    const DexFile::CodeItem* code_item) {
1349f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // If the native GC map is null and the Dex `code_item` is not
1350f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // null, then this method has been compiled with the optimizing
1351f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // compiler.
13520a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() != nullptr &&
13530a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetGcMap() == nullptr &&
13540a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
13550a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  }
13560a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray
13570a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
13580a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // the dextodex compiler?
13590a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
13600a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray                                                  const DexFile::CodeItem* code_item) {
13610a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // If the quick code is null, the Dex `code_item` is not
13620a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // null, and the vmap table is not null, then this method has been compiled
13630a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // with the dextodex compiler.
13640a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() == nullptr &&
13650a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetVmapTable() != nullptr &&
13660a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
1367f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1368f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
13698f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
1370f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const OatFile::OatMethod& oat_method,
1371f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const DexFile::CodeItem* code_item,
1372f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  size_t offset) {
1373f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // This method is only relevant for oat methods compiled with the
1374f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // optimizing compiler.
1375f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1376f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
1377f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // The optimizing compiler outputs its CodeInfo data in the vmap table.
1378f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    const void* raw_code_info = oat_method.GetVmapTable();
1379f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (raw_code_info != nullptr) {
1380f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      CodeInfo code_info(raw_code_info);
1381f677ebfd832c9c614fea5e6735725fec2f7a3f2aDavid Brazdil      StackMapEncoding encoding = code_info.ExtractEncoding();
1382f677ebfd832c9c614fea5e6735725fec2f7a3f2aDavid Brazdil      StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
1383f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (stack_map.IsValid()) {
13848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
13858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                       code_item->registers_size_);
1386f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1387f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1388f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1389f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
13908f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
1391673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier                                         StackHandleScope<1>* hs,
13928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         uint32_t dex_method_idx,
13932ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
13942ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
13952ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
13962ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
13972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
13982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1399d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier      Runtime* const runtime = Runtime::Current();
1400eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1401d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier          hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1402d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier                                                                   runtime->GetLinearAlloc())));
140332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
1404e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      return verifier::MethodVerifier::VerifyMethodAndDump(
14058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
14068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          &class_def, code_item, nullptr, method_access_flags);
14072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
14082ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
14092ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
14102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
14112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
14128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCode(VariableIndentationOutputStream* vios,
14138f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                verifier::MethodVerifier* verifier,
14142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
14152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1416ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1417ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
14182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
14192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
14202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1421956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
14228f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "NO CODE!\n";
1423b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1424956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1425ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1426ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1427ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
14282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
14298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
14302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
14318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
14322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
14338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          uint32_t dex_pc =
14348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
14352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
14368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko            DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
14372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
14388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
14392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1440ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
14412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
14422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1443b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1444b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1445b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
14463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
14472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
144832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
144932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1450a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1451ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
14522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1453aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1454aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
14553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
145627ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
14570b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  ImageDumper(std::ostream* os,
14580b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier              gc::space::ImageSpace& image_space,
14590b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier              const ImageHeader& image_header,
14600b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier              OatDumperOptions* oat_dumper_options)
14612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
14628f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios_(os),
14638f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent1_(&vios_),
14642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
14652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
14662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
146727ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
146890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
14692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
14708f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& indent_os = vios_.Stream();
14718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
14722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
14732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1474dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1475dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao
14762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
14772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1478e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1479e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1480e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1481e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto section = static_cast<ImageHeader::ImageSections>(i);
1482e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1483e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
148431e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
14852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
14862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1487700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
14882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1489700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1490700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1491700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1492700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1493700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
14942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1495a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1496a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
149746774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
149846774767fcf7780d1455e755729198648d08742eIgor Murashkin
14992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
15002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1501e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_roots_descriptions_) ==
1502e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
15032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
15042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
15052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
15062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
15078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
15082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
15092dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1510fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
15118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent2(&vios_);
1512277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1513277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1514fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1515277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1516277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1517fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1518fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1519fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1520fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1521fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1522fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
15238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d: ", j);
1524fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
15258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d to %zd: ", j, j + run);
1526277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1527fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
15282ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
15298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              PrettyObjectValue(indent_os, value->GetClass(), value);
15302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
15318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << j << ": null\n";
15322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1533d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
153434f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
153534f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
15368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    }
1537e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
15388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    {
1539e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "METHOD ROOTS\n";
1540e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_methods_descriptions_) ==
1541e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1542e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1543e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1544e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const char* description = image_methods_descriptions_[i];
1545e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto* image_method = image_header_.GetImageMethod(image_root);
15468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", description, image_method);
1547e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
154827ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
15492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
155027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1551f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    Runtime* const runtime = Runtime::Current();
1552f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    ClassLinker* class_linker = runtime->GetClassLinker();
15532ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
15542ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
15552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
15562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
15578d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
15580b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const OatFile* oat_file = image_space_.GetOatFile();
1559a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
15600b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
15610b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
15620b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (oat_file == nullptr) {
15630b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      oat_file = OatFile::Open(oat_location,
15640b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               oat_location,
15650b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               nullptr,
15660b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               nullptr,
15670b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               false,
15680b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               /*low_4gb*/false,
15690b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               nullptr,
1570b1d8c314b55bb2df2b2bb72a3daaf5db65b7ebc7Igor Murashkin                               &error_msg);
15710b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
15720b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (oat_file == nullptr) {
15730b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      os << "OAT FILE NOT FOUND: " << error_msg << "\n";
15740b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      return EXIT_FAILURE;
1575aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
15762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1577aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
15783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
157978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
158032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
15813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
158202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
15832ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
158402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
158502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
158605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
158705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
15882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1589b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1590dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    // Loop through the image space and dump its objects.
1591f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    gc::Heap* heap = runtime->GetHeap();
159250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1593357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1594c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1595c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1596c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1597c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
159890d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
159990d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
160090d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1601f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier      ScopedThreadSuspension sts(self, kNative);
16024f55e22630d99ca0edd9e951e5ee96b57bb9b980Mathieu Chartier      ScopedSuspendAll ssa(__FUNCTION__);
1603f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier      heap->RevokeAllThreadLocalAllocationStacks(self);
1604357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
16052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
1606e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      // Mark dex caches.
160705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      dex_caches_.clear();
1608e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      {
1609e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        ReaderMutexLock mu(self, *class_linker->DexLock());
161004302dbb106d590ff72c0dfecda23d85b6565059Hiroshi Yamauchi        for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
1611673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          mirror::DexCache* dex_cache =
161204302dbb106d590ff72c0dfecda23d85b6565059Hiroshi Yamauchi              down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
1613673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          if (dex_cache != nullptr) {
161405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            dex_caches_.insert(dex_cache);
1615673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          }
1616e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        }
1617e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
16182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1619dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // Dump the normal objects before ArtMethods.
1620dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1621dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      indent_os << "\n";
1622dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // TODO: Dump fields.
1623dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // Dump methods after.
1624dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      const auto& methods_section = image_header_.GetMethodsSection();
1625dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      DumpArtMethodVisitor visitor(this);
16260b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      methods_section.VisitPackedArtMethods(&visitor,
16270b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                            image_space_.Begin(),
16280b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                            image_header_.GetPointerSize());
16292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1630bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
16312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
1632b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
16332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1634700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
16350b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    size_t data_size = image_header_.GetDataSize();  // stored size in file.
16360b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (file == nullptr) {
16372ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
16380b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    } else {
16392ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
16400b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      // If the image is compressed, adjust to decompressed size.
16410b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
16420b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
16430b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
16440b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      }
16450b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      stats_.file_bytes += uncompressed_size - data_size;
16466f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
16473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
1648ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
1649e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1650e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& method_section = image_header_.GetMethodsSection();
165105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    const auto& dex_cache_arrays_section = image_header_.GetImageSection(
165205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        ImageHeader::kSectionDexCacheArrays);
1653d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    const auto& intern_section = image_header_.GetImageSection(
1654d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier        ImageHeader::kSectionInternedStrings);
1655208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    const auto& class_table_section = image_header_.GetImageSection(
1656208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier        ImageHeader::kSectionClassTable);
1657ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1658ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
16593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
1660ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1661ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Objects are kObjectAlignment-aligned.
1662ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1663ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    if (object_section.Offset() > header_bytes) {
1664ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe      stats_.alignment_bytes += object_section.Offset() - header_bytes;
1665ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    }
1666ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1667ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Field section is 4-byte aligned.
1668ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    constexpr size_t kFieldSectionAlignment = 4U;
1669ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_objects = object_section.Offset() + object_section.Size();
1670ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1671ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += field_section.Offset() - end_objects;
1672ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1673ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1674ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_fields = field_section.Offset() + field_section.Size();
1675ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(method_section.Offset(), 4);
1676ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += method_section.Offset() - end_fields;
1677ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1678ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1679ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_methods = method_section.Offset() + method_section.Size();
1680ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1681ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1682ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1683ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Intern table is 8-byte aligned.
1684ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1685ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1686ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += intern_section.Offset() - end_caches;
1687ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1688ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Add space between intern table and class table.
1689ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1690ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1691ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
16920b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
16930b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
1694ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
16950b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
1696ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1697e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.bitmap_bytes += bitmap_section.Size();
1698e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.art_field_bytes += field_section.Size();
1699cf36d493124d8048efa0bd6f67d817ce3cd6b725Vladimir Marko    stats_.art_method_bytes += method_section.Size();
170005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
1701d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    stats_.interned_strings_bytes += intern_section.Size();
1702208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    stats_.class_table_bytes += class_table_section.Size();
17038f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    stats_.Dump(os, indent_os);
17042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
170527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
17062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
17073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
17093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1710d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
17113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
171254d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier  class DumpArtMethodVisitor : public ArtMethodVisitor {
171354d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier   public:
171454d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
171554d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
171654d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
171754d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      std::ostream& indent_os = image_dumper_->vios_.Stream();
171854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
17190b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      image_dumper_->DumpMethod(method, indent_os);
172054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      indent_os << "\n";
172154d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    }
172254d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
172354d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier   private:
172454d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    ImageDumper* const image_dumper_;
172554d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier  };
172654d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
17272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
172890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
17292ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
17302ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
17312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1732d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
17332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
17342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
173568b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
173664b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
17372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
17382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1739d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
17402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1741d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1742d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1743d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1744c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
174590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
174661c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
174708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
174808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1749ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
175008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
175108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
17522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
175308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
175408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
17552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
175608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
175708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
17582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
175908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
176008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
176137f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
176208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
176308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
176437f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
176508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
176608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
176737f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
176837f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
176908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
177008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
177137f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
177208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
177308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
177408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
177508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
177608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
177708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
177808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
177950239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
178008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1781daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
178208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
178308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
178408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
178508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
178608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
178708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
178850239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
178908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
179048efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
179108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
179208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
179308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1794d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1795d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1796d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
17972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
179890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
17992dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
18002ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
18012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1802d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
180354d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    for (ArtField& field : klass->GetIFields()) {
180454d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      PrintField(os, &field, obj);
1805d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1806d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1807d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
18082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
18093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1810f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
18113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18120b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const void* GetQuickOatCodeBegin(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
1813a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
18140b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        image_header_.GetPointerSize());
18156f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1816ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
18173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1818f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1819ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1820f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1821ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
18223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
18233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1824e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uint32_t GetQuickOatCodeSize(ArtMethod* m)
182590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1826ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1827ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1828f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1829f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1830f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1831f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1832f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1833e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCodeEnd(ArtMethod* m)
183490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1835ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
18362ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
18372ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1838f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1839ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1840f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1841f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
184290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
18432ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
18442ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
18453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
184678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
184778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
184878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1849916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1850916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1851916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1852916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1853916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1854916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& os = state->vios_.Stream();
18568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
18572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1858d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
18592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
18602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1861d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
18622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
18632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
18642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1865d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
18662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
186768b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
186878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
18692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
187078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
18718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&state->vios_);
18728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    DumpFields(os, obj, obj_class);
18730b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const size_t image_pointer_size = state->image_header_.GetPointerSize();
1874d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
1875e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto* obj_array = obj->AsObjectArray<mirror::Object>();
1876e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
18772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1878d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1879d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1880d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1881d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1882d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1883d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1884d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1885d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1886d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
18878f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d: ", i);
188878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
18898f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d to %zd: ", i, i + run);
1890d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
189178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
18922ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
18932ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
18948f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        PrettyObjectValue(os, value_class, value);
1895d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1896d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1897c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      mirror::Class* klass = obj->AsClass();
189854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      if (klass->NumStaticFields() != 0) {
18998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        os << "STATICS:\n";
19008f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(&state->vios_);
190154d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier        for (ArtField& field : klass->GetSFields()) {
190254d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier          PrintField(os, &field, field.GetDeclaringClass());
1903d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1904d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1905e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
190605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      auto it = state->dex_caches_.find(obj);
190705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      if (it != state->dex_caches_.end()) {
190805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        auto* dex_cache = down_cast<mirror::DexCache*>(obj);
1909e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& field_section = state->image_header_.GetImageSection(
1910e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            ImageHeader::kSectionArtFields);
1911e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& method_section = state->image_header_.GetMethodsSection();
191205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        size_t num_methods = dex_cache->NumResolvedMethods();
191305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        if (num_methods != 0u) {
191405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          os << "Methods (size=" << num_methods << "):";
191505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          ScopedIndentation indent2(&state->vios_);
191605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          auto* resolved_methods = dex_cache->GetResolvedMethods();
191705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
19180b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
19190b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                             i,
19200b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                             image_pointer_size);
192105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            size_t run = 0;
192205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            for (size_t j = i + 1;
192305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
192405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           j,
192505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           image_pointer_size);
192605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                ++j, ++run) {}
192705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (run == 0) {
192805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd: ", i);
192905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
193005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd to %zd: ", i, i + run);
193105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              i = i + run;
193205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
193305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            std::string msg;
193405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (elem == nullptr) {
193505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "null";
193605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else if (method_section.Contains(
193705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
193805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
193905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
194005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "<not in method section>";
194105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
194205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            os << StringPrintf("%p   %s\n", elem, msg.c_str());
1943e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
194405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        }
194505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        size_t num_fields = dex_cache->NumResolvedFields();
194605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        if (num_fields != 0u) {
194705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          os << "Fields (size=" << num_fields << "):";
194805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          ScopedIndentation indent2(&state->vios_);
194905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          auto* resolved_fields = dex_cache->GetResolvedFields();
195005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
195105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
195205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            size_t run = 0;
195305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            for (size_t j = i + 1;
195405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
195505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           j,
195605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           image_pointer_size);
195705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                ++j, ++run) {}
195805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (run == 0) {
195905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd: ", i);
196005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
196105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd to %zd: ", i, i + run);
196205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              i = i + run;
196305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
196405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            std::string msg;
196505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (elem == nullptr) {
196605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "null";
196705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else if (field_section.Contains(
196805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
196905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = PrettyField(reinterpret_cast<ArtField*>(elem));
197005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
197105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "<not in field section>";
197205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
197305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            os << StringPrintf("%p   %s\n", elem, msg.c_str());
1974e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
19753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1976e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1977e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
1978e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::string temp;
1979e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
1980e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  }
19813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19820b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  void DumpMethod(ArtMethod* method, std::ostream& indent_os)
198390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1984e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    DCHECK(method != nullptr);
19850b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
19860b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
1987524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray    OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1988524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray        reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
1989e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    if (method->IsNative()) {
1990524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray      if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
1991524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray        DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1992524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray        DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
1993524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray      }
1994e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
19950b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
19960b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1997e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
19980b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        stats_.native_to_managed_code_bytes += quick_oat_code_size;
1999e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
20000b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
20010b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier          image_header_.GetPointerSize())) {
20026bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray        indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
2003e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2004796d63050a18f263b93ea34951a61deaecab3422Nicolas Geoffray    } else if (method->IsAbstract() ||
2005796d63050a18f263b93ea34951a61deaecab3422Nicolas Geoffray               method->IsCalleeSaveMethod() ||
2006796d63050a18f263b93ea34951a61deaecab3422Nicolas Geoffray               method->IsResolutionMethod() ||
2007796d63050a18f263b93ea34951a61deaecab3422Nicolas Geoffray               (method == Runtime::Current()->GetImtConflictMethod()) ||
2008796d63050a18f263b93ea34951a61deaecab3422Nicolas Geoffray               method->IsImtUnimplementedMethod() ||
2009796d63050a18f263b93ea34951a61deaecab3422Nicolas Geoffray               method->IsClassInitializer()) {
2010796d63050a18f263b93ea34951a61deaecab3422Nicolas Geoffray      // Don't print information for these.
2011e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
2012e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const DexFile::CodeItem* code_item = method->GetCodeItem();
2013e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
20140b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      stats_.dex_instruction_bytes += dex_instruction_bytes;
2015e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
2016e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
20170b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      size_t gc_map_bytes = ComputeOatSize(method_header->GetNativeGcMap(), &first_occurrence);
2018e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
20190b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        stats_.gc_map_bytes += gc_map_bytes;
2020e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2021e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
20220b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      size_t pc_mapping_table_bytes = ComputeOatSize(
2023524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray          method_header->GetMappingTable(), &first_occurrence);
2024e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
20250b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2026e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2027e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
20286d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain      size_t vmap_table_bytes = 0u;
2029524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray      if (!method_header->IsOptimized()) {
20306d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        // Method compiled with the optimizing compiler have no vmap table.
20310b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
20326d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        if (first_occurrence) {
20330b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier          stats_.vmap_table_bytes += vmap_table_bytes;
20346d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        }
2035e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2036e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
20370b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
20380b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2039e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
20400b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        stats_.managed_code_bytes += quick_oat_code_size;
2041e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        if (method->IsConstructor()) {
2042e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          if (method->IsStatic()) {
20430b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier            stats_.class_initializer_code_bytes += quick_oat_code_size;
2044e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
20450b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier            stats_.large_initializer_code_bytes += quick_oat_code_size;
20460d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
2047e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
20480b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier          stats_.large_method_code_bytes += quick_oat_code_size;
20493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
2050e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
20510b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
20523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20537617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      uint32_t method_access_flags = method->GetAccessFlags();
20547617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin
2055e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
20567617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
20577617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
20587617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                method_access_flags);
20593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2060e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
20610b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier          vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
20623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2063e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      double expansion =
2064e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
20650b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      stats_.ComputeOutliers(total_size, expansion, method);
206678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
206778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
206827ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
20693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
20703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
20713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
2072a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
20733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
2074a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
20753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
20763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
2077a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
20783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
208027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
2081916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2082916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
2083916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
20843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
2085916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
2086916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2087916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
2088916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
2089e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_field_bytes;
2090e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_method_bytes;
209105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    size_t dex_cache_arrays_bytes;
2092d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    size_t interned_strings_bytes;
2093208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    size_t class_table_bytes;
209432327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
2095916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
2096916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2097916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
20983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
2099916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
2100916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
21010d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
21020d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
21030d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
2104916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
2106916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
21073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
2108916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2109916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
2110916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2111e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::vector<ArtMethod*> method_outlier;
21123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
21133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
2114700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
21153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21163887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain    Stats()
21173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
21183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
2119916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
2120916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
2121e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_field_bytes(0),
2122e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_method_bytes(0),
212305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          dex_cache_arrays_bytes(0),
2124d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier          interned_strings_bytes(0),
2125208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier          class_table_bytes(0),
212632327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
2127916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
2128916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
21293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
2130916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
2131916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
21320d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
21330d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
21340d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
21353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
2136916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
21373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
2138916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
2139916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2140a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
2141277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
2142a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
2143a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
2144a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
2145a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2146a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
2147a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
2148277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
2149a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2150a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
2151277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
2152a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
2153a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
2154277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
2155a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
2156a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
2157916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
21593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
21603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
21613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2162916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
2163916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2164916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2165916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2166916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
2167916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2168916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2169916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2170e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
21713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
21723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
21733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
21743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
21753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
217600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
217790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier        SHARED_REQUIRES(Locks::mutator_lock_) {
21783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
21793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
21803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
21813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
21823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
2183dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      if (n == 0) {
2184dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao        return;
2185dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      }
21863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
21873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
21883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
21893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
21903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
21913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
21923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
21933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
21943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
21953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
21963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
21973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
21983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
21993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
22003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
22013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
22023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
22033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
22043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
22053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
22063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
22073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
22083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
22093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
22103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
22113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
22123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
22133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
22143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
22153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
22163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
22173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
22183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
22193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
22203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
2221c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
22223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
22233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
22242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
2225c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
22263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
22273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
22283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
22293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
22303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
22313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
22323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
22333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
22342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2235c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
22363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2237c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
22383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
22393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
22403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
22413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
22423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
22433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
22443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
22453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
22463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
22473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
22483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
22493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
22503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
22513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
22523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
22533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
22543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
22553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
22563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
22573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
22583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
22593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
22603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
2261c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
22623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
22633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
22642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2265c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
22663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
22673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
22683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
22693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
22703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
22713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
22723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
22733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
22742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2275c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
22763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2277c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
22783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
22793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
22808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    void Dump(std::ostream& os, std::ostream& indent_os)
228190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier        SHARED_REQUIRES(Locks::mutator_lock_) {
22822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
22832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
22842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
228505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        indent_os << StringPrintf("header_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
228605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "object_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
228705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "art_field_bytes        =  %8zd (%2.0f%% of art file bytes)\n"
228805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "art_method_bytes       =  %8zd (%2.0f%% of art file bytes)\n"
228905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "dex_cache_arrays_bytes =  %8zd (%2.0f%% of art file bytes)\n"
229005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "interned_string_bytes  =  %8zd (%2.0f%% of art file bytes)\n"
2291208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                                  "class_table_bytes      =  %8zd (%2.0f%% of art file bytes)\n"
229205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "bitmap_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
229305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "alignment_bytes        =  %8zd (%2.0f%% of art file bytes)\n\n",
22942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
22952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
2296e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_field_bytes, PercentOfFileBytes(art_field_bytes),
2297e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_method_bytes, PercentOfFileBytes(art_method_bytes),
229805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  dex_cache_arrays_bytes,
229905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  PercentOfFileBytes(dex_cache_arrays_bytes),
2300d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  interned_strings_bytes,
2301d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  PercentOfFileBytes(interned_strings_bytes),
2302208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                                  class_table_bytes, PercentOfFileBytes(class_table_bytes),
230332327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
23042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
23052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
2306208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier        CHECK_EQ(file_bytes,
2307208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2308208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2309208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 bitmap_bytes + alignment_bytes);
23102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2311916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
23122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2313916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
231402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
231502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
231602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
231702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
231802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
23192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2320a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
232102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
232202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
232302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2324916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2325c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2326916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2327916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
23282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
23292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
23302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
23312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
23322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
23332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
23342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
233505f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
23362ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
23372ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
23382ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
23392ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
23402ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
23412ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
23422ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
23432ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
23442ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
23452ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
23462ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
23472ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
23482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
234902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
23502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
235102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
235202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
235305f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
235405f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
23552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
23562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
23572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
235805f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
235905f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
236005f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2361c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2362916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
23632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
23642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
23652ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
23662ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2367c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2368cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2369c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
23703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
23713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2372916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2373916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2374916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2375916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
23760d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
23770d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
23780d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
23790d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
23800d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
23810d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
23820d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
23830d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
23848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
23858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // For performance, use the *os_ directly for anything that doesn't need indentation
23868f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // and prepare an indentation stream with default indentation 1.
23872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
23888f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  VariableIndentationOutputStream vios_;
23898f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  ScopedIndentation indent1_;
23908f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
23911d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
23923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
23932cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
2394df2bb1f36c0d755f0bdb2b1562e52669f3de4a09Andreas Gampe  OatDumperOptions* oat_dumper_options_;
239505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko  std::set<mirror::Object*> dex_caches_;
2396d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
23973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
239878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
239978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
24000b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartierstatic int DumpImage(gc::space::ImageSpace* image_space,
24010b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                     OatDumperOptions* options,
24020b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                     std::ostream* os) SHARED_REQUIRES(Locks::mutator_lock_) {
24030b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const ImageHeader& image_header = image_space->GetImageHeader();
24040b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  if (!image_header.IsValid()) {
24050b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
24060b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    return EXIT_FAILURE;
24070b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  }
24080b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  ImageDumper image_dumper(os, *image_space, image_header, options);
24090b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  if (!image_dumper.Dump()) {
24100b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    return EXIT_FAILURE;
24110b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  }
24120b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  return EXIT_SUCCESS;
24130b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier}
24140b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier
24150b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartierstatic int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
241600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
24179865bde5d822f56c4732214c2005dfcaa41f94cfMathieu Chartier  ScopedNullHandle<mirror::ClassLoader> null_class_loader;
241800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
241900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
242000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
24210b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  if (options->app_image_ != nullptr) {
24220b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (options->app_oat_ == nullptr) {
24230b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      LOG(ERROR) << "Can not dump app image without app oat file";
2424dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      return EXIT_FAILURE;
2425dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    }
24260b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
24270b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
24280b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // pointers into 32 bit pointer sized ArtMethods.
24290b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    std::string error_msg;
24300b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
24310b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    options->app_oat_,
24320b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    nullptr,
24330b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    nullptr,
24340b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    false,
24350b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    /*low_4gb*/true,
24360b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    nullptr,
24370b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    &error_msg));
24380b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (oat_file == nullptr) {
24390b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
2440dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      return EXIT_FAILURE;
2441dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    }
24420b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    std::unique_ptr<gc::space::ImageSpace> space(
24430b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
24440b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (space == nullptr) {
24450b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
24460b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                 << error_msg;
24470b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
24480b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // Open dex files for the image.
24490b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    std::vector<std::unique_ptr<const DexFile>> dex_files;
24500b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
24510b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
24520b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                 << error_msg;
24530b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
24540b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // Dump the actual image.
24550b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    int result = DumpImage(space.get(), options, os);
24560b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (result != EXIT_SUCCESS) {
24570b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      return result;
24580b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
24590b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // Fall through to dump the boot images.
24600b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  }
24610b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier
24620b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  gc::Heap* heap = runtime->GetHeap();
24630b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  CHECK(heap->HasBootImageSpace()) << "No image spaces";
24640b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
24650b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    int result = DumpImage(image_space, options, os);
24660b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (result != EXIT_SUCCESS) {
24670b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      return result;
24680b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
2469dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
2470dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return EXIT_SUCCESS;
247178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
247278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
247300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
247400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
247500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
247600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
247700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
247800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
247900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
248000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
248100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
248200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
248300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
248400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
2485d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier  runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
2486ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  std::vector<const DexFile*> class_path;
248700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
248800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2489ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
249000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
2491d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier    class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
2492ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    class_path.push_back(dex_file);
249300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
249400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
249500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
249600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2497966878d987cec1940fdfa8633fc79f8112320821Mathieu Chartier  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
249800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
249900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
250000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
250100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
250200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
250300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
250400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
250532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
250600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
250700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
250800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
250900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
251000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
251132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
251200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
25139865bde5d822f56c4732214c2005dfcaa41f94cfMathieu Chartier  ScopedNullHandle<mirror::ClassLoader> null_class_loader;
251400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
251500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
251632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
251700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
251800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
251900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
252000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
252100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
252200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
252300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
25240b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  OatFile* oat_file = OatFile::Open(oat_filename,
25250b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    oat_filename,
25260b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
25270b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
25280b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    false,
25290b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    /*low_4gb*/false,
25300b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
25310b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    &error_msg);
253200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
253300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
253400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
253500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
253600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
253700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
253800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
253900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
254000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
254100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
254200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
254300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25442fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbeckystatic int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
254500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
25460b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  OatFile* oat_file = OatFile::Open(oat_filename,
25470b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    oat_filename,
25480b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
25490b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
25500b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    false,
25510b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    /*low_4gb*/false,
25520b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
25530b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    &error_msg);
255400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
255500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
255600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
255700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
255800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25592d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  bool result;
25602d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
25612d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  // files for 64-bit code in the past.
25622d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
25632fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
25642d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    result = oat_symbolizer.Symbolize();
25652d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  } else {
25662fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
25672d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    result = oat_symbolizer.Symbolize();
25682d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  }
25692d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  if (!result) {
257000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
257100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
257200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
257300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
257400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
257500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
257600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25773774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
25783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
25793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
258000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
25823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
25833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
25843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
25853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
25863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
25873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
258800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
258900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
25913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
25923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
25933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
25943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
25953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
25963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
25973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
25983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
25993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
2600f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else if (option =="--dump:code_info_stack_maps") {
2601f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_ = true;
26023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
26033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
2604c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil    } else if (option =="--header-only") {
2605c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      dump_header_only_ = true;
26063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
26073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
26083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
26092fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else if (option.starts_with("--only-keep-debug")) {
26102fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      only_keep_debug_ = true;
261132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
261232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
26133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
26143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
261532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
261632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
261732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
261832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
261932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
262032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
262132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
262232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
262332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
262432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
262532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
26260b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    } else if (option.starts_with("--app-image=")) {
26270b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      app_image_ = option.substr(strlen("--app-image=")).data();
26280b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    } else if (option.starts_with("--app-oat=")) {
26290b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      app_oat_ = option.substr(strlen("--app-oat=")).data();
26303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
26313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
263200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
263300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
26353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
26363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
26383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
26393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
26403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
264100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
264200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
26443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
26453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
26463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
264700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
264800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
26503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
26513774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
26523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
26533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
26543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
26553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
26563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
26573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
26593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
26603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
26623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
26633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
26653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
26663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
26673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
26683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
26693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
26703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
26713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
26723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
26733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
26743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
26750b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "\n"
26760b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "  --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
26770b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        " boot image and app oat file.\n"
26780b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "      Example: --app-image=app.art\n"
26790b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "\n"
26800b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "  --app-oat=<file.odex>: specifies an input app oat.\n"
26810b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "      Example: --app-oat=app.odex\n"
26823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
26833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
26853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
26873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
26883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
26893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
269019510f02b011e545665f6219e6144c8e47aed5f0Mathieu Chartier        "  --dump:raw_gc_map enables dumping of the GC map.\n"
26913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
26923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
26933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
26943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
26953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2696f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2697f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "      Example: --dump:code_info_stack_maps\n"
2698f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "\n"
26993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
27003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
27013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2702c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "  --header-only may be used to print only the oat header.\n"
2703c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "      Example: --header-only\n"
2704c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "\n"
270532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
270632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
270732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
270832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
270932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
271032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
271132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
271232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
271332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
271432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
271532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
27162fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "  --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
27172fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "      .rodata and .text sections are omitted in the output file to save space.\n"
27182fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "      Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
27192fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "\n"
272032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
272132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
272232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
27233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
27243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
272532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
272632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
272732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
272832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
272932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
273032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
273132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
27323774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
27333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
27343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
273500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
273600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
27373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
273800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
273932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
27403fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
274100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
274200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
274300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
274400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
274500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
2746f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  bool dump_code_info_stack_maps_ = false;
274700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
274800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
27492fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  bool only_keep_debug_ = false;
275032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
275132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
2752c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil  bool dump_header_only_ = false;
275332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
275432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
27550b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const char* app_image_ = nullptr;
27560b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const char* app_oat_ = nullptr;
275700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
275800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
27593774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
27603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
27613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
276200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
27633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
27643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
27653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
27660b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    oat_dumper_options_.reset(new OatDumperOptions(
27673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
27683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
27693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
2770f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        args_->dump_code_info_stack_maps_,
27713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
27723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
277332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
277432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
277532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
277632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
2777c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        args_->dump_header_only_,
277832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
27790b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        args_->app_image_,
27800b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        args_->app_oat_,
278132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
27823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
27833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
27843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
278500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
278600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
27873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
27883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2789c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
27903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2791d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
27923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2793c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
27942fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // ELF has special kind of section called SHT_NOBITS which allows us to create
27952fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // sections which exist but their data is omitted from the ELF file to save space.
27962fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // This is what "strip --only-keep-debug" does when it creates separate ELF file
27972fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // with only debug data. We use it in similar way to exclude .rodata and .text.
27982fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      bool no_bits = args_->only_keep_debug_;
27992fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
2800c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2801c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2802c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
280332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2804c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2805c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
280600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
280700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
28083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
28093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
28103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
28113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
28123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
28133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
281432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
28153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
281600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
281700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
28180b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
281900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
282000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
28213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
28223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
282300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
28247934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
282578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
282678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
28273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
28283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
282978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2830