oatdump.cc revision bc90fd09e09a845ae6ea0d84ad67560575a94142
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"
30761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h"
3178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include "class_linker.h"
322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h"
334f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
34e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes#include "dex_instruction.h"
353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers#include "disassembler.h"
3654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include "elf_builder.h"
372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "gc_map.h"
381d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h"
391d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/large_object_space.h"
401d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
4178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include "image.h"
422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "indenter.h"
431809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "mapping_table.h"
44ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.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
67ff1ed4770bf7ff024a807b9f909b1a26abb78341Ian Rogersconst char* image_roots_descriptions_[] = {
681984651929744dd603fd082e23eacd877b9bc177Ian Rogers  "kResolutionMethod",
6988474b416eb257078e590bf9bc7957cee604a186Jeff Hao  "kImtConflictMethod",
702d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier  "kImtUnimplementedMethod",
7188474b416eb257078e590bf9bc7957cee604a186Jeff Hao  "kDefaultImt",
72e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  "kCalleeSaveMethod",
73aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsOnlySaveMethod",
74aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsAndArgsSaveMethod",
7558ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  "kDexCaches",
7634f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom  "kClassRoots",
7778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
7878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
79bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbeckyclass OatSymbolizer FINAL {
8054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe public:
81bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  class RodataWriter FINAL : public CodeOutput {
82bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   public:
83bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
8454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
85bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool Write(OutputStream* out) OVERRIDE {
86bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
87bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      return out->WriteFully(oat_file_->Begin(), rodata_size);
88bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    }
8954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
90bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   private:
91bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    const OatFile* oat_file_;
92bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  };
9354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
94bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  class TextWriter FINAL : public CodeOutput {
95bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   public:
96bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
9754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
98bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool Write(OutputStream* out) OVERRIDE {
99bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
100bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
101bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      return out->WriteFully(text_begin, oat_file_->End() - text_begin);
10254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
10354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
104bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky   private:
105bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    const OatFile* oat_file_;
106bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  };
107bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky
108bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky  explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
109bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      oat_file_(oat_file), builder_(nullptr),
110bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
11154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
11254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
11454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
11554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const OatFile::OatMethod&,
11654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile&,
11754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
11854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile::CodeItem*,
11954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t);
12054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
12154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Symbolize() {
122bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
123bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
124bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t text_size = size - rodata_size;
125bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    uint32_t bss_size = oat_file_->BssSize();
126bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    RodataWriter rodata_writer(oat_file_);
127bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    TextWriter text_writer(oat_file_);
128bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    builder_.reset(new ElfBuilder<ElfTypes32>(
129bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        oat_file_->GetOatHeader().GetInstructionSet(),
130bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        rodata_size, &rodata_writer,
131bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        text_size, &text_writer,
132bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky        bss_size));
13354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::RegisterForDedup);
13554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    NormalizeState();
13754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::AddSymbol);
13954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
140bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
141bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    bool result = builder_->Write(elf_output);
14254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1434303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    // Ignore I/O errors.
144bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky    UNUSED(elf_output->FlushClose());
14554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return result;
14754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
14854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void Walk(Callback callback) {
15054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
15154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t i = 0; i < oat_dex_files.size(); i++) {
15254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
1532cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      CHECK(oat_dex_file != nullptr);
15454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatDexFile(oat_dex_file, callback);
15554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
15654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
15754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
15854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
15954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::string error_msg;
16054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
16154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dex_file.get() == nullptr) {
16254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
16354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
16454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t class_def_index = 0;
16554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index < dex_file->NumClassDefs();
16654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index++) {
16754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
16854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
16954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      OatClassType type = oat_class.GetType();
17054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      switch (type) {
17154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassAllCompiled:
17254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassSomeCompiled:
17354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
17454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
17554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassNoneCompiled:
17754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassMax:
17854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          // Ignore.
17954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
18054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
18154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
18254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
18354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
18454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
18554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                    const DexFile::ClassDef& class_def, Callback callback) {
18613735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
18754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
18854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
18954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
19054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    // Note: even if this is an interface or a native class, we still have to walk it, as there
19154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    //       might be a static initializer.
19254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    ClassDataItemIterator it(dex_file, class_data);
19354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    SkipAllFields(&it);
19454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t class_method_idx = 0;
19554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextDirectMethod()) {
19654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
19754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
1985182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
19954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
20054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
20154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextVirtualMethod()) {
20354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
20454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
2055182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
20654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
20754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
20854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DCHECK(!it.HasNext());
21054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
21154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
21254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
21354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     const OatFile::OatMethod& oat_method, const DexFile& dex_file,
21454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
21554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t method_access_flags, Callback callback) {
21654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if ((method_access_flags & kAccAbstract) != 0) {
21754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // Abstract method, no code.
21854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
21954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
22054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (oat_method.GetCodeOffset() == 0) {
22154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // No code.
22254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
22354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
22454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
22554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
22654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                      method_access_flags);
22754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
22854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2296a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2306a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t class_method_index ATTRIBUTE_UNUSED,
2316a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const OatFile::OatMethod& oat_method,
2326a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile& dex_file ATTRIBUTE_UNUSED,
2336a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t dex_method_idx ATTRIBUTE_UNUSED,
2346a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2356a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t method_access_flags ATTRIBUTE_UNUSED) {
23654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[oat_method.GetCodeOffset()]++;
23754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
23854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
23954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void NormalizeState() {
24054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (auto& x : state_) {
24154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (x.second == 1) {
24254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        state_[x.first] = 0;
24354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
24454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
24554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
24654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
24754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  enum class DedupState {  // private
24854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kNotDeduplicated,
24954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedFirst,
25054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedOther
25154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  };
25254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  DedupState IsDuplicated(uint32_t offset) {
25354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 0) {
25454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kNotDeduplicated;
25554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 1) {
25754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kDeduplicatedOther;
25854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[offset] = 1;
26054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return DedupState::kDeduplicatedFirst;
26154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
26254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2636a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2646a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t class_method_index ATTRIBUTE_UNUSED,
2656a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const OatFile::OatMethod& oat_method,
2666a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile& dex_file,
2676a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t dex_method_idx,
2686a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2696a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
27054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
27154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dedup != DedupState::kDeduplicatedOther) {
27254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
27354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
27454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (dedup == DedupState::kDeduplicatedFirst) {
27554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        pretty_name = "[Dedup]" + pretty_name;
27654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
27754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
278bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      auto* symtab = builder_->GetSymtab();
27954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
280bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbecky      symtab->AddSymbol(pretty_name, builder_->GetText(),
2810279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
2820279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
28354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
28454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
28554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
28654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe private:
28754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  static void SkipAllFields(ClassDataItemIterator* it) {
28854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextStaticField()) {
28954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
29054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextInstanceField()) {
29254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
29354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
29554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
29654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  const OatFile* oat_file_;
297533c207f9d2da6d913c4b10f6f757fe9d6367b10David Srbecky  std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
29854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  std::unordered_map<uint32_t, uint32_t> state_;
2990279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  const std::string output_name_;
30054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe};
30154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass OatDumperOptions {
3032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
3042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  OatDumperOptions(bool dump_raw_mapping_table,
3052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_raw_gc_map,
3062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_vmap,
3072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
30800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
30932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
31032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
31132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
31232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
31332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
31432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
3152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    : dump_raw_mapping_table_(dump_raw_mapping_table),
3162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_raw_gc_map_(dump_raw_gc_map),
3172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_vmap_(dump_vmap),
3182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
31900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
32032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
3213fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
32232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
32332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
32432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
32532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
3263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
3272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
3282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_mapping_table_;
3292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_gc_map_;
3302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
3312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
3322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
33332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
3343fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
33532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
33632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
33732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
33832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
33900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
3402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
3412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
342e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
343aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
34432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
3459583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
346a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
3472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
34832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
349a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
350a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
35132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
352a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
353a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 true /* can_read_litals_ */))) {
35432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
35532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
35632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
3573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
3583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
3593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
3602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
3612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
3622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
3632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
364a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
365a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
366a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
367a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
3682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
3692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
3703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
371aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
372aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
373aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
374aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
375aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
376ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
377aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
378a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
379a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
380a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
3816f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
3826f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
3836f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
3846f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
3856f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
3866f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
3876f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
3887020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
389aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
390aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
391aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3922ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
3932ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
3942ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
39532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
3962ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
3972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
3982ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
3992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
4002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
4012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
4022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
4032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
4042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
4052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
4062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
4072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
4082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
4092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
4102ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
4132ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
4142ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
4152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
416aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
4182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
4192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
4202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
421a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
42228db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
42328db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
42428db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
42528db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
426700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
42781f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
42822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
42922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
43022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
43122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
43222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
43322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
43422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
43522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
43622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
43722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
43822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
43922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
44081f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
44132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
4422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
4432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
4442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
4462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
4472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
448aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
4502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
451aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
452aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
453aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
45432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
45532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
45632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
45732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
45832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
45932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
46032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
4613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
4632ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
46432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
46532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // If file export selected skip file analysis
46632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.export_dex_location_) {
46732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!ExportDexFile(os, *oat_dex_file)) {
46832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
46932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
47032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
47132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!DumpOatDexFile(os, *oat_dex_file)) {
47232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
47332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
4742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
4753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
4762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
4772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
4783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
4793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
4803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
48113735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
48213735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
4833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
4843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
485ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
486ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
487ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
4883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
489ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
4903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
4913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
492e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
493a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
494f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
495f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
496f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
497ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5008d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
5018d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
502700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5038d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5048d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5058d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5068d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
507e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5088b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
509e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
5102ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
5118b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
512d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
5133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
514d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
5153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
5163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
517aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5182ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
519aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
520aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
521aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
5223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
523e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
524e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
525e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
526e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
5273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5292ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
5308d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
531700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5328d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5338d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5348d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
536e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
537ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
5382ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
5392ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
5402ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
541e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
542d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
54313735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
5442ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
545e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
546e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
547e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
548e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
549d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
550e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
551e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
552e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
553d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
554e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
555e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
556e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
557e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
558e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
559e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
560e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
561e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
562e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
563ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
564e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
565e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
5672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
5682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
5692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
570e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
57195ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
57295ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
57395ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
57495ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
57595ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
576e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetMappingTableOffset());
577e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
578957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    offsets_.insert(oat_method.GetGcMapOffset());
579e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
580e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
5822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
58332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
5842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
585a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
586ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
587590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
588590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // Create the verifier early.
589590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
5908d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
591700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
5922ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (dex_file.get() == nullptr) {
5938d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
5942cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
5952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
596aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
5982ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
5992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
600aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
601aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
60232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
60332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
60432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
60532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
60632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
60732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
6082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
609d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
6102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
6112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
612d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
613d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
614d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
6152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
6162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indented_os(&indent_filter);
61732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
61832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
6192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
6202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
62132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
62232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
62332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
62432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
625aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
626aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
627aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
6282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
629aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
630aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
63132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
63232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
63332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
63432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
63532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
63632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
63732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
63832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
63932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
64132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
64332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
64432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
64532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
64932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
65032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
65132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
65232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
65632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
65732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
66132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
66232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
66332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
66432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
66532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
66632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
66832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
66932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
67032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
67132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
67232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
67432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
67532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
67732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
67832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
67932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
68132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
68232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
68332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
68432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
68632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
68732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
68832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
68932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
69332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
69432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
70432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
70532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
70732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
709e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
7100571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
7110571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
712aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7130571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
7140571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
715aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
716e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
717aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
7182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
71932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
7202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
72132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
72213735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
7232ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
7242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
7252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
726e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
727e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
728e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
7292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
7300571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
7312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
7322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
73332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
73632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
73732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
73832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
73932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7410571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
742aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7430571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
7442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
7452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
74632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
74932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
75032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
75132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
75232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7540571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
755aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7560571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
757aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
7582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
759aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
760e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
7612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
7622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
7642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
7652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
7678b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
7682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
7692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
77032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
7712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
77232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
77332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
77432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
77532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
7763fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
7773fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
7783fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
77932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
7802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
7813fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray                       class_method_index, pretty_method.c_str(),
782db7bdc1086c44fe628b6743f98d7d85a5c03343aIan Rogers                       dex_method_idx);
78332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
78432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
7852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
786a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
787a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
788a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
78932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
79132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
79232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
79332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
79432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
79532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
79632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
80032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
80132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
80232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
804a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      *indent1_os << "DEX CODE:\n";
805a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      DumpDexCode(*indent2_os, dex_file, code_item);
8062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8072ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
8082ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
8092ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
810a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
8112ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
8122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
8132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "OatMethodOffsets ";
81632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("%p ", oat_method_offsets);
8182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
8202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
8212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf(
8222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
8232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
8242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
8252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        os << std::flush;
8262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
828a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
8292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
8302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
8312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
8322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "code offset 0x%08x is past end of file 0x%08zx.\n",
8342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    aligned_code_begin, oat_file_.Size());
8352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
8383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
8392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "gc_map: ";
84032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
841957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
8423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
843957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
8442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
8452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (gc_map_offset > oat_file_.Size()) {
8462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
8482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    gc_map_offset, oat_file_.Size());
8492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
85032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_gc_map_) {
851a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe        Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
8522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent3_os(&indent3_filter);
8532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpGcMap(indent3_os, oat_method, code_item);
8542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
8562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
8572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "OatQuickMethodHeader ";
8582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
8592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
8602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
86132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("%p ", method_header);
8632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
8652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
8662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf(
8672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
8682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
8692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
8702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        os << std::flush;
8712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "mapping_table: ";
87532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
8772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
8792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
8802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (mapping_table_offset > oat_file_.Size()) {
8812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "mapping table offset 0x%08x is past end of file 0x%08zx. "
8832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "mapping table offset was loaded from offset 0x%08x.\n",
8842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    mapping_table_offset, oat_file_.Size(),
8852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    oat_method.GetMappingTableOffsetOffset());
8862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
88732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_mapping_table_) {
888a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe        Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
8892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent3_os(&indent3_filter);
8902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpMappingTable(indent3_os, oat_method);
8912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8922cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8932cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "vmap_table: ";
89432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
8962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
8982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
8992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
9002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
9012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "vmap table offset 0x%08x is past end of file 0x%08zx. "
9022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "vmap table offset was loaded from offset 0x%08x.\n",
9032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    vmap_table_offset, oat_file_.Size(),
9042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    oat_method.GetVmapTableOffsetOffset());
9052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
90632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
907442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpVmapData(*indent2_os, oat_method, code_item);
9082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
9092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
9112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "QuickMethodFrameInfo\n";
9122cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
9142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
9152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
9162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
9172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
9182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
9192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
9202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // Based on spill masks from QuickMethodFrameInfo so placed
9232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // after it is dumped, but useful for understanding quick
9242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // code, so dumped here.
9252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpVregLocations(*indent2_os, oat_method, code_item);
9262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "CODE: ";
9292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
9302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
9312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
9322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "code size offset 0x%08x is past end of file 0x%08zx.",
9332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size_offset, oat_file_.Size());
9342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
9362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
9372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
9382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
939590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
94032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
9412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent1_os << StringPrintf("%p ", code);
9422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
9442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_offset,
9452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size_offset,
9462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size,
9472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code != nullptr ? "..." : "");
9482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
9502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "start of code at 0x%08x is past end of file 0x%08zx.",
9522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      aligned_code_begin, oat_file_.Size());
9532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
9542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
9552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
9572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size is 0x%08x loaded from offset 0x%08x.\n",
9582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      aligned_code_end, oat_file_.Size(),
9592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, code_size_offset);
9602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
96132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
9662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
9672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size %d is bigger than max expected threshold of %d. "
9692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size is 0x%08x loaded from offset 0x%08x.\n",
9702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, kMaxCodeSize,
9712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, code_size_offset);
9722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
97332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
97832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
9792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
9802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
9842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
9853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
9863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
9873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
9883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
9893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
9903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
9912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
9923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
9933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
9943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
9953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
9963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
9973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
9983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
9993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
10003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
10013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
10023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
10043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
10063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
10083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1010442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
1011442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapData(std::ostream& os,
1012442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1013442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1014957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    if (oat_method.GetGcMap() == nullptr) {
1015442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // If the native GC map is null, then this method has been
1016442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // compiled with the optimizing compiler. The optimizing
1017442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // compiler currently outputs its stack maps in the vmap table.
1018442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1019442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1020442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1021442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
1022442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpCodeInfo(os, code_info, *code_item);
1023442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1024442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
1025442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // Otherwise, display the vmap table.
1026442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const uint8_t* raw_table = oat_method.GetVmapTable();
1027442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_table != nullptr) {
1028442b46a087c389a91a0b51547ac9205058432364Roland Levillain        VmapTable vmap_table(raw_table);
1029442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpVmapTable(os, oat_method, vmap_table);
1030442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
103120d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1032442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1033442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1034442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
1035442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpCodeInfo(std::ostream& os,
1036442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1037442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
1038004c230b4cfc856690c61faabc41864061813c88Nicolas Geoffray    code_info.Dump(os, code_item.registers_size_);
1039442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1040442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1041442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a vmap table.
1042442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapTable(std::ostream& os,
1043442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const OatFile::OatMethod& oat_method,
1044442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const VmapTable& vmap_table) {
1045442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool first = true;
1046442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool processing_fp = false;
1047442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1048442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < vmap_table.Size(); i++) {
1049442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint16_t dex_reg = vmap_table[i];
1050442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1051442b46a087c389a91a0b51547ac9205058432364Roland Levillain                                                    processing_fp ? kFloatVReg : kIntVReg);
1052442b46a087c389a91a0b51547ac9205058432364Roland Levillain      os << (first ? "v" : ", v")  << dex_reg;
1053442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp) {
1054442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/r" << cpu_reg;
1055442b46a087c389a91a0b51547ac9205058432364Roland Levillain      } else {
1056442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/fr" << cpu_reg;
1057442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1058442b46a087c389a91a0b51547ac9205058432364Roland Levillain      first = false;
1059442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp && dex_reg == 0xFFFF) {
1060442b46a087c389a91a0b51547ac9205058432364Roland Levillain        processing_fp = true;
1061442b46a087c389a91a0b51547ac9205058432364Roland Levillain        spill_mask = oat_method.GetFpSpillMask();
1062442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1064442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "\n";
10653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1067faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1068faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1069faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1070faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1071faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1072faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1073faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1074faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1075faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1076faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1077faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1078faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1079faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1080faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1081faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1082faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1083faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1084faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1085faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
108615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
108715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
108815b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
108915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
109015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
109115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
109215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1093faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1094faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1095faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1096faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1097faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1098faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1099faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1100faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1101faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1102faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1103faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1104faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1105faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1106faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1107faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1108faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1109b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
11102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
11111809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* raw_table = oat_method.GetVmapTable();
11122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (raw_table != nullptr) {
1113b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      const VmapTable vmap_table(raw_table);
1114b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      uint32_t vmap_offset;
11151809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
11162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
11172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
11182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                       : oat_method.GetCoreSpillMask();
11192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1120b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      } else {
112115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
112215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
112315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
112415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
112515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
112615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
112715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1128b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        os << "[sp + #" << offset << "]";
1129b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
1130b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1131b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1132b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1133ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1134ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          const DexFile::CodeItem* code_item,
1135ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          size_t num_regs, const uint8_t* reg_bitmap) {
1136ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    bool first = true;
1137ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t reg = 0; reg < num_regs; reg++) {
1138ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1139ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        if (first) {
1140ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << "  v" << reg << " (";
1141ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1142ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1143ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          first = false;
1144ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else {
1145ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ", v" << reg << " (";
1146ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1147ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1148ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        }
1149ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      }
1150ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1151ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (first) {
1152ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "No registers in GC map\n";
1153ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    } else {
1154ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "\n";
1155ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1156ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
1157b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1158b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers                 const DexFile::CodeItem* code_item) {
1159957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1160ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (gc_map_raw == nullptr) {
1161ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return;  // No GC map.
11623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1163ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1164956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    NativePcOffsetToReferenceMap map(gc_map_raw);
1165956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1166956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1167956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes          map.GetNativePcOffset(entry);
1168956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      os << StringPrintf("%p", native_pc);
1169956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
11703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1171aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
1172e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
11733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
1174ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1175ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (quick_code == nullptr) {
11762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      return;
11772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
11781809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
11791809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (table.TotalSize() != 0) {
11801809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
11811809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      std::ostream indent_os(&indent_filter);
11821809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.PcToDexSize() != 0) {
11831809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::PcToDexIterator It;
11841809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "suspend point mappings {\n";
11851809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
11861809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
11871809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
11881809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "}\n";
11891809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      }
11901809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.DexToPcSize() != 0) {
11911809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::DexToPcIterator It;
11921809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "catch entry mappings {\n";
11931809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
11941809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
11951809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
11961809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "}\n";
11973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
11983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
11993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
12003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12011809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12021809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
12031809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12041809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
12051809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
12061809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12071809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12084b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
12091809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
12101809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1211b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12121809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
12131809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
12141809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12151809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12164b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
12171809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1218b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1219b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1222b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1226957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
12272ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1228b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
12292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1230b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
12312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1232b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1233b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1234b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1235b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
12362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
12372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1238b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1239b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1240b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1241b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
12422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1243b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1244b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1245b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1246b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1247b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1248b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1249b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1250b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1252b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1254590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1255590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1256590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1257590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
12587b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
12592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
12602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
12612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
12622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
12632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
12642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
12652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
12662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
12672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
12682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
12692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
12702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
12712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
12722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
12732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
12742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
12762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
12772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
12792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
12802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
12822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
12832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
12842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
12862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
12872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
12892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
12902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1291b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
12922ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1293b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1294b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1295b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
129632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
129732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1298b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1299e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1300e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1301b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1302b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
13032ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
13042ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
13052ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
13062ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
13072ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
13082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
13092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1310bf99f77dda749e2b653e8c45259b1fb56e7bb012Mathieu Chartier      StackHandleScope<1> hs(soa.Self());
1311eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1312eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier          hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
131332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
13142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
13152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           dex_cache,
131632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                                           *options_.class_loader_,
13172ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           &class_def, code_item,
13182ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           NullHandle<mirror::ArtMethod>(),
13192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           method_access_flags);
13202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13212ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
13222ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
13232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1325590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
13262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
13272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1328ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1329ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
13302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
13312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
13322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1333956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
13342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "NO CODE!\n";
1335b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1336956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1337ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1338ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1339ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
13402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
13412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          DumpMappingAtOffset(os, oat_method, offset, false);
13422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
1343ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        offset += disassembler_->Dump(os, quick_native_pc + offset);
13442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
13452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
13462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
13472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
13482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
13492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
13502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1351ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
13522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1354b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1355b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1356b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
13573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
13582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
135932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
136032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1361a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1362ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
13632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1364aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1365aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
13663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
136727ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
13682ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom  explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
13692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
13702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
13712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
13722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
13732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
137427ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
13752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
13772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
13782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
13792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
13802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
138131e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier    os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
138231e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier       << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
138331e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
13842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
13852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1386700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
13872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1388700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1389700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1390700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1391700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1392700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
13932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1394a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1395a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
139646774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
139746774767fcf7780d1455e755729198648d08742eIgor Murashkin
13982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
13992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
14002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
14012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent1_os(&indent1_filter);
14022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
14032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
14042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
14052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
14062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
14072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
14082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
14092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
14102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          std::ostream indent2_os(&indent2_filter);
14112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1412fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
1413277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1414277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1415fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1416277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1417277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1418fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1419fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1420fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1421fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1422fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1423fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
1424277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d: ", j);
1425fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
1426277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1427277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1428fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
14292ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
14302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              PrettyObjectValue(indent2_os, value->GetClass(), value);
14312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
1432277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << j << ": null\n";
14332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1434d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
143534f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
143634f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
143727ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
14382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
143927ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1440aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
14412ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
14422ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
14432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
14442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
14458d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
1446a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1447a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
1448e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler      oat_file = OatFile::Open(oat_location, oat_location,
1449e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               nullptr, nullptr, false, nullptr,
1450e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               &error_msg);
1451a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (oat_file == nullptr) {
1452a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        os << "NOT FOUND: " << error_msg << "\n";
14532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
1454a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
1455aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
14562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1457aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
14583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
145978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
146032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
14613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
146202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
14632ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
146402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
146502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
146605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
146705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
14682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1469b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1470b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    // Loop through all the image spaces and dump their objects.
14711d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
14721d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
147350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1474357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1475c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1476c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1477c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1478c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
147990d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
148090d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
148190d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1482c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1483c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromRunnableToSuspended(kNative);
1484c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        ThreadList* thread_list = Runtime::Current()->GetThreadList();
1485bf9fc581e8870faddbd320a935f9a627da724c48Mathieu Chartier        thread_list->SuspendAll(__FUNCTION__);
1486c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->RevokeAllThreadLocalAllocationStacks(self);
1487c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        thread_list->ResumeAll();
1488c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromSuspendedToRunnable();
1489c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
1490357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
14912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
14922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream* saved_os = os_;
14932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
14942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent_os(&indent_filter);
14952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = &indent_os;
14962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
149702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& space : spaces) {
14982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (space->IsImageSpace()) {
14991d54e73444e017d3a65234e0f193846f3e27472bIan Rogers          gc::space::ImageSpace* image_space = space->AsImageSpace();
15002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << "\n";
15022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
15032fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier      }
15042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1505bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
15072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = saved_os;
1508b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
15092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1510700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
15112ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() == nullptr) {
15122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
15136f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() != nullptr) {
15152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
15166f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
15183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
15193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
15203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.alignment_bytes += alignment_bytes;
152132327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
152232327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
15232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    stats_.Dump(os);
15242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
152527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
15262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
15273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
15282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
15293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1530d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
15313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
15322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
1533b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15342ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
15352ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
15362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1537d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
15382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
15392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
154068b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
154164b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
15422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
15432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1544ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (type->IsArtMethodClass()) {
1545ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = value->AsArtMethod();
15462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Method: %s\n", method, PrettyMethod(method).c_str());
1547d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
15482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1549d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1550d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1551d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1552c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
1553b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
155461c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
155508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
155608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1557ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
155808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
155908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
15602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
156108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
156208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
15632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
156408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
156508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
15662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
156708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
156808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
156937f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
157008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
157237f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
157308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
157537f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
157637f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
157708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
157937f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
158008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
158108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
158208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
158308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
158408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
158508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
158608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
158750239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
158808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1589daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
159008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
159108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
159208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
159308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
159408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
159508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
159650239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
159708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
159848efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
159908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
160008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
160108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1602d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1603d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1604d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
1606b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
16082ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
16092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1610d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1611c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier    ArtField* fields = klass->GetIFields();
1612c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier    for (size_t i = 0, count = klass->NumInstanceFields(); i < count; i++) {
1613c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      PrintField(os, &fields[i], obj);
1614d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1615d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1616d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
16183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1619f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
16203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1621ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
1622b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1623a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1624a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
16256f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1626ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
16273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1628f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1629ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1630f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1631ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
16323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
16333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1634ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
1635b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1636ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1637ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1638f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1639f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1640f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1641f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1642f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1643ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
1644b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1645ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
16462ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
16472ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1648f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1649ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1650f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1651f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
16522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void Callback(mirror::Object* obj, void* arg)
1653b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16542ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
16552ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
16563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
165778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
165878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
165978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1660916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1661916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1662916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1663916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1664916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1665916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
16662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *state->os_;
16672dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1668d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
16692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
16702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1671d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
16722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
16732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
16742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1675ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
16764b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom      os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
1677ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         PrettyMethod(obj->AsArtMethod()).c_str());
1678d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
16792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
168068b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
168178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
16822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
168378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
16842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
16852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream indent_os(&indent_filter);
16862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    DumpFields(indent_os, obj, obj_class);
1687d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
16882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
1689d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      int32_t length = obj_array->GetLength();
1690d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      for (int32_t i = 0; i < length; i++) {
16912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1692d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1693d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1694d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1695d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1696d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1697d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1698d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1699d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1700d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
17012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d: ", i);
170278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
17032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d to %zd: ", i, i + run);
1704d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
170578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
17062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
17072ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
17082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        PrettyObjectValue(indent_os, value_class, value);
1709d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1710d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1711c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      mirror::Class* klass = obj->AsClass();
1712c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      ArtField* sfields = klass->GetSFields();
1713c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      const size_t num_fields = klass->NumStaticFields();
1714c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      if (num_fields != 0) {
17152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << "STATICS:\n";
17162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
17172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent2_os(&indent2_filter);
1718c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier        for (size_t i = 0; i < num_fields; i++) {
1719c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier          PrintField(indent2_os, &sfields[i], sfields[i].GetDeclaringClass());
1720d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1721d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1722ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
1723a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      const size_t image_pointer_size = InstructionSetPointerSize(
1724a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier          state->oat_dumper_->GetOatInstructionSet());
1725ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = obj->AsArtMethod();
1726d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      if (method->IsNative()) {
1727957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1728a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
17293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first_occurrence;
1730ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1731ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1732ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code, &first_occurrence);
17333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (first_occurrence) {
1734ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
17353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1736a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1737a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            image_pointer_size)) {
1738ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
17393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17401984651929744dd603fd082e23eacd877b9bc177Ian Rogers      } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
174188474b416eb257078e590bf9bc7957cee604a186Jeff Hao          method->IsResolutionMethod() || method->IsImtConflictMethod() ||
17422d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier          method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1743957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1744a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1745916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      } else {
1746bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        const DexFile::CodeItem* code_item = method->GetCodeItem();
1747d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers        size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1748916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom        state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1749e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom
1750a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        bool first_occurrence;
1751957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        size_t gc_map_bytes =
1752957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier            state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1753a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.gc_map_bytes += gc_map_bytes;
17553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t pc_mapping_table_bytes =
1758a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
1759a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
17613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t vmap_table_bytes =
1764a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
1765a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.vmap_table_bytes += vmap_table_bytes;
17673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1769ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1770ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1771ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1772ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1773a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
1774ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.managed_code_bytes += quick_oat_code_size;
17750d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          if (method->IsConstructor()) {
17760d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            if (method->IsStatic()) {
1777ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.class_initializer_code_bytes += quick_oat_code_size;
17780d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1779ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.large_initializer_code_bytes += quick_oat_code_size;
17800d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            }
17810d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1782ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            state->stats_.large_method_code_bytes += quick_oat_code_size;
17830d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
17843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1785ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
17863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1787ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
17882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
17892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
17903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1792ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            vmap_table_bytes + quick_oat_code_size + object_bytes;
17933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double expansion =
1795ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
17963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        state->stats_.ComputeOutliers(total_size, expansion, method);
179778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      }
179878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
17991ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    std::string temp;
18001ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
180178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
180227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
18033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
18043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
18053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
1806a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
18073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
1808a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
18093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
18103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
1811a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
18123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
181427ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
1815916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1816916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
1817916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
18183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
1819916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
1820916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1821916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
1822916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
182332327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
1824916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
1825916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1826916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
18273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
1828916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
1829916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
18300d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
18310d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
18320d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
1833916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
1835916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
18363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
1837916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1838916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
1839916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1840ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    std::vector<mirror::ArtMethod*> method_outlier;
18413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
18423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
1843700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
18443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    explicit Stats()
18463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
18473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
1848916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
1849916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
185032327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
1851916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
1852916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
18533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
1854916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
1855916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
18560d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
18570d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
18580d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
18593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
1860916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
18613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
1862916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
1863916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1864a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
1865277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
1866a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
1867a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
1868a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
1869a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1870a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
1871a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
1872277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
1873a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1874a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
1875277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
1876a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
1877a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
1878277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
1879a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
1880a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
1881916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
18833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
18843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1886916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
1887916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1888916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1889916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1890916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
1891916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1892916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1893916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1894ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
18953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
18963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
18973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
18983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
190000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
1901b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
19023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
19033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
19043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
19053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
19063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
19073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
19083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
19093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
19103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
19113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
19123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
19133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
19143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
19163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
19173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
19183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
19193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
19203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
19223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
19233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
19243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
19253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
19263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
19273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
19283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
19293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
19303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
19313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
19323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
19333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
19343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
19353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
19363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
19373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
19383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
19393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
19413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
1942c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
19433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
19443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
1946c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
19473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
19483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
19493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
19503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
19513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
19523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
19533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
19552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
1956c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
19573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
1958c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
19593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
19613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
19623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
19633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
19643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
19653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
19663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
19673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
19683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
19693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
19703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
19713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
19723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
19733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
19743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
19753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
19763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
19773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
19783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
19803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
19813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
1982c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
19833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
19843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
1986c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
19873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
19883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
19893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
19903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
19913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
19923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
19933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
19952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
1996c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
19973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
1998c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
19993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2001b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
20032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
20042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
20052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
20062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent_os(&indent_filter);
20072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("header_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "object_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
200932327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  "bitmap_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "alignment_bytes =  %8zd (%2.0f%% of art file bytes)\n\n",
20112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
20122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
201332327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
20142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
20152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
201632327098e300ab66671b891a18bf669576fc896aMathieu Chartier        CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
20172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2018916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2020916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
202102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
202202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
202302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
202402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
202502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
20262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2027a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
202802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
202902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
203002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2031916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2032c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2033916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2034916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
20362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
20372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
20392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
204205f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
20432ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
20442ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
20452ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
20462ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
20472ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
20482ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
20492ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
20502ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
20512ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
20522ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
20532ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
20542ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
20552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
205602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
20572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
205802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
205902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
206005f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
206105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
20622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
20632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
20642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
206505f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
206605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
206705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2068c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2069916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
20712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
20722ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
20732ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2074c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2075cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2076c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
20773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2079916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2080916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2081916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2082916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
20830d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
20840d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
20850d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
20860d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
20870d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
20880d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
20890d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
20900d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
20912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
20921d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
20933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
20942cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
20952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2096d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
20973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
209878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
209978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
210000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
210100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                     std::ostream* os) {
210200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
210300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
210400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
210500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
210600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
210700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  gc::Heap* heap = runtime->GetHeap();
21081d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace* image_space = heap->GetImageSpace();
21092ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  CHECK(image_space != nullptr);
211078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  const ImageHeader& image_header = image_space->GetImageHeader();
211178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  if (!image_header.IsValid()) {
21120f5baa079eea76758aee32d7f87cb175deab8308Brian Carlstrom    fprintf(stderr, "Invalid image header %s\n", image_location);
211378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    return EXIT_FAILURE;
211478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
21153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
211600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ImageDumper image_dumper(os, *image_space, image_header, options);
21173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
21182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool success = image_dumper.Dump();
21192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
212078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
212178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
212200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
212300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
212400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
212500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
212600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
212700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
212800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
212900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
213000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
213100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
213200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
213300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
213400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  class_linker->RegisterOatFile(oat_file);
2135fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
213600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
213700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2138fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
213900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
214000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    class_linker->RegisterDexFile(*dex_file);
2141fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    dex_files.push_back(std::move(dex_file));
214200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
214300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
214400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
214500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2146fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<const DexFile*> class_path;
2147fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
2148fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    class_path.push_back(dex_file.get());
2149fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  }
215081c6f8db12b203878a7d72444ead2bc7cf5c47adAndreas Gampe  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
215100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
215200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
215300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
215400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
215500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
215600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
215700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
215832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
215900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
216000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
216100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
216200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
216300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
216432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
216500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
216600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
216700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
216800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
216932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
217000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
217100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
217200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
217300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
217400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
217500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
217600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
21773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2178e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
217900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
218000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
218100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
218200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
218300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
218400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
218500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
218600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
218700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
218800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
218900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
219000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
219100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int SymbolizeOat(const char* oat_filename, std::string& output_name) {
219200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
21933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2194e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
219500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
219600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
219700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
219800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
219900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
220000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  OatSymbolizer oat_symbolizer(oat_file, output_name);
220100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Symbolize()) {
220200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
220300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
220400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
220500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
220600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
220700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
220800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22093774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
22103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
22113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
221200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
22143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
22153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
22163774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
22173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
22183774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
22193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
222000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
222100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
22233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
22243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
22253774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
22263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
22273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
22283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
22293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
22303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
22313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
22323774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
22333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
22343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
22353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
22363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
223732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
223832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
22393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
22403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
224132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
224232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
224332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
224432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
224532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
224632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
224732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
224832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
224932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
225032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
225132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
22523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
22533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
225400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
225500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
22573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
22583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
22603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
22613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
22623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
226300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
226400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
22663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
22673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
22683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
226900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
227000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
22723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
22733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
22743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
22753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
22763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
22773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
22783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
22793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
22813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
22823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
22843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
22853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
22873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
22883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
22893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
22903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
22913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
22923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
22933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
22943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
22953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
22963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
22973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
22983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
23003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
23023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
23033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
23043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the GC map.\n"
23063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
23073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
23093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
23103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
23123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
23133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
231432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
231532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
231632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
231732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
231832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
231932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
232032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
232132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
232232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
232332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
232432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
232532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
232632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
232732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
23283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
23293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
233032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
233132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
233232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
233332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
233432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
233532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
233632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
23373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
23383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
234000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
234100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
234300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
234432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
23453fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
234600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
234700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
234800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
234900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
235000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
235100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
235200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
235332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
235432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
235532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
235632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
235700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
235800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23593774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
23603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
23613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
236200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
23643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
23653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
23673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
23683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
23693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
23703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
23713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
237232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
237332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
237432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
237532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
237632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
237732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
23783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
23803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
238100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
238200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
23843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2385c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
23863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2387d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
23883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2389c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
2390c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2391c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2392c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2393c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
239432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2395c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2396c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
239700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
239800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
24003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
24013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
24033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
24043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
240532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
24063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
240700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
240800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
240932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
24103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      == EXIT_SUCCESS;
241100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
241200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
24143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
241500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24167934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
241778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
241878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
24193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
24203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
242178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2422