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"
411d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h"
421d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/large_object_space.h"
431d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
444a26f17b055cadc949c3e9fdfa637fe5656339d9Mathieu Chartier#include "image-inl.h"
452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "indenter.h"
46131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko#include "linker/buffered_output_stream.h"
47131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko#include "linker/file_output_stream.h"
482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/array-inl.h"
492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
5005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko#include "mirror/dex_cache-inl.h"
512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
53700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "oat.h"
548a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko#include "oat_file-inl.h"
55f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "oat_file_manager.h"
56e5448b5a12003b405b22cde3b94f962ab4888a87Elliott Hughes#include "os.h"
57a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
5800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
596bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray#include "stack_map.h"
6000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "ScopedLocalRef.h"
61c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier#include "thread_list.h"
6200bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe#include "type_lookup_table.h"
632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "verifier/method_verifier.h"
6400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "well_known_classes.h"
6578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <sys/stat.h>
673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include "cmdline.h"
6878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
693774335b08076117d6950cd472cdd59a167470b5Igor Murashkinnamespace art {
7078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
71e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_methods_descriptions_[] = {
721984651929744dd603fd082e23eacd877b9bc177Ian Rogers  "kResolutionMethod",
7388474b416eb257078e590bf9bc7957cee604a186Jeff Hao  "kImtConflictMethod",
742d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier  "kImtUnimplementedMethod",
75e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  "kCalleeSaveMethod",
76aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsOnlySaveMethod",
77aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsAndArgsSaveMethod",
78e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier};
79e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
80e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_roots_descriptions_[] = {
8158ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  "kDexCaches",
8234f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom  "kClassRoots",
8378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
8478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
85ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier// Map is so that we don't allocate multiple dex files for the same OatDexFile.
86ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartierstatic std::map<const OatFile::OatDexFile*,
87ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier                std::unique_ptr<const DexFile>> opened_dex_files;
88ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier
89ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartierconst DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
90ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  DCHECK(oat_dex_file != nullptr);
91ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  auto it = opened_dex_files.find(oat_dex_file);
92ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  if (it != opened_dex_files.end()) {
93ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    return it->second.get();
94ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  }
95ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
96ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
97ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  return ret;
98ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier}
99ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier
1002d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampetemplate <typename ElfTypes>
101bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbeckyclass OatSymbolizer FINAL {
10254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe public:
1032fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
1042fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      oat_file_(oat_file),
1052fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      builder_(nullptr),
1062fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      output_name_(output_name.empty() ? "symbolized.oat" : output_name),
1072fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      no_bits_(no_bits) {
10854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
10954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Symbolize() {
1116d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
1125d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky    const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
1135d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky        isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
1146d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1156d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
1166d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    std::unique_ptr<BufferedOutputStream> output_stream(
11710c13565474de2786aad7c2e79757ea250747a15Vladimir Marko        MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
1182d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
1196d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1206d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    builder_->Start();
1216d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1226d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* rodata = builder_->GetRoData();
1236d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* text = builder_->GetText();
1246d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* bss = builder_->GetBss();
1256d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1266d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const uint8_t* rodata_begin = oat_file_->Begin();
1276d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
1282fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    if (no_bits_) {
1292fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->WriteNoBitsSection(rodata_size);
1302fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else {
1312fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->Start();
1322fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->WriteFully(rodata_begin, rodata_size);
1332fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->End();
1342fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    }
1356d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1366d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
1376d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const size_t text_size = oat_file_->End() - text_begin;
1382fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    if (no_bits_) {
1392fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->WriteNoBitsSection(text_size);
1402fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else {
1412fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->Start();
1422fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->WriteFully(text_begin, text_size);
1432fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->End();
1442fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    }
1456d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1466d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    if (oat_file_->BssSize() != 0) {
1475b1c2ca30dad519be285f0a1e839c23cc4e3a51dDavid Srbecky      bss->WriteNoBitsSection(oat_file_->BssSize());
1486d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    }
1496d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
150316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung    if (isa == kMips || isa == kMips64) {
151316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung      builder_->WriteMIPSabiflagsSection();
152316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung    }
153944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko    builder_->PrepareDynamicSection(
154944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko        elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
155944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko    builder_->WriteDynamicSection();
15654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1575d950769b607b4f76413212db640a32d796911deDavid Srbecky    Walk();
1585d950769b607b4f76413212db640a32d796911deDavid Srbecky    for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
1595d950769b607b4f76413212db640a32d796911deDavid Srbecky      method_debug_infos_.push_back(trampoline);
1605d950769b607b4f76413212db640a32d796911deDavid Srbecky    }
16154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1625d950769b607b4f76413212db640a32d796911deDavid Srbecky    debug::WriteDebugInfo(builder_.get(),
1635d950769b607b4f76413212db640a32d796911deDavid Srbecky                          ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
1645d950769b607b4f76413212db640a32d796911deDavid Srbecky                          dwarf::DW_DEBUG_FRAME_FORMAT,
1655d950769b607b4f76413212db640a32d796911deDavid Srbecky                          true /* write_oat_patches */);
16654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1676d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    builder_->End();
16854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
16910c13565474de2786aad7c2e79757ea250747a15Vladimir Marko    return builder_->Good();
17054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
17154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1725d950769b607b4f76413212db640a32d796911deDavid Srbecky  void Walk() {
17354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
17454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t i = 0; i < oat_dex_files.size(); i++) {
17554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
1762cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      CHECK(oat_dex_file != nullptr);
1775d950769b607b4f76413212db640a32d796911deDavid Srbecky      WalkOatDexFile(oat_dex_file);
17854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
17954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
18054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1815d950769b607b4f76413212db640a32d796911deDavid Srbecky  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
18254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::string error_msg;
183ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
184ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    if (dex_file == nullptr) {
18554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
18654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
18754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t class_def_index = 0;
18854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index < dex_file->NumClassDefs();
18954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index++) {
19054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
19154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      OatClassType type = oat_class.GetType();
19254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      switch (type) {
19354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassAllCompiled:
19454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassSomeCompiled:
1955d950769b607b4f76413212db640a32d796911deDavid Srbecky          WalkOatClass(oat_class, *dex_file, class_def_index);
19654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
19754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
19854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassNoneCompiled:
19954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassMax:
20054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          // Ignore.
20154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
20254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
20354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
20554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2065d950769b607b4f76413212db640a32d796911deDavid Srbecky  void WalkOatClass(const OatFile::OatClass& oat_class,
2075d950769b607b4f76413212db640a32d796911deDavid Srbecky                    const DexFile& dex_file,
2085d950769b607b4f76413212db640a32d796911deDavid Srbecky                    uint32_t class_def_index) {
2095d950769b607b4f76413212db640a32d796911deDavid Srbecky    const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
21013735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
21154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
21254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
21354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
21454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    // Note: even if this is an interface or a native class, we still have to walk it, as there
21554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    //       might be a static initializer.
21654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    ClassDataItemIterator it(dex_file, class_data);
21754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t class_method_idx = 0;
2185d950769b607b4f76413212db640a32d796911deDavid Srbecky    for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
2195d950769b607b4f76413212db640a32d796911deDavid Srbecky    for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
2205d950769b607b4f76413212db640a32d796911deDavid Srbecky    for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
2215d950769b607b4f76413212db640a32d796911deDavid Srbecky      WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
2225d950769b607b4f76413212db640a32d796911deDavid Srbecky                    dex_file,
2235d950769b607b4f76413212db640a32d796911deDavid Srbecky                    class_def_index,
2245d950769b607b4f76413212db640a32d796911deDavid Srbecky                    it.GetMemberIndex(),
2255d950769b607b4f76413212db640a32d796911deDavid Srbecky                    it.GetMethodCodeItem(),
2265d950769b607b4f76413212db640a32d796911deDavid Srbecky                    it.GetMethodAccessFlags());
22754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
22854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DCHECK(!it.HasNext());
22954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
23054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2315d950769b607b4f76413212db640a32d796911deDavid Srbecky  void WalkOatMethod(const OatFile::OatMethod& oat_method,
2325d950769b607b4f76413212db640a32d796911deDavid Srbecky                     const DexFile& dex_file,
2335d950769b607b4f76413212db640a32d796911deDavid Srbecky                     uint32_t class_def_index,
2345d950769b607b4f76413212db640a32d796911deDavid Srbecky                     uint32_t dex_method_index,
2355d950769b607b4f76413212db640a32d796911deDavid Srbecky                     const DexFile::CodeItem* code_item,
2365d950769b607b4f76413212db640a32d796911deDavid Srbecky                     uint32_t method_access_flags) {
23754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if ((method_access_flags & kAccAbstract) != 0) {
23854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // Abstract method, no code.
23954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
24054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
2415d950769b607b4f76413212db640a32d796911deDavid Srbecky    const OatHeader& oat_header = oat_file_->GetOatHeader();
2425d950769b607b4f76413212db640a32d796911deDavid Srbecky    const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
2435d950769b607b4f76413212db640a32d796911deDavid Srbecky    if (method_header == nullptr || method_header->GetCodeSize() == 0) {
24454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // No code.
24554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
24654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
24754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
248a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky    uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
249a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky    // Clear Thumb2 bit.
250a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky    const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
251a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky
2525d950769b607b4f76413212db640a32d796911deDavid Srbecky    debug::MethodDebugInfo info = debug::MethodDebugInfo();
2535d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.trampoline_name = nullptr;
2545d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.dex_file = &dex_file;
2555d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.class_def_index = class_def_index;
2565d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.dex_method_index = dex_method_index;
2575d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.access_flags = method_access_flags;
2585d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.code_item = code_item;
2595d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.isa = oat_header.GetInstructionSet();
2605d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
2615d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.is_native_debuggable = oat_header.IsNativeDebuggable();
2625d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.is_optimized = method_header->IsOptimized();
2635d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.is_code_address_text_relative = true;
264a1b4c5f562c67398e14756bd127ed806937ff35aDavid Srbecky    info.code_address = reinterpret_cast<uintptr_t>(code_address);
2655d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.code_size = method_header->GetCodeSize();
2665d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
2675d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
2685d950769b607b4f76413212db640a32d796911deDavid Srbecky    info.cfi = ArrayRef<uint8_t>();
2695d950769b607b4f76413212db640a32d796911deDavid Srbecky    method_debug_infos_.push_back(info);
27054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
27154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
27254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe private:
27354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  const OatFile* oat_file_;
2742d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
2755d950769b607b4f76413212db640a32d796911deDavid Srbecky  std::vector<debug::MethodDebugInfo> method_debug_infos_;
2765d950769b607b4f76413212db640a32d796911deDavid Srbecky  std::unordered_set<uint32_t> seen_offsets_;
2770279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  const std::string output_name_;
2782fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  bool no_bits_;
27954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe};
28054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass OatDumperOptions {
2822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
2839d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko  OatDumperOptions(bool dump_vmap,
284f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   bool dump_code_info_stack_maps,
2852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
28600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
28732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
28832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
28932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
29032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
291c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil                   bool dump_header_only,
29232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
2930b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                   const char* app_image,
2940b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                   const char* app_oat,
29532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
2969d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko    : dump_vmap_(dump_vmap),
297f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_(dump_code_info_stack_maps),
2982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
29900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
30032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
3013fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
30232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
30332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
304c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      dump_header_only_(dump_header_only),
30532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
3060b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      app_image_(app_image),
3070b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      app_oat_(app_oat),
30832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
3093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
3102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
3112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
312f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  const bool dump_code_info_stack_maps_;
3132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
3142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
31532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
3163fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
31732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
31832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
319c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil  const bool dump_header_only_;
32032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
3210b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const char* const app_image_;
3220b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const char* const app_oat_;
32332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
32400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
3252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
3262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
327e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
328aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
3293887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
3309583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
331a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
3322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
33332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
334a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
335a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
33632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
337a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
338a6e95b32d499811bbb37602fc7446a5a0d05b9f8Aart Bik                                                                 oat_file.End(),
339eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                                                 true /* can_read_literals_ */))) {
34032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
34132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
34232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
3433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
3443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
3453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
3462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
3472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
3482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
3492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
350a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
351a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
352a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
353a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
3542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
3552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
3563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
357aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
358aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
359aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
360aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3614b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao    os << "LOCATION:\n";
3624b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao    os << oat_file_.GetLocation() << "\n\n";
3634b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao
364aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
365ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
366aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
367a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
368a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
369a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
3706f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
3716f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
3726f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
3736f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
3746f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
3756f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
3766f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
3777020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
378aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
379aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
380aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3812ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
3822ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
3832ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
38432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
3852ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
3862ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
3872ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
3882ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
3892ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
3902ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
3912ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
3922ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
3932ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
3942ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
3952ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
3962ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
3972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
3982ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
3992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
4022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
4032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
4042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
405aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
4072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
4082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
4092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
410a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
41128db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
41228db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
41328db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
41428db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
415700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
41681f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
41722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
41822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
41922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
42022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
42122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
42222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
42322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
42422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
42522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
42622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
42722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
42822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
42981f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
43032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
4312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
4322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
4332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
4352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
4362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
437aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
4392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
440aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
441aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
442aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
44332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
44432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
44532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
44632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
44732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
44832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
44932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
450c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil    if (!options_.dump_header_only_) {
451c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      for (size_t i = 0; i < oat_dex_files_.size(); i++) {
452c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
453c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        CHECK(oat_dex_file != nullptr);
45432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
455c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        // If file export selected skip file analysis
456c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        if (options_.export_dex_location_) {
457c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          if (!ExportDexFile(os, *oat_dex_file)) {
458c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil            success = false;
459c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          }
460c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        } else {
461c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          if (!DumpOatDexFile(os, *oat_dex_file)) {
462c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil            success = false;
463c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          }
46432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
4652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
4663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
467c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil
4682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
4692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
4703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
4713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
4723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
47313735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
47413735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
4753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
4763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
477ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
478ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
479ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
4803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
481ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
4823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
4833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
484e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
485a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
486f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
487f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
488f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
48990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
4903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
4928d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
4938d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
494ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
495ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (dex_file == nullptr) {
4968d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
4978d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
4988d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
499e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5008b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
501e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
5022ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
5038b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
504d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
5053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
506d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
5073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
5083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
509aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5102ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
511aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
512aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
513aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
5143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
515e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
516e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
517e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
518e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
5193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5212ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
5228d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
523ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
524ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (dex_file == nullptr) {
5258d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5268d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
528e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
529ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
5302ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
5312ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
5322ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
533e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
534d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
53513735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
5362ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
537e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
538e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
539e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
540e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
541d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
542e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
543e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
544e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
545d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
546e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
547e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
548e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
549e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
550e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
551e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
552e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
553e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
554e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
555ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
556e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
557e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
5592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
5602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
5612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
562e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
56395ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
56495ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
56595ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
56695ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
56795ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
568e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
569e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
570e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
5722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
57332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
5742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
575a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
576ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
577590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
57800bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    // Print embedded dex file data range.
57900bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
58000bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
58100bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - oat_file_begin);
58200bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
58300bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe                       dex_offset,
58400bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe                       dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
585590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
58600bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    // Create the dex file early. A lot of print-out things depend on it.
5878d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
588ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
589ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    if (dex_file == nullptr) {
5908d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
5912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
5922cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
593aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5948f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
59500bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    // Print lookup table, if it exists.
59600bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    if (oat_dex_file.GetLookupTableData() != nullptr) {
59700bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe      uint32_t table_offset = dchecked_integral_cast<uint32_t>(
59800bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe          oat_dex_file.GetLookupTableData() - oat_file_begin);
59900bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe      uint32_t table_size = TypeLookupTable::RawDataLength(*dex_file);
60000bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe      os << StringPrintf("type-table: 0x%08x..0x%08x\n",
60100bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe                         table_offset,
60200bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe                         table_offset + table_size - 1);
60300bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe    }
60400bb716039d23e02797a3858fcdb0380a9bb8855Andreas Gampe
6058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    VariableIndentationOutputStream vios(&os);
6068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&vios);
6072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
6082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
6092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
610aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
611aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
61232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
61332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
61432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
61532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
61632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
61732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
6182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
619d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
6202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
6212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
622d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
623d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
624d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
62532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
626ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
6272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
6282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
62932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
63032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
63132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
63232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
633aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
634aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
635aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
6362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
637aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
638aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
63932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
64032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
64132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
64232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
643ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
64432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
64532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
64632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
64932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
65132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
65232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
65332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
65732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
65832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
65932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
66032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
66432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
66532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
66932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
67032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
67132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
67232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
67432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
67632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
67732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
67832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
67932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
68032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
68132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
68232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
68432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
68532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
68632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
68732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
68932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
69032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
69132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
70132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
70232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
70332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
70732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
71032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
71132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
71232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
71332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
71432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
71532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
71632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
717e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
7180571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
7190571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
720aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7210571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
7220571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
723aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
724e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
725aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
7268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatClass(VariableIndentationOutputStream* vios,
7278f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                    const OatFile::OatClass& oat_class, const DexFile& dex_file,
72832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
7292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
73032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
73113735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
7322ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
7338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << std::flush;
7342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
735e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
736e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
737e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
7382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
7390571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
7408f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
7412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
74232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
74532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
74632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
74732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
74832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7500571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
751aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7520571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
7538f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
7542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
75532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
75832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
75932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
76032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
76132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7630571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
764aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7650571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
7668f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
7672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
768aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
769e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
7702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
7712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
7732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
7742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatMethod(VariableIndentationOutputStream* vios,
7768f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                     const DexFile::ClassDef& class_def,
7778b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
7782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
7792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
78032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
7812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
78232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
78332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
78432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
78532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
7863fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
7873fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
7883fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
78932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
7908f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
7918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   class_method_index, pretty_method.c_str(),
7928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   dex_method_idx);
79332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
79432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
79632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
80032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
80132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
80232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
80332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
80432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
80532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
80632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
80732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8088f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    // Everything below is indented at least once.
8098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(vios);
8108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
8112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "DEX CODE:\n";
8138f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8148f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpDexCode(vios->Stream(), dex_file, code_item);
8152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
817673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier    std::unique_ptr<StackHandleScope<1>> hs;
8182ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
8192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
820673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      // We need to have the handle scope stay live until after the verifier since the verifier has
821673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      // a handle to the dex cache from hs.
822673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      hs.reset(new StackHandleScope<1>(Thread::Current()));
8238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
8248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
825673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      verifier.reset(DumpVerifier(vios, hs.get(),
8268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                  dex_method_idx, &dex_file, class_def, code_item,
8272ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
8282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8308f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatMethodOffsets ";
83132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method_offsets);
8332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
8352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
8368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
8372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
8382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
8392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
8408f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
8412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
843a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
8448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
8462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
8472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
8488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code offset 0x%08x is past end of file 0x%08zx.\n",
8508f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       aligned_code_begin, oat_file_.Size());
8512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8538f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
8542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
8552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
8568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatQuickMethodHeader ";
8572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
8582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
8592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
86032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8618f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", method_header);
8622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8638f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
8642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
8658f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
8662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
8672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
8682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
8698f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
8702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "vmap_table: ";
87532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8768f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
8772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
8798f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
8802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
8818f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8828f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset 0x%08x is past end of file 0x%08zx. "
8838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset was loaded from offset 0x%08x.\n",
8848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       vmap_table_offset, oat_file_.Size(),
8858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetVmapTableOffsetOffset());
8862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
88732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
8888f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapData(vios, oat_method, code_item);
8892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "QuickMethodFrameInfo\n";
8938f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
8948f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8958f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream()
8968f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
8978f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
8988f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
8998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9008f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
9018f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
9028f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // Based on spill masks from QuickMethodFrameInfo so placed
9068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // after it is dumped, but useful for understanding quick
9078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // code, so dumped here.
9088f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpVregLocations(vios->Stream(), oat_method, code_item);
9102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "CODE: ";
9132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
9142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
9158f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
9168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9178f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code size offset 0x%08x is past end of file 0x%08zx.",
9188f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset, oat_file_.Size());
9192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
9212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
9222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
9232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
924590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
92532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
9268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("%p ", code);
9272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
9298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_offset,
9308f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset,
9318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size,
9328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code != nullptr ? "..." : "");
9332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
9352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
9368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("WARNING: "
9378f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         "start of code at 0x%08x is past end of file 0x%08zx.",
9388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         aligned_code_begin, oat_file_.Size());
9392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
9402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
9418f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
9428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
9438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
9448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
9458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              aligned_code_end, oat_file_.Size(),
9468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
9472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
94832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9509d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko              DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
9512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
9532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
9548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
9558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
9568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size %d is bigger than max expected threshold of %d. "
9578f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
9588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, kMaxCodeSize,
9598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
9602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
96132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9639d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko              DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
9642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
96632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
9679d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko          DumpCode(vios, oat_method, code_item, !success, 0);
9682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
9722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
9733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
9743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
9753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
9763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
9773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
9783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
9792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
9803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
9813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
9823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
9833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
9843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
9853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
9863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
9873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
9883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
9893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
9903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
9913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
9923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
9933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
9943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
9953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
9963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
9973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
998442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
9998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpVmapData(VariableIndentationOutputStream* vios,
1000442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1001442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1002f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1003f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The optimizing compiler outputs its CodeInfo data in the vmap table.
1004442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1005442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1006442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1007442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
10088f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
10098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpCodeInfo(vios, code_info, oat_method, *code_item);
1010442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10110a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
10120a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // We don't encode the size in the table, so just emit that we have quickened
10130a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // information.
10140a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      ScopedIndentation indent(vios);
10150a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      vios->Stream() << "quickened data\n";
1016442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
10179d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko      // Otherwise, there is nothing to display.
101820d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1019442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1020442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1021442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
10228f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCodeInfo(VariableIndentationOutputStream* vios,
1023442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1024f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                    const OatFile::OatMethod& oat_method,
1025442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
10268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    code_info.Dump(vios,
1027f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   oat_method.GetCodeOffset(),
1028f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   code_item.registers_size_,
1029f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   options_.dump_code_info_stack_maps_);
1030442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1031442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1032faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1033faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1034faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1035faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1036faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1037faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1038faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1039faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1040faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1041faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1042faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1043faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1044faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1045faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1046faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1047faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1048faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1049faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1050faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
105115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
105215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
105315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
105415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
105515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
105615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
105715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1058faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1059faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1060faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1061faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1062faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1063faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1064faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1065faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1066faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1067faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1068faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1069faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1070faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1071faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1072faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1073faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1074b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
10752ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1076b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1077b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1078b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
107932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
108032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1081b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1082e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1083e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1084b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1085b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1086f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1087f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // the optimizing compiler?
1088f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1089f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                                    const DexFile::CodeItem* code_item) {
1090f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // If the native GC map is null and the Dex `code_item` is not
1091f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // null, then this method has been compiled with the optimizing
1092f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // compiler.
10930a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() != nullptr &&
10949d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko           oat_method.GetVmapTable() != nullptr &&
10950a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
10960a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  }
10970a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray
10980a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
10990a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // the dextodex compiler?
11000a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
11010a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray                                                  const DexFile::CodeItem* code_item) {
11020a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // If the quick code is null, the Dex `code_item` is not
11030a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // null, and the vmap table is not null, then this method has been compiled
11040a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // with the dextodex compiler.
11050a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() == nullptr &&
11060a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetVmapTable() != nullptr &&
11070a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
1108f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1109f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
11108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
1111673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier                                         StackHandleScope<1>* hs,
11128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         uint32_t dex_method_idx,
11132ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
11142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
11152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
11162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
11172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
11182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1119d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier      Runtime* const runtime = Runtime::Current();
1120eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1121c9dbb1df3b5c06ba122cacaf35b17cb53c6be3c6Mathieu Chartier          hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
112232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
1123e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      return verifier::MethodVerifier::VerifyMethodAndDump(
11248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
11258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          &class_def, code_item, nullptr, method_access_flags);
11262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
11272ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
11282ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
11292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
11302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1131adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko  // The StackMapsHelper provides the stack maps in the native PC order.
1132adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko  // For identical native PCs, the order from the CodeInfo is preserved.
1133adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko  class StackMapsHelper {
1134adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko   public:
1135adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    explicit StackMapsHelper(const uint8_t* raw_code_info)
1136adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        : code_info_(raw_code_info),
1137adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          encoding_(code_info_.ExtractEncoding()),
1138adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1139adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          indexes_(),
1140adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          offset_(static_cast<size_t>(-1)),
1141adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          stack_map_index_(0u) {
1142adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      if (number_of_stack_maps_ != 0u) {
1143adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        // Check if native PCs are ordered.
1144adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        bool ordered = true;
1145adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1146adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1147adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          StackMap current = code_info_.GetStackMapAt(i, encoding_);
1148adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1149adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko              current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1150adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko            ordered = false;
1151adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko            break;
1152adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          }
1153adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          last = current;
1154adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        }
1155adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        if (!ordered) {
1156adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          // Create indirection indexes for access in native PC order. We do not optimize
1157adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          // for the fact that there can currently be only two separately ordered ranges,
1158adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          // namely normal stack maps and catch-point stack maps.
1159adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          indexes_.resize(number_of_stack_maps_);
1160adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          std::iota(indexes_.begin(), indexes_.end(), 0u);
1161adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          std::sort(indexes_.begin(),
1162adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                    indexes_.end(),
1163adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                    [this](size_t lhs, size_t rhs) {
1164adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                      StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1165adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                      uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1166adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                      StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1167adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                      uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1168adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                      // If the PCs are the same, compare indexes to preserve the original order.
1169adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                      return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1170adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                    });
1171adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        }
1172adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1173adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      }
1174adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    }
1175adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko
1176adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    const CodeInfo& GetCodeInfo() const {
1177adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      return code_info_;
1178adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    }
1179adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko
1180adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    const CodeInfoEncoding& GetEncoding() const {
1181adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      return encoding_;
1182adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    }
1183adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko
1184adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    size_t GetOffset() const {
1185adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      return offset_;
1186adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    }
1187adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko
1188adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    StackMap GetStackMap() const {
1189adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      return GetStackMapAt(stack_map_index_);
1190adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    }
1191adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko
1192adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    void Next() {
1193adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      ++stack_map_index_;
1194adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      offset_ = (stack_map_index_ == number_of_stack_maps_)
1195adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          ? static_cast<size_t>(-1)
1196adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1197adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    }
1198adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko
1199adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko   private:
1200adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    StackMap GetStackMapAt(size_t i) const {
1201adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      if (!indexes_.empty()) {
1202adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        i = indexes_[i];
1203adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      }
1204adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      DCHECK_LT(i, number_of_stack_maps_);
1205adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      return code_info_.GetStackMapAt(i, encoding_);
1206adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    }
1207adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko
1208adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    const CodeInfo code_info_;
1209adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    const CodeInfoEncoding encoding_;
1210adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    const size_t number_of_stack_maps_;
1211adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    dchecked_vector<size_t> indexes_;  // Used if stack map native PCs are not ordered.
1212adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    size_t offset_;
1213adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    size_t stack_map_index_;
1214adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko  };
1215adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko
12168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCode(VariableIndentationOutputStream* vios,
12172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
12182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1219ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1220ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
12212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
12222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
12232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1224956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
12258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "NO CODE!\n";
1226b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1227adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1228adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      // The optimizing compiler outputs its CodeInfo data in the vmap table.
1229adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      StackMapsHelper helper(oat_method.GetVmapTable());
1230ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1231ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1232ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
12338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1234adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        if (offset == helper.GetOffset()) {
1235adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          ScopedIndentation indent1(vios);
1236adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          StackMap stack_map = helper.GetStackMap();
1237adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          DCHECK(stack_map.IsValid());
1238adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          stack_map.Dump(vios,
1239adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                         helper.GetCodeInfo(),
1240adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                         helper.GetEncoding(),
1241adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                         oat_method.GetCodeOffset(),
1242adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko                         code_item->registers_size_);
1243adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          do {
1244adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko            helper.Next();
1245adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko            // There may be multiple stack maps at a given PC. We display only the first one.
1246adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko          } while (offset == helper.GetOffset());
12472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
1248adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        DCHECK_LT(offset, helper.GetOffset());
1249adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      }
1250adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko    } else {
1251adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1252adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      size_t offset = 0;
1253adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko      while (offset < code_size) {
1254adbf93edb12dee174be3c5cad4d65ea97b40ffb5Vladimir Marko        offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
12552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1256b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1257b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1258b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
12593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
12602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
126132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
126232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1263a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1264ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
12652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1266aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1267aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
12683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
126927ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
12700b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  ImageDumper(std::ostream* os,
12710b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier              gc::space::ImageSpace& image_space,
12720b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier              const ImageHeader& image_header,
12730b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier              OatDumperOptions* oat_dumper_options)
12742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
12758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios_(os),
12768f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent1_(&vios_),
12772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
12782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
12792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
128027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
128190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
12822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
12838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& indent_os = vios_.Stream();
12848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
12852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
12862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1287dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1288dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao
12892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
12902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1291e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1292e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1293e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1294e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto section = static_cast<ImageHeader::ImageSections>(i);
1295e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1296e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
129731e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
12982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
12992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1300700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
13012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1302700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1303700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1304700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1305700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1306700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
13072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1308a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1309a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
131046774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
131146774767fcf7780d1455e755729198648d08742eIgor Murashkin
13122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
13132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1314e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_roots_descriptions_) ==
1315e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
13162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
13172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
13182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
13192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
13208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
13212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
13222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1323fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
13248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent2(&vios_);
1325277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1326277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1327fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1328277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1329277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1330fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1331fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1332fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1333fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1334fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1335fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
13368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d: ", j);
1337fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
13388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d to %zd: ", j, j + run);
1339277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1340fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
13412ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
13428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              PrettyObjectValue(indent_os, value->GetClass(), value);
13432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
13448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << j << ": null\n";
13452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1346d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
134734f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
134834f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
13498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    }
1350e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
13518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    {
1352e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "METHOD ROOTS\n";
1353e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_methods_descriptions_) ==
1354e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1355e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1356e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1357e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const char* description = image_methods_descriptions_[i];
1358e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto* image_method = image_header_.GetImageMethod(image_root);
13598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", description, image_method);
1360e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
136127ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
13622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
136327ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1364f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    Runtime* const runtime = Runtime::Current();
1365f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    ClassLinker* class_linker = runtime->GetClassLinker();
13662ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
13672ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
13682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
13692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
13708d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
13710b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const OatFile* oat_file = image_space_.GetOatFile();
1372a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
13730b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
13740b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
13750b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (oat_file == nullptr) {
13760b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      oat_file = OatFile::Open(oat_location,
13770b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               oat_location,
13780b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               nullptr,
13790b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               nullptr,
13800b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               false,
13810b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               /*low_4gb*/false,
13820b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                               nullptr,
1383b1d8c314b55bb2df2b2bb72a3daaf5db65b7ebc7Igor Murashkin                               &error_msg);
13840b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
13850b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (oat_file == nullptr) {
13860b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      os << "OAT FILE NOT FOUND: " << error_msg << "\n";
13870b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      return EXIT_FAILURE;
1388aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
13892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1390aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
13913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
139278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
139332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
13943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
139502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
13962ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
139702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
139802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
139905f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
140005f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
14012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1402b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1403dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    // Loop through the image space and dump its objects.
1404f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    gc::Heap* heap = runtime->GetHeap();
140550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1406357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1407c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1408c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1409c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1410c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
141190d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
141290d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
141390d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1414f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier      ScopedThreadSuspension sts(self, kNative);
14154f55e22630d99ca0edd9e951e5ee96b57bb9b980Mathieu Chartier      ScopedSuspendAll ssa(__FUNCTION__);
1416f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier      heap->RevokeAllThreadLocalAllocationStacks(self);
1417357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
14182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
1419e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      // Mark dex caches.
142005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      dex_caches_.clear();
1421e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      {
1422e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        ReaderMutexLock mu(self, *class_linker->DexLock());
142304302dbb106d590ff72c0dfecda23d85b6565059Hiroshi Yamauchi        for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
1424673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          mirror::DexCache* dex_cache =
142504302dbb106d590ff72c0dfecda23d85b6565059Hiroshi Yamauchi              down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
1426673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          if (dex_cache != nullptr) {
142705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            dex_caches_.insert(dex_cache);
1428673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          }
1429e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        }
1430e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
14312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1432dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // Dump the normal objects before ArtMethods.
1433dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1434dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      indent_os << "\n";
1435dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // TODO: Dump fields.
1436dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // Dump methods after.
1437dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      DumpArtMethodVisitor visitor(this);
1438cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier      image_header_.VisitPackedArtMethods(&visitor,
1439cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier                                          image_space_.Begin(),
1440cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier                                          image_header_.GetPointerSize());
14412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1442bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
14432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
1444b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
14452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1446700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
14470b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    size_t data_size = image_header_.GetDataSize();  // stored size in file.
14480b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (file == nullptr) {
14492ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
14500b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    } else {
14512ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
14520b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      // If the image is compressed, adjust to decompressed size.
14530b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
14540b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
14550b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
14560b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      }
14570b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      stats_.file_bytes += uncompressed_size - data_size;
14586f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
14593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
1460ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
1461e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1462e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& method_section = image_header_.GetMethodsSection();
146305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    const auto& dex_cache_arrays_section = image_header_.GetImageSection(
146405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        ImageHeader::kSectionDexCacheArrays);
1465d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    const auto& intern_section = image_header_.GetImageSection(
1466d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier        ImageHeader::kSectionInternedStrings);
1467208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    const auto& class_table_section = image_header_.GetImageSection(
1468208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier        ImageHeader::kSectionClassTable);
1469ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1470ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
14713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
1472ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1473ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Objects are kObjectAlignment-aligned.
1474ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1475ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    if (object_section.Offset() > header_bytes) {
1476ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe      stats_.alignment_bytes += object_section.Offset() - header_bytes;
1477ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    }
1478ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1479ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Field section is 4-byte aligned.
1480ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    constexpr size_t kFieldSectionAlignment = 4U;
1481ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_objects = object_section.Offset() + object_section.Size();
1482ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1483ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += field_section.Offset() - end_objects;
1484ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1485ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1486ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_fields = field_section.Offset() + field_section.Size();
1487ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(method_section.Offset(), 4);
1488ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += method_section.Offset() - end_fields;
1489ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1490ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1491ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_methods = method_section.Offset() + method_section.Size();
1492ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1493ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1494ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1495ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Intern table is 8-byte aligned.
1496ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1497ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1498ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += intern_section.Offset() - end_caches;
1499ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1500ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Add space between intern table and class table.
1501ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1502ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1503ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
15040b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
15050b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
1506ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
15070b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
1508ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1509e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.bitmap_bytes += bitmap_section.Size();
1510e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.art_field_bytes += field_section.Size();
1511cf36d493124d8048efa0bd6f67d817ce3cd6b725Vladimir Marko    stats_.art_method_bytes += method_section.Size();
151205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
1513d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    stats_.interned_strings_bytes += intern_section.Size();
1514208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    stats_.class_table_bytes += class_table_section.Size();
15158f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    stats_.Dump(os, indent_os);
15162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
151727ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
15182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
15193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
15202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
15213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1522d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
15233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
152454d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier  class DumpArtMethodVisitor : public ArtMethodVisitor {
152554d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier   public:
152654d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
152754d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
152854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
152954d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      std::ostream& indent_os = image_dumper_->vios_.Stream();
153054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
15310b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      image_dumper_->DumpMethod(method, indent_os);
153254d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      indent_os << "\n";
153354d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    }
153454d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
153554d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier   private:
153654d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    ImageDumper* const image_dumper_;
153754d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier  };
153854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
15392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
154090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
15412ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
15422ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
15432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1544d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
15452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
15462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
154768b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
154864b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
15492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
15502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1551d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
15522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1553d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1554d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1555d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1556c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
155790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
155861c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
155908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
156008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1561ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
156208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
156308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
15642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
156508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
156608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
15672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
156808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
156908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
15702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
157108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
157337f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
157408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
157637f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
157708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
157937f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
158037f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
158108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
158208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
158337f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
158408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
158508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
158608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
158708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
158808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
158908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
159008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
159150239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
159208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1593daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
159408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
159508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
159608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
159708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
159808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
159908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
160050239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
160108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
160248efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
160308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
160408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
160508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1606d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1607d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1608d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16092dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
161090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
16112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
16122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
16132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1614d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
161554d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    for (ArtField& field : klass->GetIFields()) {
161654d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      PrintField(os, &field, obj);
1617d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1618d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1619d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
16213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1622f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
16233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
16240b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const void* GetQuickOatCodeBegin(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
1625a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
16260b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        image_header_.GetPointerSize());
16276f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1628ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
16293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1630f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1631ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1632f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1633ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
16343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
16353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1636e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uint32_t GetQuickOatCodeSize(ArtMethod* m)
163790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1638ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1639ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1640f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1641f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1642f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1643f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1644f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1645e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCodeEnd(ArtMethod* m)
164690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1647ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
16482ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
16492ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1650f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1651ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1652f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1653f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
165490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
16552ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
16562ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
16573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
165878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
165978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
166078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1661916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1662916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1663916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1664916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1665916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1666916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
16678f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& os = state->vios_.Stream();
16688f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
16692dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1670d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
16712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
16722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1673d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
16742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
16752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
16762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1677d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
16782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
167968b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
168078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
16812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
168278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
16838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&state->vios_);
16848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    DumpFields(os, obj, obj_class);
16850b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const size_t image_pointer_size = state->image_header_.GetPointerSize();
1686d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
1687e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto* obj_array = obj->AsObjectArray<mirror::Object>();
1688e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
16892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1690d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1691d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1692d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1693d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1694d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1695d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1696d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1697d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1698d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
16998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d: ", i);
170078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
17018f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d to %zd: ", i, i + run);
1702d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
170378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
17042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
17052ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
17068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        PrettyObjectValue(os, value_class, value);
1707d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1708d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1709c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      mirror::Class* klass = obj->AsClass();
171054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      if (klass->NumStaticFields() != 0) {
17118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        os << "STATICS:\n";
17128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(&state->vios_);
171354d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier        for (ArtField& field : klass->GetSFields()) {
171454d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier          PrintField(os, &field, field.GetDeclaringClass());
1715d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1716d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1717e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
171805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      auto it = state->dex_caches_.find(obj);
171905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      if (it != state->dex_caches_.end()) {
172005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        auto* dex_cache = down_cast<mirror::DexCache*>(obj);
1721e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& field_section = state->image_header_.GetImageSection(
1722e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            ImageHeader::kSectionArtFields);
1723e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& method_section = state->image_header_.GetMethodsSection();
172405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        size_t num_methods = dex_cache->NumResolvedMethods();
172505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        if (num_methods != 0u) {
172605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          os << "Methods (size=" << num_methods << "):";
172705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          ScopedIndentation indent2(&state->vios_);
172805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          auto* resolved_methods = dex_cache->GetResolvedMethods();
172905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
17300b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
17310b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                             i,
17320b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                             image_pointer_size);
173305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            size_t run = 0;
173405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            for (size_t j = i + 1;
173505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
173605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           j,
173705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           image_pointer_size);
173805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                ++j, ++run) {}
173905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (run == 0) {
174005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd: ", i);
174105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
174205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd to %zd: ", i, i + run);
174305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              i = i + run;
174405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
174505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            std::string msg;
174605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (elem == nullptr) {
174705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "null";
174805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else if (method_section.Contains(
174905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
175005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
175105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
175205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "<not in method section>";
175305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
175405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            os << StringPrintf("%p   %s\n", elem, msg.c_str());
1755e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
175605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        }
175705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        size_t num_fields = dex_cache->NumResolvedFields();
175805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        if (num_fields != 0u) {
175905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          os << "Fields (size=" << num_fields << "):";
176005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          ScopedIndentation indent2(&state->vios_);
176105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          auto* resolved_fields = dex_cache->GetResolvedFields();
176205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
176305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
176405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            size_t run = 0;
176505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            for (size_t j = i + 1;
176605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
176705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           j,
176805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           image_pointer_size);
176905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                ++j, ++run) {}
177005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (run == 0) {
177105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd: ", i);
177205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
177305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd to %zd: ", i, i + run);
177405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              i = i + run;
177505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
177605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            std::string msg;
177705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (elem == nullptr) {
177805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "null";
177905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else if (field_section.Contains(
178005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
178105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = PrettyField(reinterpret_cast<ArtField*>(elem));
178205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
178305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "<not in field section>";
178405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
178505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            os << StringPrintf("%p   %s\n", elem, msg.c_str());
1786e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
17873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1788e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1789e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
1790e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::string temp;
1791e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
1792e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  }
17933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17940b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  void DumpMethod(ArtMethod* method, std::ostream& indent_os)
179590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1796e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    DCHECK(method != nullptr);
17970b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
17980b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
1799cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier    const size_t pointer_size = image_header_.GetPointerSize();
1800524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray    OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1801524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray        reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
1802e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    if (method->IsNative()) {
1803e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
18040b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
18050b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1806e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
18070b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        stats_.native_to_managed_code_bytes += quick_oat_code_size;
1808e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
18090b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
18100b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier          image_header_.GetPointerSize())) {
18116bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray        indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
1812e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1813cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier    } else if (method->IsAbstract() || method->IsClassInitializer()) {
1814796d63050a18f263b93ea34951a61deaecab3422Nicolas Geoffray      // Don't print information for these.
1815cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier    } else if (method->IsRuntimeMethod()) {
1816cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier      ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
1817cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier      if (table != nullptr) {
1818cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier        indent_os << "IMT conflict table " << table << " method: ";
1819cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier        for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
1820cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier          indent_os << PrettyMethod(table->GetImplementationMethod(i, pointer_size)) << " ";
1821cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier        }
1822cdca476bf3394ce9d97a369e84e701b427009318Mathieu Chartier      }
1823e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
1824e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const DexFile::CodeItem* code_item = method->GetCodeItem();
1825e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
18260b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      stats_.dex_instruction_bytes += dex_instruction_bytes;
1827e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1828e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
18296d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain      size_t vmap_table_bytes = 0u;
1830524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray      if (!method_header->IsOptimized()) {
18316d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        // Method compiled with the optimizing compiler have no vmap table.
18320b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
18336d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        if (first_occurrence) {
18340b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier          stats_.vmap_table_bytes += vmap_table_bytes;
18356d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        }
1836e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1837e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
18380b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
18390b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1840e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
18410b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        stats_.managed_code_bytes += quick_oat_code_size;
1842e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        if (method->IsConstructor()) {
1843e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          if (method->IsStatic()) {
18440b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier            stats_.class_initializer_code_bytes += quick_oat_code_size;
1845e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
18460b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier            stats_.large_initializer_code_bytes += quick_oat_code_size;
18470d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
1848e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
18490b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier          stats_.large_method_code_bytes += quick_oat_code_size;
18503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1851e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
18520b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
18533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18547617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      uint32_t method_access_flags = method->GetAccessFlags();
18557617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin
1856e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
18579d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko      indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
18589d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko                                dex_instruction_bytes,
18599d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko                                vmap_table_bytes,
18607617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                method_access_flags);
18613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18629d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko      size_t total_size = dex_instruction_bytes +
18630b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier          vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
18643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1865e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      double expansion =
1866e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
18670b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      stats_.ComputeOutliers(total_size, expansion, method);
186878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
186978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
187027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
18713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
18723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
18733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
1874a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
18753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
1876a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
18773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
18783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
1879a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
18803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
188227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
1883916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1884916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
1885916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
18863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
1887916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
1888916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1889916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
1890916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
1891e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_field_bytes;
1892e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_method_bytes;
189305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    size_t dex_cache_arrays_bytes;
1894d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    size_t interned_strings_bytes;
1895208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    size_t class_table_bytes;
189632327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
1897916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
1898916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1899916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
19003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
1901916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
1902916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
19030d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
19040d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
19050d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
1906916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
19073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
1908916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1909916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
1910916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1911e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::vector<ArtMethod*> method_outlier;
19123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
19133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
1914700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
19153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19163887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain    Stats()
19173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
19183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
1919916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
1920916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
1921e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_field_bytes(0),
1922e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_method_bytes(0),
192305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          dex_cache_arrays_bytes(0),
1924d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier          interned_strings_bytes(0),
1925208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier          class_table_bytes(0),
192632327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
1927916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
1928916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
19293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
1930916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
1931916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
19320d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
19330d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
19340d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
19353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
1936916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
1937916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1938a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
1939277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
1940a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
1941a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
1942a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
1943a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1944a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
1945a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
1946277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
1947a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1948a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
1949277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
1950a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
1951a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
1952277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
1953a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
1954a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
1955916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
19563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
19573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
19583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1960916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
1961916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1962916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1963916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1964916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
1965916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1966916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1967916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1968e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
19693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
19703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
19713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
19723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
197400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
197590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier        SHARED_REQUIRES(Locks::mutator_lock_) {
19763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
19773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
19783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
19793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
19803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
1981dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      if (n == 0) {
1982dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao        return;
1983dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      }
19843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
19853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
19863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
19873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
19883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
19893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
19903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
19913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
19933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
19943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
19953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
19963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
19973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
19993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
20003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
20013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
20023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
20033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
20043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
20053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
20063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
20073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
20083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
20093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
20103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
20113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
20123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
20133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
20143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
20153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
20163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
20183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
2019c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
20203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
20213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
2023c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
20243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
20253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
20263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
20273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
20283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
20293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
20303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
20313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
20322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2033c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
20343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2035c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
20363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
20383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
20393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
20403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
20413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
20423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
20433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
20443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
20453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
20463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
20473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
20483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
20493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
20503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
20513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
20523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
20533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
20543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
20553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
20573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
20583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
2059c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
20603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
20613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2063c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
20643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
20653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
20663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
20673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
20683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
20693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
20703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
20713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
20722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2073c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
20743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2075c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
20763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    void Dump(std::ostream& os, std::ostream& indent_os)
207990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier        SHARED_REQUIRES(Locks::mutator_lock_) {
20802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
20812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
20822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
208305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        indent_os << StringPrintf("header_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
208405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "object_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
208505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "art_field_bytes        =  %8zd (%2.0f%% of art file bytes)\n"
208605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "art_method_bytes       =  %8zd (%2.0f%% of art file bytes)\n"
208705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "dex_cache_arrays_bytes =  %8zd (%2.0f%% of art file bytes)\n"
208805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "interned_string_bytes  =  %8zd (%2.0f%% of art file bytes)\n"
2089208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                                  "class_table_bytes      =  %8zd (%2.0f%% of art file bytes)\n"
209005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "bitmap_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
209105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "alignment_bytes        =  %8zd (%2.0f%% of art file bytes)\n\n",
20922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
20932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
2094e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_field_bytes, PercentOfFileBytes(art_field_bytes),
2095e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_method_bytes, PercentOfFileBytes(art_method_bytes),
209605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  dex_cache_arrays_bytes,
209705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  PercentOfFileBytes(dex_cache_arrays_bytes),
2098d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  interned_strings_bytes,
2099d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  PercentOfFileBytes(interned_strings_bytes),
2100208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                                  class_table_bytes, PercentOfFileBytes(class_table_bytes),
210132327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
21022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
21032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
2104208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier        CHECK_EQ(file_bytes,
2105208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2106208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2107208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 bitmap_bytes + alignment_bytes);
21082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2109916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2111916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
211202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
211302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
211402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
211502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
211602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
21172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2118a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
211902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
212002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
212102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2122916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2123c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2124916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2125916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
21272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
21282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
21292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
21302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
21312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
21322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
213305f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
21342ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
21352ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
21362ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
21372ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
21382ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
21392ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
21402ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
21412ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
21422ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
21432ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
21442ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
21452ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
21462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
214702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
21482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
214902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
215002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
215105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
215205f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
21539d07e3d128ccfa0ef7670feadd424a825e447d1dVladimir Marko      os << "\n" << StringPrintf("vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
215405f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2155c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2156916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
21582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
21592ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
21602ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2161c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2162cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2163c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
21643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2166916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2167916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2168916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2169916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
21700d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
21710d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
21720d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
21730d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
21740d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
21750d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
21760d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
21770d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
21788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
21798f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // For performance, use the *os_ directly for anything that doesn't need indentation
21808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // and prepare an indentation stream with default indentation 1.
21812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
21828f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  VariableIndentationOutputStream vios_;
21838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  ScopedIndentation indent1_;
21848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
21851d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
21863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
21872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
2188df2bb1f36c0d755f0bdb2b1562e52669f3de4a09Andreas Gampe  OatDumperOptions* oat_dumper_options_;
218905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko  std::set<mirror::Object*> dex_caches_;
2190d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
21913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
219278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
219378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
21940b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartierstatic int DumpImage(gc::space::ImageSpace* image_space,
21950b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                     OatDumperOptions* options,
21960b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                     std::ostream* os) SHARED_REQUIRES(Locks::mutator_lock_) {
21970b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const ImageHeader& image_header = image_space->GetImageHeader();
21980b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  if (!image_header.IsValid()) {
21990b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
22000b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    return EXIT_FAILURE;
22010b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  }
22020b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  ImageDumper image_dumper(os, *image_space, image_header, options);
22030b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  if (!image_dumper.Dump()) {
22040b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    return EXIT_FAILURE;
22050b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  }
22060b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  return EXIT_SUCCESS;
22070b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier}
22080b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier
22090b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartierstatic int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
221000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
22119865bde5d822f56c4732214c2005dfcaa41f94cfMathieu Chartier  ScopedNullHandle<mirror::ClassLoader> null_class_loader;
221200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
221300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
221400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
22150b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  if (options->app_image_ != nullptr) {
22160b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (options->app_oat_ == nullptr) {
22170b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      LOG(ERROR) << "Can not dump app image without app oat file";
2218dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      return EXIT_FAILURE;
2219dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    }
22200b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
22210b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
22220b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // pointers into 32 bit pointer sized ArtMethods.
22230b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    std::string error_msg;
22240b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
22250b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    options->app_oat_,
22260b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    nullptr,
22270b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    nullptr,
22280b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    false,
22290b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    /*low_4gb*/true,
22300b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    nullptr,
22310b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                                    &error_msg));
22320b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (oat_file == nullptr) {
22330b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
2234dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      return EXIT_FAILURE;
2235dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    }
22360b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    std::unique_ptr<gc::space::ImageSpace> space(
22370b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
22380b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (space == nullptr) {
22390b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
22400b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                 << error_msg;
22410b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
22420b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // Open dex files for the image.
22430b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    std::vector<std::unique_ptr<const DexFile>> dex_files;
22440b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
22450b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
22460b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                 << error_msg;
22470b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
22480b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // Dump the actual image.
22490b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    int result = DumpImage(space.get(), options, os);
22500b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (result != EXIT_SUCCESS) {
22510b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      return result;
22520b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
22530b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    // Fall through to dump the boot images.
22540b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  }
22550b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier
22560b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  gc::Heap* heap = runtime->GetHeap();
22570b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  CHECK(heap->HasBootImageSpace()) << "No image spaces";
22580b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
22590b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    int result = DumpImage(image_space, options, os);
22600b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    if (result != EXIT_SUCCESS) {
22610b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      return result;
22620b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    }
2263dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
2264dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return EXIT_SUCCESS;
226578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
226678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
226700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
226800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
226900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
227000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
227100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
227200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
227300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
227400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
227500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
227600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
227700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
227800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
2279d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier  runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
2280ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  std::vector<const DexFile*> class_path;
228100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
228200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2283ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
228400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
2285c9dbb1df3b5c06ba122cacaf35b17cb53c6be3c6Mathieu Chartier    class_linker->RegisterDexFile(*dex_file, nullptr);
2286ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    class_path.push_back(dex_file);
228700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
228800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
228900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
229000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2291966878d987cec1940fdfa8633fc79f8112320821Mathieu Chartier  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
229200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
229300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
229400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
229500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
229600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
229700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
229800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
229932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
230000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
230100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
230200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
230300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
230400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
230532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
230600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
23079865bde5d822f56c4732214c2005dfcaa41f94cfMathieu Chartier  ScopedNullHandle<mirror::ClassLoader> null_class_loader;
230800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
230900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
231032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
231100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
231200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
231300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
231400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
231500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
231600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
231700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
23180b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  OatFile* oat_file = OatFile::Open(oat_filename,
23190b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    oat_filename,
23200b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
23210b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
23220b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    false,
23230b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    /*low_4gb*/false,
23240b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
23250b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    &error_msg);
232600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
232700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
232800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
232900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
233000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
233100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
233200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
233300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
233400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
233500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
233600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
233700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23382fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbeckystatic int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
233900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
23400b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  OatFile* oat_file = OatFile::Open(oat_filename,
23410b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    oat_filename,
23420b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
23430b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
23440b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    false,
23450b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    /*low_4gb*/false,
23460b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    nullptr,
23470b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier                                    &error_msg);
234800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
234900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
235000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
235100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
235200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23532d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  bool result;
23542d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
23552d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  // files for 64-bit code in the past.
23562d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
23572fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
23582d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    result = oat_symbolizer.Symbolize();
23592d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  } else {
23602fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
23612d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    result = oat_symbolizer.Symbolize();
23622d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  }
23632d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  if (!result) {
236400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
236500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
236600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
236700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
236800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
236900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
237000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23713774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
23723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
23733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
237400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
23763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
23773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
23783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
23793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
23803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
23813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
238200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
238300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
23853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
23863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
23873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
23883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
23893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
2390f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else if (option =="--dump:code_info_stack_maps") {
2391f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_ = true;
23923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
23933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
2394c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil    } else if (option =="--header-only") {
2395c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      dump_header_only_ = true;
23963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
23973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
23983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
23992fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else if (option.starts_with("--only-keep-debug")) {
24002fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      only_keep_debug_ = true;
240132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
240232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
24033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
24043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
240532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
240632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
240732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
240832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
240932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
241032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
241132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
241232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
241332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
241432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
241532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
24160b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    } else if (option.starts_with("--app-image=")) {
24170b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      app_image_ = option.substr(strlen("--app-image=")).data();
24180b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    } else if (option.starts_with("--app-oat=")) {
24190b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier      app_oat_ = option.substr(strlen("--app-oat=")).data();
24203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
24213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
242200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
242300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
24253774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
24263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
24283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
24293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
24303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
243100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
243200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
24343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
24353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
24363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
243700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
243800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
24403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
24413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
24423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
24433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
24443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
24453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
24463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
24473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
24493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
24503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24513774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
24523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
24533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
24553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
24563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
24573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
24583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
24593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
24603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
24613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
24623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
24633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
24643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
24650b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "\n"
24660b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "  --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
24670b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        " boot image and app oat file.\n"
24680b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "      Example: --app-image=app.art\n"
24690b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "\n"
24700b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "  --app-oat=<file.odex>: specifies an input app oat.\n"
24710b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        "      Example: --app-oat=app.odex\n"
24723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
24733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
24753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
24773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
24783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
24793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2480f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2481f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "      Example: --dump:code_info_stack_maps\n"
2482f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "\n"
24833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
24843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
24853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2486c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "  --header-only may be used to print only the oat header.\n"
2487c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "      Example: --header-only\n"
2488c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "\n"
248932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
249032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
249132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
249232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
249332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
249432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
249532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
249632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
249732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
249832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
249932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
25002fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "  --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
25012fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "      .rodata and .text sections are omitted in the output file to save space.\n"
25022fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "      Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
25032fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "\n"
250432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
250532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
250632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
25073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
25083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
250932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
251032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
251132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
251232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
251332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
251432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
251532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
25163774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
25173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25183774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
251900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
252000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
252200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
252332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
25243fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
252500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
252600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
252700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
2528f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  bool dump_code_info_stack_maps_ = false;
252900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
253000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
25312fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  bool only_keep_debug_ = false;
253232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
253332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
2534c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil  bool dump_header_only_ = false;
253532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
253632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
25370b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const char* app_image_ = nullptr;
25380b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier  const char* app_oat_ = nullptr;
253900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
254000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25413774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
25423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
25433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
254400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
25463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
25473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25480b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    oat_dumper_options_.reset(new OatDumperOptions(
25493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
2550f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        args_->dump_code_info_stack_maps_,
25513774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
25523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
255332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
255432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
255532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
255632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
2557c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        args_->dump_header_only_,
255832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
25590b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        args_->app_image_,
25600b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier        args_->app_oat_,
256132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
25623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
25643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
256500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
256600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
25683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2569c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
25703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2571d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
25723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2573c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
25742fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // ELF has special kind of section called SHT_NOBITS which allows us to create
25752fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // sections which exist but their data is omitted from the ELF file to save space.
25762fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // This is what "strip --only-keep-debug" does when it creates separate ELF file
25772fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // with only debug data. We use it in similar way to exclude .rodata and .text.
25782fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      bool no_bits = args_->only_keep_debug_;
25792fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
2580c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2581c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2582c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
258332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2584c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2585c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
258600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
258700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
25893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
25903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
25923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
25933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
259432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
25953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
259600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
259700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25980b4cbd0c2a75b47ae09d21e5d73d2b1709cb5b9eMathieu Chartier    return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
259900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
260000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
26023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
260300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26047934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
260578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
260678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
26073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
26083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
260978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2610