oatdump.cc revision daaf3265806eb2eadb2e03302bd68022fab5ca28
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"
29761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h"
3078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include "class_linker.h"
312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h"
324f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
33e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes#include "dex_instruction.h"
343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers#include "disassembler.h"
3554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include "elf_builder.h"
362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "gc_map.h"
371d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h"
381d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/large_object_space.h"
391d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
4078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include "image.h"
412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "indenter.h"
421809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "mapping_table.h"
43ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.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
9554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    builder_.reset(new ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
9654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                  Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(
9754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        this,
9854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        elf_output_,
9954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        oat_file_->GetOatHeader().GetInstructionSet(),
10054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        0,
10154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        oat_data_size,
10254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        oat_data_size,
10354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        oat_exec_size,
1045c42c29b89286e5efa4a4613132b09051ce5945bVladimir Marko        RoundUp(oat_data_size + oat_exec_size, kPageSize),
1055c42c29b89286e5efa4a4613132b09051ce5945bVladimir Marko        oat_bss_size,
10654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        true,
10754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        false));
10854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
10954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (!builder_->Init()) {
11054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      builder_.reset(nullptr);
11154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return false;
11254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
11354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return true;
11554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
11654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
11854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
11954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const OatFile::OatMethod&,
12054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile&,
12154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
12254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile::CodeItem*,
12354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t);
12454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
12554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Symbolize() {
12654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (builder_.get() == nullptr) {
12754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return false;
12854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
12954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::RegisterForDedup);
13154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    NormalizeState();
13354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    Walk(&art::OatSymbolizer::AddSymbol);
13554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
13654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    bool result = builder_->Write();
13754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1384303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    // Ignore I/O errors.
1394303ba97313458491e038d78efa041d41cf7bb43Andreas Gampe    UNUSED(elf_output_->FlushClose());
14054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return result;
14254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
14354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
14454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void Walk(Callback callback) {
14554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
14654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t i = 0; i < oat_dex_files.size(); i++) {
14754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
14854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      CHECK(oat_dex_file != NULL);
14954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatDexFile(oat_dex_file, callback);
15054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
15154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
15254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
15354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
15454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::string error_msg;
15554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
15654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dex_file.get() == nullptr) {
15754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
15854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
15954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t class_def_index = 0;
16054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index < dex_file->NumClassDefs();
16154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index++) {
16254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
16354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
16454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      OatClassType type = oat_class.GetType();
16554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      switch (type) {
16654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassAllCompiled:
16754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassSomeCompiled:
16854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
16954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
17054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassNoneCompiled:
17254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassMax:
17354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          // Ignore.
17454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
17554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
17654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
17754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
17854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
18054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                    const DexFile::ClassDef& class_def, Callback callback) {
18113735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
18254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
18354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
18454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
18554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    // Note: even if this is an interface or a native class, we still have to walk it, as there
18654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    //       might be a static initializer.
18754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    ClassDataItemIterator it(dex_file, class_data);
18854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    SkipAllFields(&it);
18954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t class_method_idx = 0;
19054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextDirectMethod()) {
19154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
19254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
1935182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
19454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
19554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
19654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
19754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextVirtualMethod()) {
19854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
19954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
2005182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
20154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
20254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
20354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
20454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DCHECK(!it.HasNext());
20554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
20654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
20754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
20854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     const OatFile::OatMethod& oat_method, const DexFile& dex_file,
20954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
21054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t method_access_flags, Callback callback) {
21154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if ((method_access_flags & kAccAbstract) != 0) {
21254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // Abstract method, no code.
21354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
21454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
21554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (oat_method.GetCodeOffset() == 0) {
21654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // No code.
21754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
21854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
21954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
22054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
22154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                      method_access_flags);
22254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
22354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2246a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2256a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t class_method_index ATTRIBUTE_UNUSED,
2266a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const OatFile::OatMethod& oat_method,
2276a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile& dex_file ATTRIBUTE_UNUSED,
2286a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t dex_method_idx ATTRIBUTE_UNUSED,
2296a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2306a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t method_access_flags ATTRIBUTE_UNUSED) {
23154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[oat_method.GetCodeOffset()]++;
23254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
23354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
23454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void NormalizeState() {
23554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (auto& x : state_) {
23654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (x.second == 1) {
23754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        state_[x.first] = 0;
23854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
23954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
24054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
24154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
24254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  enum class DedupState {  // private
24354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kNotDeduplicated,
24454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedFirst,
24554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedOther
24654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  };
24754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  DedupState IsDuplicated(uint32_t offset) {
24854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 0) {
24954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kNotDeduplicated;
25054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 1) {
25254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kDeduplicatedOther;
25354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[offset] = 1;
25554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return DedupState::kDeduplicatedFirst;
25654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
25754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2586a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
2596a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t class_method_index ATTRIBUTE_UNUSED,
2606a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const OatFile::OatMethod& oat_method,
2616a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile& dex_file,
2626a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t dex_method_idx,
2636a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
2646a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
26554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
26654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dedup != DedupState::kDeduplicatedOther) {
26754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
26854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
26954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (dedup == DedupState::kDeduplicatedFirst) {
27054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        pretty_name = "[Dedup]" + pretty_name;
27154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
27254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
27354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      ElfSymtabBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr,
2740279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers      Elf32_Sym, Elf32_Shdr>* symtab = builder_->GetSymtabBuilder();
27554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
2760279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers      symtab->AddSymbol(pretty_name, &builder_->GetTextBuilder(),
2770279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
2780279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers          true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
27954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
28054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
28154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
282f4da675bbc4615c5f854c81964cac9dd1153baeaVladimir Marko  // Set oat data offset. Required by ElfBuilder/CodeOutput.
2836a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void SetCodeOffset(size_t offset ATTRIBUTE_UNUSED) {
284f4da675bbc4615c5f854c81964cac9dd1153baeaVladimir Marko    // Nothing to do.
285f4da675bbc4615c5f854c81964cac9dd1153baeaVladimir Marko  }
286f4da675bbc4615c5f854c81964cac9dd1153baeaVladimir Marko
28754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  // Write oat code. Required by ElfBuilder/CodeOutput.
28854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Write(OutputStream* out) {
28954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return out->WriteFully(oat_file_->Begin(), oat_file_->End() - oat_file_->Begin());
29054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
29154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
29254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe private:
29354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  static void SkipAllFields(ClassDataItemIterator* it) {
29454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextStaticField()) {
29554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
29654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextInstanceField()) {
29854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
29954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
30054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
30154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
30254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  const OatFile* oat_file_;
30354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  std::unique_ptr<ElfBuilder<Elf32_Word, Elf32_Sword, Elf32_Addr, Elf32_Dyn,
30454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                              Elf32_Sym, Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr> > builder_;
30554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  File* elf_output_;
30654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  std::unordered_map<uint32_t, uint32_t> state_;
3070279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  const std::string output_name_;
30854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe};
30954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass OatDumperOptions {
3112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
3122cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  OatDumperOptions(bool dump_raw_mapping_table,
3132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_raw_gc_map,
3142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_vmap,
3152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
31600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
31732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
31832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
31932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
32032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
32132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
32232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
3232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    : dump_raw_mapping_table_(dump_raw_mapping_table),
3242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_raw_gc_map_(dump_raw_gc_map),
3252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_vmap_(dump_vmap),
3262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
32700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
32832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
3293fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
33032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
33132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
33232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
33332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
3343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
3352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
3362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_mapping_table_;
3372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_gc_map_;
3382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
3392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
3402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
34132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
3423fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
34332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
34432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
34532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
34632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
34700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
3482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
3492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
350e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
351aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
35232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
3539583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
354a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
3552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
35632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
357a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
358a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
35932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
360a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
361a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 true /* can_read_litals_ */))) {
36232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
36332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
36432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
3653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
3663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
3673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
3682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
3692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
3702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
3712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
372a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
373a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
374a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
375a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
3762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
3772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
3783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
379aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
380aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
381aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
382aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
383aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
384ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
385aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
386a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
387a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
388a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
3896f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
3906f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
3916f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
3926f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
3936f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
3946f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
3956f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
3967020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
397aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
398aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
399aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
4012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
4022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
40332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
4042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
4052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
4062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
4072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
4082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
4092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
4102ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
4112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
4122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
4132ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
4142ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
4152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
4162ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
4172ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
4182ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4192ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4202ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
4212ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
4222ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
4232ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
424aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
4262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
4272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
4282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
429a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
43028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
43128db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
43228db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
43328db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
434700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
43581f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
43622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
43722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
43822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
43922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
44022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
44122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
44222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
44322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
44422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
44522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
44622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
44722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
44881f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
44932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
4502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
4512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
4522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
4542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
4552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
456aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
4582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
459aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
460aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
461aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
46232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
46332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
46432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
46532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
46632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
46732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
46832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
4693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
4703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
4712ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
47232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
47332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // If file export selected skip file analysis
47432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.export_dex_location_) {
47532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!ExportDexFile(os, *oat_dex_file)) {
47632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
47732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
47832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
47932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (!DumpOatDexFile(os, *oat_dex_file)) {
48032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          success = false;
48132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
4822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
4833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
4842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
4852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
4863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
4873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
4883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
48913735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
49013735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
4913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
4923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
493ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
494ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
495ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
4963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
497ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
4983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
4993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
500e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
501a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
502f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
503f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
504f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
505ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
5063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5088d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
5098d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
510700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5118d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5128d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5138d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5148d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
515e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5168b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
517e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
5182ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
5198b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
520d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
5213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
522d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
5233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
5243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
525aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
5262ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
527aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
528aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
529aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
5303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
531e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
532e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
533e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
534e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
5353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5372ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
5388d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
539700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
5408d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      if (dex_file.get() == nullptr) {
5418d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5428d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
544e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
545ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
5462ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
5472ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
5482ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
549e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
550d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
55113735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
5522ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
553e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
554e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
555e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
556e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
557d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
558e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
559e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
560e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
561d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
562e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
563e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
564e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
565e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
566e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
567e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
568e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
569e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
570e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
571ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
572e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
573e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
5752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
5762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
5772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
578e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
57995ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
58095ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
58195ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
58295ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
58395ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
584e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetMappingTableOffset());
585e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
586957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    offsets_.insert(oat_method.GetGcMapOffset());
587e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
588e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
5892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
5902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
59132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
5922cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
593a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
594ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
595590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
596590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // Create the verifier early.
597590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
5988d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
599700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
6002ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (dex_file.get() == nullptr) {
6018d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
6022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
6032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
604aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
6052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
6062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
6072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
608aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
609aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
61032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
61132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
61232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
61332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
61432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
61532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
6162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
617d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
6182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
6192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
620d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
621d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
622d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
6232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
6242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indented_os(&indent_filter);
62532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
62632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
6272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
6282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
62932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
63032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
63132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
63232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
633aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
634aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
635aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
6362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
637aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
638aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
63932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
64032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
64132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
64232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
64332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
64432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
64532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
64632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
64732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
64832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
64932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
65132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
65232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
65332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
65432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
65532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
65632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
65732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
65832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
65932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
66032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
66432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
66532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
66632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
66732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
66832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
66932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
67032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
67132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
67232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
67432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
67532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
67632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
67732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
67832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
67932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
68032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
68132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
68232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
68432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
68532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
68632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
68732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
68832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
68932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
69032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
69132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
70132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
70232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
70332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
70532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
70632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
70732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
70932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
71032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
71132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
71232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
71332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
71432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
71532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
71632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
717e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
7180571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
7190571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
720aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7210571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
7220571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
723aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
724e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
725aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
7262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
72732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
7282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
72932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
73013735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
7312ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
7322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
7332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
734e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
735e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
736e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
7372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
7380571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
7392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
7402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
74132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
74432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
74532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
74632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
74732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7490571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
750aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7510571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
7522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
7532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
75432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
7552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
75732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
75832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
75932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
76032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
7612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
7620571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
763aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
7640571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
765aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
7662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
767aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
768e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
7692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
7702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
7722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
7732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
7742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
7758b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
7762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
7772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
77832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
7792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
78032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
78132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
78232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
78332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
7843fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
7853fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
7863fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
78732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
7882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
7893fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray                       class_method_index, pretty_method.c_str(),
790db7bdc1086c44fe628b6743f98d7d85a5c03343aIan Rogers                       dex_method_idx);
79132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
79232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
7932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
794a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
795a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
796a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe    std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
80032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
80132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
80232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
80332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
80432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
80532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
80632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
80732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
80832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
80932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
81032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
812a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      *indent1_os << "DEX CODE:\n";
813a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      DumpDexCode(*indent2_os, dex_file, code_item);
8142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
8162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
8172ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
818a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe      *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
8192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
8202ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
8212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "OatMethodOffsets ";
82432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("%p ", oat_method_offsets);
8262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
8282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
8292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf(
8302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
8312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
8322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
8332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        os << std::flush;
8342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
836a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
8372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
8382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
8392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
8402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "code offset 0x%08x is past end of file 0x%08zx.\n",
8422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    aligned_code_begin, oat_file_.Size());
8432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
8463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
8472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "gc_map: ";
84832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
849957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
8503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
851957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
8522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
8532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (gc_map_offset > oat_file_.Size()) {
8542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
8562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    gc_map_offset, oat_file_.Size());
8572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
85832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_gc_map_) {
859a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe        Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
8602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent3_os(&indent3_filter);
8612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpGcMap(indent3_os, oat_method, code_item);
8622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
8642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
8652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "OatQuickMethodHeader ";
8662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
8672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
8682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
86932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("%p ", method_header);
8712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
8722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
8732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
8742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf(
8752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
8762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
8772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
8782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        os << std::flush;
8792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
8802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "mapping_table: ";
88332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
8842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
8852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
8862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
8872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
8882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (mapping_table_offset > oat_file_.Size()) {
8892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
8902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "mapping table offset 0x%08x is past end of file 0x%08zx. "
8912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "mapping table offset was loaded from offset 0x%08x.\n",
8922cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    mapping_table_offset, oat_file_.Size(),
8932cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    oat_method.GetMappingTableOffsetOffset());
8942cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
89532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_mapping_table_) {
896a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe        Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
8972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent3_os(&indent3_filter);
8982cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpMappingTable(indent3_os, oat_method);
8992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "vmap_table: ";
90232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
9042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
9062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
9072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
9082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
9092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "vmap table offset 0x%08x is past end of file 0x%08zx. "
9102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "vmap table offset was loaded from offset 0x%08x.\n",
9112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    vmap_table_offset, oat_file_.Size(),
9122cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    oat_method.GetVmapTableOffsetOffset());
9132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
91432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
915442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpVmapData(*indent2_os, oat_method, code_item);
9162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
9172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
9192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "QuickMethodFrameInfo\n";
9202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
9222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
9232cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
9242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
9252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
9262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
9272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent2_os << "\n";
9282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // Based on spill masks from QuickMethodFrameInfo so placed
9312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // after it is dumped, but useful for understanding quick
9322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // code, so dumped here.
9332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        DumpVregLocations(*indent2_os, oat_method, code_item);
9342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      *indent1_os << "CODE: ";
9372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
9382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
9392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent2_os << StringPrintf("WARNING: "
9402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    "code size offset 0x%08x is past end of file 0x%08zx.",
9412cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size_offset, oat_file_.Size());
9422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9432cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
9442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
9452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
9462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
947590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
94832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
9492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent1_os << StringPrintf("%p ", code);
9502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9512cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
9522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_offset,
9532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size_offset,
9542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code_size,
9552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                    code != nullptr ? "..." : "");
9562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
9582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "start of code at 0x%08x is past end of file 0x%08zx.",
9602cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      aligned_code_begin, oat_file_.Size());
9612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
9622cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
9632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
9652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size is 0x%08x loaded from offset 0x%08x.\n",
9662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      aligned_code_end, oat_file_.Size(),
9672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, code_size_offset);
9682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
96932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9732cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
9742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
9752cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          *indent2_os << StringPrintf("WARNING: "
9762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size %d is bigger than max expected threshold of %d. "
9772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      "code size is 0x%08x loaded from offset 0x%08x.\n",
9782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, kMaxCodeSize,
9792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                                      code_size, code_size_offset);
9802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
98132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
9822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
9832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
9842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
9852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
98632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
9872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
9882cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
9892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9912cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
9922cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
9933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
9943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
9953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
9963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
9973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
9983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
9992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
10003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
10013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
10023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
10033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
10043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
10063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
10083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
10093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
10103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
10113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
10123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
10133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
10143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
10163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1018442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
1019442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapData(std::ostream& os,
1020442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1021442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1022957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    if (oat_method.GetGcMap() == nullptr) {
1023442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // If the native GC map is null, then this method has been
1024442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // compiled with the optimizing compiler. The optimizing
1025442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // compiler currently outputs its stack maps in the vmap table.
1026442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1027442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1028442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1029442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
1030442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpCodeInfo(os, code_info, *code_item);
1031442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1032442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
1033442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // Otherwise, display the vmap table.
1034442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const uint8_t* raw_table = oat_method.GetVmapTable();
1035442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_table != nullptr) {
1036442b46a087c389a91a0b51547ac9205058432364Roland Levillain        VmapTable vmap_table(raw_table);
1037442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DumpVmapTable(os, oat_method, vmap_table);
1038442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
103920d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1040442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1041442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1042442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
1043442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpCodeInfo(std::ostream& os,
1044442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1045442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
1046004c230b4cfc856690c61faabc41864061813c88Nicolas Geoffray    code_info.Dump(os, code_item.registers_size_);
1047442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1048442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1049442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a vmap table.
1050442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapTable(std::ostream& os,
1051442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const OatFile::OatMethod& oat_method,
1052442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const VmapTable& vmap_table) {
1053442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool first = true;
1054442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool processing_fp = false;
1055442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1056442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < vmap_table.Size(); i++) {
1057442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint16_t dex_reg = vmap_table[i];
1058442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1059442b46a087c389a91a0b51547ac9205058432364Roland Levillain                                                    processing_fp ? kFloatVReg : kIntVReg);
1060442b46a087c389a91a0b51547ac9205058432364Roland Levillain      os << (first ? "v" : ", v")  << dex_reg;
1061442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp) {
1062442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/r" << cpu_reg;
1063442b46a087c389a91a0b51547ac9205058432364Roland Levillain      } else {
1064442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/fr" << cpu_reg;
1065442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1066442b46a087c389a91a0b51547ac9205058432364Roland Levillain      first = false;
1067442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp && dex_reg == 0xFFFF) {
1068442b46a087c389a91a0b51547ac9205058432364Roland Levillain        processing_fp = true;
1069442b46a087c389a91a0b51547ac9205058432364Roland Levillain        spill_mask = oat_method.GetFpSpillMask();
1070442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
10713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1072442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "\n";
10733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1075faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1076faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1077faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1078faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1079faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1080faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1081faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1082faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1083faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1084faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1085faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1086faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1087faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1088faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1089faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1090faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1091faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1092faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1093faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
109415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
109515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
109615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
109715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
109815b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
109915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
110015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1101faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1102faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1103faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1104faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1105faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1106faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1107faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1108faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1109faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1110faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1111faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1112faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1113faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1114faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1115faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1116faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1117b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
11182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
11191809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* raw_table = oat_method.GetVmapTable();
11202ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (raw_table != nullptr) {
1121b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      const VmapTable vmap_table(raw_table);
1122b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      uint32_t vmap_offset;
11231809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
11242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
11252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
11262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                       : oat_method.GetCoreSpillMask();
11272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1128b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      } else {
112915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
113015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
113115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
113215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
113315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
113415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
113515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1136b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        os << "[sp + #" << offset << "]";
1137b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
1138b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1139b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1140b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1141ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1142ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          const DexFile::CodeItem* code_item,
1143ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          size_t num_regs, const uint8_t* reg_bitmap) {
1144ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    bool first = true;
1145ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t reg = 0; reg < num_regs; reg++) {
1146ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1147ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        if (first) {
1148ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << "  v" << reg << " (";
1149ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1150ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1151ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          first = false;
1152ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else {
1153ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ", v" << reg << " (";
1154ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1155ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1156ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        }
1157ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      }
1158ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1159ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (first) {
1160ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "No registers in GC map\n";
1161ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    } else {
1162ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "\n";
1163ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1164ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
1165b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1166b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers                 const DexFile::CodeItem* code_item) {
1167957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1168ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (gc_map_raw == nullptr) {
1169ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return;  // No GC map.
11703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1171ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1172956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    NativePcOffsetToReferenceMap map(gc_map_raw);
1173956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1174956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1175956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes          map.GetNativePcOffset(entry);
1176956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      os << StringPrintf("%p", native_pc);
1177956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
11783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1179aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
1180e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
11813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
1182ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1183ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (quick_code == nullptr) {
11842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      return;
11852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
11861809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
11871809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (table.TotalSize() != 0) {
11881809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
11891809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      std::ostream indent_os(&indent_filter);
11901809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.PcToDexSize() != 0) {
11911809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::PcToDexIterator It;
11921809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "suspend point mappings {\n";
11931809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
11941809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
11951809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
11961809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "}\n";
11971809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      }
11981809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.DexToPcSize() != 0) {
11991809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::DexToPcIterator It;
12001809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "catch entry mappings {\n";
12011809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12021809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12031809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12041809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "}\n";
12053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
12063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
12083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12091809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12101809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
12111809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12121809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
12131809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
12141809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12151809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12164b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
12171809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
12181809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1219b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12201809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
12211809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
12221809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12231809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12244b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
12251809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1226b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1227b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1230b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1234957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
12352ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1236b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
12372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1238b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
12392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1240b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1241b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1242b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1243b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
12442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
12452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1246b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1247b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1248b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1249b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
12502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1251b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1252b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1253b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1254b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1255b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1256b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1257b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1258b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1260b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1262590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1263590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1264590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1265590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
12667b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
12672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
12682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
12692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
12702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
12712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
12722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
12732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
12742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
12752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
12762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
12772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
12782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
12792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
12802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
12812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
12822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
12842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
12852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
12872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
12882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
12892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
12902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
12912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
12922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
12942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
12952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
12972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
12982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1299b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
13002ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1301b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1302b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1303b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
130432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
130532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1306b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1307e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1308e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1309b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1310b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
13112ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
13122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
13132ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
13142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
13152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
13162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
13172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1318bf99f77dda749e2b653e8c45259b1fb56e7bb012Mathieu Chartier      StackHandleScope<1> hs(soa.Self());
1319eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1320eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier          hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
132132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
13222ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
13232ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           dex_cache,
132432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                                           *options_.class_loader_,
13252ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           &class_def, code_item,
13262ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           NullHandle<mirror::ArtMethod>(),
13272ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           method_access_flags);
13282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13292ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
13302ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
13312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1333590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
13342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
13352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1336ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1337ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
13382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
13392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
13402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1341956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
13422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "NO CODE!\n";
1343b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1344956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1345ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1346ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1347ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
13482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
13492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          DumpMappingAtOffset(os, oat_method, offset, false);
13502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
1351ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        offset += disassembler_->Dump(os, quick_native_pc + offset);
13522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
13532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
13542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
13552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
13562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
13572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
13582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1359ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
13602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1362b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1363b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1364b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
13653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
13662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
136732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
136832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1369a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1370ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
13712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1372aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1373aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
13743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
137527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
13762ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom  explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
13772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
13782cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
13792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
13802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
13812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
138227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
13832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
13852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
13862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
13872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
13882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
138931e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier    os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
139031e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier       << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
139131e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
13922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
13932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1394700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
13952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1396700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1397700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1398700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1399700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1400700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
14012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1402a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1403a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
140446774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
140546774767fcf7780d1455e755729198648d08742eIgor Murashkin
14062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
14072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
14082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
14092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent1_os(&indent1_filter);
14102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
14112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
14122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
14132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
14142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
14152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
14162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
14172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
14182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          std::ostream indent2_os(&indent2_filter);
14192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1420fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
1421277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1422277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1423fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1424277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1425277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1426fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1427fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1428fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1429fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1430fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1431fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
1432277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d: ", j);
1433fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
1434277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1435277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1436fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
14372ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
14382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              PrettyObjectValue(indent2_os, value->GetClass(), value);
14392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
1440277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << j << ": null\n";
14412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1442d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
144334f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
144434f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
144527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
14462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
144727ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1448aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
14492ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
14502ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
14512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
14522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
14538d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
1454a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1455a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
1456e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler      oat_file = OatFile::Open(oat_location, oat_location,
1457e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               nullptr, nullptr, false, nullptr,
1458e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               &error_msg);
1459a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (oat_file == nullptr) {
1460a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        os << "NOT FOUND: " << error_msg << "\n";
14612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
1462a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
1463aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
14642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1465aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
14663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
146778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
146832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
14693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
147002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
14712ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
147202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
147302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
147405f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
147505f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
14762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1477b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1478b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    // Loop through all the image spaces and dump their objects.
14791d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
14801d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
148150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1482357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1483c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1484c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1485c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1486c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
148790d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
148890d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
148990d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1490c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1491c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromRunnableToSuspended(kNative);
1492c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        ThreadList* thread_list = Runtime::Current()->GetThreadList();
1493bf9fc581e8870faddbd320a935f9a627da724c48Mathieu Chartier        thread_list->SuspendAll(__FUNCTION__);
1494c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->RevokeAllThreadLocalAllocationStacks(self);
1495c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        thread_list->ResumeAll();
1496c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromSuspendedToRunnable();
1497c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
1498357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
14992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
15002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream* saved_os = os_;
15012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
15022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent_os(&indent_filter);
15032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = &indent_os;
15042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
150502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& space : spaces) {
15062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (space->IsImageSpace()) {
15071d54e73444e017d3a65234e0f193846f3e27472bIan Rogers          gc::space::ImageSpace* image_space = space->AsImageSpace();
15082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << "\n";
15102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
15112fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier      }
15122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1513bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
15152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = saved_os;
1516b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
15172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1518700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
15192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() == nullptr) {
15202ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
15216f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15222ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() != nullptr) {
15232ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
15246f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
15263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
15273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
15283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.alignment_bytes += alignment_bytes;
152932327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
153032327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
15312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    stats_.Dump(os);
15322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
153327ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
15342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
15353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
15362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
15373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1538d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
15393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
15402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
1541b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15422ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
15432ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
15442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1545d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
15462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
15472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
154868b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
154964b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
15502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
15512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1552ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (type->IsArtFieldClass()) {
1553ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtField* field = value->AsArtField();
15542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Field: %s\n", field, PrettyField(field).c_str());
1555ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (type->IsArtMethodClass()) {
1556ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = value->AsArtMethod();
15572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Method: %s\n", method, PrettyMethod(method).c_str());
1558d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
15592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1560d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1561d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1562d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1563ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
1564b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156561c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
156608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
156708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1568ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
156908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
15712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
157208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
15742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
157508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
15772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
157808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
157908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
158037f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
158108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
158208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
158337f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
158408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
158508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
158637f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
158737f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
158808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
158908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
159037f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
159108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
159208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
159308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
159408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
159508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
159608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
159708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
159850239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
159908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1600daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
160108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
160208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
160308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
160408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
160508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
160608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
160750239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
160808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
160948efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
161008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
161108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
161208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1613d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1614d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1615d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
1617b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
16192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
16202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1621d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1622ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
16232ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (fields != nullptr) {
1624d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      for (int32_t i = 0; i < fields->GetLength(); i++) {
1625ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        mirror::ArtField* field = fields->Get(i);
16262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        PrintField(os, field, obj);
1627d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1628d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1629d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1630d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
16323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1633f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
16343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1635ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
1636b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1637a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1638a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
16396f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1640ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
16413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1642f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1643ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1644f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1645ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
16463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
16473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1648ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
1649b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1650ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1651ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1652f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1653f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1654f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1655f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1656f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1657ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
1658b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1659ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
16602ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
16612ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1662f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1663ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1664f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1665f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
16662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void Callback(mirror::Object* obj, void* arg)
1667b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16682ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
16692ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
16703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
167178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
167278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
167378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1674916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1675916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1676916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1677916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1678916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1679916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
16802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *state->os_;
16812dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1682d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
16832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
16842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1685d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
16862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
16872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
16882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1689ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtField()) {
16904b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom      os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
1691ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         PrettyField(obj->AsArtField()).c_str());
1692ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
16934b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom      os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
1694ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         PrettyMethod(obj->AsArtMethod()).c_str());
1695d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
16962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
169768b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
169878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
16992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
170078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
17012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
17022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream indent_os(&indent_filter);
17032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    DumpFields(indent_os, obj, obj_class);
1704d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
17052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
1706d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      int32_t length = obj_array->GetLength();
1707d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      for (int32_t i = 0; i < length; i++) {
17082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1709d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1710d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1711d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1712d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1713d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1714d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1715d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1716d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1717d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
17182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d: ", i);
171978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
17202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d to %zd: ", i, i + run);
1721d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
172278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
17232ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
17242ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
17252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        PrettyObjectValue(indent_os, value_class, value);
1726d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1727d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1728ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
17292ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      if (sfields != nullptr) {
17302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << "STATICS:\n";
17312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
17322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent2_os(&indent2_filter);
1733d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t i = 0; i < sfields->GetLength(); i++) {
1734ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom          mirror::ArtField* field = sfields->Get(i);
17352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          PrintField(indent2_os, field, field->GetDeclaringClass());
1736d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1737d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1738ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
1739a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      const size_t image_pointer_size = InstructionSetPointerSize(
1740a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier          state->oat_dumper_->GetOatInstructionSet());
1741ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = obj->AsArtMethod();
1742d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      if (method->IsNative()) {
1743957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1744a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
17453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first_occurrence;
1746ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1747ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1748ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code, &first_occurrence);
17493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (first_occurrence) {
1750ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
17513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1752a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1753a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            image_pointer_size)) {
1754ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
17553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17561984651929744dd603fd082e23eacd877b9bc177Ian Rogers      } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
175788474b416eb257078e590bf9bc7957cee604a186Jeff Hao          method->IsResolutionMethod() || method->IsImtConflictMethod() ||
17582d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier          method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1759957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1760a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1761916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      } else {
1762bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        const DexFile::CodeItem* code_item = method->GetCodeItem();
1763d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers        size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1764916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom        state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1765e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom
1766a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        bool first_occurrence;
1767957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        size_t gc_map_bytes =
1768957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier            state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1769a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.gc_map_bytes += gc_map_bytes;
17713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t pc_mapping_table_bytes =
1774a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
1775a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
17773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t vmap_table_bytes =
1780a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
1781a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
17823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.vmap_table_bytes += vmap_table_bytes;
17833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
17843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1785ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1786ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1787ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1788ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1789a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
1790ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.managed_code_bytes += quick_oat_code_size;
17910d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          if (method->IsConstructor()) {
17920d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            if (method->IsStatic()) {
1793ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.class_initializer_code_bytes += quick_oat_code_size;
17940d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1795ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.large_initializer_code_bytes += quick_oat_code_size;
17960d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            }
17970d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1798ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            state->stats_.large_method_code_bytes += quick_oat_code_size;
17990d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
18003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1801ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
18023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1803ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
18042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
18052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
18063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1808ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            vmap_table_bytes + quick_oat_code_size + object_bytes;
18093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double expansion =
1811ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
18123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        state->stats_.ComputeOutliers(total_size, expansion, method);
181378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      }
181478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
18151ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    std::string temp;
18161ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
181778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
181827ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
18193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
18203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
18213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
1822a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
18233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
1824a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
18253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
18263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
1827a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
18283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
183027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
1831916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1832916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
1833916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
18343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
1835916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
1836916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1837916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
1838916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
183932327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
1840916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
1841916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1842916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
18433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
1844916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
1845916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
18460d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
18470d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
18480d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
1849916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
1851916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
18523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
1853916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1854916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
1855916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1856ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    std::vector<mirror::ArtMethod*> method_outlier;
18573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
18583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
1859700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
18603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    explicit Stats()
18623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
18633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
1864916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
1865916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
186632327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
1867916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
1868916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
18693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
1870916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
1871916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
18720d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
18730d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
18740d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
18753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
1876916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
18773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
1878916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
1879916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1880a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
1881277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
1882a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
1883a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
1884a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
1885a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1886a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
1887a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
1888277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
1889a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1890a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
1891277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
1892a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
1893a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
1894277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
1895a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
1896a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
1897916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
18993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
19003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1902916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
1903916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1904916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1905916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1906916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
1907916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1908916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1909916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1910ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
19113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
19123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
19133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
19143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
191600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
1917b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
19183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
19193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
19203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
19213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
19223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
19233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
19243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
19253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
19263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
19273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
19283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
19293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
19303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
19323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
19333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
19343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
19353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
19363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
19383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
19393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
19403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
19413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
19423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
19433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
19443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
19453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
19463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
19473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
19483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
19493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
19503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
19513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
19523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
19533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
19543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
19553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
19573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
1958c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
19593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
19603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
1962c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
19633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
19643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
19653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
19663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
19673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
19683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
19693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
19712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
1972c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
19733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
1974c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
19753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
19773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
19783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
19793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
19803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
19813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
19823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
19833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
19843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
19853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
19863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
19873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
19883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
19893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
19903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
19913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
19923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
19933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
19943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
19953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
19963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
19973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
1998c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
19993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
20003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2002c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
20033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
20043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
20053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
20063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
20073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
20083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
20093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
20103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
20112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2012c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
20133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2014c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
20153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2017b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
20192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
20202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
20212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
20222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent_os(&indent_filter);
20232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("header_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "object_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
202532327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  "bitmap_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "alignment_bytes =  %8zd (%2.0f%% of art file bytes)\n\n",
20272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
20282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
202932327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
20302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
20312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
203232327098e300ab66671b891a18bf669576fc896aMathieu Chartier        CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
20332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2034916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2036916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
203702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
203802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
203902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
204002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
204102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
20422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2043a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
204402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
204502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
204602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2047916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2048c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2049916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2050916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
20522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
20532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
20552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
205805f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
20592ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
20602ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
20612ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
20622ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
20632ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
20642ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
20652ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
20662ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
20672ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
20682ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
20692ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
20702ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
20712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
207202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
20732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
207402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
207502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
207605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
207705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
20782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
20792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
20802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
208105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
208205f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
208305f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2084c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2085916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
20872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
20882ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
20892ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2090c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2091cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2092c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
20933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2095916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2096916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2097916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2098916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
20990d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
21000d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
21010d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
21020d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
21030d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
21040d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
21050d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
21060d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
21072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
21081d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
21093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
21102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
21112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2112d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
21133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
211478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
211578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
211600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
211700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                     std::ostream* os) {
211800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
211900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
212000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
212100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
212200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
212300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  gc::Heap* heap = runtime->GetHeap();
21241d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace* image_space = heap->GetImageSpace();
21252ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  CHECK(image_space != nullptr);
212678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  const ImageHeader& image_header = image_space->GetImageHeader();
212778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  if (!image_header.IsValid()) {
21280f5baa079eea76758aee32d7f87cb175deab8308Brian Carlstrom    fprintf(stderr, "Invalid image header %s\n", image_location);
212978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    return EXIT_FAILURE;
213078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
21313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
213200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ImageDumper image_dumper(os, *image_space, image_header, options);
21333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
21342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool success = image_dumper.Dump();
21352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
213678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
213778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
213800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
213900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
214000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
214100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
214200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
214300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
214400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
214500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
214600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
214700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
214800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
214900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
215000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  class_linker->RegisterOatFile(oat_file);
2151fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
215200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
215300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2154fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
215500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
215600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    class_linker->RegisterDexFile(*dex_file);
2157fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    dex_files.push_back(std::move(dex_file));
215800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
215900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
216000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
216100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2162fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<const DexFile*> class_path;
2163fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
2164fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    class_path.push_back(dex_file.get());
2165fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  }
216681c6f8db12b203878a7d72444ead2bc7cf5c47adAndreas Gampe  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
216700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
216800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
216900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
217000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
217100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
217200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
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 DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
218032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
218100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
218200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
218300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
218400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
218532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
218600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
218700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
218800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
218900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
219000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
219100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
219200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
21933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2194e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
219500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
219600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
219700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
219800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
219900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
220000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
220100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
220200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
220300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
220400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
220500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
220600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
220700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int SymbolizeOat(const char* oat_filename, std::string& output_name) {
220800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
22093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2210e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                                    nullptr, &error_msg);
221100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
221200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
221300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
221400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
221500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
221600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  OatSymbolizer oat_symbolizer(oat_file, output_name);
221700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Init()) {
221800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to initialize symbolizer\n");
221900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
222000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
222100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Symbolize()) {
222200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
222300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
222400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
222500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
222600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
222700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
222800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22293774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
22303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
22313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
223200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
22343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
22353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
22363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
22373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
22383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
22393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
224000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
224100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
22433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
22443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
22453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
22463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
22473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
22483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
22493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
22503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
22513774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
22523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
22533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
22543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
22553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
22563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
225732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
225832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
22593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
22603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
226132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
226232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
226332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
226432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
226532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
226632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
226732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
226832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
226932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
227032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
227132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
22723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
22733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
227400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
227500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
22773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
22783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
22793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
22803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
22813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
22823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
228300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
228400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
22863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
22873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
22883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
228900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
229000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
22923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
22933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
22943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
22953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
22963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
22973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
22983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
22993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
23013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
23023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
23043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
23053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
23073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
23083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
23093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
23103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
23123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
23133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
23143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23153774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
23163774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
23173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
23183774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
23203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
23223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
23233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
23243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23253774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the GC map.\n"
23263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
23273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
23293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
23303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
23323774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
23333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
233432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
233532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
233632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
233732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
233832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
233932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
234032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
234132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
234232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
234332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
234432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
234532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
234632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
234732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
23483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
23493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
235032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
235132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
235232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
235332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
235432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
235532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
235632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
23573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
23583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
236000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
236100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
236300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
236432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
23653fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
236600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
236700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
236800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
236900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
237000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
237100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
237200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
237332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
237432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
237532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
237632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
237700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
237800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23793774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
23803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
23813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
238200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
23843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
23853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
23873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
23883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
23893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
23903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
23913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
239232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
239332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
239432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
239532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
239632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
239732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
23983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
24003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
240100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
240200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
24043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2405c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
24063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2407d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
24083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2409c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
2410c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2411c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2412c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2413c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
241432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2415c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2416c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
241700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
241800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24193774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
24203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
24213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
24233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
24243774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
242532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
24263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
242700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
242800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
242932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
24303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      == EXIT_SUCCESS;
243100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
243200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
24343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
243500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24367934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
243778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
243878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
24393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
24403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
244178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2442