oatdump.cc revision f2650d1f957b158496de8016bc43fb575e81d6bc
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,
307f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   bool dump_code_info_stack_maps,
3082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
30900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
31032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
31132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
31232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
31332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
31432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
31532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
3162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    : dump_raw_mapping_table_(dump_raw_mapping_table),
3172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_raw_gc_map_(dump_raw_gc_map),
3182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_vmap_(dump_vmap),
319f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_(dump_code_info_stack_maps),
3202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
32100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
32232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
3233fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
32432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
32532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
32632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
32732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
3283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
3292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
3302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_mapping_table_;
3312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_gc_map_;
3322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
333f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  const bool dump_code_info_stack_maps_;
3342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
3352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
33632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
3373fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
33832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
33932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
34032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
34132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
34200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
3432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
3442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
345e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
346aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
34732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
3489583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
349a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
3502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
35132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
352a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
353a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
35432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
355a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
356a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 true /* can_read_litals_ */))) {
35732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
35832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
35932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
3603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
3613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
3623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
3632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
3642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
3652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
3662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
367a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
368a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
369a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
370a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
3712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
3722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
3733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
374aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
375aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
376aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
377aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
378aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
379ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
380aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
381a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
382a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
383a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
3846f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
3856f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
3866f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
3876f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
3886f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
3896f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
3906f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
3917020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
392aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
393aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
394aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3952ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
3962ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
3972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
39832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
3992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
4002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
4012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
4022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
4032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
4042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
4052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
4062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
4072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
4082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
4092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
4102ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
4112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
4122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
4132ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4142ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
4162ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
4172ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
4182ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
419aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
4212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
4222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
4232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
424a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
42528db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
42628db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
42728db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
42828db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
429700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
43081f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
43122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
43222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
43322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
43422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
43522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
43622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
43722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
43822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
43922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
44022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
44122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
44222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
44381f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
44432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
4452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
4462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
4472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
4492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
4502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
451aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
4532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
454aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
455aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
456aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
45732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
45832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
45932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
46032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
46132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
46232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
46332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
4643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
4662ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
46732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
46832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // If file export selected skip file analysis
46932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.export_dex_location_) {
47032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!ExportDexFile(os, *oat_dex_file)) {
47132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
47232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
47332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
47432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!DumpOatDexFile(os, *oat_dex_file)) {
47532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
47632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
4772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
4783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
4792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
4802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
4813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
4823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
4833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
48413735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
48513735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
4863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
4873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
488ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
489ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
490ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
4913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
492ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
4933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
4943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
495e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
496a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
497f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
498f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
499f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
500ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
5013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5038d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
5048d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
505700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5068d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5078d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5088d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5098d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
510e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5118b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
512e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
5132ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
5148b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
515d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
5163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
517d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
5183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
5193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
520aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5212ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
522aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
523aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
524aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
5253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
526e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
527e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
528e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
529e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
5303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5322ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
5338d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
534700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5358d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5368d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5378d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
539e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
540ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
5412ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
5422ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
5432ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
544e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
545d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
54613735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
5472ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
548e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
549e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
550e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
551e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
552d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
553e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
554e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
555e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
556d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
557e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
558e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
559e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
560e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
561e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
562e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
563e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
564e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
565e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
566ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
567e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
568e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
5702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
5712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
5722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
573e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
57495ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
57595ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
57695ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
57795ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
57895ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
579e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetMappingTableOffset());
580e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
581957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    offsets_.insert(oat_method.GetGcMapOffset());
582e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
583e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
5852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
58632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
5872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
588a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
589ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
590590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
591590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // Create the verifier early.
592590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
5938d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
594700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
5952ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (dex_file.get() == nullptr) {
5968d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
5972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
5982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
599aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
6002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
6012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
6022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
603aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
604aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
60532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
60632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
60732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
60832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
60932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
61032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
6112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
612d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
6132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
6142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
615d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
616d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
617d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
6182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
6192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indented_os(&indent_filter);
62032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
62132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
6222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
6232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
62432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
62532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
62632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
62732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
628aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
629aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
630aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
6312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
632aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
633aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
63432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
63532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
63632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
63732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
63832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
63932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
64032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
64132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
64432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
64632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
64732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
64832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
65232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
65332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
65432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
65532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
65932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
66032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
66432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
66532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
66632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
66732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
66832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
66932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
67132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
67232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
67432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
67532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
67632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
67732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
67832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
68032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
68132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
68232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
68432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
68532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
68632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
68732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
68932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
69032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
69132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
69232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
70232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
70332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
70732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
71032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
71132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
712e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
7130571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
7140571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
715aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7160571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
7170571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
718aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
719e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
720aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
7212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
72232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
7232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
72432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
72513735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
7262ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
7272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
7282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
729e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
730e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
731e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
7322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
7330571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
7342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
7352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
73632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
73932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
74032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
74132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
74232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7440571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
745aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7460571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
7472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
7482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
74932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
75232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
75332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
75432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
75532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7570571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
758aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7590571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
760aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
7612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
762aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
763e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
7642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
7652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
7672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
7682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
7708b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
7712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
7722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
77332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
7742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
77532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
77632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
77732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
77832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
7793fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
7803fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
7813fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
78232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
7832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
7843fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray                       class_method_index, pretty_method.c_str(),
785db7bdc1086c44fe628b6743f98d7d85a5c03343aIan Rogers                       dex_method_idx);
78632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
78732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
7882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
789a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
790a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
791a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
79232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
79432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
79532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
79632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
80032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
80132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
80232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
80332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
80432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
80532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
807a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      *indent1_os << "DEX CODE:\n";
808a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      DumpDexCode(*indent2_os, dex_file, code_item);
8092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8102ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
8112ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
8122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
813a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
8142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
8152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
8162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "OatMethodOffsets ";
81932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("%p ", oat_method_offsets);
8212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
8232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
8242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf(
8252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
8262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
8272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
8282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        os << std::flush;
8292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
831a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
8322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
8332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
8342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
8352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "code offset 0x%08x is past end of file 0x%08zx.\n",
8372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    aligned_code_begin, oat_file_.Size());
8382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
8413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
8422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "gc_map: ";
84332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
844957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
8453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
846957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
8472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
8482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (gc_map_offset > oat_file_.Size()) {
8492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
8512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    gc_map_offset, oat_file_.Size());
8522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
85332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_gc_map_) {
854a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe        Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
8552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent3_os(&indent3_filter);
8562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpGcMap(indent3_os, oat_method, code_item);
8572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
8592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
8602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "OatQuickMethodHeader ";
8612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
8622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
8632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
86432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("%p ", method_header);
8662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
8682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
8692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf(
8702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
8712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
8722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
8732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        os << std::flush;
8742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "mapping_table: ";
87832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
8802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
8822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
8832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (mapping_table_offset > oat_file_.Size()) {
8842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "mapping table offset 0x%08x is past end of file 0x%08zx. "
8862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "mapping table offset was loaded from offset 0x%08x.\n",
8872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    mapping_table_offset, oat_file_.Size(),
8882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    oat_method.GetMappingTableOffsetOffset());
8892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
89032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_mapping_table_) {
891a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe        Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
8922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent3_os(&indent3_filter);
8932cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpMappingTable(indent3_os, oat_method);
8942cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "vmap_table: ";
89732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
8992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
9012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
9022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
9032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
9042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "vmap table offset 0x%08x is past end of file 0x%08zx. "
9052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "vmap table offset was loaded from offset 0x%08x.\n",
9062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    vmap_table_offset, oat_file_.Size(),
9072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    oat_method.GetVmapTableOffsetOffset());
9082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
90932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
910442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpVmapData(*indent2_os, oat_method, code_item);
9112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
9122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
9142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "QuickMethodFrameInfo\n";
9152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
9172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
9182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
9192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
9202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
9212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
9222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
9232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // Based on spill masks from QuickMethodFrameInfo so placed
9262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // after it is dumped, but useful for understanding quick
9272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // code, so dumped here.
9282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpVregLocations(*indent2_os, oat_method, code_item);
9292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "CODE: ";
9322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
9332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
9342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
9352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "code size offset 0x%08x is past end of file 0x%08zx.",
9362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size_offset, oat_file_.Size());
9372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
9392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
9402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
9412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
942590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
94332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
9442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent1_os << StringPrintf("%p ", code);
9452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
9472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_offset,
9482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size_offset,
9492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size,
9502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code != nullptr ? "..." : "");
9512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
9532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "start of code at 0x%08x is past end of file 0x%08zx.",
9552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      aligned_code_begin, oat_file_.Size());
9562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
9572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
9582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
9602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size is 0x%08x loaded from offset 0x%08x.\n",
9612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      aligned_code_end, oat_file_.Size(),
9622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, code_size_offset);
9632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
96432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
9692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
9702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size %d is bigger than max expected threshold of %d. "
9722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size is 0x%08x loaded from offset 0x%08x.\n",
9732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, kMaxCodeSize,
9742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, code_size_offset);
9752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
97632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
98132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
9822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
9832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
9872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
9883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
9893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
9903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
9913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
9923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
9933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
9942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
9953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
9963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
9973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
9983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
9993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
10013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
10033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
10043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
10053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
10073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
10093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
10113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1013442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
1014442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapData(std::ostream& os,
1015442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1016442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1017f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1018f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The optimizing compiler outputs its CodeInfo data in the vmap table.
1019442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1020442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1021442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1022442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
1023f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        DumpCodeInfo(os, code_info, oat_method, *code_item);
1024442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1025442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
1026442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // Otherwise, display the vmap table.
1027442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const uint8_t* raw_table = oat_method.GetVmapTable();
1028442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_table != nullptr) {
1029442b46a087c389a91a0b51547ac9205058432364Roland Levillain        VmapTable vmap_table(raw_table);
1030442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpVmapTable(os, oat_method, vmap_table);
1031442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
103220d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1033442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1034442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1035442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
1036442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpCodeInfo(std::ostream& os,
1037442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1038f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                    const OatFile::OatMethod& oat_method,
1039442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
1040f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    code_info.Dump(os,
1041f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   oat_method.GetCodeOffset(),
1042f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   code_item.registers_size_,
1043f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   options_.dump_code_info_stack_maps_);
1044442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1045442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1046442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a vmap table.
1047442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapTable(std::ostream& os,
1048442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const OatFile::OatMethod& oat_method,
1049442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const VmapTable& vmap_table) {
1050442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool first = true;
1051442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool processing_fp = false;
1052442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1053442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < vmap_table.Size(); i++) {
1054442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint16_t dex_reg = vmap_table[i];
1055442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1056442b46a087c389a91a0b51547ac9205058432364Roland Levillain                                                    processing_fp ? kFloatVReg : kIntVReg);
1057442b46a087c389a91a0b51547ac9205058432364Roland Levillain      os << (first ? "v" : ", v")  << dex_reg;
1058442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp) {
1059442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/r" << cpu_reg;
1060442b46a087c389a91a0b51547ac9205058432364Roland Levillain      } else {
1061442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/fr" << cpu_reg;
1062442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1063442b46a087c389a91a0b51547ac9205058432364Roland Levillain      first = false;
1064442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp && dex_reg == 0xFFFF) {
1065442b46a087c389a91a0b51547ac9205058432364Roland Levillain        processing_fp = true;
1066442b46a087c389a91a0b51547ac9205058432364Roland Levillain        spill_mask = oat_method.GetFpSpillMask();
1067442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1069442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "\n";
10703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1072faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1073faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1074faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1075faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1076faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1077faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1078faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1079faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1080faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1081faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1082faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1083faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1084faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1085faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1086faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1087faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1088faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1089faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1090faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
109115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
109215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
109315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
109415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
109515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
109615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
109715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1098faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1099faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1100faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1101faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1102faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1103faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1104faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1105faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1106faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1107faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1108faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1109faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1110faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1111faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1112faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1113faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1114b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
11152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
11161809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* raw_table = oat_method.GetVmapTable();
11172ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (raw_table != nullptr) {
1118b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      const VmapTable vmap_table(raw_table);
1119b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      uint32_t vmap_offset;
11201809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
11212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
11222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
11232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                       : oat_method.GetCoreSpillMask();
11242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1125b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      } else {
112615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
112715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
112815b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
112915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
113015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
113115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
113215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1133b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        os << "[sp + #" << offset << "]";
1134b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
1135b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1136b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1137b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1138ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1139ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          const DexFile::CodeItem* code_item,
1140ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          size_t num_regs, const uint8_t* reg_bitmap) {
1141ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    bool first = true;
1142ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t reg = 0; reg < num_regs; reg++) {
1143ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1144ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        if (first) {
1145ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << "  v" << reg << " (";
1146ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1147ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1148ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          first = false;
1149ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else {
1150ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ", v" << reg << " (";
1151ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1152ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1153ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        }
1154ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      }
1155ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1156ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (first) {
1157ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "No registers in GC map\n";
1158ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    } else {
1159ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "\n";
1160ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1161ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
1162b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1163b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers                 const DexFile::CodeItem* code_item) {
1164957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1165ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (gc_map_raw == nullptr) {
1166ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return;  // No GC map.
11673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1168ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1169956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    NativePcOffsetToReferenceMap map(gc_map_raw);
1170956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1171956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1172956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes          map.GetNativePcOffset(entry);
1173956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      os << StringPrintf("%p", native_pc);
1174956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
11753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1176aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
1177e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
11783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
1179ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1180ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (quick_code == nullptr) {
11812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      return;
11822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
11831809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
11841809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (table.TotalSize() != 0) {
11851809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
11861809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      std::ostream indent_os(&indent_filter);
11871809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.PcToDexSize() != 0) {
11881809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::PcToDexIterator It;
11891809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "suspend point mappings {\n";
11901809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
11911809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
11921809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
11931809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "}\n";
11941809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      }
11951809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.DexToPcSize() != 0) {
11961809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::DexToPcIterator It;
11971809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "catch entry mappings {\n";
11981809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
11991809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12001809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12011809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "}\n";
12023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
12033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
12053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1206f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  uint32_t DumpInformationAtOffset(std::ostream& os,
1207f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const OatFile::OatMethod& oat_method,
1208f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const DexFile::CodeItem* code_item,
1209f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   size_t offset,
1210f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   bool suspend_point_mapping) {
1211f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1212f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (suspend_point_mapping) {
1213f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        DumpDexRegisterMapAtOffset(os, oat_method, code_item, offset);
1214f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1215f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The return value is not used in the case of a method compiled
1216f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // with the optimizing compiler.
1217f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      return DexFile::kDexNoIndex;
1218f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else {
1219f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      return DumpMappingAtOffset(os, oat_method, offset, suspend_point_mapping);
1220f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1221f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1222f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
12231809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12241809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
12251809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12261809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
12271809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
12281809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12291809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12304b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
12311809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
12321809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1233b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12341809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
12351809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
12361809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12371809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12384b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
12391809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1240b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1241b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1244b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1248957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
12492ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1250b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
12512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1252b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
12532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1254b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1255b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1256b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1257b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
12582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
12592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1260b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1261b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1262b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1263b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
12642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1265b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1266b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1267b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1268b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1269b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1270b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1271b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1272b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1274b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1276590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1277590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1278590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1279590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
12807b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
12812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
12822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
12832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
12842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
12852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
12862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
12872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
12882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
12892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
12902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
12912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
12922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
12932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
12942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
12952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
12962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
12982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
12992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
13012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
13052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
13062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
13082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
13092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
13122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1313b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
13142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1315b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1316b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1317b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
131832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
131932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1320b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1321e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1322e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1323b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1324b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1325f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1326f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // the optimizing compiler?
1327f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1328f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                                    const DexFile::CodeItem* code_item) {
1329f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // If the native GC map is null and the Dex `code_item` is not
1330f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // null, then this method has been compiled with the optimizing
1331f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // compiler.
1332f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    return oat_method.GetGcMap() == nullptr && code_item != nullptr;
1333f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1334f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
1335f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  void DumpDexRegisterMapAtOffset(std::ostream& os,
1336f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const OatFile::OatMethod& oat_method,
1337f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const DexFile::CodeItem* code_item,
1338f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  size_t offset) {
1339f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // This method is only relevant for oat methods compiled with the
1340f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // optimizing compiler.
1341f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1342f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
1343f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // The optimizing compiler outputs its CodeInfo data in the vmap table.
1344f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    const void* raw_code_info = oat_method.GetVmapTable();
1345f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (raw_code_info != nullptr) {
1346f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      CodeInfo code_info(raw_code_info);
1347f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset);
1348f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (stack_map.IsValid()) {
1349f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        stack_map.Dump(os, code_info, oat_method.GetCodeOffset(), code_item->registers_size_);
1350f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1351f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1352f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1353f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
13542ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
13552ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
13562ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
13572ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
13582ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
13592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
13602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1361bf99f77dda749e2b653e8c45259b1fb56e7bb012Mathieu Chartier      StackHandleScope<1> hs(soa.Self());
1362eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1363eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier          hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
136432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
13652ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
13662ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           dex_cache,
136732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                                           *options_.class_loader_,
13682ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           &class_def, code_item,
13692ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           NullHandle<mirror::ArtMethod>(),
13702ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           method_access_flags);
13712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13722ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
13732ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
13742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1376590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
13772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
13782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1379ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1380ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
13812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
13822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
13832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1384956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
13852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "NO CODE!\n";
1386b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1387956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1388ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1389ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1390ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
13912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
1392f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain          DumpInformationAtOffset(os, oat_method, code_item, offset, false);
13932cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
1394ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        offset += disassembler_->Dump(os, quick_native_pc + offset);
13952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
1396f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain          uint32_t dex_pc = DumpInformationAtOffset(os, oat_method, code_item, offset, true);
13972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
13982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
13992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
14002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
14012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1402ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
14032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
14042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1405b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1406b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1407b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
14083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
14092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
141032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
141132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1412a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1413ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
14142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1415aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1416aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
14173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
141827ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
14192ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom  explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
14202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
14212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
14222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
14232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
14242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
142527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
14262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
14282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
14292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
14302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
14312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
143231e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier    os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
143331e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier       << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
143431e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
14352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
14362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1437700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
14382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1439700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1440700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1441700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1442700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1443700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
14442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1445a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1446a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
144746774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
144846774767fcf7780d1455e755729198648d08742eIgor Murashkin
14492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
14502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
14512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
14522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent1_os(&indent1_filter);
14532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
14542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
14552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
14562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
14572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
14582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
14592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
14602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
14612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          std::ostream indent2_os(&indent2_filter);
14622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1463fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
1464277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1465277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1466fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1467277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1468277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1469fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1470fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1471fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1472fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1473fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1474fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
1475277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d: ", j);
1476fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
1477277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1478277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1479fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
14802ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
14812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              PrettyObjectValue(indent2_os, value->GetClass(), value);
14822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
1483277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << j << ": null\n";
14842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1485d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
148634f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
148734f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
148827ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
14892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
149027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1491aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
14922ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
14932ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
14942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
14952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
14968d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
1497a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1498a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
1499e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler      oat_file = OatFile::Open(oat_location, oat_location,
1500e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               nullptr, nullptr, false, nullptr,
1501e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               &error_msg);
1502a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (oat_file == nullptr) {
1503a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        os << "NOT FOUND: " << error_msg << "\n";
15042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
1505a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
1506aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
15072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1508aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
15093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
151078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
151132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
15123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
151302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
15142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
151502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
151602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
151705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
151805f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
15192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1520b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1521b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    // Loop through all the image spaces and dump their objects.
15221d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
15231d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
152450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1525357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1526c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1527c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1528c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1529c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
153090d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
153190d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
153290d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1533c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1534c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromRunnableToSuspended(kNative);
1535c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        ThreadList* thread_list = Runtime::Current()->GetThreadList();
1536bf9fc581e8870faddbd320a935f9a627da724c48Mathieu Chartier        thread_list->SuspendAll(__FUNCTION__);
1537c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->RevokeAllThreadLocalAllocationStacks(self);
1538c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        thread_list->ResumeAll();
1539c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromSuspendedToRunnable();
1540c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
1541357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
15422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
15432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream* saved_os = os_;
15442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
15452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent_os(&indent_filter);
15462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = &indent_os;
15472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
154802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& space : spaces) {
15492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (space->IsImageSpace()) {
15501d54e73444e017d3a65234e0f193846f3e27472bIan Rogers          gc::space::ImageSpace* image_space = space->AsImageSpace();
15512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << "\n";
15532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
15542fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier      }
15552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1556bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
15582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = saved_os;
1559b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
15602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1561700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
15622ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() == nullptr) {
15632ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
15646f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15652ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() != nullptr) {
15662ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
15676f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
15693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
15703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
15713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.alignment_bytes += alignment_bytes;
157232327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
157332327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
15742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    stats_.Dump(os);
15752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
157627ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
15772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
15783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
15792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
15803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1581d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
15823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
15832dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
1584b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15852ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
15862ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
15872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1588d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
15892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
15902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
159168b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
159264b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
15932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
15942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1595ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (type->IsArtMethodClass()) {
1596ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = value->AsArtMethod();
15972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Method: %s\n", method, PrettyMethod(method).c_str());
1598d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
15992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1600d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1601d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1602d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1603c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
1604b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
160561c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
160608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
160708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1608ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
160908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
161008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
16112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
161208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
161308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
16142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
161508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
161608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
16172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
161808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
161908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
162037f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
162108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
162208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
162337f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
162408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
162508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
162637f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
162737f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
162808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
162908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
163037f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
163108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
163208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
163308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
163408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
163508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
163608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
163708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
163850239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
163908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1640daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
164108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
164208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
164308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
164408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
164508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
164608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
164750239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
164808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
164948efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
165008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
165108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
165208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1653d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1654d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1655d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16562dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
1657b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
16592ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
16602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1661d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1662c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier    ArtField* fields = klass->GetIFields();
1663c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier    for (size_t i = 0, count = klass->NumInstanceFields(); i < count; i++) {
1664c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      PrintField(os, &fields[i], obj);
1665d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1666d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1667d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16682dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
16693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1670f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
16713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1672ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
1673b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1674a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1675a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
16766f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1677ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
16783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1679f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1680ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1681f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1682ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
16833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
16843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1685ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
1686b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1687ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1688ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1689f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1690f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1691f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1692f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1693f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1694ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
1695b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1696ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
16972ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
16982ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1699f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1700ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1701f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1702f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
17032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void Callback(mirror::Object* obj, void* arg)
1704b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17052ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
17062ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
17073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
170878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
170978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
171078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1711916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1712916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1713916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1714916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1715916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1716916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
17172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *state->os_;
17182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1719d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
17202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
17212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1722d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
17232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
17242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
17252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1726ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
17274b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom      os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
1728ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         PrettyMethod(obj->AsArtMethod()).c_str());
1729d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
17302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
173168b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
173278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
17332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
173478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
17352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
17362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream indent_os(&indent_filter);
17372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    DumpFields(indent_os, obj, obj_class);
1738d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
17392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
1740d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      int32_t length = obj_array->GetLength();
1741d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      for (int32_t i = 0; i < length; i++) {
17422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1743d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1744d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1745d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1746d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1747d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1748d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1749d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1750d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1751d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
17522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d: ", i);
175378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
17542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d to %zd: ", i, i + run);
1755d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
175678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
17572ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
17582ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
17592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        PrettyObjectValue(indent_os, value_class, value);
1760d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1761d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1762c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      mirror::Class* klass = obj->AsClass();
1763c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      ArtField* sfields = klass->GetSFields();
1764c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      const size_t num_fields = klass->NumStaticFields();
1765c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      if (num_fields != 0) {
17662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << "STATICS:\n";
17672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
17682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent2_os(&indent2_filter);
1769c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier        for (size_t i = 0; i < num_fields; i++) {
1770c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier          PrintField(indent2_os, &sfields[i], sfields[i].GetDeclaringClass());
1771d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1772d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1773ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
1774a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      const size_t image_pointer_size = InstructionSetPointerSize(
1775a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier          state->oat_dumper_->GetOatInstructionSet());
1776ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = obj->AsArtMethod();
1777d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      if (method->IsNative()) {
1778957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1779a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
17803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first_occurrence;
1781ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1782ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1783ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code, &first_occurrence);
17843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (first_occurrence) {
1785ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
17863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1787a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1788a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            image_pointer_size)) {
1789ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
17903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17911984651929744dd603fd082e23eacd877b9bc177Ian Rogers      } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
179288474b416eb257078e590bf9bc7957cee604a186Jeff Hao          method->IsResolutionMethod() || method->IsImtConflictMethod() ||
17932d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier          method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1794957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1795a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1796916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      } else {
1797bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        const DexFile::CodeItem* code_item = method->GetCodeItem();
1798d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers        size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1799916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom        state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1800e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom
1801a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        bool first_occurrence;
1802957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        size_t gc_map_bytes =
1803957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier            state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1804a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
18053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.gc_map_bytes += gc_map_bytes;
18063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
18073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t pc_mapping_table_bytes =
1809a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
1810a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
18113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
18123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
18133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t vmap_table_bytes =
1815a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
1816a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
18173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.vmap_table_bytes += vmap_table_bytes;
18183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
18193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1820ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1821ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1822ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1823ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1824a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
1825ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.managed_code_bytes += quick_oat_code_size;
18260d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          if (method->IsConstructor()) {
18270d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            if (method->IsStatic()) {
1828ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.class_initializer_code_bytes += quick_oat_code_size;
18290d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1830ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.large_initializer_code_bytes += quick_oat_code_size;
18310d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            }
18320d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1833ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            state->stats_.large_method_code_bytes += quick_oat_code_size;
18340d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
18353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1836ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
18373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1838ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
18392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
18402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
18413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1843ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            vmap_table_bytes + quick_oat_code_size + object_bytes;
18443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double expansion =
1846ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
18473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        state->stats_.ComputeOutliers(total_size, expansion, method);
184878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      }
184978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
18501ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    std::string temp;
18511ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
185278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
185327ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
18543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
18553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
18563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
1857a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
18583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
1859a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
18603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
18613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
1862a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
18633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
186527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
1866916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1867916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
1868916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
18693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
1870916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
1871916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1872916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
1873916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
187432327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
1875916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
1876916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1877916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
18783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
1879916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
1880916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
18810d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
18820d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
18830d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
1884916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
1886916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
18873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
1888916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1889916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
1890916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1891ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    std::vector<mirror::ArtMethod*> method_outlier;
18923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
18933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
1894700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
18953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    explicit Stats()
18973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
18983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
1899916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
1900916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
190132327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
1902916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
1903916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
19043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
1905916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
1906916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
19070d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
19080d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
19090d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
19103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
1911916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
19123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
1913916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
1914916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1915a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
1916277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
1917a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
1918a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
1919a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
1920a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1921a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
1922a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
1923277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
1924a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1925a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
1926277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
1927a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
1928a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
1929277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
1930a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
1931a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
1932916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
19333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
19343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
19353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1937916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
1938916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1939916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1940916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1941916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
1942916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1943916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1944916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1945ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
19463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
19473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
19483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
19493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
195100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
1952b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
19533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
19543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
19553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
19563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
19573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
19583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
19593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
19603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
19613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
19623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
19633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
19643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
19653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
19673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
19683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
19693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
19703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
19713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
19733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
19743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
19753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
19763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
19773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
19783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
19793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
19803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
19813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
19823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
19833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
19843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
19853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
19863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
19873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
19883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
19893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
19903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
19923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
1993c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
19943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
19953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
1997c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
19983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
19993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
20003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
20013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
20023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
20033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
20043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
20053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
20062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2007c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
20083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2009c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
20103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
20123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
20133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
20143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
20153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
20163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
20173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
20183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
20193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
20203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
20213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
20223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
20233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
20243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
20253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
20263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
20273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
20283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
20293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
20313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
20323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
2033c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
20343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
20353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2037c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
20383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
20393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
20403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
20413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
20423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
20433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
20443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
20453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
20462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2047c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
20483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2049c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
20503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2052b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
20542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
20552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
20562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
20572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent_os(&indent_filter);
20582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("header_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "object_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
206032327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  "bitmap_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "alignment_bytes =  %8zd (%2.0f%% of art file bytes)\n\n",
20622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
20632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
206432327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
20652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
20662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
206732327098e300ab66671b891a18bf669576fc896aMathieu Chartier        CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
20682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2069916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2071916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
207202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
207302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
207402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
207502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
207602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
20772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2078a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
207902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
208002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
208102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2082916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2083c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2084916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2085916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
20872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
20882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
20902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
209305f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
20942ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
20952ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
20962ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
20972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
20982ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
20992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
21002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
21012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
21022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
21032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
21042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
21052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
21062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
210702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
21082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
210902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
211002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
211105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
211205f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
21132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
21142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
21152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
211605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
211705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
211805f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2119c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2120916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
21222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
21232ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
21242ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2125c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2126cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2127c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
21283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2130916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2131916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2132916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2133916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
21340d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
21350d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
21360d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
21370d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
21380d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
21390d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
21400d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
21410d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
21422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
21431d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
21443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
21452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
2146df2bb1f36c0d755f0bdb2b1562e52669f3de4a09Andreas Gampe  OatDumperOptions* oat_dumper_options_;
2147d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
21483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
214978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
215078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
215100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
215200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                     std::ostream* os) {
215300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
215400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
215500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
215600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
215700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
215800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  gc::Heap* heap = runtime->GetHeap();
21591d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace* image_space = heap->GetImageSpace();
21602ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  CHECK(image_space != nullptr);
216178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  const ImageHeader& image_header = image_space->GetImageHeader();
216278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  if (!image_header.IsValid()) {
21630f5baa079eea76758aee32d7f87cb175deab8308Brian Carlstrom    fprintf(stderr, "Invalid image header %s\n", image_location);
216478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    return EXIT_FAILURE;
216578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
21663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
216700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ImageDumper image_dumper(os, *image_space, image_header, options);
21683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
21692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool success = image_dumper.Dump();
21702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
217178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
217278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
217300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
217400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
217500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
217600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
217700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
217800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
217900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
218000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
218100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
218200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
218300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
218400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
218500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  class_linker->RegisterOatFile(oat_file);
2186fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
218700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
218800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2189fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
219000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
219100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    class_linker->RegisterDexFile(*dex_file);
2192fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    dex_files.push_back(std::move(dex_file));
219300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
219400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
219500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
219600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2197fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<const DexFile*> class_path;
2198fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
2199fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    class_path.push_back(dex_file.get());
2200fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  }
220181c6f8db12b203878a7d72444ead2bc7cf5c47adAndreas Gampe  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
220200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
220300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
220400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
220500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
220600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
220700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
220800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
220932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
221000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
221100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
221200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
221300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
221400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
221532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
221600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
221700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
221800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
221900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
222032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
222100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
222200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
222300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
222400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
222500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
222600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
222700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
22283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2229e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
223000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
223100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
223200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
223300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
223400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
223500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
223600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
223700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
223800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
223900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
224000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
224100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
224200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int SymbolizeOat(const char* oat_filename, std::string& output_name) {
224300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
22443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2245e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
224600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
224700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
224800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
224900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
225000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
225100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  OatSymbolizer oat_symbolizer(oat_file, output_name);
225200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Symbolize()) {
225300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
225400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
225500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
225600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
225700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
225800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
225900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22603774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
22613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
22623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
226300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
22653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
22663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
22673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
22683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
22693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
22703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
227100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
227200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
22743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
22753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
22763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
22773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
22783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
22793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
22803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
22813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
22823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
2283f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else if (option =="--dump:code_info_stack_maps") {
2284f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_ = true;
22853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
22863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
22873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
22883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
22893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
229032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
229132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
22923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
22933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
229432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
229532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
229632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
229732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
229832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
229932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
230032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
230132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
230232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
230332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
230432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
23053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
23063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
230700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
230800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
23103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
23113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
23133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
23143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
23153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
231600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
231700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23183774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
23193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
23203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
23213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
232200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
232300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
23253774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
23263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
23273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
23283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
23293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
23303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
23313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
23323774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
23343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
23353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
23373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
23383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
23403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
23413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
23423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
23433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
23453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
23463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
23473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
23493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
23503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
23513774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
23533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
23553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
23563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
23573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
235819510f02b011e545665f6219e6144c8e47aed5f0Mathieu Chartier        "  --dump:raw_gc_map enables dumping of the GC map.\n"
23593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
23603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
23623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
23633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2364f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2365f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "      Example: --dump:code_info_stack_maps\n"
2366f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "\n"
23673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
23683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
23693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
237032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
237132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
237232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
237332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
237432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
237532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
237632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
237732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
237832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
237932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
238032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
238132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
238232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
238332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
23843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
23853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
238632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
238732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
238832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
238932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
239032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
239132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
239232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
23933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
23943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
239600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
239700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
239900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
240032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
24013fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
240200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
240300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
240400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
240500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
240600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
2407f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  bool dump_code_info_stack_maps_ = false;
240800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
240900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
241032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
241132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
241232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
241332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
241400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
241500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24163774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
24173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
24183774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
241900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
24213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
24223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
24243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
24253774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
24263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
2427f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        args_->dump_code_info_stack_maps_,
24283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
24293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
243032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
243132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
243232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
243332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
243432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
243532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
24363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
24383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
243900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
244000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
24423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2443c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
24443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2445d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
24463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2447c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
2448c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2449c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2450c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2451c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
245232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2453c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2454c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
245500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
245600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
24583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
24593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
24613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
24623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
246332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
24643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
246500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
246600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
246732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
24683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      == EXIT_SUCCESS;
246900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
247000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
24723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
247300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24747934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
247578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
247678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
24773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
24783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
247978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2480