oatdump.cc revision 7617abdb402fd0419daa3eefb2ad059ccbb8b6db
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"
472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
49700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "oat.h"
508a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko#include "oat_file-inl.h"
51e5448b5a12003b405b22cde3b94f962ab4888a87Elliott Hughes#include "os.h"
5254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include "output_stream.h"
53a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
5400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
5500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "ScopedLocalRef.h"
56c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier#include "thread_list.h"
57ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "verifier/dex_gc_map.h"
582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "verifier/method_verifier.h"
591809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "vmap_table.h"
6000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "well_known_classes.h"
6178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <sys/stat.h>
633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include "cmdline.h"
6478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
653774335b08076117d6950cd472cdd59a167470b5Igor Murashkinnamespace art {
6678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
67e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_methods_descriptions_[] = {
681984651929744dd603fd082e23eacd877b9bc177Ian Rogers  "kResolutionMethod",
6988474b416eb257078e590bf9bc7957cee604a186Jeff Hao  "kImtConflictMethod",
702d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier  "kImtUnimplementedMethod",
71e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  "kCalleeSaveMethod",
72aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsOnlySaveMethod",
73aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsAndArgsSaveMethod",
74e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier};
75e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
76e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_roots_descriptions_[] = {
7758ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  "kDexCaches",
7834f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom  "kClassRoots",
7978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
8078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
81bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbeckyclass OatSymbolizer FINAL {
8254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe public:
83bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  class RodataWriter FINAL : public CodeOutput {
84bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   public:
85bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
8654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
87bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool Write(OutputStream* out) OVERRIDE {
88bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
89bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      return out->WriteFully(oat_file_->Begin(), rodata_size);
90bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    }
9154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
92bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   private:
93bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    const OatFile* oat_file_;
94bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  };
9554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
96bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  class TextWriter FINAL : public CodeOutput {
97bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   public:
98bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
9954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
100bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool Write(OutputStream* out) OVERRIDE {
101bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
102bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
103bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      return out->WriteFully(text_begin, oat_file_->End() - text_begin);
10454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
10554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
106bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   private:
107bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    const OatFile* oat_file_;
108bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  };
109bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky
110bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
111bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      oat_file_(oat_file), builder_(nullptr),
112bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
11354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
11454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
11654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
11754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const OatFile::OatMethod&,
11854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile&,
11954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
12054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile::CodeItem*,
12154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t);
12254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
12354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Symbolize() {
124bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
125bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
126bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t text_size = size - rodata_size;
127bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t bss_size = oat_file_->BssSize();
128bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    RodataWriter rodata_writer(oat_file_);
129bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    TextWriter text_writer(oat_file_);
130bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    builder_.reset(new ElfBuilder<ElfTypes32>(
131bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        oat_file_->GetOatHeader().GetInstructionSet(),
132bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        rodata_size, &rodata_writer,
133bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        text_size, &text_writer,
134bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        bss_size));
13554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::RegisterForDedup);
13754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    NormalizeState();
13954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::AddSymbol);
14154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
142bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
143bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool result = builder_->Write(elf_output);
14454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1454303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    // Ignore I/O errors.
146bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    UNUSED(elf_output->FlushClose());
14754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return result;
14954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
15054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
15154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void Walk(Callback callback) {
15254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
15354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t i = 0; i < oat_dex_files.size(); i++) {
15454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
1552cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      CHECK(oat_dex_file != nullptr);
15654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatDexFile(oat_dex_file, callback);
15754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
15854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
15954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
16054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
16154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::string error_msg;
16254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
16354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dex_file.get() == nullptr) {
16454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
16554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
16654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t class_def_index = 0;
16754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index < dex_file->NumClassDefs();
16854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index++) {
16954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
17054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
17154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      OatClassType type = oat_class.GetType();
17254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      switch (type) {
17354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassAllCompiled:
17454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassSomeCompiled:
17554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
17654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
17754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassNoneCompiled:
17954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassMax:
18054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          // Ignore.
18154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
18254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
18354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
18454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
18554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
18654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
18754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                    const DexFile::ClassDef& class_def, Callback callback) {
18813735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
18954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
19054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
19154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
19254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    // Note: even if this is an interface or a native class, we still have to walk it, as there
19354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    //       might be a static initializer.
19454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    ClassDataItemIterator it(dex_file, class_data);
19554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    SkipAllFields(&it);
19654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t class_method_idx = 0;
19754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextDirectMethod()) {
19854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
19954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
2005182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
20154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
20254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
20354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextVirtualMethod()) {
20554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
20654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
2075182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
20854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
20954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
21054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
21154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DCHECK(!it.HasNext());
21254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
21354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
21454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
21554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     const OatFile::OatMethod& oat_method, const DexFile& dex_file,
21654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
21754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t method_access_flags, Callback callback) {
21854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if ((method_access_flags & kAccAbstract) != 0) {
21954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // Abstract method, no code.
22054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
22154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
22254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (oat_method.GetCodeOffset() == 0) {
22354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // No code.
22454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
22554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
22654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
22754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
22854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                      method_access_flags);
22954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
23054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2316a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2326a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t class_method_index ATTRIBUTE_UNUSED,
2336a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const OatFile::OatMethod& oat_method,
2346a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile& dex_file ATTRIBUTE_UNUSED,
2356a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t dex_method_idx ATTRIBUTE_UNUSED,
2366a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2376a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t method_access_flags ATTRIBUTE_UNUSED) {
23854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[oat_method.GetCodeOffset()]++;
23954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
24054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
24154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void NormalizeState() {
24254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (auto& x : state_) {
24354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (x.second == 1) {
24454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        state_[x.first] = 0;
24554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
24654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
24754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
24854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
24954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  enum class DedupState {  // private
25054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kNotDeduplicated,
25154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedFirst,
25254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedOther
25354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  };
25454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  DedupState IsDuplicated(uint32_t offset) {
25554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 0) {
25654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kNotDeduplicated;
25754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 1) {
25954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kDeduplicatedOther;
26054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
26154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[offset] = 1;
26254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return DedupState::kDeduplicatedFirst;
26354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
26454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2656a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2666a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t class_method_index ATTRIBUTE_UNUSED,
2676a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const OatFile::OatMethod& oat_method,
2686a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile& dex_file,
2696a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t dex_method_idx,
2706a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2716a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
27254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
27354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dedup != DedupState::kDeduplicatedOther) {
27454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
27554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
27654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (dedup == DedupState::kDeduplicatedFirst) {
27754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        pretty_name = "[Dedup]" + pretty_name;
27854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
27954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
280bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      auto* symtab = builder_->GetSymtab();
28154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
282bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      symtab->AddSymbol(pretty_name, builder_->GetText(),
2830279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
2840279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
28554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
28654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
28754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
28854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe private:
28954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  static void SkipAllFields(ClassDataItemIterator* it) {
29054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextStaticField()) {
29154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
29254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextInstanceField()) {
29454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
29554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
29754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
29854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  const OatFile* oat_file_;
299533c207f9d2da6d913c4b10f6f757fe9d6367b10David Srbecky  std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
30054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  std::unordered_map<uint32_t, uint32_t> state_;
3010279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  const std::string output_name_;
30254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe};
30354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass OatDumperOptions {
3052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
3062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  OatDumperOptions(bool dump_raw_mapping_table,
3072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_raw_gc_map,
3082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_vmap,
309f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   bool dump_code_info_stack_maps,
3102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
31100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
31232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
31332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
31432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
31532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
31632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
31732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
3182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    : dump_raw_mapping_table_(dump_raw_mapping_table),
3192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_raw_gc_map_(dump_raw_gc_map),
3202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_vmap_(dump_vmap),
321f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_(dump_code_info_stack_maps),
3222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
32300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
32432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
3253fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
32632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
32732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
32832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
32932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
3303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
3312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
3322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_mapping_table_;
3332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_gc_map_;
3342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
335f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  const bool dump_code_info_stack_maps_;
3362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
3372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
33832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
3393fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
34032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
34132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
34232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
34332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
34400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
3452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
3462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
347e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
348aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
34932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
3509583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
351a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
3522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
35332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
354a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
355a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
35632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
357a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
358eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                                                 true /* can_read_literals_ */))) {
35932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
36032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
36132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
3623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
3633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
3643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
3652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
3662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
3672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
3682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
369a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
370a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
371a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
372a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
3732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
3742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
3753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
376aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
377aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
378aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
379aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
380aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
381ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
382aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
383a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
384a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
385a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
3866f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
3876f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
3886f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
3896f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
3906f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
3916f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
3926f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
3937020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
394aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
395aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
396aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
3982ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
3992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
40032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
4012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
4022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
4032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
4042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
4052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
4062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
4072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
4082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
4092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
4102ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
4112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
4122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
4132ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
4142ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
4152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4162ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4172ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
4182ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
4192ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
4202ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
421aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
4232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
4242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
4252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
426a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
42728db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
42828db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
42928db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
43028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
431700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
43281f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
43322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
43422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
43522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
43622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
43722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
43822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
43922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
44022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
44122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
44222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
44322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
44422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
44581f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
44632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
4472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
4482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
4492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
4512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
4522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
453aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
4552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
456aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
457aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
458aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
45932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
46032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
46132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
46232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
46332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
46432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
46532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
4663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
4682ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
46932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
47032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // If file export selected skip file analysis
47132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.export_dex_location_) {
47232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!ExportDexFile(os, *oat_dex_file)) {
47332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
47432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
47532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
47632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!DumpOatDexFile(os, *oat_dex_file)) {
47732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
47832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
4792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
4803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
4812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
4822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
4833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
4843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
4853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
48613735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
48713735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
4883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
4893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
490ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
491ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
492ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
4933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
494ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
4953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
4963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
497e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
498a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
499f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
500f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
501f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
502e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCode(ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
5033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5058d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
5068d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
507700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5088d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5098d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5108d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5118d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
512e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5138b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
514e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
5152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
5168b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
517d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
5183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
519d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
5203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
5213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
522aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5232ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
524aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
525aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
526aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
5273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
528e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
529e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
530e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
531e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
5323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5342ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
5358d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
536700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5378d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5388d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5398d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
541e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
542ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
5432ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
5442ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
5452ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
546e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
547d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
54813735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
5492ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
550e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
551e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
552e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
553e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
554d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
555e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
556e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
557e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
558d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
559e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
560e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
561e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
562e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
563e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
564e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
565e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
566e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
567e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
568ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
569e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
570e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
5722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
5732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
5742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
575e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
57695ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
57795ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
57895ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
57995ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
58095ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
581e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetMappingTableOffset());
582e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
583957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    offsets_.insert(oat_method.GetGcMapOffset());
584e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
585e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
5872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
58832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
5892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
590a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
591ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
592590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
593590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // Create the verifier early.
594590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
5958d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
596700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
5972ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (dex_file.get() == nullptr) {
5988d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
5992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
6002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
601aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
6028f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
6038f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    VariableIndentationOutputStream vios(&os);
6048f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&vios);
6052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
6062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
6072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
608aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
609aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
61032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
61132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
61232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
61332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
61432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
61532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
6162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
617d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
6182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
6192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
620d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
621d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
622d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
62332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
6248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatClass(&vios, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
6252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
6262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
62732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
62832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
62932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
63032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
631aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
632aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
633aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
6342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
635aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
636aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
63732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
63832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
63932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
64032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
64232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
64332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
64432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
64732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
64932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
65032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
65132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
65532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
65632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
65732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
65832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
66232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
66332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
66732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
66832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
66932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
67032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
67132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
67232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
67432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
67532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
67632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
67732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
67832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
67932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
68032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
68232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
68332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
68432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
68532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
68732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
68832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
68932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
69332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
69432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
70132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
70532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
70632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
71032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
71132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
71232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
71332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
71432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
715e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
7160571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
7170571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
718aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7190571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
7200571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
721aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
722e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
723aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
7248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatClass(VariableIndentationOutputStream* vios,
7258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                    const OatFile::OatClass& oat_class, const DexFile& dex_file,
72632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
7272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
72832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
72913735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
7302ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
7318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << std::flush;
7322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
733e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
734e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
735e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
7362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
7370571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
7388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
7392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
74032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
74332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
74432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
74532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
74632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7480571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
749aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7500571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
7518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
7522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
75332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
75632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
75732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
75832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
75932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7610571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
762aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7630571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
7648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
7652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
766aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
767e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
7682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
7692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
7712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
7722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatMethod(VariableIndentationOutputStream* vios,
7748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                     const DexFile::ClassDef& class_def,
7758b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
7762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
7772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
77832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
7792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
78032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
78132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
78232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
78332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
7843fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
7853fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
7863fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
78732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
7888f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
7898f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   class_method_index, pretty_method.c_str(),
7908f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   dex_method_idx);
79132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
79232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
79432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
79532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
79632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
80032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
80132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
80232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
80332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
80432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
80532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    // Everything below is indented at least once.
8078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(vios);
8088f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
8092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "DEX CODE:\n";
8118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpDexCode(vios->Stream(), dex_file, code_item);
8132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
8152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
8162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
8178f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
8188f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      verifier.reset(DumpVerifier(vios,
8208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                  dex_method_idx, &dex_file, class_def, code_item,
8212ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
8222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatMethodOffsets ";
82532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method_offsets);
8272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
8292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
8308f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
8312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
8322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
8332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
8348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
8352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
837a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
8388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8398f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
8402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
8412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
8428f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code offset 0x%08x is past end of file 0x%08zx.\n",
8448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       aligned_code_begin, oat_file_.Size());
8452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8478f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
8483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
8498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "gc_map: ";
85032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
8523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
853957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
8548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
8552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (gc_map_offset > oat_file_.Size()) {
8568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8578f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                           "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
8588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                           gc_map_offset, oat_file_.Size());
8592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
86032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_gc_map_) {
8618f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent3(vios);
8628f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpGcMap(vios->Stream(), oat_method, code_item);
8632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
8652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
8668f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatQuickMethodHeader ";
8672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
8682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
8692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
87032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", method_header);
8722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
8742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
8758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
8762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
8772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
8782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
8798f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
8802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "mapping_table: ";
88532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8868f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
8872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
8898f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
8902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (mapping_table_offset > oat_file_.Size()) {
8918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
8928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "mapping table offset 0x%08x is past end of file 0x%08zx. "
8938f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "mapping table offset was loaded from offset 0x%08x.\n",
8948f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       mapping_table_offset, oat_file_.Size(),
8958f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetMappingTableOffsetOffset());
8962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
89732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_mapping_table_) {
8988f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent3(vios);
8998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpMappingTable(vios, oat_method);
9002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9028f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "vmap_table: ";
90332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9048f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
9052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
9078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
9082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
9098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset 0x%08x is past end of file 0x%08zx. "
9118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset was loaded from offset 0x%08x.\n",
9128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       vmap_table_offset, oat_file_.Size(),
9138f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetVmapTableOffsetOffset());
9142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
91532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
9168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapData(vios, oat_method, code_item);
9172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
9182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
9208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "QuickMethodFrameInfo\n";
9218f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
9228f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream()
9248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
9258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
9268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
9278f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
9298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
9308f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // Based on spill masks from QuickMethodFrameInfo so placed
9348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // after it is dumped, but useful for understanding quick
9358f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // code, so dumped here.
9368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9378f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpVregLocations(vios->Stream(), oat_method, code_item);
9382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9408f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "CODE: ";
9412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
9422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
9438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
9448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code size offset 0x%08x is past end of file 0x%08zx.",
9468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset, oat_file_.Size());
9472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
9492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
9502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
9512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
952590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
95332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
9548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("%p ", code);
9552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9568f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
9578f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_offset,
9588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset,
9598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size,
9608f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code != nullptr ? "..." : "");
9612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9628f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
9632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
9648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("WARNING: "
9658f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         "start of code at 0x%08x is past end of file 0x%08zx.",
9668f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         aligned_code_begin, oat_file_.Size());
9672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
9682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
9698f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
9708f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
9718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
9728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
9738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              aligned_code_end, oat_file_.Size(),
9748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
9752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
97632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
9812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
9828f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
9838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
9848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size %d is bigger than max expected threshold of %d. "
9858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
9868f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, kMaxCodeSize,
9878f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
9882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
98932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9922cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9932cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
99432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
9958f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
9962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
10002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
10013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
10033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
10043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
10053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
10063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
10083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
10093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
10103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
10113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
10123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
10143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
10163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
10173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
10183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
10203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
10223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
10243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1026442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
10278f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpVmapData(VariableIndentationOutputStream* vios,
1028442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1029442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1030f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1031f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The optimizing compiler outputs its CodeInfo data in the vmap table.
1032442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1033442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1034442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1035442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
10368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
10378f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpCodeInfo(vios, code_info, oat_method, *code_item);
1038442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10390a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
10400a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // We don't encode the size in the table, so just emit that we have quickened
10410a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // information.
10420a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      ScopedIndentation indent(vios);
10430a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      vios->Stream() << "quickened data\n";
1044442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
1045442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // Otherwise, display the vmap table.
1046442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const uint8_t* raw_table = oat_method.GetVmapTable();
1047442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_table != nullptr) {
1048442b46a087c389a91a0b51547ac9205058432364Roland Levillain        VmapTable vmap_table(raw_table);
10498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapTable(vios->Stream(), oat_method, vmap_table);
1050442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
105120d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1052442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1053442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1054442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
10558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCodeInfo(VariableIndentationOutputStream* vios,
1056442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1057f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                    const OatFile::OatMethod& oat_method,
1058442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
10598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    code_info.Dump(vios,
1060f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   oat_method.GetCodeOffset(),
1061f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   code_item.registers_size_,
1062f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   options_.dump_code_info_stack_maps_);
1063442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1064442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1065442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a vmap table.
1066442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapTable(std::ostream& os,
1067442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const OatFile::OatMethod& oat_method,
1068442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const VmapTable& vmap_table) {
1069442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool first = true;
1070442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool processing_fp = false;
1071442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1072442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < vmap_table.Size(); i++) {
1073442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint16_t dex_reg = vmap_table[i];
1074442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1075442b46a087c389a91a0b51547ac9205058432364Roland Levillain                                                    processing_fp ? kFloatVReg : kIntVReg);
1076442b46a087c389a91a0b51547ac9205058432364Roland Levillain      os << (first ? "v" : ", v")  << dex_reg;
1077442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp) {
1078442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/r" << cpu_reg;
1079442b46a087c389a91a0b51547ac9205058432364Roland Levillain      } else {
1080442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/fr" << cpu_reg;
1081442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1082442b46a087c389a91a0b51547ac9205058432364Roland Levillain      first = false;
1083442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp && dex_reg == 0xFFFF) {
1084442b46a087c389a91a0b51547ac9205058432364Roland Levillain        processing_fp = true;
1085442b46a087c389a91a0b51547ac9205058432364Roland Levillain        spill_mask = oat_method.GetFpSpillMask();
1086442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1088442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "\n";
10893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1091faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1092faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1093faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1094faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1095faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1096faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1097faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1098faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1099faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1100faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1101faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1102faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1103faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1104faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1105faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1106faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1107faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1108faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1109faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
111015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
111115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
111215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
111315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
111415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
111515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
111615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1117faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1118faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1119faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1120faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1121faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1122faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1123faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1124faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1125faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1126faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1127faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1128faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1129faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1130faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1131faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1132faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1133b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
11342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
11351809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* raw_table = oat_method.GetVmapTable();
11362ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (raw_table != nullptr) {
1137b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      const VmapTable vmap_table(raw_table);
1138b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      uint32_t vmap_offset;
11391809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
11402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
11412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
11422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                       : oat_method.GetCoreSpillMask();
11432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1144b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      } else {
114515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
114615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
114715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
114815b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
114915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
115015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
115115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1152b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        os << "[sp + #" << offset << "]";
1153b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
1154b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1155b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1156b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1157ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1158ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          const DexFile::CodeItem* code_item,
1159ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          size_t num_regs, const uint8_t* reg_bitmap) {
1160ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    bool first = true;
1161ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t reg = 0; reg < num_regs; reg++) {
1162ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1163ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        if (first) {
1164ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << "  v" << reg << " (";
1165ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1166ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1167ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          first = false;
1168ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else {
1169ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ", v" << reg << " (";
1170ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1171ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1172ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        }
1173ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      }
1174ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1175ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (first) {
1176ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "No registers in GC map\n";
1177ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    } else {
1178ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "\n";
1179ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1180ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
1181b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1182b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers                 const DexFile::CodeItem* code_item) {
1183957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1184ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (gc_map_raw == nullptr) {
1185ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return;  // No GC map.
11863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1187ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1188956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    NativePcOffsetToReferenceMap map(gc_map_raw);
1189956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1190956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1191956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes          map.GetNativePcOffset(entry);
1192956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      os << StringPrintf("%p", native_pc);
1193956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
11943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1195aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
1196e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
11978f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpMappingTable(VariableIndentationOutputStream* vios,
11988f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                        const OatFile::OatMethod& oat_method) {
1199ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1200ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (quick_code == nullptr) {
12012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      return;
12022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12031809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12041809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (table.TotalSize() != 0) {
12051809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.PcToDexSize() != 0) {
12061809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::PcToDexIterator It;
12078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "suspend point mappings {\n";
12081809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent1(vios);
12108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12111809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "}\n";
12131809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      }
12141809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.DexToPcSize() != 0) {
12151809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::DexToPcIterator It;
12168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "catch entry mappings {\n";
12171809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12188f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent1(vios);
12198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12201809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12218f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "}\n";
12223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
12233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
12253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
1227f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const OatFile::OatMethod& oat_method,
1228f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const DexFile::CodeItem* code_item,
1229f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   size_t offset,
1230f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   bool suspend_point_mapping) {
1231f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1232f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (suspend_point_mapping) {
12338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
12348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
1235f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1236f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The return value is not used in the case of a method compiled
1237f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // with the optimizing compiler.
1238f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      return DexFile::kDexNoIndex;
1239f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else {
12408f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
1241f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1242f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1243f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
12441809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12451809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
12461809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12471809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
12481809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
12491809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12501809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12514b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
12521809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
12531809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1254b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12551809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
12561809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
12571809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12581809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12594b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
12601809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1261b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1262b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1265b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1269957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
12702ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1271b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
12722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1273b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
12742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1275b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1276b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1277b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1278b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
12792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
12802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1281b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1282b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1283b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1284b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
12852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1286b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1287b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1288b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1289b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1290b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1291b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1292b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1293b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1295b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1297590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1298590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1299590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1300590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
13017b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
13022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
13032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
13042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
13052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
13062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
13072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
13082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
13092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
13102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
13112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
13132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
13142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
13152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
13162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
13192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
13202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
13222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
13262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
13272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
13292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
13302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
13332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1334b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
13352ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1336b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1337b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1338b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
133932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
134032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1341b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1342e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1343e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1344b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1345b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1346f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1347f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // the optimizing compiler?
1348f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1349f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                                    const DexFile::CodeItem* code_item) {
1350f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // If the native GC map is null and the Dex `code_item` is not
1351f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // null, then this method has been compiled with the optimizing
1352f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // compiler.
13530a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() != nullptr &&
13540a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetGcMap() == nullptr &&
13550a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
13560a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  }
13570a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray
13580a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
13590a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // the dextodex compiler?
13600a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
13610a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray                                                  const DexFile::CodeItem* code_item) {
13620a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // If the quick code is null, the Dex `code_item` is not
13630a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // null, and the vmap table is not null, then this method has been compiled
13640a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // with the dextodex compiler.
13650a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() == nullptr &&
13660a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetVmapTable() != nullptr &&
13670a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
1368f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1369f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
13708f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
1371f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const OatFile::OatMethod& oat_method,
1372f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const DexFile::CodeItem* code_item,
1373f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  size_t offset) {
1374f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // This method is only relevant for oat methods compiled with the
1375f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // optimizing compiler.
1376f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1377f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
1378f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // The optimizing compiler outputs its CodeInfo data in the vmap table.
1379f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    const void* raw_code_info = oat_method.GetVmapTable();
1380f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (raw_code_info != nullptr) {
1381f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      CodeInfo code_info(raw_code_info);
1382f677ebfd832c9c614fea5e6735725fec2f7a3f2aDavid Brazdil      StackMapEncoding encoding = code_info.ExtractEncoding();
1383f677ebfd832c9c614fea5e6735725fec2f7a3f2aDavid Brazdil      StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
1384f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (stack_map.IsValid()) {
13858f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
13868f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                       code_item->registers_size_);
1387f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1388f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1389f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1390f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
13918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
13928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         uint32_t dex_method_idx,
13932ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
13942ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
13952ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
13962ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
13972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
13982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1399bf99f77dda749e2b653e8c45259b1fb56e7bb012Mathieu Chartier      StackHandleScope<1> hs(soa.Self());
1400eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1401eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier          hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
140232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
1403e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      return verifier::MethodVerifier::VerifyMethodAndDump(
14048f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
14058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          &class_def, code_item, nullptr, method_access_flags);
14062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
14072ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
14082ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
14092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
14102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
14118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCode(VariableIndentationOutputStream* vios,
14128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                verifier::MethodVerifier* verifier,
14132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
14142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1415ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1416ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
14172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
14182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
14192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1420956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
14218f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "NO CODE!\n";
1422b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1423956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1424ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1425ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1426ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
14272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
14288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
14292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
14308f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
14312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
14328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          uint32_t dex_pc =
14338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
14342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
14358f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko            DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
14362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
14378f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
14382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1439ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
14402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
14412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1442b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1443b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1444b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
14453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
14462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
144732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
144832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1449a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1450ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
14512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1452aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1453aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
14543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
145527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
14562ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom  explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
14572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
14582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
14598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios_(os),
14608f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent1_(&vios_),
14612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
14622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
14632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
146427ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
14652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
14678f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& indent_os = vios_.Stream();
14688f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
14692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
14702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
14712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
14722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1473e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1474e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1475e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1476e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto section = static_cast<ImageHeader::ImageSections>(i);
1477e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1478e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
147931e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
14802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
14812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1482700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
14832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1484700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1485700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1486700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1487700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1488700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
14892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1490a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1491a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
149246774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
149346774767fcf7780d1455e755729198648d08742eIgor Murashkin
14942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
14952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1496e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_roots_descriptions_) ==
1497e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
14982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
14992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
15002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
15012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
15028f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
15032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
15042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1505fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
15068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent2(&vios_);
1507277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1508277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1509fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1510277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1511277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1512fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1513fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1514fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1515fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1516fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1517fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
15188f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d: ", j);
1519fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
15208f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d to %zd: ", j, j + run);
1521277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1522fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
15232ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
15248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              PrettyObjectValue(indent_os, value->GetClass(), value);
15252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
15268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << j << ": null\n";
15272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1528d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
152934f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
153034f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
15318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    }
1532e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
15338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    {
1534e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "METHOD ROOTS\n";
1535e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_methods_descriptions_) ==
1536e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1537e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1538e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1539e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const char* description = image_methods_descriptions_[i];
1540e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto* image_method = image_header_.GetImageMethod(image_root);
15418f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", description, image_method);
1542e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
154327ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
15442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
154527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1546aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
15472ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
15482ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
15492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
15502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
15518d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
1552a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1553a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
1554e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler      oat_file = OatFile::Open(oat_location, oat_location,
1555e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               nullptr, nullptr, false, nullptr,
1556e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               &error_msg);
1557a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (oat_file == nullptr) {
1558a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        os << "NOT FOUND: " << error_msg << "\n";
15592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
1560a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
1561aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
15622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1563aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
15643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
156578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
156632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
15673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
156802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
15692ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
157002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
157102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
157205f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
157305f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
15742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1575b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1576b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    // Loop through all the image spaces and dump their objects.
15771d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
15781d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
157950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1580357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1581c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1582c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1583c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1584c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
158590d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
158690d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
158790d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1588c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1589c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromRunnableToSuspended(kNative);
1590c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        ThreadList* thread_list = Runtime::Current()->GetThreadList();
1591bf9fc581e8870faddbd320a935f9a627da724c48Mathieu Chartier        thread_list->SuspendAll(__FUNCTION__);
1592c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->RevokeAllThreadLocalAllocationStacks(self);
1593c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        thread_list->ResumeAll();
1594c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromSuspendedToRunnable();
1595c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
1596357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
15972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
1598e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      // Mark dex caches.
1599e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      dex_cache_arrays_.clear();
1600e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      {
1601e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        ReaderMutexLock mu(self, *class_linker->DexLock());
1602e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        for (size_t i = 0; i < class_linker->GetDexCacheCount(); ++i) {
1603e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          auto* dex_cache = class_linker->GetDexCache(i);
1604e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          dex_cache_arrays_.insert(dex_cache->GetResolvedFields());
1605e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          dex_cache_arrays_.insert(dex_cache->GetResolvedMethods());
1606e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        }
1607e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
16082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
160902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& space : spaces) {
16102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (space->IsImageSpace()) {
1611e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          auto* image_space = space->AsImageSpace();
1612e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          // Dump the normal objects before ArtMethods.
16132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
16142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << "\n";
1615e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          // TODO: Dump fields.
1616e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          // Dump methods after.
1617e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          const auto& methods_section = image_header_.GetMethodsSection();
1618e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          const auto pointer_size =
1619e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier              InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1620e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          const auto method_size = ArtMethod::ObjectSize(pointer_size);
1621e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          for (size_t pos = 0; pos < methods_section.Size(); pos += method_size) {
1622e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            auto* method = reinterpret_cast<ArtMethod*>(
1623e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                image_space->Begin() + pos + methods_section.Offset());
1624e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1625e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            DumpMethod(method, this, indent_os);
1626e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            indent_os << "\n";
1627e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
16282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
16292fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier      }
16302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1631bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
16322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
1633b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
16342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1635700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
16362ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() == nullptr) {
16372ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
16386f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
16392ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() != nullptr) {
16402ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
16416f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
16423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
1643e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1644e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1645e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& method_section = image_header_.GetMethodsSection();
1646d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    const auto& intern_section = image_header_.GetImageSection(
1647d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier        ImageHeader::kSectionInternedStrings);
16483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
16493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
16503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.alignment_bytes += alignment_bytes;
1651e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1652e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.bitmap_bytes += bitmap_section.Size();
1653e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.art_field_bytes += field_section.Size();
16546fcaf3f7b5c4fca67b506c4a4233e8373be6d48eNicolas Geoffray    // RoundUp to 8 bytes to match the intern table alignment expectation.
16556fcaf3f7b5c4fca67b506c4a4233e8373be6d48eNicolas Geoffray    stats_.art_method_bytes += RoundUp(method_section.Size(), sizeof(uint64_t));
1656d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    stats_.interned_strings_bytes += intern_section.Size();
16578f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    stats_.Dump(os, indent_os);
16582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
165927ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
16602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
16613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
16622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
16633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1664d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
16653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
16662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
1667b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16682ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
16692ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
16702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1671d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
16722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
16732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
167468b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
167564b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
16762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
16772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1678d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
16792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1680d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1681d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1682d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1683c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
1684b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
168561c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
168608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
168708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1688ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
168908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
169008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
16912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
169208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
169308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
16942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
169508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
169608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
16972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
169808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
169908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
170037f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
170108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
170208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
170337f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
170408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
170508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
170637f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
170737f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
170808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
170908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
171037f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
171108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
171208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
171308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
171408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
171508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
171608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
171708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
171850239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
171908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1720daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
172108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
172208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
172308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
172408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
172508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
172608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
172750239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
172808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
172948efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
173008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
173108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
173208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1733d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1734d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1735d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
17362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
1737b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
17392ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
17402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1741d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1742c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier    ArtField* fields = klass->GetIFields();
1743c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier    for (size_t i = 0, count = klass->NumInstanceFields(); i < count; i++) {
1744c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      PrintField(os, &fields[i], obj);
1745d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1746d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1747d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
17482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
17493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1750f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
17513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1752e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCodeBegin(ArtMethod* m)
1753b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1754a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1755a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
17566f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1757ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
17583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1759f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1760ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1761f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1762ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
17633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
17643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1765e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uint32_t GetQuickOatCodeSize(ArtMethod* m)
1766b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1767ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1768ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1769f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1770f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1771f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1772f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1773f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1774e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCodeEnd(ArtMethod* m)
1775b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1776ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
17772ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
17782ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1779f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1780ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1781f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1782f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1783e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  static void Callback(mirror::Object* obj, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17842ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
17852ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
17863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
178778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
178878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
178978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1790916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1791916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1792916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1793916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1794916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1795916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
17968f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& os = state->vios_.Stream();
17978f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
17982dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1799d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
18002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
18012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1802d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
18032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
18042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
18052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1806d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
18072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
180868b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
180978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
18102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
181178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
18128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&state->vios_);
18138f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    DumpFields(os, obj, obj_class);
1814e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto image_pointer_size =
1815e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1816d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
1817e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto* obj_array = obj->AsObjectArray<mirror::Object>();
1818e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
18192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1820d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1821d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1822d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1823d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1824d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1825d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1826d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1827d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1828d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
18298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d: ", i);
183078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
18318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d to %zd: ", i, i + run);
1832d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
183378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
18342ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
18352ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
18368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        PrettyObjectValue(os, value_class, value);
1837d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1838d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1839c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      mirror::Class* klass = obj->AsClass();
1840c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      ArtField* sfields = klass->GetSFields();
1841c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      const size_t num_fields = klass->NumStaticFields();
1842c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      if (num_fields != 0) {
18438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        os << "STATICS:\n";
18448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(&state->vios_);
1845c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier        for (size_t i = 0; i < num_fields; i++) {
18468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          PrintField(os, &sfields[i], sfields[i].GetDeclaringClass());
1847d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1848d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1849e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
1850e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto it = state->dex_cache_arrays_.find(obj);
1851e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (it != state->dex_cache_arrays_.end()) {
1852e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& field_section = state->image_header_.GetImageSection(
1853e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            ImageHeader::kSectionArtFields);
1854e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& method_section = state->image_header_.GetMethodsSection();
1855e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto* arr = down_cast<mirror::PointerArray*>(obj);
1856e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        for (int32_t i = 0, length = arr->GetLength(); i < length; i++) {
1857e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          void* elem = arr->GetElementPtrSize<void*>(i, image_pointer_size);
1858e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          size_t run = 0;
1859e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          for (int32_t j = i + 1; j < length &&
1860e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier              elem == arr->GetElementPtrSize<void*>(j, image_pointer_size); j++, run++) { }
1861e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          if (run == 0) {
18628f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko            os << StringPrintf("%d: ", i);
1863e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          } else {
18648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko            os << StringPrintf("%d to %zd: ", i, i + run);
1865e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            i = i + run;
1866e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
1867e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          auto offset = reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin();
1868e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          std::string msg;
1869e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          if (field_section.Contains(offset)) {
1870e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1871e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          } else if (method_section.Contains(offset)) {
1872e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1873e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          } else {
1874e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            msg = "Unknown type";
1875e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
18768f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%p   %s\n", elem, msg.c_str());
18773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1878e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1879e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
1880e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::string temp;
1881e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
1882e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  }
18833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1884e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
1885e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1886e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    DCHECK(method != nullptr);
1887e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto image_pointer_size =
1888e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1889e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    if (method->IsNative()) {
1890e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1891e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1892e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
1893e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1894e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1895e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->ComputeOatSize(quick_oat_code, &first_occurrence);
1896e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
1897e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1898e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1899e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1900e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
1901e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1902e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1903e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1904e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1905e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1906e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1907e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
1908e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const DexFile::CodeItem* code_item = method->GetCodeItem();
1909e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1910e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1911e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1912e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
1913e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t gc_map_bytes = state->ComputeOatSize(
1914e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1915e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
1916e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.gc_map_bytes += gc_map_bytes;
1917e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1918e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1919e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t pc_mapping_table_bytes = state->ComputeOatSize(
1920e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          method->GetMappingTable(image_pointer_size), &first_occurrence);
1921e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
1922e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1923e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1924e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
19256d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain      size_t vmap_table_bytes = 0u;
19266d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain      if (!method->IsOptimized(image_pointer_size)) {
19276d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        // Method compiled with the optimizing compiler have no vmap table.
19286d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        vmap_table_bytes = state->ComputeOatSize(
19296d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain            method->GetVmapTable(image_pointer_size), &first_occurrence);
19306d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        if (first_occurrence) {
19316d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain          state->stats_.vmap_table_bytes += vmap_table_bytes;
19326d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        }
1933e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1934e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1935e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1936e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1937e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1938e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1939e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
1940e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.managed_code_bytes += quick_oat_code_size;
1941e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        if (method->IsConstructor()) {
1942e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          if (method->IsStatic()) {
1943e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            state->stats_.class_initializer_code_bytes += quick_oat_code_size;
1944e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1945e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            state->stats_.large_initializer_code_bytes += quick_oat_code_size;
19460d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
1947e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1948e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          state->stats_.large_method_code_bytes += quick_oat_code_size;
19493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1950e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
1951e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
19523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19537617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      uint32_t method_access_flags = method->GetAccessFlags();
19547617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin
1955e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
19567617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
19577617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
19587617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                method_access_flags);
19593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1960e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1961e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          vmap_table_bytes + quick_oat_code_size + ArtMethod::ObjectSize(image_pointer_size);
19623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1963e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      double expansion =
1964e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
1965e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.ComputeOutliers(total_size, expansion, method);
196678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
196778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
196827ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
19693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
19703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
19713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
1972a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
19733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
1974a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
19753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
19763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
1977a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
19783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
198027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
1981916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1982916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
1983916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
19843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
1985916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
1986916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1987916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
1988916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
1989e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_field_bytes;
1990e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_method_bytes;
1991d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    size_t interned_strings_bytes;
199232327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
1993916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
1994916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1995916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
19963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
1997916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
1998916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
19990d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
20000d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
20010d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
2002916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
2004916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
20053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
2006916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2007916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
2008916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2009e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::vector<ArtMethod*> method_outlier;
20103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
20113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
2012700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
20133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    explicit Stats()
20153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
20163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
2017916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
2018916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
2019e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_field_bytes(0),
2020e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_method_bytes(0),
2021d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier          interned_strings_bytes(0),
202232327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
2023916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
2024916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
20253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
2026916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
2027916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
20280d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
20290d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
20300d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
20313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
2032916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
20333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
2034916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
2035916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2036a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
2037277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
2038a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
2039a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
2040a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
2041a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2042a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
2043a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
2044277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
2045a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2046a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
2047277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
2048a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
2049a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
2050277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
2051a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
2052a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
2053916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
20553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
20563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2058916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
2059916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2060916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2061916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2062916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
2063916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2064916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2065916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2066e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
20673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
20683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
20693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
20703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
207200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
2073b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
20753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
20763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
20773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
20783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
20793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
20803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
20813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
20823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
20833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
20843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
20853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
20863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
20873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
20883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
20893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
20903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
20913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
20923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
20943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
20953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
20963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
20973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
20983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
20993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
21003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
21013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
21023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
21033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
21043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
21053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
21063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
21073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
21083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
21093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
21103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
21113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
21123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
21133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
2114c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
21153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
21163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
21172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
2118c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
21193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
21203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
21213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
21223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
21233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
21243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
21253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
21263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
21272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2128c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
21293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2130c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
21313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
21333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
21343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
21353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
21363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
21373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
21383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
21393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
21403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
21413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
21423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
21433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
21443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
21453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
21463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
21473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
21483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
21493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
21503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
21513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
21523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
21533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
2154c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
21553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
21563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
21572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2158c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
21593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
21603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
21613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
21623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
21633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
21643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
21653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
21663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
21672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2168c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
21693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2170c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
21713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
21723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    void Dump(std::ostream& os, std::ostream& indent_os)
21748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
21752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
21762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
21772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
2178d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier        indent_os << StringPrintf("header_bytes          =  %8zd (%2.0f%% of art file bytes)\n"
2179d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  "object_bytes          =  %8zd (%2.0f%% of art file bytes)\n"
2180d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  "art_field_bytes       =  %8zd (%2.0f%% of art file bytes)\n"
2181d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  "art_method_bytes      =  %8zd (%2.0f%% of art file bytes)\n"
2182d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  "interned_string_bytes =  %8zd (%2.0f%% of art file bytes)\n"
2183d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  "bitmap_bytes          =  %8zd (%2.0f%% of art file bytes)\n"
2184d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  "alignment_bytes       =  %8zd (%2.0f%% of art file bytes)\n\n",
21852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
21862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
2187e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_field_bytes, PercentOfFileBytes(art_field_bytes),
2188e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_method_bytes, PercentOfFileBytes(art_method_bytes),
2189d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  interned_strings_bytes,
2190d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  PercentOfFileBytes(interned_strings_bytes),
219132327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
21922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
21932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
2194e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2195d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                 interned_strings_bytes + bitmap_bytes + alignment_bytes);
21962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2197916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2199916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
220002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
220102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
220202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
220302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
220402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
22052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2206a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
220702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
220802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
220902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2210916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2211c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2212916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2213916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
22142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
22152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
22162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
22172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
22182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
22192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
22202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
222105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
22222ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
22232ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
22242ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
22252ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
22262ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
22272ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
22282ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
22292ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
22302ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
22312ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
22322ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
22332ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
22342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
223502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
22362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
223702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
223802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
223905f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
224005f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
22412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
22422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
22432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
224405f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
224505f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
224605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2247c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2248916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
22492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
22502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
22512ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
22522ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2253c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2254cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2255c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
22563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
22573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2258916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2259916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2260916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2261916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
22620d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
22630d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
22640d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
22650d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
22660d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
22670d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
22680d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
22690d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
22708f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
22718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // For performance, use the *os_ directly for anything that doesn't need indentation
22728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // and prepare an indentation stream with default indentation 1.
22732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
22748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  VariableIndentationOutputStream vios_;
22758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  ScopedIndentation indent1_;
22768f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
22771d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
22783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
22792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
2280df2bb1f36c0d755f0bdb2b1562e52669f3de4a09Andreas Gampe  OatDumperOptions* oat_dumper_options_;
2281e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  std::set<mirror::Object*> dex_cache_arrays_;
2282d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
22833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
228478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
228578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
228600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
228700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                     std::ostream* os) {
228800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
228900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
229000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
229100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
229200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
229300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  gc::Heap* heap = runtime->GetHeap();
22941d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace* image_space = heap->GetImageSpace();
22952ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  CHECK(image_space != nullptr);
229678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  const ImageHeader& image_header = image_space->GetImageHeader();
229778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  if (!image_header.IsValid()) {
22980f5baa079eea76758aee32d7f87cb175deab8308Brian Carlstrom    fprintf(stderr, "Invalid image header %s\n", image_location);
229978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    return EXIT_FAILURE;
230078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
23013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
230200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ImageDumper image_dumper(os, *image_space, image_header, options);
23033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool success = image_dumper.Dump();
23052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
230678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
230778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
230800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
230900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
231000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
231100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
231200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
231300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
231400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
231500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
231600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
231700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
231800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
231900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
232000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  class_linker->RegisterOatFile(oat_file);
2321fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
232200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
232300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2324fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
232500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
232600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    class_linker->RegisterDexFile(*dex_file);
2327fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    dex_files.push_back(std::move(dex_file));
232800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
232900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
233000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
233100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2332fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<const DexFile*> class_path;
2333fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
2334fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    class_path.push_back(dex_file.get());
2335fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  }
233681c6f8db12b203878a7d72444ead2bc7cf5c47adAndreas Gampe  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
233700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
233800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
233900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
234000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
234100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
234200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
234300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
234432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
234500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
234600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
234700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
234800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
234900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
235032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
235100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
235200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
235300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
235400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
235532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
235600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
235700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
235800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
235900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
236000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
236100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
236200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
23633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2364e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
236500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
236600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
236700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
236800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
236900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
237000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
237100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
237200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
237300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
237400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
237500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
237600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
237700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int SymbolizeOat(const char* oat_filename, std::string& output_name) {
237800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
23793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2380e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
238100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
238200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
238300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
238400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
238500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
238600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  OatSymbolizer oat_symbolizer(oat_file, output_name);
238700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Symbolize()) {
238800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
238900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
239000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
239100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
239200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
239300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
239400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23953774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
23963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
23973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
239800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
24003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
24013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
24023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
24033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
24043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
24053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
240600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
240700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
24093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
24103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
24113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
24123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
24133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
24143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
24153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
24163774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
24173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
2418f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else if (option =="--dump:code_info_stack_maps") {
2419f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_ = true;
24203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
24213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
24223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
24233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
24243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
242532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
242632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
24273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
24283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
242932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
243032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
243132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
243232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
243332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
243432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
243532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
243632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
243732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
243832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
243932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
24403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
24413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
244200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
244300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
24453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
24463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
24483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
24493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
24503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
245100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
245200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
24543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
24553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
24563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
245700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
245800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
24603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
24613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
24623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
24633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
24643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
24653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
24663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
24673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
24693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
24703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
24723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
24733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
24753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
24763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
24773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
24783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
24793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
24803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
24813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
24823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
24833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
24843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
24853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
24863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
24883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
24903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
24913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
24923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
249319510f02b011e545665f6219e6144c8e47aed5f0Mathieu Chartier        "  --dump:raw_gc_map enables dumping of the GC map.\n"
24943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
24953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
24963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
24973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
24983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2499f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2500f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "      Example: --dump:code_info_stack_maps\n"
2501f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "\n"
25023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
25033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
25043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
250532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
250632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
250732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
250832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
250932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
251032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
251132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
251232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
251332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
251432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
251532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
251632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
251732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
251832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
25193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
25203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
252132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
252232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
252332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
252432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
252532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
252632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
252732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
25283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
25293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
253100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
253200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
253400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
253532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
25363fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
253700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
253800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
253900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
254000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
254100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
2542f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  bool dump_code_info_stack_maps_ = false;
254300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
254400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
254532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
254632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
254732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
254832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
254900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
255000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25513774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
25523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
25533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
255400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
25563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
25573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
25593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
25603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
25613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
2562f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        args_->dump_code_info_stack_maps_,
25633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
25643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
256532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
256632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
256732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
256832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
256932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
257032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
25713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
25733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
257400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
257500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
25773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2578c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
25793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2580d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
25813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2582c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
2583c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2584c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2585c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2586c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
258732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2588c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2589c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
259000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
259100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
25933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
25943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
25953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
25963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
25973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
259832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
25993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
260000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
260100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
260232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
26033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      == EXIT_SUCCESS;
260400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
260500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
26073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
260800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26097934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
261078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
261178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
26123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
26133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
261478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2615