oatdump.cc revision f9c6fc610b27887f832e453a0da1789187293408
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"
31761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h"
3278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include "class_linker.h"
332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h"
344f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
35e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes#include "dex_instruction.h"
363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers#include "disassembler.h"
3754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include "elf_builder.h"
382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "gc_map.h"
391d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h"
401d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/large_object_space.h"
411d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
4278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include "image.h"
432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "indenter.h"
441809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "mapping_table.h"
452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/array-inl.h"
462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
4705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko#include "mirror/dex_cache-inl.h"
482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
50700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "oat.h"
518a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko#include "oat_file-inl.h"
52f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "oat_file_manager.h"
53e5448b5a12003b405b22cde3b94f962ab4888a87Elliott Hughes#include "os.h"
5454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include "output_stream.h"
55a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
5600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
5700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "ScopedLocalRef.h"
58c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier#include "thread_list.h"
59ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "verifier/dex_gc_map.h"
602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "verifier/method_verifier.h"
611809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "vmap_table.h"
6200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "well_known_classes.h"
6378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <sys/stat.h>
653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include "cmdline.h"
6678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
673774335b08076117d6950cd472cdd59a167470b5Igor Murashkinnamespace art {
6878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
69e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_methods_descriptions_[] = {
701984651929744dd603fd082e23eacd877b9bc177Ian Rogers  "kResolutionMethod",
7188474b416eb257078e590bf9bc7957cee604a186Jeff Hao  "kImtConflictMethod",
722d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier  "kImtUnimplementedMethod",
73e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  "kCalleeSaveMethod",
74aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsOnlySaveMethod",
75aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsAndArgsSaveMethod",
76e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier};
77e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
78e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_roots_descriptions_[] = {
7958ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  "kDexCaches",
8034f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom  "kClassRoots",
8178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
8278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
83ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier// Map is so that we don't allocate multiple dex files for the same OatDexFile.
84ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartierstatic std::map<const OatFile::OatDexFile*,
85ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier                std::unique_ptr<const DexFile>> opened_dex_files;
86ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier
87ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartierconst DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
88ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  DCHECK(oat_dex_file != nullptr);
89ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  auto it = opened_dex_files.find(oat_dex_file);
90ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  if (it != opened_dex_files.end()) {
91ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    return it->second.get();
92ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  }
93ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
94ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
95ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  return ret;
96ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier}
97ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier
98bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbeckyclass OatSymbolizer FINAL {
9954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe public:
100bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  class RodataWriter FINAL : public CodeOutput {
101bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   public:
102bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
10354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
104bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool Write(OutputStream* out) OVERRIDE {
105bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
106bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      return out->WriteFully(oat_file_->Begin(), rodata_size);
107bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    }
10854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
109bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   private:
110bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    const OatFile* oat_file_;
111bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  };
11254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
113bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  class TextWriter FINAL : public CodeOutput {
114bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   public:
115bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
11654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
117bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool Write(OutputStream* out) OVERRIDE {
118bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
119bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
120bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      return out->WriteFully(text_begin, oat_file_->End() - text_begin);
12154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
12254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
123bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   private:
124bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    const OatFile* oat_file_;
125bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  };
126bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky
1273887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
128bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      oat_file_(oat_file), builder_(nullptr),
129bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
13054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
13154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
13354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
13454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const OatFile::OatMethod&,
13554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile&,
13654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
13754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile::CodeItem*,
13854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t);
13954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Symbolize() {
141bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
142bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
143bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t text_size = size - rodata_size;
144bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t bss_size = oat_file_->BssSize();
145bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    RodataWriter rodata_writer(oat_file_);
146bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    TextWriter text_writer(oat_file_);
147bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    builder_.reset(new ElfBuilder<ElfTypes32>(
148bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        oat_file_->GetOatHeader().GetInstructionSet(),
149bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        rodata_size, &rodata_writer,
150bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        text_size, &text_writer,
151bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        bss_size));
15254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
15354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::RegisterForDedup);
15454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
15554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    NormalizeState();
15654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
15754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::AddSymbol);
15854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
159bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
160bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool result = builder_->Write(elf_output);
16154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1624303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    // Ignore I/O errors.
163bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    UNUSED(elf_output->FlushClose());
16454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
16554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return result;
16654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
16754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
16854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void Walk(Callback callback) {
16954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
17054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t i = 0; i < oat_dex_files.size(); i++) {
17154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
1722cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      CHECK(oat_dex_file != nullptr);
17354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatDexFile(oat_dex_file, callback);
17454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
17554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
17654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
17854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::string error_msg;
179ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
180ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    if (dex_file == nullptr) {
18154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
18254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
18354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t class_def_index = 0;
18454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index < dex_file->NumClassDefs();
18554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index++) {
18654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
18754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
18854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      OatClassType type = oat_class.GetType();
18954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      switch (type) {
19054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassAllCompiled:
19154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassSomeCompiled:
192ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier          WalkOatClass(oat_class, *dex_file, class_def, callback);
19354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
19454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
19554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassNoneCompiled:
19654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassMax:
19754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          // Ignore.
19854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
19954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
20054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
20254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
20354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
20454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                    const DexFile::ClassDef& class_def, Callback callback) {
20513735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
20654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
20754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
20854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    // Note: even if this is an interface or a native class, we still have to walk it, as there
21054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    //       might be a static initializer.
21154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    ClassDataItemIterator it(dex_file, class_data);
21254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    SkipAllFields(&it);
21354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t class_method_idx = 0;
21454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextDirectMethod()) {
21554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
21654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
2175182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
21854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
21954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
22054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
22154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextVirtualMethod()) {
22254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
22354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
2245182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
22554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
22654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
22754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
22854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DCHECK(!it.HasNext());
22954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
23054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
23154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
23254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     const OatFile::OatMethod& oat_method, const DexFile& dex_file,
23354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
23454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t method_access_flags, Callback callback) {
23554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if ((method_access_flags & kAccAbstract) != 0) {
23654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // Abstract method, no code.
23754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
23854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
23954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (oat_method.GetCodeOffset() == 0) {
24054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // No code.
24154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
24254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
24354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
24454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
24554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                      method_access_flags);
24654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
24754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2486a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2496a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t class_method_index ATTRIBUTE_UNUSED,
2506a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const OatFile::OatMethod& oat_method,
2516a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile& dex_file ATTRIBUTE_UNUSED,
2526a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t dex_method_idx ATTRIBUTE_UNUSED,
2536a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2546a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t method_access_flags ATTRIBUTE_UNUSED) {
25554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[oat_method.GetCodeOffset()]++;
25654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
25754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
25854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void NormalizeState() {
25954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (auto& x : state_) {
26054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (x.second == 1) {
26154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        state_[x.first] = 0;
26254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
26354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
26454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
26554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
26654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  enum class DedupState {  // private
26754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kNotDeduplicated,
26854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedFirst,
26954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedOther
27054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  };
27154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  DedupState IsDuplicated(uint32_t offset) {
27254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 0) {
27354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kNotDeduplicated;
27454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
27554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 1) {
27654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kDeduplicatedOther;
27754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
27854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[offset] = 1;
27954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return DedupState::kDeduplicatedFirst;
28054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
28154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2826a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2836a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t class_method_index ATTRIBUTE_UNUSED,
2846a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const OatFile::OatMethod& oat_method,
2856a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile& dex_file,
2866a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t dex_method_idx,
2876a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2886a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
28954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
29054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dedup != DedupState::kDeduplicatedOther) {
29154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
29254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
29354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (dedup == DedupState::kDeduplicatedFirst) {
29454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        pretty_name = "[Dedup]" + pretty_name;
29554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
29654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
297bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      auto* symtab = builder_->GetSymtab();
29854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
299bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      symtab->AddSymbol(pretty_name, builder_->GetText(),
3000279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
3010279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
30254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
30354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
30454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
30554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe private:
30654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  static void SkipAllFields(ClassDataItemIterator* it) {
30754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextStaticField()) {
30854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
30954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
31054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextInstanceField()) {
31154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
31254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
31354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
31454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
31554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  const OatFile* oat_file_;
316533c207f9d2da6d913c4b10f6f757fe9d6367b10David Srbecky  std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
31754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  std::unordered_map<uint32_t, uint32_t> state_;
3180279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  const std::string output_name_;
31954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe};
32054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass OatDumperOptions {
3222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
3232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  OatDumperOptions(bool dump_raw_mapping_table,
3242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_raw_gc_map,
3252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_vmap,
326f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   bool dump_code_info_stack_maps,
3272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
32800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
32932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
33032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
33132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
33232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
33332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
33432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
3352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    : dump_raw_mapping_table_(dump_raw_mapping_table),
3362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_raw_gc_map_(dump_raw_gc_map),
3372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_vmap_(dump_vmap),
338f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_(dump_code_info_stack_maps),
3392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
34000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
34132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
3423fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
34332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
34432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
34532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
34632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
3473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
3482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
3492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_mapping_table_;
3502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_gc_map_;
3512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
352f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  const bool dump_code_info_stack_maps_;
3532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
3542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
35532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
3563fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
35732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
35832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
35932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
36032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
36100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
3622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
3632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
364e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
365aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
3663887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
3679583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
368a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
3692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
37032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
371a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
372a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
37332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
374a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
375eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                                                 true /* can_read_literals_ */))) {
37632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
37732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
37832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
3793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
3803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
3813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
3822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
3832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
3842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
3852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
386a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
387a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
388a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
389a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
3902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
3912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
3923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
393aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
394aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
395aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
396aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
397aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
398ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
399aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
400a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
401a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
402a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
4036f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
4046f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
4056f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
4066f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
4076f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
4086f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
4096f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
4107020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
411aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
412aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
413aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4142ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
4152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
4162ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
41732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
4182ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
4192ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
4202ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
4212ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
4222ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
4232ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
4242ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
4252ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
4262ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
4272ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
4282ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
4292ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
4302ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
4312ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
4322ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4332ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4342ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
4352ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
4362ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
4372ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
438aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
4402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
4412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
4422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
443a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
44428db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
44528db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
44628db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
44728db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
448700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
44981f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
45022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
45122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
45222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
45322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
45422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
45522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
45622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
45722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
45822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
45922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
46022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
46122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
46281f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
46332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
4642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
4652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
4662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
4682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
4692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
470aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
4722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
473aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
474aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
475aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
47632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
47732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
47832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
47932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
48032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
48132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
48232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
4833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
4852ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
48632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
48732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // If file export selected skip file analysis
48832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.export_dex_location_) {
48932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!ExportDexFile(os, *oat_dex_file)) {
49032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
49132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
49232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
49332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!DumpOatDexFile(os, *oat_dex_file)) {
49432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
49532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
4962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
4973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
4982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
4992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
5003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
5013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
5023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
50313735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
50413735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
5053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
5063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
507ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
508ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
509ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
5103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
511ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
5123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
5133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
514e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
515a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
516f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
517f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
518f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
51990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
5203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5228d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
5238d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
524ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
525ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (dex_file == nullptr) {
5268d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5278d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5288d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
529e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5308b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
531e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
5322ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
5338b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
534d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
5353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
536d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
5373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
5383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
539aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5402ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
541aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
542aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
543aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
5443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
545e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
546e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
547e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
548e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
5493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5512ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
5528d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
553ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
554ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (dex_file == nullptr) {
5558d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5568d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
558e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
559ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
5602ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
5612ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
5622ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
563e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
564d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
56513735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
5662ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
567e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
568e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
569e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
570e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
571d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
572e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
573e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
574e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
575d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
576e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
577e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
578e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
579e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
580e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
581e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
582e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
583e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
584e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
585ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
586e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
587e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
5892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
5902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
5912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
592e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
59395ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
59495ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
59595ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
59695ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
59795ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
598e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetMappingTableOffset());
599e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
600957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    offsets_.insert(oat_method.GetGcMapOffset());
601e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
602e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
6032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
6042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
60532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
6062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
607a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
608ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
609590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
610590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // Create the verifier early.
611590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
6128d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
613ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
614ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    if (dex_file == nullptr) {
6158d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
6162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
6172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
618aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
6198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
6208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    VariableIndentationOutputStream vios(&os);
6218f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&vios);
6222ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
6232ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
6242ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
625aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
626aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
62732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
62832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
62932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
63032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
63132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
63232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
6332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
634d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
6352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
6362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
637d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
638d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
639d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
64032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
641ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
6422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
6432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
64432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
64532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
64632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
64732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
648aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
649aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
650aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
6512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
652aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
653aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
65432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
65532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
65632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
65732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
658ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
65932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
66032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
66132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
66432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
66632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
66732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
66832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
67032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
67232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
67432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
67532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
67632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
67732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
67932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
68032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
68132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
68332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
68432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
68532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
68632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
68732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
68832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
68932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
69132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
69232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
69332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
69432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
70132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
70232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
70432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
70532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
70632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
71032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
71132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
71232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
71332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
71432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
71532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
71632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
71732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
71832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
71932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
72032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
72132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
72232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
72332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
72432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
72532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
72632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
72732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
72832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
72932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
73032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
73132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
732e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
7330571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
7340571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
735aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7360571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
7370571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
738aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
739e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
740aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
7418f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatClass(VariableIndentationOutputStream* vios,
7428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                    const OatFile::OatClass& oat_class, const DexFile& dex_file,
74332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
7442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
74532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
74613735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
7472ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
7488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << std::flush;
7492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
750e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
751e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
752e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
7532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
7540571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
7558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
7562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
75732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
76032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
76132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
76232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
76332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7650571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
766aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7670571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
7688f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
7692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
77032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
77332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
77432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
77532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
77632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7780571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
779aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7800571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
7818f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
7822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
783aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
784e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
7852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
7862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
7882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
7892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7908f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatMethod(VariableIndentationOutputStream* vios,
7918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                     const DexFile::ClassDef& class_def,
7928b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
7932cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
7942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
79532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
7962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
80032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
8013fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
8023fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
8033fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
80432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
8058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
8068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   class_method_index, pretty_method.c_str(),
8078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   dex_method_idx);
80832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
80932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
81032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
81132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
81232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
81332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
81432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
81532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
81632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
81732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
81832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
81932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
82032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
82132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
82232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    // Everything below is indented at least once.
8248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(vios);
8258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
8262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8278f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "DEX CODE:\n";
8288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpDexCode(vios->Stream(), dex_file, code_item);
8302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8312ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
832673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier    std::unique_ptr<StackHandleScope<1>> hs;
8332ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
8342ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
835673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      // We need to have the handle scope stay live until after the verifier since the verifier has
836673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      // a handle to the dex cache from hs.
837673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      hs.reset(new StackHandleScope<1>(Thread::Current()));
8388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
8398f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
840673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      verifier.reset(DumpVerifier(vios, hs.get(),
8418f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                  dex_method_idx, &dex_file, class_def, code_item,
8422ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
8432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatMethodOffsets ";
84632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8478f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method_offsets);
8482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
8502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
8518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
8522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
8532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
8542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
8558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
8562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
858a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
8598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8608f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
8612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
8622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
8638f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code offset 0x%08x is past end of file 0x%08zx.\n",
8658f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       aligned_code_begin, oat_file_.Size());
8662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8688f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
8693946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
8708f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "gc_map: ";
87132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
8733946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
874957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
8758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
8762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (gc_map_offset > oat_file_.Size()) {
8778f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                           "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
8798f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                           gc_map_offset, oat_file_.Size());
8802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
88132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_gc_map_) {
8828f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent3(vios);
8838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpGcMap(vios->Stream(), oat_method, code_item);
8842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
8862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
8878f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatQuickMethodHeader ";
8882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
8892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
8902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
89132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", method_header);
8932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8948f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
8952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
8968f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
8972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
8982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
8992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
9008f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
9012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
9022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9048f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "mapping_table: ";
90632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
9082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
9108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
9112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (mapping_table_offset > oat_file_.Size()) {
9128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9138f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "mapping table offset 0x%08x is past end of file 0x%08zx. "
9148f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "mapping table offset was loaded from offset 0x%08x.\n",
9158f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       mapping_table_offset, oat_file_.Size(),
9168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetMappingTableOffsetOffset());
9172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
91832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_mapping_table_) {
9198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent3(vios);
9208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpMappingTable(vios, oat_method);
9212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "vmap_table: ";
92432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
9262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
9288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
9292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
9308f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset 0x%08x is past end of file 0x%08zx. "
9328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset was loaded from offset 0x%08x.\n",
9338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       vmap_table_offset, oat_file_.Size(),
9348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetVmapTableOffsetOffset());
9352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
93632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
9378f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapData(vios, oat_method, code_item);
9382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
9392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
9418f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "QuickMethodFrameInfo\n";
9428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
9438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream()
9458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
9468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
9478f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
9488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
9508f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
9518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // Based on spill masks from QuickMethodFrameInfo so placed
9558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // after it is dumped, but useful for understanding quick
9568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // code, so dumped here.
9578f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpVregLocations(vios->Stream(), oat_method, code_item);
9592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9618f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "CODE: ";
9622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
9632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
9648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
9658f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9668f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code size offset 0x%08x is past end of file 0x%08zx.",
9678f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset, oat_file_.Size());
9682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
9702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
9712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
9722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
973590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
97432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
9758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("%p ", code);
9762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9778f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
9788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_offset,
9798f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset,
9808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size,
9818f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code != nullptr ? "..." : "");
9822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
9842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
9858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("WARNING: "
9868f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         "start of code at 0x%08x is past end of file 0x%08zx.",
9878f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         aligned_code_begin, oat_file_.Size());
9882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
9892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
9908f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
9918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
9928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
9938f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
9948f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              aligned_code_end, oat_file_.Size(),
9958f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
9962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
99732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
10002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
10012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
10022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
10038f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
10048f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
10058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size %d is bigger than max expected threshold of %d. "
10068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
10078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, kMaxCodeSize,
10088f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
10092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
101032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
10112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
10128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
10132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
10142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
101532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
10168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
10172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
10182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
10192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
10208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
10212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
10223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
10243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
10253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
10263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
10273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
10293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
10303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
10313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
10323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
10333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
10353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
10373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
10383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
10393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
10413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
10433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
10453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1047442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
10488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpVmapData(VariableIndentationOutputStream* vios,
1049442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1050442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1051f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1052f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The optimizing compiler outputs its CodeInfo data in the vmap table.
1053442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1054442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1055442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1056442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
10578f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
10588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpCodeInfo(vios, code_info, oat_method, *code_item);
1059442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10600a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
10610a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // We don't encode the size in the table, so just emit that we have quickened
10620a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // information.
10630a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      ScopedIndentation indent(vios);
10640a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      vios->Stream() << "quickened data\n";
1065442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
1066442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // Otherwise, display the vmap table.
1067442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const uint8_t* raw_table = oat_method.GetVmapTable();
1068442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_table != nullptr) {
1069442b46a087c389a91a0b51547ac9205058432364Roland Levillain        VmapTable vmap_table(raw_table);
10708f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapTable(vios->Stream(), oat_method, vmap_table);
1071442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
107220d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1073442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1074442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1075442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
10768f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCodeInfo(VariableIndentationOutputStream* vios,
1077442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1078f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                    const OatFile::OatMethod& oat_method,
1079442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
10808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    code_info.Dump(vios,
1081f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   oat_method.GetCodeOffset(),
1082f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   code_item.registers_size_,
1083f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   options_.dump_code_info_stack_maps_);
1084442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1085442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1086442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a vmap table.
1087442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapTable(std::ostream& os,
1088442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const OatFile::OatMethod& oat_method,
1089442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const VmapTable& vmap_table) {
1090442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool first = true;
1091442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool processing_fp = false;
1092442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1093442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < vmap_table.Size(); i++) {
1094442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint16_t dex_reg = vmap_table[i];
1095442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1096442b46a087c389a91a0b51547ac9205058432364Roland Levillain                                                    processing_fp ? kFloatVReg : kIntVReg);
1097442b46a087c389a91a0b51547ac9205058432364Roland Levillain      os << (first ? "v" : ", v")  << dex_reg;
1098442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp) {
1099442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/r" << cpu_reg;
1100442b46a087c389a91a0b51547ac9205058432364Roland Levillain      } else {
1101442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/fr" << cpu_reg;
1102442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1103442b46a087c389a91a0b51547ac9205058432364Roland Levillain      first = false;
1104442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp && dex_reg == 0xFFFF) {
1105442b46a087c389a91a0b51547ac9205058432364Roland Levillain        processing_fp = true;
1106442b46a087c389a91a0b51547ac9205058432364Roland Levillain        spill_mask = oat_method.GetFpSpillMask();
1107442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
11083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1109442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "\n";
11103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
11113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1112faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1113faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1114faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1115faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1116faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1117faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1118faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1119faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1120faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1121faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1122faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1123faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1124faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1125faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1126faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1127faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1128faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1129faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1130faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
113115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
113215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
113315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
113415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
113515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
113615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
113715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1138faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1139faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1140faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1141faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1142faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1143faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1144faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1145faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1146faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1147faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1148faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1149faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1150faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1151faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1152faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1153faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1154b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
11552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
11561809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* raw_table = oat_method.GetVmapTable();
11572ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (raw_table != nullptr) {
1158b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      const VmapTable vmap_table(raw_table);
1159b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      uint32_t vmap_offset;
11601809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
11612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
11622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
11632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                       : oat_method.GetCoreSpillMask();
11642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1165b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      } else {
116615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
116715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
116815b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
116915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
117015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
117115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
117215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1173b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        os << "[sp + #" << offset << "]";
1174b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
1175b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1176b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1177b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1178ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1179ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          const DexFile::CodeItem* code_item,
1180ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          size_t num_regs, const uint8_t* reg_bitmap) {
1181ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    bool first = true;
1182ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t reg = 0; reg < num_regs; reg++) {
1183ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1184ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        if (first) {
1185ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << "  v" << reg << " (";
1186ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1187ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1188ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          first = false;
1189ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else {
1190ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ", v" << reg << " (";
1191ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1192ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1193ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        }
1194ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      }
1195ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1196ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (first) {
1197ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "No registers in GC map\n";
1198ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    } else {
1199ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "\n";
1200ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1201ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
1202b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1203b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers                 const DexFile::CodeItem* code_item) {
1204957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1205ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (gc_map_raw == nullptr) {
1206ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return;  // No GC map.
12073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1208ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1209956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    NativePcOffsetToReferenceMap map(gc_map_raw);
1210956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1211956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1212956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes          map.GetNativePcOffset(entry);
1213956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      os << StringPrintf("%p", native_pc);
1214956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
12153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1216aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
1217e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
12188f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpMappingTable(VariableIndentationOutputStream* vios,
12198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                        const OatFile::OatMethod& oat_method) {
1220ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1221ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (quick_code == nullptr) {
12222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      return;
12232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12241809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12251809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (table.TotalSize() != 0) {
12261809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.PcToDexSize() != 0) {
12271809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::PcToDexIterator It;
12288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "suspend point mappings {\n";
12291809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12308f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent1(vios);
12318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12321809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "}\n";
12341809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      }
12351809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.DexToPcSize() != 0) {
12361809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::DexToPcIterator It;
12378f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "catch entry mappings {\n";
12381809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12398f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent1(vios);
12408f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12411809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "}\n";
12433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
12443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
12463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12478f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
1248f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const OatFile::OatMethod& oat_method,
1249f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const DexFile::CodeItem* code_item,
1250f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   size_t offset,
1251f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   bool suspend_point_mapping) {
1252f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1253f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (suspend_point_mapping) {
12548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
12558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
1256f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1257f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The return value is not used in the case of a method compiled
1258f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // with the optimizing compiler.
1259f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      return DexFile::kDexNoIndex;
1260f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else {
12618f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
1262f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1263f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1264f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
12651809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12661809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
12671809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12681809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
12691809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
12701809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12711809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12724b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
12731809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
12741809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1275b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12761809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
12771809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
12781809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12791809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12804b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
12811809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1282b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1283b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1286b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1290957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
12912ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1292b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
12932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1294b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
12952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1296b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1297b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1298b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1299b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
13002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
13012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1302b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1303b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1304b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1305b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
13062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1307b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1308b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1309b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1310b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1311b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1312b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1313b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1314b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
13153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1316b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
13173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1318590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1319590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1320590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1321590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
13227b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
13232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
13242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
13252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
13262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
13272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
13282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
13292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
13302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
13312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
13322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
13342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
13352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
13362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
13372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
13402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
13412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
13432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
13472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
13482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
13502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
13512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
13542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1355b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
13562ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1357b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1358b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1359b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
136032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
136132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1362b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1363e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1364e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1365b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1366b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1367f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1368f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // the optimizing compiler?
1369f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1370f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                                    const DexFile::CodeItem* code_item) {
1371f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // If the native GC map is null and the Dex `code_item` is not
1372f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // null, then this method has been compiled with the optimizing
1373f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // compiler.
13740a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() != nullptr &&
13750a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetGcMap() == nullptr &&
13760a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
13770a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  }
13780a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray
13790a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
13800a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // the dextodex compiler?
13810a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
13820a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray                                                  const DexFile::CodeItem* code_item) {
13830a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // If the quick code is null, the Dex `code_item` is not
13840a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // null, and the vmap table is not null, then this method has been compiled
13850a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // with the dextodex compiler.
13860a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() == nullptr &&
13870a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetVmapTable() != nullptr &&
13880a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
1389f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1390f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
13918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
1392f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const OatFile::OatMethod& oat_method,
1393f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const DexFile::CodeItem* code_item,
1394f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  size_t offset) {
1395f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // This method is only relevant for oat methods compiled with the
1396f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // optimizing compiler.
1397f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1398f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
1399f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // The optimizing compiler outputs its CodeInfo data in the vmap table.
1400f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    const void* raw_code_info = oat_method.GetVmapTable();
1401f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (raw_code_info != nullptr) {
1402f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      CodeInfo code_info(raw_code_info);
1403f677ebfd832c9c614fea5e6735725fec2f7a3f2aDavid Brazdil      StackMapEncoding encoding = code_info.ExtractEncoding();
1404f677ebfd832c9c614fea5e6735725fec2f7a3f2aDavid Brazdil      StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
1405f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (stack_map.IsValid()) {
14068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
14078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                       code_item->registers_size_);
1408f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1409f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1410f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1411f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
14128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
1413673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier                                         StackHandleScope<1>* hs,
14148f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         uint32_t dex_method_idx,
14152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
14162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
14172ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
14182ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
14192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
14202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1421eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1422673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          hs->NewHandle(Runtime::Current()->GetClassLinker()->RegisterDexFile(*dex_file)));
142332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
1424e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      return verifier::MethodVerifier::VerifyMethodAndDump(
14258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
14268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          &class_def, code_item, nullptr, method_access_flags);
14272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
14282ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
14292ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
14302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
14312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
14328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCode(VariableIndentationOutputStream* vios,
14338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                verifier::MethodVerifier* verifier,
14342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
14352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1436ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1437ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
14382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
14392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
14402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1441956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
14428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "NO CODE!\n";
1443b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1444956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1445ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1446ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1447ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
14482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
14498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
14502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
14518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
14522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
14538f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          uint32_t dex_pc =
14548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
14552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
14568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko            DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
14572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
14588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
14592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1460ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
14612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
14622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1463b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1464b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1465b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
14663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
14672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
146832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
146932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1470a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1471ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
14722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1473aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1474aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
14753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
147627ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
14773887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
14783887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain              const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
14792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
14808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios_(os),
14818f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent1_(&vios_),
14822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
14832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
14842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
148527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
148690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
14872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
14888f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& indent_os = vios_.Stream();
14898f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
14902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
14912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
14922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
14932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1494e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1495e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1496e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1497e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto section = static_cast<ImageHeader::ImageSections>(i);
1498e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1499e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
150031e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
15012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
15022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1503700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
15042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1505700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1506700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1507700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1508700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1509700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
15102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1511a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1512a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
151346774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
151446774767fcf7780d1455e755729198648d08742eIgor Murashkin
15152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
15162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1517e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_roots_descriptions_) ==
1518e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
15192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
15202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
15212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
15222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
15238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
15242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
15252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1526fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
15278f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent2(&vios_);
1528277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1529277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1530fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1531277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1532277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1533fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1534fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1535fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1536fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1537fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1538fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
15398f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d: ", j);
1540fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
15418f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d to %zd: ", j, j + run);
1542277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1543fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
15442ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
15458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              PrettyObjectValue(indent_os, value->GetClass(), value);
15462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
15478f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << j << ": null\n";
15482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1549d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
155034f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
155134f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
15528f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    }
1553e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
15548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    {
1555e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "METHOD ROOTS\n";
1556e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_methods_descriptions_) ==
1557e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1558e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1559e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1560e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const char* description = image_methods_descriptions_[i];
1561e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto* image_method = image_header_.GetImageMethod(image_root);
15628f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", description, image_method);
1563e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
156427ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
15652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
156627ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1567f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    Runtime* const runtime = Runtime::Current();
1568f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    ClassLinker* class_linker = runtime->GetClassLinker();
15692ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
15702ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
15712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
15722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
15738d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
1574f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1575f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        oat_location);
1576a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
1577e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler      oat_file = OatFile::Open(oat_location, oat_location,
1578e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               nullptr, nullptr, false, nullptr,
1579b1d8c314b55bb2df2b2bb72a3daaf5db65b7ebc7Igor Murashkin                               &error_msg);
1580a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (oat_file == nullptr) {
1581a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        os << "NOT FOUND: " << error_msg << "\n";
15822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
1583a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
1584aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
15852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1586aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
15873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
158878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
158932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
15903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
159102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
15922ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
159302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
159402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
159505f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
159605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
15972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1598b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1599b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    // Loop through all the image spaces and dump their objects.
1600f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    gc::Heap* heap = runtime->GetHeap();
16011d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
160250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1603357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1604c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1605c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1606c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1607c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
160890d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
160990d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
161090d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1611f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier      ScopedThreadSuspension sts(self, kNative);
16124f55e22630d99ca0edd9e951e5ee96b57bb9b980Mathieu Chartier      ScopedSuspendAll ssa(__FUNCTION__);
1613f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier      heap->RevokeAllThreadLocalAllocationStacks(self);
1614357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
16152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
1616e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      // Mark dex caches.
161705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      dex_caches_.clear();
1618e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      {
1619e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        ReaderMutexLock mu(self, *class_linker->DexLock());
1620673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier        for (jobject weak_root : class_linker->GetDexCaches()) {
1621673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          mirror::DexCache* dex_cache =
1622673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier              down_cast<mirror::DexCache*>(self->DecodeJObject(weak_root));
1623673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          if (dex_cache != nullptr) {
162405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            dex_caches_.insert(dex_cache);
1625673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          }
1626e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        }
1627e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
16282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
162902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& space : spaces) {
16302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (space->IsImageSpace()) {
1631e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          auto* image_space = space->AsImageSpace();
1632e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          // Dump the normal objects before ArtMethods.
16332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
16342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << "\n";
1635e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          // TODO: Dump fields.
1636e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          // Dump methods after.
1637e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          const auto& methods_section = image_header_.GetMethodsSection();
163854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier          const size_t pointer_size =
1639e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier              InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
164054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier          DumpArtMethodVisitor visitor(this);
1641cf36d493124d8048efa0bd6f67d817ce3cd6b725Vladimir Marko          methods_section.VisitPackedArtMethods(&visitor, image_space->Begin(), pointer_size);
16422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
16432fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier      }
16442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1645bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
16462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
1647b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
16482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1649700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
16502ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() == nullptr) {
16512ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
16526f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
16532ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() != nullptr) {
16542ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
16556f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
16563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
1657e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1658e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1659e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& method_section = image_header_.GetMethodsSection();
166005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    const auto& dex_cache_arrays_section = image_header_.GetImageSection(
166105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        ImageHeader::kSectionDexCacheArrays);
1662d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    const auto& intern_section = image_header_.GetImageSection(
1663d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier        ImageHeader::kSectionInternedStrings);
16643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
1665cf36d493124d8048efa0bd6f67d817ce3cd6b725Vladimir Marko    stats_.alignment_bytes += RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1666cf36d493124d8048efa0bd6f67d817ce3cd6b725Vladimir Marko    // Add padding between the field and method section.
1667cf36d493124d8048efa0bd6f67d817ce3cd6b725Vladimir Marko    // (Field section is 4-byte aligned, method section is 8-byte aligned on 64-bit targets.)
166805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    stats_.alignment_bytes += method_section.Offset() -
166905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        (field_section.Offset() + field_section.Size());
167005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    // Add padding between the dex cache arrays section and the intern table. (Dex cache
167105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    // arrays section is 4-byte aligned on 32-bit targets, intern table is 8-byte aligned.)
167205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    stats_.alignment_bytes += intern_section.Offset() -
167305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        (dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size());
1674e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1675e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.bitmap_bytes += bitmap_section.Size();
1676e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.art_field_bytes += field_section.Size();
1677cf36d493124d8048efa0bd6f67d817ce3cd6b725Vladimir Marko    stats_.art_method_bytes += method_section.Size();
167805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
1679d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    stats_.interned_strings_bytes += intern_section.Size();
16808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    stats_.Dump(os, indent_os);
16812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
168227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
16832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
16843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
16852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
16863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1687d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
16883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
168954d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier  class DumpArtMethodVisitor : public ArtMethodVisitor {
169054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier   public:
169154d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
169254d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
169354d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
169454d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      std::ostream& indent_os = image_dumper_->vios_.Stream();
169554d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
169654d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      image_dumper_->DumpMethod(method, image_dumper_, indent_os);
169754d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      indent_os << "\n";
169854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    }
169954d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
170054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier   private:
170154d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    ImageDumper* const image_dumper_;
170254d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier  };
170354d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
17042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
170590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
17062ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
17072ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
17082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1709d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
17102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
17112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
171268b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
171364b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
17142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
17152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1716d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
17172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1718d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1719d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1720d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1721c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
172290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
172361c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
172408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
172508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1726ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
172708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
172808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
17292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
173008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
173108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
17322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
173308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
173408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
17352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
173608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
173708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
173837f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
173908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
174008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
174137f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
174208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
174308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
174437f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
174537f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
174608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
174708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
174837f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
174908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
175008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
175108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
175208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
175308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
175408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
175508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
175650239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
175708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1758daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
175908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
176008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
176108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
176208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
176308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
176408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
176550239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
176608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
176748efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
176808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
176908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
177008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1771d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1772d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1773d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
17742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
177590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
17762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
17772ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
17782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1779d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
178054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    for (ArtField& field : klass->GetIFields()) {
178154d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      PrintField(os, &field, obj);
1782d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1783d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1784d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
17852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
17863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1787f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
17883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1789e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCodeBegin(ArtMethod* m)
179090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1791a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1792a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
17936f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1794ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
17953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1796f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1797ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1798f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1799ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
18003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
18013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1802e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uint32_t GetQuickOatCodeSize(ArtMethod* m)
180390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1804ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1805ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1806f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1807f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1808f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1809f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1810f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1811e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCodeEnd(ArtMethod* m)
181290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1813ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
18142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
18152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1816f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1817ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1818f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1819f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
182090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
18212ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
18222ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
18233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
182478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
182578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
182678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1827916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1828916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1829916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1830916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1831916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1832916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& os = state->vios_.Stream();
18348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
18352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1836d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
18372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
18382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1839d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
18402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
18412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
18422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1843d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
18442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
184568b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
184678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
18472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
184878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
18498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&state->vios_);
18508f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    DumpFields(os, obj, obj_class);
1851e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto image_pointer_size =
1852e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1853d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
1854e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto* obj_array = obj->AsObjectArray<mirror::Object>();
1855e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
18562dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1857d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1858d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1859d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1860d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1861d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1862d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1863d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1864d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1865d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
18668f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d: ", i);
186778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
18688f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d to %zd: ", i, i + run);
1869d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
187078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
18712ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
18722ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
18738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        PrettyObjectValue(os, value_class, value);
1874d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1875d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1876c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      mirror::Class* klass = obj->AsClass();
187754d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      if (klass->NumStaticFields() != 0) {
18788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        os << "STATICS:\n";
18798f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(&state->vios_);
188054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier        for (ArtField& field : klass->GetSFields()) {
188154d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier          PrintField(os, &field, field.GetDeclaringClass());
1882d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1883d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1884e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
188505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      auto it = state->dex_caches_.find(obj);
188605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      if (it != state->dex_caches_.end()) {
188705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        auto* dex_cache = down_cast<mirror::DexCache*>(obj);
1888e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& field_section = state->image_header_.GetImageSection(
1889e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            ImageHeader::kSectionArtFields);
1890e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& method_section = state->image_header_.GetMethodsSection();
189105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        size_t num_methods = dex_cache->NumResolvedMethods();
189205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        if (num_methods != 0u) {
189305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          os << "Methods (size=" << num_methods << "):";
189405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          ScopedIndentation indent2(&state->vios_);
189505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          auto* resolved_methods = dex_cache->GetResolvedMethods();
189605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
189705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
189805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            size_t run = 0;
189905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            for (size_t j = i + 1;
190005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
190105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           j,
190205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           image_pointer_size);
190305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                ++j, ++run) {}
190405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (run == 0) {
190505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd: ", i);
190605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
190705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd to %zd: ", i, i + run);
190805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              i = i + run;
190905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
191005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            std::string msg;
191105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (elem == nullptr) {
191205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "null";
191305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else if (method_section.Contains(
191405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
191505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
191605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
191705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "<not in method section>";
191805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
191905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            os << StringPrintf("%p   %s\n", elem, msg.c_str());
1920e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
192105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        }
192205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        size_t num_fields = dex_cache->NumResolvedFields();
192305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        if (num_fields != 0u) {
192405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          os << "Fields (size=" << num_fields << "):";
192505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          ScopedIndentation indent2(&state->vios_);
192605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          auto* resolved_fields = dex_cache->GetResolvedFields();
192705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
192805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
192905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            size_t run = 0;
193005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            for (size_t j = i + 1;
193105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
193205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           j,
193305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           image_pointer_size);
193405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                ++j, ++run) {}
193505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (run == 0) {
193605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd: ", i);
193705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
193805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd to %zd: ", i, i + run);
193905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              i = i + run;
194005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
194105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            std::string msg;
194205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (elem == nullptr) {
194305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "null";
194405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else if (field_section.Contains(
194505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
194605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = PrettyField(reinterpret_cast<ArtField*>(elem));
194705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
194805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "<not in field section>";
194905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
195005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            os << StringPrintf("%p   %s\n", elem, msg.c_str());
1951e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
19523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1953e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1954e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
1955e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::string temp;
1956e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
1957e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  }
19583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1959e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
196090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1961e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    DCHECK(method != nullptr);
1962e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto image_pointer_size =
1963e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1964e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    if (method->IsNative()) {
1965e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1966e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1967e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
1968e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1969e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1970e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->ComputeOatSize(quick_oat_code, &first_occurrence);
1971e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
1972e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1973e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1974e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1975e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
1976e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1977e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1978e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1979e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1980e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1981e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1982e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
1983e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const DexFile::CodeItem* code_item = method->GetCodeItem();
1984e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1985e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1986e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1987e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
1988e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t gc_map_bytes = state->ComputeOatSize(
1989e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1990e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
1991e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.gc_map_bytes += gc_map_bytes;
1992e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1993e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1994e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t pc_mapping_table_bytes = state->ComputeOatSize(
1995e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          method->GetMappingTable(image_pointer_size), &first_occurrence);
1996e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
1997e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1998e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1999e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
20006d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain      size_t vmap_table_bytes = 0u;
20016d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain      if (!method->IsOptimized(image_pointer_size)) {
20026d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        // Method compiled with the optimizing compiler have no vmap table.
20036d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        vmap_table_bytes = state->ComputeOatSize(
20046d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain            method->GetVmapTable(image_pointer_size), &first_occurrence);
20056d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        if (first_occurrence) {
20066d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain          state->stats_.vmap_table_bytes += vmap_table_bytes;
20076d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        }
2008e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2009e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
2010e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2011e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
2012e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2013e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2014e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
2015e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.managed_code_bytes += quick_oat_code_size;
2016e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        if (method->IsConstructor()) {
2017e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          if (method->IsStatic()) {
2018e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2019e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2020e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            state->stats_.large_initializer_code_bytes += quick_oat_code_size;
20210d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
2022e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2023e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          state->stats_.large_method_code_bytes += quick_oat_code_size;
20243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
2025e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2026e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
20273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20287617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      uint32_t method_access_flags = method->GetAccessFlags();
20297617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin
2030e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
20317617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
20327617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
20337617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                method_access_flags);
20343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2035e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
203614632857428b7e37761e6e811c19021715a400f8Vladimir Marko          vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
20373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2038e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      double expansion =
2039e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2040e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.ComputeOutliers(total_size, expansion, method);
204178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
204278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
204327ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
20443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
20453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
20463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
2047a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
20483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
2049a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
20503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
20513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
2052a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
20533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
205527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
2056916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2057916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
2058916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
20593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
2060916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
2061916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2062916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
2063916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
2064e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_field_bytes;
2065e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_method_bytes;
206605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    size_t dex_cache_arrays_bytes;
2067d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    size_t interned_strings_bytes;
206832327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
2069916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
2070916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2071916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
20723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
2073916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
2074916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
20750d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
20760d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
20770d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
2078916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
2080916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
20813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
2082916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2083916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
2084916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2085e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::vector<ArtMethod*> method_outlier;
20863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
20873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
2088700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
20893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20903887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain    Stats()
20913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
20923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
2093916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
2094916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
2095e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_field_bytes(0),
2096e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_method_bytes(0),
209705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          dex_cache_arrays_bytes(0),
2098d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier          interned_strings_bytes(0),
209932327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
2100916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
2101916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
21023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
2103916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
2104916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
21050d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
21060d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
21070d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
21083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
2109916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
21103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
2111916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
2112916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2113a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
2114277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
2115a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
2116a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
2117a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
2118a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2119a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
2120a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
2121277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
2122a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2123a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
2124277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
2125a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
2126a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
2127277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
2128a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
2129a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
2130916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
21323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
21333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
21343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2135916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
2136916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2137916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2138916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2139916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
2140916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2141916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2142916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2143e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
21443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
21453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
21463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
21473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
21483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
214900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
215090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier        SHARED_REQUIRES(Locks::mutator_lock_) {
21513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
21523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
21533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
21543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
21553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
21563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
21573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
21583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
21593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
21603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
21613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
21623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
21633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
21643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
21653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
21663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
21673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
21683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
21693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
21713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
21723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
21733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
21743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
21753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
21763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
21773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
21783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
21793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
21803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
21813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
21823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
21833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
21843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
21853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
21863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
21873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
21883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
21893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
21903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
2191c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
21923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
21933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
21942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
2195c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
21963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
21973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
21983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
21993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
22003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
22013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
22023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
22033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
22042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2205c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
22063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2207c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
22083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
22093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
22103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
22113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
22123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
22133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
22143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
22153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
22163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
22173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
22183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
22193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
22203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
22213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
22223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
22233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
22243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
22253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
22263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
22273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
22283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
22293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
22303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
2231c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
22323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
22333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
22342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2235c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
22363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
22373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
22383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
22393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
22403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
22413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
22423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
22433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
22442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2245c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
22463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2247c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
22483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
22493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
22508f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    void Dump(std::ostream& os, std::ostream& indent_os)
225190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier        SHARED_REQUIRES(Locks::mutator_lock_) {
22522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
22532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
22542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
225505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        indent_os << StringPrintf("header_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
225605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "object_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
225705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "art_field_bytes        =  %8zd (%2.0f%% of art file bytes)\n"
225805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "art_method_bytes       =  %8zd (%2.0f%% of art file bytes)\n"
225905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "dex_cache_arrays_bytes =  %8zd (%2.0f%% of art file bytes)\n"
226005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "interned_string_bytes  =  %8zd (%2.0f%% of art file bytes)\n"
226105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "bitmap_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
226205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "alignment_bytes        =  %8zd (%2.0f%% of art file bytes)\n\n",
22632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
22642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
2265e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_field_bytes, PercentOfFileBytes(art_field_bytes),
2266e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_method_bytes, PercentOfFileBytes(art_method_bytes),
226705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  dex_cache_arrays_bytes,
226805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  PercentOfFileBytes(dex_cache_arrays_bytes),
2269d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  interned_strings_bytes,
2270d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  PercentOfFileBytes(interned_strings_bytes),
227132327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
22722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
22732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
2274e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
227505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                 dex_cache_arrays_bytes + interned_strings_bytes + bitmap_bytes + alignment_bytes);
22762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2277916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
22782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2279916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
228002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
228102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
228202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
228302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
228402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
22852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2286a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
228702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
228802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
228902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2290916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2291c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2292916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2293916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
22942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
22952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
22962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
22972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
22982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
22992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
23002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
230105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
23022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
23032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
23042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
23052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
23062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
23072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
23082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
23092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
23102ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
23112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
23122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
23132ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
23142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
231502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
23162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
231702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
231802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
231905f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
232005f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
23212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
23222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
23232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
232405f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
232505f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
232605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2327c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2328916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
23292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
23302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
23312ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
23322ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2333c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2334cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2335c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
23363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
23373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2338916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2339916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2340916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2341916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
23420d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
23430d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
23440d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
23450d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
23460d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
23470d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
23480d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
23490d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
23508f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
23518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // For performance, use the *os_ directly for anything that doesn't need indentation
23528f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // and prepare an indentation stream with default indentation 1.
23532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
23548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  VariableIndentationOutputStream vios_;
23558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  ScopedIndentation indent1_;
23568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
23571d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
23583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
23592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
2360df2bb1f36c0d755f0bdb2b1562e52669f3de4a09Andreas Gampe  OatDumperOptions* oat_dumper_options_;
236105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko  std::set<mirror::Object*> dex_caches_;
2362d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
23633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
236478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
236578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
236600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
236700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                     std::ostream* os) {
236800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
236900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
237000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
237100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
237200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
237300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  gc::Heap* heap = runtime->GetHeap();
23741d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace* image_space = heap->GetImageSpace();
23752ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  CHECK(image_space != nullptr);
237678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  const ImageHeader& image_header = image_space->GetImageHeader();
237778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  if (!image_header.IsValid()) {
23780f5baa079eea76758aee32d7f87cb175deab8308Brian Carlstrom    fprintf(stderr, "Invalid image header %s\n", image_location);
237978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    return EXIT_FAILURE;
238078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
23813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
238200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ImageDumper image_dumper(os, *image_space, image_header, options);
23833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool success = image_dumper.Dump();
23852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
238678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
238778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
238800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
238900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
239000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
239100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
239200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
239300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
239400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
239500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
239600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
239700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
239800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
239900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
2400f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier  Runtime::Current()->GetOatFileManager().RegisterOatFile(
2401f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier      std::unique_ptr<const OatFile>(oat_file));
2402ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  std::vector<const DexFile*> class_path;
240300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
240400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2405ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
240600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
240700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    class_linker->RegisterDexFile(*dex_file);
2408ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    class_path.push_back(dex_file);
240900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
241000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
241100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
241200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
241381c6f8db12b203878a7d72444ead2bc7cf5c47adAndreas Gampe  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
241400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
241500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
241600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
241700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
241800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
241900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
242000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
242132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
242200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
242300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
242400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
242500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
242600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
242732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
242800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
242900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
243000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
243100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
243232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
243300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
243400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
243500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
243600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
243700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
243800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
243900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
24403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2441b1d8c314b55bb2df2b2bb72a3daaf5db65b7ebc7Igor Murashkin                                    nullptr, &error_msg);
244200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
244300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
244400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
244500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
244600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
244700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
244800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
244900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
245000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
245100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
245200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
245300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
245400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int SymbolizeOat(const char* oat_filename, std::string& output_name) {
245500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
24563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2457b1d8c314b55bb2df2b2bb72a3daaf5db65b7ebc7Igor Murashkin                                    nullptr, &error_msg);
245800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
245900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
246000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
246100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
246200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
246300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  OatSymbolizer oat_symbolizer(oat_file, output_name);
246400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Symbolize()) {
246500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
246600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
246700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
246800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
246900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
247000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
247100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24723774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
24733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
24743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
247500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
24773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
24783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
24793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
24803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
24813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
24823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
248300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
248400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
24863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
24873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
24883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
24893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
24903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
24913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
24923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
24933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
24943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
2495f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else if (option =="--dump:code_info_stack_maps") {
2496f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_ = true;
24973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
24983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
24993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
25003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
25013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
250232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
250332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
25043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
25053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
250632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
250732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
250832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
250932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
251032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
251132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
251232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
251332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
251432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
251532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
251632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
25173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
25183774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
251900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
252000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
25223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
25233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
25253774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
25263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
25273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
252800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
252900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
25313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
25323774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
25333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
253400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
253500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
25373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
25383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
25393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
25403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
25413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
25423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
25433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
25443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
25463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
25473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
25493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
25503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25513774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
25523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
25533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
25543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
25553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
25563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
25573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
25583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
25593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
25603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
25613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
25623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
25633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
25653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
25673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
25683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
25693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
257019510f02b011e545665f6219e6144c8e47aed5f0Mathieu Chartier        "  --dump:raw_gc_map enables dumping of the GC map.\n"
25713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
25723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
25733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
25743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
25753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2576f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2577f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "      Example: --dump:code_info_stack_maps\n"
2578f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "\n"
25793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
25803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
25813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
258232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
258332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
258432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
258532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
258632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
258732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
258832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
258932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
259032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
259132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
259232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
259332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
259432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
259532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
25963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
25973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
259832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
259932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
260032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
260132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
260232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
260332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
260432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
26053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
26063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
260800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
260900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
261100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
261232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
26133fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
261400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
261500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
261600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
261700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
261800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
2619f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  bool dump_code_info_stack_maps_ = false;
262000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
262100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
262232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
262332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
262432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
262532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
262600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
262700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26283774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
26293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
26303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
263100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26323774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
26333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
26343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
26363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
26373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
26383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
2639f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        args_->dump_code_info_stack_maps_,
26403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
26413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
264232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
264332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
264432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
264532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
264632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
264732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
26483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
26503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
265100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
265200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
26543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2655c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
26563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2657d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
26583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2659c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
2660c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2661c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2662c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2663c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
266432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2665c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2666c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
266700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
266800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
26703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
26713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
26733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
26743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
267532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
26763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
267700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
267800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
267932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
26803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      == EXIT_SUCCESS;
268100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
268200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
26843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
268500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26867934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
268778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
268878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
26893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
26903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
269178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2692