oatdump.cc revision 2cebb24bfc3247d3e9be138a3350106737455918
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
790279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogersclass OatSymbolizer FINAL : public CodeOutput {
8054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe public:
810279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
820279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers      oat_file_(oat_file), builder_(nullptr), elf_output_(nullptr),
830279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers      output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
840279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  }
8554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
8654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Init() {
8754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Elf32_Word oat_data_size = oat_file_->GetOatHeader().GetExecutableOffset();
8854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
8954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t diff = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
9054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t oat_exec_size = diff - oat_data_size;
915c42c29b89286e5efa4a4613132b09051ce5945bVladimir Marko    uint32_t oat_bss_size = oat_file_->BssSize();
9254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
9354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    elf_output_ = OS::CreateEmptyFile(output_name_.c_str());
9454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
95533c207f9d2da6d913c4b10f6f757fe9d6367b10David Srbecky    builder_.reset(new ElfBuilder<ElfTypes32>(
9654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        this,
9754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        elf_output_,
9854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        oat_file_->GetOatHeader().GetInstructionSet(),
9954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        0,
10054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        oat_data_size,
10154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        oat_data_size,
10254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        oat_exec_size,
1035c42c29b89286e5efa4a4613132b09051ce5945bVladimir Marko        RoundUp(oat_data_size + oat_exec_size, kPageSize),
1045c42c29b89286e5efa4a4613132b09051ce5945bVladimir Marko        oat_bss_size,
10554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        true,
10654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        false));
10754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
10854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (!builder_->Init()) {
10954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      builder_.reset(nullptr);
11054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return false;
11154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
11254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return true;
11454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
11554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
11754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
11854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const OatFile::OatMethod&,
11954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile&,
12054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
12154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile::CodeItem*,
12254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t);
12354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
12454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Symbolize() {
12554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (builder_.get() == nullptr) {
12654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return false;
12754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
12854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
12954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::RegisterForDedup);
13054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    NormalizeState();
13254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::AddSymbol);
13454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    bool result = builder_->Write();
13654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1374303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    // Ignore I/O errors.
1384303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    UNUSED(elf_output_->FlushClose());
13954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return result;
14154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
14254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void Walk(Callback callback) {
14454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
14554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t i = 0; i < oat_dex_files.size(); i++) {
14654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
1472cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      CHECK(oat_dex_file != nullptr);
14854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatDexFile(oat_dex_file, callback);
14954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
15054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
15154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
15254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
15354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::string error_msg;
15454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
15554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dex_file.get() == nullptr) {
15654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
15754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
15854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t class_def_index = 0;
15954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index < dex_file->NumClassDefs();
16054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index++) {
16154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
16254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
16354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      OatClassType type = oat_class.GetType();
16454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      switch (type) {
16554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassAllCompiled:
16654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassSomeCompiled:
16754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
16854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
16954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassNoneCompiled:
17154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassMax:
17254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          // Ignore.
17354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
17454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
17554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
17654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
17754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
17954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                    const DexFile::ClassDef& class_def, Callback callback) {
18013735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
18154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
18254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
18354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
18454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    // Note: even if this is an interface or a native class, we still have to walk it, as there
18554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    //       might be a static initializer.
18654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    ClassDataItemIterator it(dex_file, class_data);
18754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    SkipAllFields(&it);
18854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t class_method_idx = 0;
18954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextDirectMethod()) {
19054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
19154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
1925182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
19354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
19454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
19554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
19654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextVirtualMethod()) {
19754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
19854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
1995182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
20054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
20154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
20254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DCHECK(!it.HasNext());
20454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
20554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
20654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
20754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     const OatFile::OatMethod& oat_method, const DexFile& dex_file,
20854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
20954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t method_access_flags, Callback callback) {
21054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if ((method_access_flags & kAccAbstract) != 0) {
21154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // Abstract method, no code.
21254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
21354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
21454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (oat_method.GetCodeOffset() == 0) {
21554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // No code.
21654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
21754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
21854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
21954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
22054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                      method_access_flags);
22154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
22254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2236a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2246a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t class_method_index ATTRIBUTE_UNUSED,
2256a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const OatFile::OatMethod& oat_method,
2266a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile& dex_file ATTRIBUTE_UNUSED,
2276a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t dex_method_idx ATTRIBUTE_UNUSED,
2286a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2296a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t method_access_flags ATTRIBUTE_UNUSED) {
23054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[oat_method.GetCodeOffset()]++;
23154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
23254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
23354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void NormalizeState() {
23454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (auto& x : state_) {
23554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (x.second == 1) {
23654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        state_[x.first] = 0;
23754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
23854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
23954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
24054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
24154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  enum class DedupState {  // private
24254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kNotDeduplicated,
24354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedFirst,
24454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedOther
24554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  };
24654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  DedupState IsDuplicated(uint32_t offset) {
24754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 0) {
24854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kNotDeduplicated;
24954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 1) {
25154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kDeduplicatedOther;
25254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[offset] = 1;
25454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return DedupState::kDeduplicatedFirst;
25554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
25654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2576a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2586a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t class_method_index ATTRIBUTE_UNUSED,
2596a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const OatFile::OatMethod& oat_method,
2606a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile& dex_file,
2616a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t dex_method_idx,
2626a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2636a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
26454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
26554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dedup != DedupState::kDeduplicatedOther) {
26654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
26754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
26854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (dedup == DedupState::kDeduplicatedFirst) {
26954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        pretty_name = "[Dedup]" + pretty_name;
27054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
27154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
272533c207f9d2da6d913c4b10f6f757fe9d6367b10David Srbecky      ElfSymtabBuilder<ElfTypes32>* symtab = builder_->GetSymtabBuilder();
27354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2740279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers      symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
2750279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
2760279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
27754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
27854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
27954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
280f4da675bbc4615c5f854c81964cac9dd1153baeaVladimir Marko  // Set oat data offset. Required by ElfBuilder/CodeOutput.
2816a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void SetCodeOffset(size_t offset ATTRIBUTE_UNUSED) {
282f4da675bbc4615c5f854c81964cac9dd1153baeaVladimir Marko    // Nothing to do.
283f4da675bbc4615c5f854c81964cac9dd1153baeaVladimir Marko  }
284f4da675bbc4615c5f854c81964cac9dd1153baeaVladimir Marko
28554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  // Write oat code. Required by ElfBuilder/CodeOutput.
28654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Write(OutputStream* out) {
28754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
28854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
28954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
29054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe private:
29154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  static void SkipAllFields(ClassDataItemIterator* it) {
29254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextStaticField()) {
29354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
29454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextInstanceField()) {
29654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
29754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
29954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
30054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  const OatFile* oat_file_;
301533c207f9d2da6d913c4b10f6f757fe9d6367b10David Srbecky  std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
30254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  File* elf_output_;
30354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  std::unordered_map<uint32_t, uint32_t> state_;
3040279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  const std::string output_name_;
30554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe};
30654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass OatDumperOptions {
3082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
3092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  OatDumperOptions(bool dump_raw_mapping_table,
3102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_raw_gc_map,
3112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_vmap,
3122cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
31300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
31432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
31532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
31632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
31732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
31832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
31932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
3202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    : dump_raw_mapping_table_(dump_raw_mapping_table),
3212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_raw_gc_map_(dump_raw_gc_map),
3222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_vmap_(dump_vmap),
3232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
32400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
32532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
3263fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
32732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
32832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
32932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
33032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
3313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
3322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
3332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_mapping_table_;
3342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_gc_map_;
3352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
3362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
3372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
33832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
3393fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
34032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
34132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
34232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
34332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
34400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
3452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
3462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
347e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
348aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
34932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
3509583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
351a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
3522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
35332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
354a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
355a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
35632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
357a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
358a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 true /* can_read_litals_ */))) {
35932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
36032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
36132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
3623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
3633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
3643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
3652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
3662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
3672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
3682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
369a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
370a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
371a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
372a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
3732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
3742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
3753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
376aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
377aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
378aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
379aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
380aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
381ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
382aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
383a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
384a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
385a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
3866f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
3876f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
3886f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
3896f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
3906f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
3916f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
3926f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
3937020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
394aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
395aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
396aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
3972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
3982ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
3992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
40032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
4012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
4022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
4032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
4042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
4052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
4062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
4072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
4082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
4092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
4102ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
4112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
4122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
4132ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
4142ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
4152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4162ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4172ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
4182ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
4192ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
4202ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
421aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
4232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
4242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
4252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
426a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
42728db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
42828db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
42928db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
43028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
431700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
43281f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
43322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
43422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
43522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
43622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
43722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
43822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
43922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
44022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
44122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
44222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
44322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
44422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
44581f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
44632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
4472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
4482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
4492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
4512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
4522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
453aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
4552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
456aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
457aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
458aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
45932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
46032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
46132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
46232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
46332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
46432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
46532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
4663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
4682ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
46932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
47032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // If file export selected skip file analysis
47132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.export_dex_location_) {
47232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!ExportDexFile(os, *oat_dex_file)) {
47332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
47432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
47532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
47632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!DumpOatDexFile(os, *oat_dex_file)) {
47732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
47832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
4792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
4803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
4812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
4822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
4833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
4843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
4853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
48613735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
48713735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
4883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
4893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
490ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
491ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
492ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
4933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
494ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
4953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
4963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
497e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
498a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
499f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
500f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
501f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
502ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
5033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5058d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
5068d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
507700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5088d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5098d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5108d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5118d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
512e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5138b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
514e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
5152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
5168b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
517d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
5183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
519d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
5203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
5213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
522aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5232ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
524aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
525aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
526aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
5273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
528e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
529e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
530e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
531e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
5323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5342ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
5358d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
536700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5378d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5388d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5398d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
541e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
542ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
5432ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
5442ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
5452ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
546e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
547d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
54813735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
5492ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
550e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
551e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
552e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
553e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
554d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
555e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
556e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
557e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
558d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
559e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
560e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
561e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
562e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
563e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
564e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
565e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
566e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
567e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
568ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
569e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
570e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
5722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
5732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
5742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
575e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
57695ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
57795ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
57895ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
57995ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
58095ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
581e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetMappingTableOffset());
582e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
583957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    offsets_.insert(oat_method.GetGcMapOffset());
584e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
585e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
5872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
58832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
5892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
590a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
591ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
592590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
593590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // Create the verifier early.
594590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
5958d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
596700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
5972ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (dex_file.get() == nullptr) {
5988d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
5992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
6002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
601aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
6022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
6032ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
6042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
605aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
606aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
60732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
60832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
60932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
61032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
61132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
61232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
6132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
614d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
6152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
6162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
617d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
618d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
619d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
6202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
6212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indented_os(&indent_filter);
62232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
62332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
6242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
6252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
62632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
62732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
62832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
62932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
630aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
631aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
632aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
6332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
634aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
635aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
63632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
63732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
63832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
63932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
64132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
64232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
64332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
64632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
64832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
64932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
65032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
65432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
65532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
65632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
65732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
66132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
66232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
66632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
66732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
66832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
66932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
67032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
67132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
67432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
67532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
67632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
67732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
67832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
67932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
68132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
68232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
68332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
68432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
68632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
68732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
68832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
68932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
69232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
69332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
69432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
70432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
70532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
70932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
71032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
71132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
71232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
71332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
714e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
7150571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
7160571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
717aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7180571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
7190571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
720aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
721e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
722aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
7232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
72432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
7252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
72632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
72713735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
7282ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
7292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
7302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
731e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
732e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
733e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
7342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
7350571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
7362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
7372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
73832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
74132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
74232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
74332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
74432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7460571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
747aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7480571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
7492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
7502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
75132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
75432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
75532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
75632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
75732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7590571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
760aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7610571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
762aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
7632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
764aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
765e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
7662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
7672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
7692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
7702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
7728b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
7732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
7742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
77532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
7762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
77732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
77832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
77932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
78032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
7813fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
7823fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
7833fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
78432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
7852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
7863fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray                       class_method_index, pretty_method.c_str(),
787db7bdc1086c44fe628b6743f98d7d85a5c03343aIan Rogers                       dex_method_idx);
78832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
78932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
7902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
791a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
792a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
793a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
79432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
79632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
80032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
80132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
80232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
80332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
80432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
80532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
80632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
80732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
809a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      *indent1_os << "DEX CODE:\n";
810a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      DumpDexCode(*indent2_os, dex_file, code_item);
8112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
8132ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
8142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
815a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
8162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
8172ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
8182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "OatMethodOffsets ";
82132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("%p ", oat_method_offsets);
8232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
8252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
8262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf(
8272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
8282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
8292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
8302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        os << std::flush;
8312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
833a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
8342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
8352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
8362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
8372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "code offset 0x%08x is past end of file 0x%08zx.\n",
8392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    aligned_code_begin, oat_file_.Size());
8402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
8433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
8442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "gc_map: ";
84532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
846957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
8473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
848957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
8492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
8502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (gc_map_offset > oat_file_.Size()) {
8512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
8532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    gc_map_offset, oat_file_.Size());
8542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
85532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_gc_map_) {
856a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe        Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
8572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent3_os(&indent3_filter);
8582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpGcMap(indent3_os, oat_method, code_item);
8592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
8612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
8622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "OatQuickMethodHeader ";
8632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
8642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
8652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
86632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("%p ", method_header);
8682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
8702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
8712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf(
8722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
8732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
8742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
8752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        os << std::flush;
8762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "mapping_table: ";
88032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
8822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
8842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
8852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (mapping_table_offset > oat_file_.Size()) {
8862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "mapping table offset 0x%08x is past end of file 0x%08zx. "
8882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "mapping table offset was loaded from offset 0x%08x.\n",
8892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    mapping_table_offset, oat_file_.Size(),
8902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    oat_method.GetMappingTableOffsetOffset());
8912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
89232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_mapping_table_) {
893a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe        Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
8942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent3_os(&indent3_filter);
8952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpMappingTable(indent3_os, oat_method);
8962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "vmap_table: ";
89932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
9012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
9032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
9042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
9052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
9062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "vmap table offset 0x%08x is past end of file 0x%08zx. "
9072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "vmap table offset was loaded from offset 0x%08x.\n",
9082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    vmap_table_offset, oat_file_.Size(),
9092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    oat_method.GetVmapTableOffsetOffset());
9102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
91132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
912442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpVmapData(*indent2_os, oat_method, code_item);
9132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
9142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
9162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "QuickMethodFrameInfo\n";
9172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
9192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
9202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
9212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
9222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
9232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
9242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
9252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // Based on spill masks from QuickMethodFrameInfo so placed
9282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // after it is dumped, but useful for understanding quick
9292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // code, so dumped here.
9302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpVregLocations(*indent2_os, oat_method, code_item);
9312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "CODE: ";
9342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
9352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
9362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
9372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "code size offset 0x%08x is past end of file 0x%08zx.",
9382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size_offset, oat_file_.Size());
9392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
9412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
9422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
9432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
944590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
94532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
9462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent1_os << StringPrintf("%p ", code);
9472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
9492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_offset,
9502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size_offset,
9512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size,
9522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code != nullptr ? "..." : "");
9532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
9552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "start of code at 0x%08x is past end of file 0x%08zx.",
9572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      aligned_code_begin, oat_file_.Size());
9582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
9592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
9602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
9622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size is 0x%08x loaded from offset 0x%08x.\n",
9632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      aligned_code_end, oat_file_.Size(),
9642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, code_size_offset);
9652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
96632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
9712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
9722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size %d is bigger than max expected threshold of %d. "
9742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size is 0x%08x loaded from offset 0x%08x.\n",
9752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, kMaxCodeSize,
9762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, code_size_offset);
9772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
97832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
98332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
9842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
9852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
9892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
9903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
9913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
9923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
9933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
9943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
9953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
9962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
9973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
9983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
9993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
10003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
10013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
10033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
10053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
10063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
10073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
10093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
10113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
10133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1015442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
1016442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapData(std::ostream& os,
1017442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1018442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1019957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    if (oat_method.GetGcMap() == nullptr) {
1020442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // If the native GC map is null, then this method has been
1021442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // compiled with the optimizing compiler. The optimizing
1022442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // compiler currently outputs its stack maps in the vmap table.
1023442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1024442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1025442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1026442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
1027442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpCodeInfo(os, code_info, *code_item);
1028442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1029442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
1030442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // Otherwise, display the vmap table.
1031442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const uint8_t* raw_table = oat_method.GetVmapTable();
1032442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_table != nullptr) {
1033442b46a087c389a91a0b51547ac9205058432364Roland Levillain        VmapTable vmap_table(raw_table);
1034442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpVmapTable(os, oat_method, vmap_table);
1035442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
103620d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1037442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1038442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1039442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
1040442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpCodeInfo(std::ostream& os,
1041442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1042442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
1043004c230b4cfc856690c61faabc41864061813c88Nicolas Geoffray    code_info.Dump(os, code_item.registers_size_);
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
12061809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12071809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
12081809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12091809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
12101809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
12111809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12121809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12134b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
12141809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
12151809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1216b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12171809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
12181809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
12191809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12201809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12214b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
12221809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1223b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1224b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1227b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1231957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
12322ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1233b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
12342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1235b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
12362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1237b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1238b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1239b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1240b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
12412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
12422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1243b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1244b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1245b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1246b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
12472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1248b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1249b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1250b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1251b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1252b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1253b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1254b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1255b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1257b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1259590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1260590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1261590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1262590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
12637b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
12642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
12652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
12662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
12672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
12682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
12692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
12702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
12712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
12722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
12732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
12742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
12752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
12762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
12772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
12782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
12792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
12812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
12822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
12842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
12852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
12872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
12882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
12892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
12912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
12922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
12942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
12952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1296b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
12972ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1298b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1299b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1300b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
130132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
130232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1303b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1304e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1305e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1306b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1307b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
13082ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
13092ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
13102ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
13112ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
13122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
13132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
13142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1315bf99f77dda749e2b653e8c45259b1fb56e7bb012Mathieu Chartier      StackHandleScope<1> hs(soa.Self());
1316eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1317eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier          hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
131832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
13192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
13202ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           dex_cache,
132132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                                           *options_.class_loader_,
13222ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           &class_def, code_item,
13232ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           NullHandle<mirror::ArtMethod>(),
13242ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           method_access_flags);
13252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13262ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
13272ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
13282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1330590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
13312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
13322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1333ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1334ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
13352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
13362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
13372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1338956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
13392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "NO CODE!\n";
1340b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1341956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1342ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1343ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1344ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
13452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
13462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          DumpMappingAtOffset(os, oat_method, offset, false);
13472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
1348ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        offset += disassembler_->Dump(os, quick_native_pc + offset);
13492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
13502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
13512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
13522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
13532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
13542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
13552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1356ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
13572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1359b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1360b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1361b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
13623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
13632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
136432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
136532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1366a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1367ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
13682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1369aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1370aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
13713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
137227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
13732ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom  explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
13742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
13752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
13762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
13772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
13782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
137927ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
13802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
13822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
13832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
13842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
13852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
138631e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier    os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
138731e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier       << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
138831e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
13892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
13902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1391700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
13922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1393700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1394700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1395700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1396700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1397700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
13982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1399a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1400a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
140146774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
140246774767fcf7780d1455e755729198648d08742eIgor Murashkin
14032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
14042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
14052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
14062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent1_os(&indent1_filter);
14072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
14082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
14092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
14102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
14112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
14122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
14132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
14142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
14152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          std::ostream indent2_os(&indent2_filter);
14162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1417fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
1418277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1419277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1420fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1421277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1422277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1423fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1424fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1425fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1426fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1427fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1428fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
1429277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d: ", j);
1430fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
1431277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1432277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1433fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
14342ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
14352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              PrettyObjectValue(indent2_os, value->GetClass(), value);
14362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
1437277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << j << ": null\n";
14382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1439d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
144034f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
144134f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
144227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
14432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
144427ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1445aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
14462ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
14472ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
14482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
14492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
14508d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
1451a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1452a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
1453e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler      oat_file = OatFile::Open(oat_location, oat_location,
1454e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               nullptr, nullptr, false, nullptr,
1455e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               &error_msg);
1456a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (oat_file == nullptr) {
1457a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        os << "NOT FOUND: " << error_msg << "\n";
14582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
1459a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
1460aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
14612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1462aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
14633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
146478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
146532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
14663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
146702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
14682ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
146902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
147002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
147105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
147205f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
14732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1474b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1475b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    // Loop through all the image spaces and dump their objects.
14761d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
14771d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
147850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1479357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1480c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1481c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1482c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1483c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
148490d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
148590d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
148690d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1487c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1488c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromRunnableToSuspended(kNative);
1489c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        ThreadList* thread_list = Runtime::Current()->GetThreadList();
1490bf9fc581e8870faddbd320a935f9a627da724c48Mathieu Chartier        thread_list->SuspendAll(__FUNCTION__);
1491c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->RevokeAllThreadLocalAllocationStacks(self);
1492c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        thread_list->ResumeAll();
1493c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromSuspendedToRunnable();
1494c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
1495357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
14962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
14972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream* saved_os = os_;
14982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
14992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent_os(&indent_filter);
15002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = &indent_os;
15012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
150202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& space : spaces) {
15032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (space->IsImageSpace()) {
15041d54e73444e017d3a65234e0f193846f3e27472bIan Rogers          gc::space::ImageSpace* image_space = space->AsImageSpace();
15052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << "\n";
15072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
15082fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier      }
15092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1510bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
15122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = saved_os;
1513b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
15142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1515700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
15162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() == nullptr) {
15172ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
15186f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() != nullptr) {
15202ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
15216f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
15233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
15243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
15253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.alignment_bytes += alignment_bytes;
152632327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
152732327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
15282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    stats_.Dump(os);
15292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
153027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
15312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
15323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
15332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
15343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1535d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
15363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
15372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
1538b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15392ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
15402ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
15412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1542d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
15432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
15442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
154568b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
154664b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
15472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
15482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1549ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (type->IsArtMethodClass()) {
1550ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = value->AsArtMethod();
15512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Method: %s\n", method, PrettyMethod(method).c_str());
1552d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
15532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1554d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1555d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1556d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1557c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
1558b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
155961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
156008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
156108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1562ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
156308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
156408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
15652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
156608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
156708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
15682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
156908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
15712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
157208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
157437f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
157508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
157737f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
157808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
158037f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
158137f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
158208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
158308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
158437f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
158508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
158608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
158708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
158808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
158908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
159008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
159108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
159250239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
159308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1594daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
159508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
159608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
159708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
159808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
159908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
160008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
160150239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
160208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
160348efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
160408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
160508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
160608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1607d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1608d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1609d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
1611b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
16132ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
16142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1615d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1616c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier    ArtField* fields = klass->GetIFields();
1617c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier    for (size_t i = 0, count = klass->NumInstanceFields(); i < count; i++) {
1618c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      PrintField(os, &fields[i], obj);
1619d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1620d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1621d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
16233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1624f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
16253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1626ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
1627b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1628a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1629a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
16306f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1631ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
16323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1633f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1634ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1635f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1636ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
16373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
16383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1639ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
1640b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1641ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1642ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1643f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1644f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1645f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1646f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1647f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1648ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
1649b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1650ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
16512ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
16522ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1653f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1654ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1655f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1656f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
16572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void Callback(mirror::Object* obj, void* arg)
1658b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16592ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
16602ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
16613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
166278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
166378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
166478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1665916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1666916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1667916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1668916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1669916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1670916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
16712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *state->os_;
16722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1673d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
16742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
16752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1676d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
16772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
16782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
16792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1680ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
16814b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom      os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
1682ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         PrettyMethod(obj->AsArtMethod()).c_str());
1683d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
16842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
168568b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
168678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
16872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
168878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
16892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
16902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream indent_os(&indent_filter);
16912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    DumpFields(indent_os, obj, obj_class);
1692d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
16932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
1694d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      int32_t length = obj_array->GetLength();
1695d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      for (int32_t i = 0; i < length; i++) {
16962dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1697d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1698d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1699d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1700d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1701d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1702d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1703d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1704d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1705d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
17062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d: ", i);
170778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
17082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d to %zd: ", i, i + run);
1709d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
171078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
17112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
17122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
17132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        PrettyObjectValue(indent_os, value_class, value);
1714d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1715d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1716c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      mirror::Class* klass = obj->AsClass();
1717c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      ArtField* sfields = klass->GetSFields();
1718c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      const size_t num_fields = klass->NumStaticFields();
1719c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      if (num_fields != 0) {
17202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << "STATICS:\n";
17212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
17222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent2_os(&indent2_filter);
1723c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier        for (size_t i = 0; i < num_fields; i++) {
1724c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier          PrintField(indent2_os, &sfields[i], sfields[i].GetDeclaringClass());
1725d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1726d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1727ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
1728a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      const size_t image_pointer_size = InstructionSetPointerSize(
1729a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier          state->oat_dumper_->GetOatInstructionSet());
1730ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = obj->AsArtMethod();
1731d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      if (method->IsNative()) {
1732957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1733a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
17343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first_occurrence;
1735ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1736ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1737ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code, &first_occurrence);
17383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (first_occurrence) {
1739ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
17403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1741a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1742a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            image_pointer_size)) {
1743ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
17443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17451984651929744dd603fd082e23eacd877b9bc177Ian Rogers      } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
174688474b416eb257078e590bf9bc7957cee604a186Jeff Hao          method->IsResolutionMethod() || method->IsImtConflictMethod() ||
17472d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier          method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1748957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1749a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1750916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      } else {
1751bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        const DexFile::CodeItem* code_item = method->GetCodeItem();
1752d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers        size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1753916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom        state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1754e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom
1755a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        bool first_occurrence;
1756957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        size_t gc_map_bytes =
1757957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier            state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1758a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.gc_map_bytes += gc_map_bytes;
17603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t pc_mapping_table_bytes =
1763a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
1764a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
17663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t vmap_table_bytes =
1769a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
1770a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.vmap_table_bytes += vmap_table_bytes;
17723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1774ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1775ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1776ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1777ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1778a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
1779ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.managed_code_bytes += quick_oat_code_size;
17800d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          if (method->IsConstructor()) {
17810d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            if (method->IsStatic()) {
1782ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.class_initializer_code_bytes += quick_oat_code_size;
17830d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1784ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.large_initializer_code_bytes += quick_oat_code_size;
17850d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            }
17860d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1787ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            state->stats_.large_method_code_bytes += quick_oat_code_size;
17880d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
17893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1790ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
17913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1792ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
17932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
17942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
17953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1797ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            vmap_table_bytes + quick_oat_code_size + object_bytes;
17983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double expansion =
1800ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
18013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        state->stats_.ComputeOutliers(total_size, expansion, method);
180278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      }
180378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
18041ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    std::string temp;
18051ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
180678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
180727ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
18083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
18093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
18103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
1811a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
18123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
1813a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
18143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
18153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
1816a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
18173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
181927ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
1820916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1821916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
1822916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
18233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
1824916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
1825916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1826916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
1827916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
182832327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
1829916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
1830916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1831916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
18323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
1833916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
1834916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
18350d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
18360d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
18370d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
1838916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
1840916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
18413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
1842916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1843916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
1844916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1845ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    std::vector<mirror::ArtMethod*> method_outlier;
18463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
18473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
1848700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
18493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    explicit Stats()
18513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
18523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
1853916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
1854916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
185532327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
1856916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
1857916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
18583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
1859916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
1860916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
18610d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
18620d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
18630d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
18643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
1865916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
18663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
1867916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
1868916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1869a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
1870277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
1871a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
1872a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
1873a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
1874a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1875a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
1876a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
1877277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
1878a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1879a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
1880277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
1881a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
1882a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
1883277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
1884a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
1885a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
1886916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
18883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
18893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1891916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
1892916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1893916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1894916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1895916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
1896916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1897916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1898916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1899ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
19003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
19013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
19023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
19033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
190500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
1906b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
19073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
19083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
19093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
19103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
19113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
19123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
19133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
19143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
19153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
19163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
19173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
19183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
19193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
19213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
19223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
19233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
19243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
19253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
19273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
19283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
19293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
19303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
19313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
19323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
19333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
19343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
19353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
19363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
19373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
19383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
19393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
19403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
19413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
19423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
19433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
19443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
19463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
1947c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
19483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
19493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
1951c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
19523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
19533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
19543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
19553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
19563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
19573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
19583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
19602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
1961c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
19623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
1963c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
19643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
19663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
19673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
19683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
19693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
19703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
19713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
19723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
19733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
19743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
19753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
19763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
19773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
19783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
19793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
19803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
19813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
19823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
19833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
19853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
19863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
1987c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
19883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
19893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
1991c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
19923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
19933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
19943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
19953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
19963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
19973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
19983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
20002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2001c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
20023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2003c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
20043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2006b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
20082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
20092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
20102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
20112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent_os(&indent_filter);
20122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("header_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "object_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
201432327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  "bitmap_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "alignment_bytes =  %8zd (%2.0f%% of art file bytes)\n\n",
20162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
20172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
201832327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
20192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
20202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
202132327098e300ab66671b891a18bf669576fc896aMathieu Chartier        CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
20222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2023916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2025916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
202602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
202702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
202802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
202902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
203002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
20312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2032a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
203302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
203402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
203502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2036916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2037c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2038916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2039916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
20412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
20422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
20442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
204705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
20482ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
20492ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
20502ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
20512ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
20522ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
20532ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
20542ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
20552ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
20562ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
20572ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
20582ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
20592ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
20602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
206102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
20622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
206302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
206402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
206505f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
206605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
20672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
20682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
20692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
207005f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
207105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
207205f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2073c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2074916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
20762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
20772ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
20782ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2079c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2080cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2081c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
20823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2084916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2085916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2086916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2087916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
20880d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
20890d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
20900d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
20910d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
20920d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
20930d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
20940d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
20950d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
20962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
20971d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
20983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
20992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
21002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2101d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
21023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
210378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
210478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
210500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
210600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                     std::ostream* os) {
210700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
210800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
210900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
211000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
211100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
211200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  gc::Heap* heap = runtime->GetHeap();
21131d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace* image_space = heap->GetImageSpace();
21142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  CHECK(image_space != nullptr);
211578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  const ImageHeader& image_header = image_space->GetImageHeader();
211678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  if (!image_header.IsValid()) {
21170f5baa079eea76758aee32d7f87cb175deab8308Brian Carlstrom    fprintf(stderr, "Invalid image header %s\n", image_location);
211878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    return EXIT_FAILURE;
211978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
21203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
212100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ImageDumper image_dumper(os, *image_space, image_header, options);
21223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
21232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool success = image_dumper.Dump();
21242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
212578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
212678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
212700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
212800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
212900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
213000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
213100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
213200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
213300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
213400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
213500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
213600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
213700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
213800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
213900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  class_linker->RegisterOatFile(oat_file);
2140fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
214100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
214200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2143fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
214400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
214500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    class_linker->RegisterDexFile(*dex_file);
2146fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    dex_files.push_back(std::move(dex_file));
214700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
214800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
214900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
215000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2151fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<const DexFile*> class_path;
2152fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
2153fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    class_path.push_back(dex_file.get());
2154fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  }
215581c6f8db12b203878a7d72444ead2bc7cf5c47adAndreas Gampe  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
215600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
215700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
215800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
215900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
216000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
216100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
216200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
216332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
216400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
216500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
216600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
216700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
216800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
216932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
217000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
217100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
217200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
217300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
217432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
217500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
217600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
217700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
217800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
217900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
218000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
218100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
21823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2183e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
218400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
218500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
218600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
218700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
218800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
218900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
219000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
219100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
219200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
219300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
219400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
219500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
219600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int SymbolizeOat(const char* oat_filename, std::string& output_name) {
219700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
21983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2199e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
220000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
220100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
220200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
220300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
220400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
220500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  OatSymbolizer oat_symbolizer(oat_file, output_name);
220600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Init()) {
220700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to initialize symbolizer\n");
220800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
220900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
221000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Symbolize()) {
221100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
221200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
221300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
221400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
221500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
221600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
221700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22183774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
22193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
22203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
222100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
22233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
22243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
22253774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
22263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
22273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
22283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
222900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
223000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
22323774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
22333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
22343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
22353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
22363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
22373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
22383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
22393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
22403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
22413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
22423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
22433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
22443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
22453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
224632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
224732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
22483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
22493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
225032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
225132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
225232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
225332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
225432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
225532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
225632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
225732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
225832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
225932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
226032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
22613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
22623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
226300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
226400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
22663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
22673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
22693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
22703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
22713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
227200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
227300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
22753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
22763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
22773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
227800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
227900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
22813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
22823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
22833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
22843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
22853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
22863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
22873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
22883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
22903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
22913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
22933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
22943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
22963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
22973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
22983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
22993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
23013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
23023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
23033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
23053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
23063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
23073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
23093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
23113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
23123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
23133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the GC map.\n"
23153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
23163774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
23183774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
23193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
23213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
23223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
232332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
232432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
232532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
232632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
232732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
232832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
232932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
233032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
233132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
233232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
233332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
233432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
233532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
233632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
23373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
23383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
233932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
234032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
234132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
234232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
234332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
234432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
234532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
23463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
23473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
234900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
235000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23513774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
235200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
235332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
23543fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
235500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
235600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
235700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
235800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
235900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
236000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
236100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
236232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
236332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
236432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
236532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
236600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
236700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23683774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
23693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
23703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
237100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
23733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
23743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
23763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
23773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
23783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
23793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
23803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
238132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
238232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
238332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
238432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
238532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
238632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
23873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
23893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
239000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
239100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
23933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2394c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
23953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2396d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
23973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2398c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
2399c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2400c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2401c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2402c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
240332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2404c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2405c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
240600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
240700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
24093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
24103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
24123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
24133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
241432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
24153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
241600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
241700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
241832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
24193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      == EXIT_SUCCESS;
242000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
242100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
24233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
242400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24257934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
242678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
242778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
24283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
24293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
243078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2431