oatdump.cc revision 15b9d5274399736ac09705f0507df24fac4f00c1
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
1042a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain  void DumpRegisterMapping(std::ostream& os,
1043a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain                           size_t dex_register_num,
1044a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain                           DexRegisterLocation::Kind kind,
1045a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain                           int32_t value,
1046a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain                           const std::string& prefix = "v",
1047a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain                           const std::string& suffix = "") {
1048a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    os << "      " << prefix << dex_register_num << ": "
1049a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain       << DexRegisterLocation::PrettyDescriptor(kind)
1050a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain       << " (" << value << ")" << suffix << '\n';
1051a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain  }
1052a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain
1053a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain  void DumpStackMapHeader(std::ostream& os, const CodeInfo& code_info, size_t stack_map_num) {
1054a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    StackMap stack_map = code_info.GetStackMapAt(stack_map_num);
1055a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    os << "    StackMap " << stack_map_num
1056a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain       << std::hex
1057a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain       << " (dex_pc=0x" << stack_map.GetDexPc()
1058a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain       << ", native_pc_offset=0x" << stack_map.GetNativePcOffset()
1059a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain       << ", register_mask=0x" << stack_map.GetRegisterMask()
1060a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain       << std::dec
1061a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain       << ", stack_mask=0b";
1062a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    MemoryRegion stack_mask = stack_map.GetStackMask();
1063a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    for (size_t i = 0, e = stack_mask.size_in_bits(); i < e; ++i) {
1064a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain      os << stack_mask.LoadBit(e - i - 1);
1065a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    }
1066a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    os << ")\n";
1067a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain  };
1068a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain
1069442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
1070442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpCodeInfo(std::ostream& os,
1071442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1072442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
1073442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint16_t number_of_dex_registers = code_item.registers_size_;
1074442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t code_info_size = code_info.GetOverallSize();
1075442b46a087c389a91a0b51547ac9205058432364Roland Levillain    size_t number_of_stack_maps = code_info.GetNumberOfStackMaps();
1076442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "  Optimized CodeInfo (size=" << code_info_size
1077442b46a087c389a91a0b51547ac9205058432364Roland Levillain       << ", number_of_dex_registers=" << number_of_dex_registers
1078442b46a087c389a91a0b51547ac9205058432364Roland Levillain       << ", number_of_stack_maps=" << number_of_stack_maps << ")\n";
1079a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain
1080a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    // Display stack maps along with Dex register maps.
1081442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < number_of_stack_maps; ++i) {
1082442b46a087c389a91a0b51547ac9205058432364Roland Levillain      StackMap stack_map = code_info.GetStackMapAt(i);
1083a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain      DumpStackMapHeader(os, code_info, i);
1084442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (stack_map.HasDexRegisterMap()) {
1085442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DexRegisterMap dex_register_map =
1086442b46a087c389a91a0b51547ac9205058432364Roland Levillain            code_info.GetDexRegisterMapOf(stack_map, number_of_dex_registers);
1087442b46a087c389a91a0b51547ac9205058432364Roland Levillain        for (size_t j = 0; j < number_of_dex_registers; ++j) {
1088a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain          DexRegisterLocation location = dex_register_map.GetLocationKindAndValue(j);
1089a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain          DumpRegisterMapping(os, j, location.GetInternalKind(), location.GetValue());
10901809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
10913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
1092442b46a087c389a91a0b51547ac9205058432364Roland Levillain    }
1093a2d8ec6876325e89e5d82f5dbeca59f96ced3ec1Roland Levillain    // TODO: Dump the stack map's inline information.
1094442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1095442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1096442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a vmap table.
1097442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapTable(std::ostream& os,
1098442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const OatFile::OatMethod& oat_method,
1099442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const VmapTable& vmap_table) {
1100442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool first = true;
1101442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool processing_fp = false;
1102442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1103442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < vmap_table.Size(); i++) {
1104442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint16_t dex_reg = vmap_table[i];
1105442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1106442b46a087c389a91a0b51547ac9205058432364Roland Levillain                                                    processing_fp ? kFloatVReg : kIntVReg);
1107442b46a087c389a91a0b51547ac9205058432364Roland Levillain      os << (first ? "v" : ", v")  << dex_reg;
1108442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp) {
1109442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/r" << cpu_reg;
1110442b46a087c389a91a0b51547ac9205058432364Roland Levillain      } else {
1111442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/fr" << cpu_reg;
1112442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1113442b46a087c389a91a0b51547ac9205058432364Roland Levillain      first = false;
1114442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp && dex_reg == 0xFFFF) {
1115442b46a087c389a91a0b51547ac9205058432364Roland Levillain        processing_fp = true;
1116442b46a087c389a91a0b51547ac9205058432364Roland Levillain        spill_mask = oat_method.GetFpSpillMask();
1117442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
11183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1119442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "\n";
11203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
11213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1122faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1123faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1124faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1125faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1126faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1127faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1128faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1129faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1130faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1131faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1132faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1133faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1134faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1135faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1136faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1137faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1138faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1139faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1140faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
114115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
114215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
114315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
114415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
114515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
114615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
114715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1148faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1149faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1150faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1151faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1152faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1153faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1154faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1155faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1156faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1157faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1158faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1159faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1160faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1161faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1162faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1163faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1164b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
11652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
11661809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* raw_table = oat_method.GetVmapTable();
11672ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (raw_table != nullptr) {
1168b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      const VmapTable vmap_table(raw_table);
1169b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      uint32_t vmap_offset;
11701809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
11712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
11722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
11732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                       : oat_method.GetCoreSpillMask();
11742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1175b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      } else {
117615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
117715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
117815b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
117915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
118015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
118115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
118215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1183b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        os << "[sp + #" << offset << "]";
1184b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
1185b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1186b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1187b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1188ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1189ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          const DexFile::CodeItem* code_item,
1190ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          size_t num_regs, const uint8_t* reg_bitmap) {
1191ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    bool first = true;
1192ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t reg = 0; reg < num_regs; reg++) {
1193ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1194ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        if (first) {
1195ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << "  v" << reg << " (";
1196ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1197ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1198ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          first = false;
1199ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else {
1200ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ", v" << reg << " (";
1201ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1202ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1203ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        }
1204ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      }
1205ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1206ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (first) {
1207ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "No registers in GC map\n";
1208ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    } else {
1209ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "\n";
1210ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1211ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
1212b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1213b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers                 const DexFile::CodeItem* code_item) {
1214957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1215ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (gc_map_raw == nullptr) {
1216ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return;  // No GC map.
12173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1218ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1219956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    NativePcOffsetToReferenceMap map(gc_map_raw);
1220956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1221956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1222956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes          map.GetNativePcOffset(entry);
1223956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      os << StringPrintf("%p", native_pc);
1224956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
12253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1226aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
1227e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
12283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
1229ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1230ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (quick_code == nullptr) {
12312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      return;
12322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12331809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12341809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (table.TotalSize() != 0) {
12351809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
12361809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      std::ostream indent_os(&indent_filter);
12371809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.PcToDexSize() != 0) {
12381809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::PcToDexIterator It;
12391809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "suspend point mappings {\n";
12401809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12411809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12421809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12431809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "}\n";
12441809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      }
12451809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.DexToPcSize() != 0) {
12461809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::DexToPcIterator It;
12471809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "catch entry mappings {\n";
12481809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12491809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
12501809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
12511809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        os << "}\n";
12523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
12533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
12553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12561809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12571809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
12581809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12591809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
12601809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
12611809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12621809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12634b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
12641809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
12651809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1266b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12671809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
12681809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
12691809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
12701809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
12714b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
12721809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1273b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1274b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
12753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
12762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1277b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
12783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
12792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
12802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1281957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
12822ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1283b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
12842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1285b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
12862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1287b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1288b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1289b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1290b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
12912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
12922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1293b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1294b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1295b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1296b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
12972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1298b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1299b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1300b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1301b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1302b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1303b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1304b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1305b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
13063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1307b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
13083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1309590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1310590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1311590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1312590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
13137b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
13142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
13152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
13162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
13172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
13182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
13192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
13202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
13212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
13222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
13232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
13252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
13262bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
13272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
13282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
13312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
13322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
13342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
13352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
13362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
13372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
13382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
13392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
13412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
13422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
13452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1346b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
13472ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1348b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1349b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1350b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
135132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
135232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1353b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1354e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1355e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1356b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1357b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
13582ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
13592ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
13602ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
13612ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
13622ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
13632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
13642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1365bf99f77dda749e2b653e8c45259b1fb56e7bb012Mathieu Chartier      StackHandleScope<1> hs(soa.Self());
1366eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1367eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier          hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
136832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
13692ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return verifier::MethodVerifier::VerifyMethodAndDump(soa.Self(), os, dex_method_idx, dex_file,
13702ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           dex_cache,
137132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                                           *options_.class_loader_,
13722ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           &class_def, code_item,
13732ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           NullHandle<mirror::ArtMethod>(),
13742ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                                           method_access_flags);
13752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
13762ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
13772ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
13782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
13792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1380590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
13812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
13822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1383ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1384ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
13852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
13862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
13872cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1388956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
13892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "NO CODE!\n";
1390b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1391956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1392ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1393ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1394ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
13952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
13962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          DumpMappingAtOffset(os, oat_method, offset, false);
13972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
1398ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        offset += disassembler_->Dump(os, quick_native_pc + offset);
13992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
14002cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
14012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
14022cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
14032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
14042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom              DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
14052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1406ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
14072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
14082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1409b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1410b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1411b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
14123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
14132cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
141432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
141532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1416a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1417ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
14182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1419aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1420aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
14213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
142227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
14232ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom  explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
14242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
14252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
14262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
14272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
14282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
142927ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
14302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
14322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
14332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
14342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
14352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
143631e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier    os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
143731e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier       << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
143831e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
14392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
14402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1441700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
14422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1443700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1444700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1445700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1446700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1447700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
14482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1449a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1450a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
145146774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
145246774767fcf7780d1455e755729198648d08742eIgor Murashkin
14532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
14542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
14552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
14562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent1_os(&indent1_filter);
14572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
14582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
14592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
14602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
14612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
14622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
14632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
14642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
14652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          std::ostream indent2_os(&indent2_filter);
14662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1467fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
1468277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1469277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1470fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1471277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1472277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1473fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1474fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1475fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1476fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1477fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1478fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
1479277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d: ", j);
1480fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
1481277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1482277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1483fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
14842ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
14852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              PrettyObjectValue(indent2_os, value->GetClass(), value);
14862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
1487277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              indent2_os << j << ": null\n";
14882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1489d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
149034f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
149134f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
149227ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
14932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
149427ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1495aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
14962ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
14972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
14982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
14992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
15008d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
1501a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1502a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
150346774767fcf7780d1455e755729198648d08742eIgor Murashkin      oat_file = OatFile::Open(oat_location, oat_location, nullptr, nullptr, false, &error_msg);
1504a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (oat_file == nullptr) {
1505a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        os << "NOT FOUND: " << error_msg << "\n";
15062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
1507a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
1508aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
15092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1510aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
15113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
151278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
151332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
15143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
151502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
15162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
151702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
151802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
151905f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
152005f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
15212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1522b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1523b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    // Loop through all the image spaces and dump their objects.
15241d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
15251d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
152650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1527357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1528c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1529c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1530c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1531c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
153290d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
153390d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
153490d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1535c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1536c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromRunnableToSuspended(kNative);
1537c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        ThreadList* thread_list = Runtime::Current()->GetThreadList();
1538c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        thread_list->SuspendAll();
1539c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->RevokeAllThreadLocalAllocationStacks(self);
1540c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        thread_list->ResumeAll();
1541c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        self->TransitionFromSuspendedToRunnable();
1542c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
1543357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
15442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
15452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream* saved_os = os_;
15462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
15472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      std::ostream indent_os(&indent_filter);
15482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = &indent_os;
15492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
155002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& space : spaces) {
15512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (space->IsImageSpace()) {
15521d54e73444e017d3a65234e0f193846f3e27472bIan Rogers          gc::space::ImageSpace* image_space = space->AsImageSpace();
15532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << "\n";
15552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
15562fde53367dbe721e5273c34b590e67112322cc9eMathieu Chartier      }
15572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1558bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
15592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
15602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os_ = saved_os;
1561b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
15622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1563700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
15642ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() == nullptr) {
15652ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
15666f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15672ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() != nullptr) {
15682ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
15696f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
15703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
15713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
15723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
15733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.alignment_bytes += alignment_bytes;
157432327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
157532327098e300ab66671b891a18bf669576fc896aMathieu Chartier    stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
15762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    stats_.Dump(os);
15772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
157827ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
15792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
15803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
15812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
15823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1583d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
15843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
15852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
1586b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15872ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
15882ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
15892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1590d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
15912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
15922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
159368b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
159464b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
15952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
15962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1597ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (type->IsArtFieldClass()) {
1598ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtField* field = value->AsArtField();
15992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Field: %s\n", field, PrettyField(field).c_str());
1600ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (type->IsArtMethodClass()) {
1601ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = value->AsArtMethod();
16022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Method: %s\n", method, PrettyMethod(method).c_str());
1603d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
16042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1605d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1606d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1607d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1608ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
1609b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
161061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
161108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
161208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1613ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
161408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
161508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
16162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
161708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
161808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
16192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
162008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
162108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
16222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
162308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
162408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
162537f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
162608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
162708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
162837f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
162908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
163008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
163137f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
163237f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
163308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
163408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
163537f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
163608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
163708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
163808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
163908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
164008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
164108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
164208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
164350239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
164408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
164508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          mirror::Class* field_type = field->GetType(false);
164608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
164708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
164808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
164908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
165008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
165108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
165250239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
165308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
165448efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
165508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
165608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
165708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1658d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1659d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1660d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
1662b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16632dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
16642ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
16652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1666d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1667ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
16682ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (fields != nullptr) {
1669d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      for (int32_t i = 0; i < fields->GetLength(); i++) {
1670ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        mirror::ArtField* field = fields->Get(i);
16712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        PrintField(os, field, obj);
1672d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1673d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1674d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1675d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
16762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
16773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1678f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
16793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1680ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeBegin(mirror::ArtMethod* m)
1681b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1682a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1683a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
16846f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1685ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
16863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1687f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1688ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1689f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1690ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
16913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
16923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1693ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint32_t GetQuickOatCodeSize(mirror::ArtMethod* m)
1694b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1695ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1696ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1697f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1698f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1699f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1700f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1701f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1702ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  const void* GetQuickOatCodeEnd(mirror::ArtMethod* m)
1703b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1704ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
17052ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
17062ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1707f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1708ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1709f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1710f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
17112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void Callback(mirror::Object* obj, void* arg)
1712b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17132ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
17142ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
17153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
171678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
171778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
171878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1719916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1720916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1721916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1722916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1723916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1724916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
17252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *state->os_;
17262dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1727d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
17282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
17292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1730d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
17312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
17322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
17332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1734ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtField()) {
17354b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom      os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
1736ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         PrettyField(obj->AsArtField()).c_str());
1737ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
17384b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom      os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
1739ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         PrettyMethod(obj->AsArtMethod()).c_str());
1740d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
17412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
174268b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
174378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
17442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
174578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
17462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
17472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream indent_os(&indent_filter);
17482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    DumpFields(indent_os, obj, obj_class);
1749d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
17502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
1751d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      int32_t length = obj_array->GetLength();
1752d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      for (int32_t i = 0; i < length; i++) {
17532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1754d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1755d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1756d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1757d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1758d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1759d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1760d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1761d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1762d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
17632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d: ", i);
176478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
17652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          indent_os << StringPrintf("%d to %zd: ", i, i + run);
1766d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
176778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
17682ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
17692ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
17702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        PrettyObjectValue(indent_os, value_class, value);
1771d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1772d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1773ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
17742ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      if (sfields != nullptr) {
17752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << "STATICS:\n";
17762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
17772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent2_os(&indent2_filter);
1778d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t i = 0; i < sfields->GetLength(); i++) {
1779ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom          mirror::ArtField* field = sfields->Get(i);
17802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          PrintField(indent2_os, field, field->GetDeclaringClass());
1781d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1782d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1783ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    } else if (obj->IsArtMethod()) {
1784a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      const size_t image_pointer_size = InstructionSetPointerSize(
1785a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier          state->oat_dumper_->GetOatInstructionSet());
1786ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* method = obj->AsArtMethod();
1787d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      if (method->IsNative()) {
1788957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1789a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
17903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first_occurrence;
1791ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1792ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1793ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code, &first_occurrence);
17943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (first_occurrence) {
1795ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
17963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1797a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(
1798a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            image_pointer_size)) {
1799ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
18003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
18011984651929744dd603fd082e23eacd877b9bc177Ian Rogers      } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
180288474b416eb257078e590bf9bc7957cee604a186Jeff Hao          method->IsResolutionMethod() || method->IsImtConflictMethod() ||
18032d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier          method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1804957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1805a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1806916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      } else {
1807bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        const DexFile::CodeItem* code_item = method->GetCodeItem();
1808d81871cbbaa34c649e488f94f61a981db33123e5Ian Rogers        size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1809916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom        state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1810e7d856b911222aa000ca2be0f8f63f5b292141c3Brian Carlstrom
1811a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        bool first_occurrence;
1812957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier        size_t gc_map_bytes =
1813957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier            state->ComputeOatSize(method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1814a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
18153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.gc_map_bytes += gc_map_bytes;
18163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
18173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t pc_mapping_table_bytes =
1819a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetMappingTable(image_pointer_size), &first_occurrence);
1820a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
18213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
18223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
18233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t vmap_table_bytes =
1825a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier            state->ComputeOatSize(method->GetVmapTable(image_pointer_size), &first_occurrence);
1826a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
18273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          state->stats_.vmap_table_bytes += vmap_table_bytes;
18283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
18293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1830ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1831ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1832ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1833ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1834a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        if (first_occurrence) {
1835ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          state->stats_.managed_code_bytes += quick_oat_code_size;
18360d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          if (method->IsConstructor()) {
18370d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            if (method->IsStatic()) {
1838ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.class_initializer_code_bytes += quick_oat_code_size;
18390d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1840ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers              state->stats_.large_initializer_code_bytes += quick_oat_code_size;
18410d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers            }
18420d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1843ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            state->stats_.large_method_code_bytes += quick_oat_code_size;
18440d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
18453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
1846ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
18473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1848ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
18492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
18502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
18513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1853ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            vmap_table_bytes + quick_oat_code_size + object_bytes;
18543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
18553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double expansion =
1856ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers            static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
18573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        state->stats_.ComputeOutliers(total_size, expansion, method);
185878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      }
185978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
18601ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    std::string temp;
18611ff3c98775a4577cf053dba9a0c2d5c21c07b298Ian Rogers    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
186278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
186327ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
18643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
18653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
18663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
1867a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
18683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
1869a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
18703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
18713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
1872a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
18733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
18743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
187527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
1876916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1877916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
1878916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
18793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
1880916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
1881916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1882916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
1883916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
188432327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
1885916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
1886916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1887916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
18883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
1889916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
1890916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
18910d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
18920d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
18930d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
1894916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
18953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
1896916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
18973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
1898916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1899916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
1900916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1901ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    std::vector<mirror::ArtMethod*> method_outlier;
19023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
19033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
1904700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
19053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    explicit Stats()
19073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
19083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
1909916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
1910916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
191132327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
1912916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
1913916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
19143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
1915916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
1916916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
19170d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
19180d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
19190d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
19203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
1921916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
19223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
1923916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
1924916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1925a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
1926277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
1927a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
1928a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
1929a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
1930a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1931a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
1932a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
1933277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
1934a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1935a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
1936277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
1937a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
1938a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
1939277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
1940a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
1941a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
1942916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
19433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
19443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
19453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1947916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
1948916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1949916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1950916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1951916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
1952916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1953916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
1954916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1955ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
19563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
19573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
19583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
19593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
19603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
196100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
1962b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
19633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
19643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
19653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
19663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
19673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
19683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
19693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
19703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
19713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
19723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
19733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
19743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
19753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
19763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
19773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
19783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
19793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
19803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
19813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
19823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
19833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
19843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
19853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
19863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
19873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
19883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
19893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
19903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
19913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
19923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
19933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
19943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
19953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
19963a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
19973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
19983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
19993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
20003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
20023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
2003c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
20043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
20053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
2007c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
20083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
20093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
20103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
20113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
20123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
20133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
20143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
20153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
20162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2017c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
20183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2019c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
20203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
20213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
20223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
20233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
20243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
20253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
20263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
20273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
20283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
20293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
20303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
20313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
20323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
20333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
20343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
20353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
20363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
20373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
20383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
20393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20403a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
20413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
20423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
2043c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
20443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
20453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
20462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2047c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
20483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
20493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
20503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
20513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
20523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
20533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
20543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
20553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
20562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2057c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
20583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2059c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
20603a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
20613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2062b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
20642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
20652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
20662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
20672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        std::ostream indent_os(&indent_filter);
20682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        indent_os << StringPrintf("header_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "object_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
207032327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  "bitmap_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
20712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  "alignment_bytes =  %8zd (%2.0f%% of art file bytes)\n\n",
20722bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
20732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
207432327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
20752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
20762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
207732327098e300ab66671b891a18bf669576fc896aMathieu Chartier        CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
20782bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2079916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2081916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
208202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
208302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
208402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
208502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
208602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
20872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2088a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
208902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
209002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
209102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2092916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2093c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2094916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2095916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
20962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
20972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
20982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
20992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
21002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
21012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
21022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
210305f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
21042ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
21052ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
21062ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
21072ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
21082ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
21092ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
21102ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
21112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
21122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
21132ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
21142ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
21152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
21162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
211702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
21182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
211902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
212002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
212105f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
212205f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
21232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
21242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
21252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
212605f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
212705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
212805f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2129c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2130916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
21322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
21332ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
21342ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2135c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2136cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2137c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
21383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21393a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2140916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2141916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2142916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2143916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
21440d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
21450d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
21460d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
21470d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
21480d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
21490d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
21500d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
21510d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
21522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
21531d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
21543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
21552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
21562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2157d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
21583a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
215978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
216078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
216100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
216200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                     std::ostream* os) {
216300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
216400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
216500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
216600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
216700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
216800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  gc::Heap* heap = runtime->GetHeap();
21691d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace* image_space = heap->GetImageSpace();
21702ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe  CHECK(image_space != nullptr);
217178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  const ImageHeader& image_header = image_space->GetImageHeader();
217278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  if (!image_header.IsValid()) {
21730f5baa079eea76758aee32d7f87cb175deab8308Brian Carlstrom    fprintf(stderr, "Invalid image header %s\n", image_location);
217478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    return EXIT_FAILURE;
217578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
21763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
217700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ImageDumper image_dumper(os, *image_space, image_header, options);
21783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
21792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool success = image_dumper.Dump();
21802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
218178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
218278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
218300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
218400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
218500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
218600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
218700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
218800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
218900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
219000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
219100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
219200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
219300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
219400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
219500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  class_linker->RegisterOatFile(oat_file);
2196fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<std::unique_ptr<const DexFile>> dex_files;
219700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
219800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2199fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
220000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
220100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    class_linker->RegisterDexFile(*dex_file);
2202fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    dex_files.push_back(std::move(dex_file));
220300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
220400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
220500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
220600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader);
220700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedLocalRef<jobject> class_loader_local(soa.Env(),
220800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader));
220900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get());
221000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2211fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  std::vector<const DexFile*> class_path;
2212fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  for (auto& dex_file : dex_files) {
2213fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler    class_path.push_back(dex_file.get());
2214fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  }
2215fbef44de596d298dc6430f482dffc933a046dd28Richard Uhler  runtime->SetCompileTimeClassPath(class_loader, class_path);
221600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
221700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
221800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
221900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
222000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
222100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
222200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
222332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
222400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
222500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
222600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
222700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
222800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
222932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
223000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
223100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  NullHandle<mirror::ClassLoader> null_class_loader;
223200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
223300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
223432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
223500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
223600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
223700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
223800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
223900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
224000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
224100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
22423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
22433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                    &error_msg);
224400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
224500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
224600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
224700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
224800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
224900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
225000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
225100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
225200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
225300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
225400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
225500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
225600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int SymbolizeOat(const char* oat_filename, std::string& output_name) {
225700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
22583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
22593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                    &error_msg);
226000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
226100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
226200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
226300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
226400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
226500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  OatSymbolizer oat_symbolizer(oat_file, output_name);
226600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Init()) {
226700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to initialize symbolizer\n");
226800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
226900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
227000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (!oat_symbolizer.Symbolize()) {
227100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
227200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
227300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
227400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
227500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
227600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
227700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22783774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
22793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
22803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
228100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
22833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
22843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
22853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
22863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
22873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
22883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
228900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
229000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
22913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
22923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
22933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
22943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
22953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
22963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
22973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
22983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
22993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
23003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
23013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
23023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
23033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
23043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
23053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
230632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
230732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
23083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
23093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
231032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
231132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
231232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
231332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
231432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
231532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
231632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
231732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
231832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
231932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
232032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
23213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
23223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
232300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
232400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23253774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
23263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
23273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
23293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
23303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
23313774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
233200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
233300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
23353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
23363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
23373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
233800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
233900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
23403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
23413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
23423774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
23433774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
23443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
23453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
23463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
23473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
23483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
23503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
23513774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
23533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
23543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
23563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
23573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
23583774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
23593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
23613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
23623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
23633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
23653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
23663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
23673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
23693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
23703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
23713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
23723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
23733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the GC map.\n"
23753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
23763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
23783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
23793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
23803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
23813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
23823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
238332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
238432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
238532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
238632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
238732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
238832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
238932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
239032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
239132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
239232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
239332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
239432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
239532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
239632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
23973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
23983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
239932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
240032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
240132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
240232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
240332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
240432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
240532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
24063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
24073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
240900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
241000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
241200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
241332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
24143fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
241500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
241600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
241700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
241800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
241900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
242000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
242100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
242232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
242332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
242432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
242532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
242600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
242700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24283774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
24293774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
24303774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
243100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24323774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
24333774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
24343774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
24363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
24373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
24383774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
24393774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
24403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
244132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
244232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
244332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
244432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
244532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
244632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
24473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
24493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
245000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
245100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24523774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
24533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2454c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
24553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2456d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
24573774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2458c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
2459c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2460c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2461c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2462c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
246332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2464c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2465c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
246600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
246700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
24693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
24703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
24713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
24723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
24733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
247432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
24753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
247600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
247700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
247832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
24793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      == EXIT_SUCCESS;
248000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
248100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
24833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
248400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
24857934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
248678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
248778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
24883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
24893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
249078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2491