oatdump.cc revision 2fdd03c982386e1d13aed9f1b51e1f0eee237971
12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
1678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
1778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include <stdio.h>
1878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include <stdlib.h>
1978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
2027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom#include <fstream>
2127ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom#include <iostream>
223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <map>
233774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <set>
2478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include <string>
2554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include <unordered_map>
2678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include <vector>
2778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
28d582fa4ea62083a7598dded5b82dc2198b3daac7Ian Rogers#include "arch/instruction_set_features.h"
29c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier#include "art_field-inl.h"
30e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
3110c13565474de2786aad7c2e79757ea250747a15Vladimir Marko#include "base/stl_util.h"
32761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/unix_file/fd_file.h"
3378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom#include "class_linker.h"
342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h"
354f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
36e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes#include "dex_instruction.h"
373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers#include "disassembler.h"
3854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe#include "elf_builder.h"
392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "gc_map.h"
401d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/image_space.h"
411d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/large_object_space.h"
421d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
434a26f17b055cadc949c3e9fdfa637fe5656339d9Mathieu Chartier#include "image-inl.h"
442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "indenter.h"
45131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko#include "linker/buffered_output_stream.h"
46131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko#include "linker/file_output_stream.h"
471809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "mapping_table.h"
482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/array-inl.h"
492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
5005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko#include "mirror/dex_cache-inl.h"
512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
53700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom#include "oat.h"
548a630577ed2d9e9571c3434c505e5de223b23c07Vladimir Marko#include "oat_file-inl.h"
55f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier#include "oat_file_manager.h"
56e5448b5a12003b405b22cde3b94f962ab4888a87Elliott Hughes#include "os.h"
57a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
5800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h"
596bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray#include "stack_map.h"
6000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "ScopedLocalRef.h"
61c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier#include "thread_list.h"
62ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers#include "verifier/dex_gc_map.h"
632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers#include "verifier/method_verifier.h"
641809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers#include "vmap_table.h"
6500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe#include "well_known_classes.h"
6678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include <sys/stat.h>
683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin#include "cmdline.h"
6978128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
703774335b08076117d6950cd472cdd59a167470b5Igor Murashkinnamespace art {
7178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
72e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_methods_descriptions_[] = {
731984651929744dd603fd082e23eacd877b9bc177Ian Rogers  "kResolutionMethod",
7488474b416eb257078e590bf9bc7957cee604a186Jeff Hao  "kImtConflictMethod",
752d2621a1463d2f3f03fa73503fa42e43657cdcfcMathieu Chartier  "kImtUnimplementedMethod",
76e24fa61603a60ade3797e4a0c8b3fccb346cb048Brian Carlstrom  "kCalleeSaveMethod",
77aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsOnlySaveMethod",
78aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  "kRefsAndArgsSaveMethod",
79e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier};
80e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
81e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartierconst char* image_roots_descriptions_[] = {
8258ae9416e197ae68ed12ed43d87407d4dfb15093Brian Carlstrom  "kDexCaches",
8334f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom  "kClassRoots",
8478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
8578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
86ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier// Map is so that we don't allocate multiple dex files for the same OatDexFile.
87ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartierstatic std::map<const OatFile::OatDexFile*,
88ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier                std::unique_ptr<const DexFile>> opened_dex_files;
89ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier
90ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartierconst DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
91ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  DCHECK(oat_dex_file != nullptr);
92ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  auto it = opened_dex_files.find(oat_dex_file);
93ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  if (it != opened_dex_files.end()) {
94ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    return it->second.get();
95ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  }
96ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
97ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
98ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  return ret;
99ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier}
100ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier
1012d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampetemplate <typename ElfTypes>
102bc90fd09e09a845ae6ea0d84ad67560575a94142David Srbeckyclass OatSymbolizer FINAL {
10354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe public:
1042fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
1052fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      oat_file_(oat_file),
1062fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      builder_(nullptr),
1072fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      output_name_(output_name.empty() ? "symbolized.oat" : output_name),
1082fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      no_bits_(no_bits) {
10954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
11054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
11254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
11354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const OatFile::OatMethod&,
11454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile&,
11554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t,
11654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          const DexFile::CodeItem*,
11754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                                          uint32_t);
11854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
11954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  bool Symbolize() {
1206d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
1215d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky    const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
1225d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky        isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
1236d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1246d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
1256d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    std::unique_ptr<BufferedOutputStream> output_stream(
12610c13565474de2786aad7c2e79757ea250747a15Vladimir Marko        MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
1272d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
1286d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1296d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    builder_->Start();
1306d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1316d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* rodata = builder_->GetRoData();
1326d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* text = builder_->GetText();
1336d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* bss = builder_->GetBss();
1346d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* strtab = builder_->GetStrTab();
1356d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    auto* symtab = builder_->GetSymTab();
1366d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1376d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const uint8_t* rodata_begin = oat_file_->Begin();
1386d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
1392fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    if (no_bits_) {
1402fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->WriteNoBitsSection(rodata_size);
1412fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else {
1422fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->Start();
1432fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->WriteFully(rodata_begin, rodata_size);
1442fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      rodata->End();
1452fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    }
1466d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1476d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
1486d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    const size_t text_size = oat_file_->End() - text_begin;
1492fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    if (no_bits_) {
1502fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->WriteNoBitsSection(text_size);
1512fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else {
1522fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->Start();
1532fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->WriteFully(text_begin, text_size);
1542fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      text->End();
1552fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    }
1566d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
1576d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    if (oat_file_->BssSize() != 0) {
1585b1c2ca30dad519be285f0a1e839c23cc4e3a51dDavid Srbecky      bss->WriteNoBitsSection(oat_file_->BssSize());
1596d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    }
1606d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky
161316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung    if (isa == kMips || isa == kMips64) {
162316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung      builder_->WriteMIPSabiflagsSection();
163316a2186b7fa9e03187d45ac0fa320f4dff1f3dfDouglas Leung    }
164944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko    builder_->PrepareDynamicSection(
165944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko        elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
166944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko    builder_->WriteDynamicSection();
16754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1682d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    Walk(&art::OatSymbolizer<ElfTypes>::RegisterForDedup);
16954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
17054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    NormalizeState();
17154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1726d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    strtab->Start();
1736d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    strtab->Write("");  // strtab should start with empty string.
1748f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    AddTrampolineSymbols();
1752d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    Walk(&art::OatSymbolizer<ElfTypes>::AddSymbol);
1766d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    strtab->End();
17754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1786d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    symtab->Start();
1796d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    symtab->Write();
1806d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    symtab->End();
18154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1826d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky    builder_->End();
18354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
18410c13565474de2786aad7c2e79757ea250747a15Vladimir Marko    return builder_->Good();
18554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
18654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
1878f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer  void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
1888f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    if (code_offset != 0) {
1898f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer      uint32_t name_offset = builder_->GetStrTab()->Write(name);
1908f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer      uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
1919e49ab114a367203256f11545c42162a419ee880Tamas Berghammer      // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
1929e49ab114a367203256f11545c42162a419ee880Tamas Berghammer      // the end of the section in case of the last symbol.
193197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky      builder_->GetSymTab()->Add(
194197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky          name_offset,
195197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky          builder_->GetText(),
196197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky          builder_->GetText()->GetAddress() + symbol_value,
197197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky          /* size */ 0,
198197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky          STB_GLOBAL,
199197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky          STT_FUNC);
2008f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    }
2018f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer  }
2028f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer
2038f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer  void AddTrampolineSymbols() {
2048f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    const OatHeader& oat_header = oat_file_->GetOatHeader();
2058f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    AddTrampolineSymbol("interpreterToInterpreterBridge",
2068f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer                        oat_header.GetInterpreterToInterpreterBridgeOffset());
2078f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    AddTrampolineSymbol("interpreterToCompiledCodeBridge",
2088f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer                        oat_header.GetInterpreterToCompiledCodeBridgeOffset());
2098f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    AddTrampolineSymbol("jniDlsymLookup",
2108f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer                        oat_header.GetJniDlsymLookupOffset());
2118f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    AddTrampolineSymbol("quickGenericJniTrampoline",
2128f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer                        oat_header.GetQuickGenericJniTrampolineOffset());
2138f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    AddTrampolineSymbol("quickImtConflictTrampoline",
2148f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer                        oat_header.GetQuickImtConflictTrampolineOffset());
2158f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    AddTrampolineSymbol("quickResolutionTrampoline",
2168f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer                        oat_header.GetQuickResolutionTrampolineOffset());
2178f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer    AddTrampolineSymbol("quickToInterpreterBridge",
2188f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer                        oat_header.GetQuickToInterpreterBridgeOffset());
2198f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer  }
2208f75c455ab41abe709e6e801cf360dbe079d5ffaTamas Berghammer
22154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void Walk(Callback callback) {
22254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
22354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t i = 0; i < oat_dex_files.size(); i++) {
22454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
2252cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier      CHECK(oat_dex_file != nullptr);
22654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatDexFile(oat_dex_file, callback);
22754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
22854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
22954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
23054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
23154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    std::string error_msg;
232ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
233ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    if (dex_file == nullptr) {
23454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
23554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
23654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (size_t class_def_index = 0;
23754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index < dex_file->NumClassDefs();
23854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        class_def_index++) {
23954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
24054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
24154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      OatClassType type = oat_class.GetType();
24254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      switch (type) {
24354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassAllCompiled:
24454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassSomeCompiled:
245ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier          WalkOatClass(oat_class, *dex_file, class_def, callback);
24654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
24754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
24854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassNoneCompiled:
24954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        case kOatClassMax:
25054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          // Ignore.
25154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe          break;
25254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
25354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
25454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
25554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
25654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
25754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                    const DexFile::ClassDef& class_def, Callback callback) {
25813735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
25954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
26054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
26154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
26254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    // Note: even if this is an interface or a native class, we still have to walk it, as there
26354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    //       might be a static initializer.
26454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    ClassDataItemIterator it(dex_file, class_data);
26554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    SkipAllFields(&it);
26654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    uint32_t class_method_idx = 0;
26754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextDirectMethod()) {
26854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
26954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
2705182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
27154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
27254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
27354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
27454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it.HasNextVirtualMethod()) {
27554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
27654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
2775182932cf6704b53e957f7b4be021fe505a55e22Andreas Gampe                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
27854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      class_method_idx++;
27954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it.Next();
28054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
28154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DCHECK(!it.HasNext());
28254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
28354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
28454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
28554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     const OatFile::OatMethod& oat_method, const DexFile& dex_file,
28654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
28754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                     uint32_t method_access_flags, Callback callback) {
28854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if ((method_access_flags & kAccAbstract) != 0) {
28954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // Abstract method, no code.
29054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
29154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (oat_method.GetCodeOffset() == 0) {
29354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      // No code.
29454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return;
29554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
29654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
29754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
29854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe                      method_access_flags);
29954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
30054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3016a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
3026a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t class_method_index ATTRIBUTE_UNUSED,
3036a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const OatFile::OatMethod& oat_method,
3046a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile& dex_file ATTRIBUTE_UNUSED,
3056a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t dex_method_idx ATTRIBUTE_UNUSED,
3066a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
3076a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                        uint32_t method_access_flags ATTRIBUTE_UNUSED) {
30854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[oat_method.GetCodeOffset()]++;
30954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
31054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
31154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  void NormalizeState() {
31254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    for (auto& x : state_) {
31354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (x.second == 1) {
31454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        state_[x.first] = 0;
31554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
31654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
31754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
31854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
31954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  enum class DedupState {  // private
32054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kNotDeduplicated,
32154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedFirst,
32254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    kDeduplicatedOther
32354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  };
32454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  DedupState IsDuplicated(uint32_t offset) {
32554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 0) {
32654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kNotDeduplicated;
32754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
32854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (state_[offset] == 1) {
32954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      return DedupState::kDeduplicatedOther;
33054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
33154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    state_[offset] = 1;
33254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    return DedupState::kDeduplicatedFirst;
33354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
33454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3356a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
3366a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t class_method_index ATTRIBUTE_UNUSED,
3376a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const OatFile::OatMethod& oat_method,
3386a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile& dex_file,
3396a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t dex_method_idx,
3406a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
3416a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers                 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
34254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
34354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    if (dedup != DedupState::kDeduplicatedOther) {
34454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
34554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
34654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      if (dedup == DedupState::kDeduplicatedFirst) {
34754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe        pretty_name = "[Dedup]" + pretty_name;
34854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      }
34954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3506d8c8f0344a706df651567387ede683ab3ec1b5fDavid Srbecky      int name_offset = builder_->GetStrTab()->Write(pretty_name);
351197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky      uint64_t address = oat_method.GetCodeOffset() -
352197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky                         oat_file_->GetOatHeader().GetExecutableOffset() +
353197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky                         builder_->GetText()->GetAddress();
354197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky      builder_->GetSymTab()->Add(name_offset,
355197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky                                 builder_->GetText(),
356197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky                                 address,
357197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky                                 oat_method.GetQuickCodeSize(),
358197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky                                 STB_GLOBAL,
359197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky                                 STT_FUNC);
36054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
36154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
36254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
36354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe private:
36454fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  static void SkipAllFields(ClassDataItemIterator* it) {
36554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextStaticField()) {
36654fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
36754fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
36854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    while (it->HasNextInstanceField()) {
36954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe      it->Next();
37054fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe    }
37154fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  }
37254fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
37354fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  const OatFile* oat_file_;
3742d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
37554fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe  std::unordered_map<uint32_t, uint32_t> state_;
3760279ebb3efd653e6bb255470c99d26949c7bcd95Ian Rogers  const std::string output_name_;
3772fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  bool no_bits_;
37854fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe};
37954fc26c7350beb782d042ba61cb06284b3a367e4Andreas Gampe
3802cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstromclass OatDumperOptions {
3812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom public:
3822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  OatDumperOptions(bool dump_raw_mapping_table,
3832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_raw_gc_map,
3842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool dump_vmap,
385f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   bool dump_code_info_stack_maps,
3862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                   bool disassemble_code,
38700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   bool absolute_addresses,
38832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* class_filter,
38932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* method_filter,
39032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_classes,
39132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   bool list_methods,
392c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil                   bool dump_header_only,
39332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   const char* export_dex_location,
39432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                   uint32_t addr2instr)
3952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    : dump_raw_mapping_table_(dump_raw_mapping_table),
3962cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_raw_gc_map_(dump_raw_gc_map),
3972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      dump_vmap_(dump_vmap),
398f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_(dump_code_info_stack_maps),
3992cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      disassemble_code_(disassemble_code),
40000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      absolute_addresses_(absolute_addresses),
40132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_(class_filter),
4023fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      method_filter_(method_filter),
40332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_(list_classes),
40432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_(list_methods),
405c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      dump_header_only_(dump_header_only),
40632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_(export_dex_location),
40732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      addr2instr_(addr2instr),
4083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      class_loader_(nullptr) {}
4092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
4102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_mapping_table_;
4112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_raw_gc_map_;
4122cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool dump_vmap_;
413f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  const bool dump_code_info_stack_maps_;
4142cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool disassemble_code_;
4152cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const bool absolute_addresses_;
41632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const class_filter_;
4173fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* const method_filter_;
41832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_classes_;
41932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const bool list_methods_;
420c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil  const bool dump_header_only_;
42132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* const export_dex_location_;
42232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_;
42300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader>* class_loader_;
4242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom};
4252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
426e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughesclass OatDumper {
427aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom public:
4283887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
4299583fbcf597eff6d0b3c5359b8e8d5f70ed82c40Nicolas Geoffray    : oat_file_(oat_file),
430a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes      oat_dex_files_(oat_file.GetOatDexFiles()),
4312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      options_(options),
43232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_(0),
433a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
434a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier      disassembler_(Disassembler::Create(instruction_set_,
43532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                                         new DisassemblerOptions(options_.absolute_addresses_,
436a37d925d405be9f589ac282869a997e73414d859Alexandre Rames                                                                 oat_file.Begin(),
437eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                                                 true /* can_read_literals_ */))) {
43832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_loader_ != nullptr);
43932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.class_filter_ != nullptr);
44032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    CHECK(options_.method_filter_ != nullptr);
4413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    AddAllOffsets();
4423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
4433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
4442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  ~OatDumper() {
4452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    delete disassembler_;
4462cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
4472cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
448a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetInstructionSet() {
449a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    return instruction_set_;
450a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  }
451a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier
4522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool Dump(std::ostream& os) {
4532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
4543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    const OatHeader& oat_header = oat_file_.GetOatHeader();
455aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
456aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "MAGIC:\n";
457aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetMagic() << "\n\n";
458aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4594b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao    os << "LOCATION:\n";
4604b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao    os << oat_file_.GetLocation() << "\n\n";
4614b07a6e518a42eac43f5746e646e90a7865beb83Jeff Hao
462aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "CHECKSUM:\n";
463ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
464aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
465a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << "INSTRUCTION SET:\n";
466a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes    os << oat_header.GetInstructionSet() << "\n\n";
467a72ec820f8cb8e04b0ba87a62e36b05a2c92ef36Elliott Hughes
4686f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    {
4696f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      std::unique_ptr<const InstructionSetFeatures> features(
4706f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
4716f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers                                             oat_header.GetInstructionSetFeaturesBitmap()));
4726f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << "INSTRUCTION SET FEATURES:\n";
4736f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers      os << features->GetFeatureString() << "\n\n";
4746f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    }
4757020278bce98a0735dc6abcbd33bdf1ed2634f1dDave Allison
476aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << "DEX FILE COUNT:\n";
477aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << oat_header.GetDexFileCount() << "\n\n";
478aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
4792ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#define DUMP_OAT_HEADER_OFFSET(label, offset) \
4802ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << label " OFFSET:\n"; \
4812ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("0x%08x", oat_header.offset()); \
48232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
4832ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
4842ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    } \
4852ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    os << StringPrintf("\n\n");
4862ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom
4872ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
4882ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
4892ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToInterpreterBridgeOffset);
4902ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
4912ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetInterpreterToCompiledCodeBridgeOffset);
4922ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
4932ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetJniDlsymLookupOffset);
4942ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
4952ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickGenericJniTrampolineOffset);
4962ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
4972ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickImtConflictTrampolineOffset);
4982ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
4992ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickResolutionTrampolineOffset);
5002ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
5012ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                           GetQuickToInterpreterBridgeOffset);
5022ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom#undef DUMP_OAT_HEADER_OFFSET
503aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
5042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "IMAGE PATCH DELTA:\n";
5052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << StringPrintf("%d (0x%08x)\n\n",
5062cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta(),
5072cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                       oat_header.GetImagePatchDelta());
508a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
50928db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
51028db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
51128db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom
51228db0129e5d7ef642cf8845c86c0e11832817085Brian Carlstrom    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
513700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
51481f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
51522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    // Print the key-value store.
51622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    {
51722f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "KEY VALUE STORE:\n";
51822f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      size_t index = 0;
51922f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* key;
52022f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      const char* value;
52122f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
52222f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        os << key << " = " << value << "\n";
52322f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe        index++;
52422f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      }
52522f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe      os << "\n";
52622f8e5c82d12951be38cd893426e13bee33fd69dAndreas Gampe    }
52781f3ca17e9e8d360cc4a1b6c3155cf01ba3be3bcBrian Carlstrom
52832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.absolute_addresses_) {
5292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "BEGIN:\n";
5302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
5312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
5322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << "END:\n";
5332cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
5342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
535aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
5362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "SIZE:\n";
5372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << oat_file_.Size() << "\n\n";
538aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
539aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
540aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
54132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // If set, adjust relative address to be searched
54232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.addr2instr_ != 0) {
54332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
54432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "SEARCH ADDRESS (executable offset + input):\n";
54532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
54632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
54732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
548c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil    if (!options_.dump_header_only_) {
549c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      for (size_t i = 0; i < oat_dex_files_.size(); i++) {
550c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
551c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        CHECK(oat_dex_file != nullptr);
55232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
553c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        // If file export selected skip file analysis
554c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        if (options_.export_dex_location_) {
555c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          if (!ExportDexFile(os, *oat_dex_file)) {
556c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil            success = false;
557c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          }
558c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        } else {
559c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          if (!DumpOatDexFile(os, *oat_dex_file)) {
560c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil            success = false;
561c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil          }
56232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        }
5632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
5643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
565c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil
5662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << std::flush;
5672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
5683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
5693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
5703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  size_t ComputeSize(const void* oat_data) {
57113735955f39b3b304c37d2b2840663c131262c18Ian Rogers    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
57213735955f39b3b304c37d2b2840663c131262c18Ian Rogers        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
5733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return 0;  // Address not in oat file
5743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
575ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
576ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
577ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    auto it = offsets_.upper_bound(begin_offset);
5783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    CHECK(it != offsets_.end());
579ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    uintptr_t end_offset = *it;
5803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return end_offset - begin_offset;
5813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
582e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
583a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet GetOatInstructionSet() {
584f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_file_.GetOatHeader().GetInstructionSet();
585f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
586f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
58790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
5883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
5893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
5908d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      CHECK(oat_dex_file != nullptr);
5918d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
592ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
593ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (dex_file == nullptr) {
5948d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
5958d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
5968d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      } else {
597e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier        const char* descriptor = m->GetDeclaringClassDescriptor();
5988b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers        const DexFile::ClassDef* class_def =
599e7c9a8c2b8481aafbc6af4ce6229bd361ba24742Mathieu Chartier            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
6002ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_def != nullptr) {
6018b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
602d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
6033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t method_index = m->GetMethodIndex();
604d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko          return oat_class.GetOatMethod(method_index).GetQuickCode();
6053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
6063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
607aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
6082ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
609aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
610aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
611aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom private:
6123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void AddAllOffsets() {
613e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // We don't know the length of the code for each method, but we need to know where to stop
614e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // when disassembling. What we do know is that a region of code will be followed by some other
615e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
616e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // of a piece of code by using upper_bound to find the start of the next region.
6173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
6183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
6192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
6208d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      std::string error_msg;
621ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
622ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (dex_file == nullptr) {
6238d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
6248d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers            << "': " << error_msg;
6253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        continue;
626e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
627ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
6282ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      for (size_t class_def_index = 0;
6292ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index < dex_file->NumClassDefs();
6302ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom           class_def_index++) {
631e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
632d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
63313735955f39b3b304c37d2b2840663c131262c18Ian Rogers        const uint8_t* class_data = dex_file->GetClassData(class_def);
6342ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe        if (class_data != nullptr) {
635e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          ClassDataItemIterator it(*dex_file, class_data);
636e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          SkipAllFields(it);
637e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          uint32_t class_method_index = 0;
638e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextDirectMethod()) {
639d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
640e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
641e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
642e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          while (it.HasNextVirtualMethod()) {
643d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko            AddOffsets(oat_class.GetOatMethod(class_method_index++));
644e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes            it.Next();
645e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes          }
646e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes        }
647e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
648e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
649e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
650e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // If the last thing in the file is code for a method, there won't be an offset for the "next"
651e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
652e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    // for the end of the file.
653ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    offsets_.insert(oat_file_.Size());
654e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
655e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
6562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
6572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
6582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  }
6592cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
660e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  void AddOffsets(const OatFile::OatMethod& oat_method) {
66195ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    uint32_t code_offset = oat_method.GetCodeOffset();
66295ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
66395ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom      code_offset &= ~0x1;
66495ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    }
66595ba0dc516531776813c426e85ce868ead564a7bBrian Carlstrom    offsets_.insert(code_offset);
666e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetMappingTableOffset());
667e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    offsets_.insert(oat_method.GetVmapTableOffset());
668957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    offsets_.insert(oat_method.GetGcMapOffset());
669e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
670e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
6712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
6722cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
67332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool stop_analysis = false;
6742cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    os << "OatDexFile:\n";
675a004aa933a58428489e42d77f707c2b063b73747Brian Carlstrom    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
676ed2adb6158ffbe85c89aa5c9892a35bafa5006cdElliott Hughes    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
677590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
678590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    // Create the verifier early.
679590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
6808d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
681ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
682ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    if (dex_file == nullptr) {
6838d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers      os << "NOT FOUND: " << error_msg << "\n\n";
6842cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << std::flush;
6852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return false;
686aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
6878f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
6888f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    VariableIndentationOutputStream vios(&os);
6898f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&vios);
6902ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    for (size_t class_def_index = 0;
6912ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index < dex_file->NumClassDefs();
6922ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom         class_def_index++) {
693aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
694aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom      const char* descriptor = dex_file->GetClassDescriptor(class_def);
69532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
69632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Support regex
69732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
69832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        continue;
69932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
70032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
7012cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
702d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
7032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
7042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
705d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetStatus() << ")"
706d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko         << " (" << oat_class.GetType() << ")\n";
707d3c5bebcb52a67cb06e7ab303eaf45f230c08b60Vladimir Marko      // TODO: include bitmap here if type is kOatClassSomeCompiled?
70832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.list_classes_) continue;
709ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier      if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
7102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
7112cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
71232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (stop_analysis) {
71332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << std::flush;
71432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
71532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
716aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
717aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
718aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    os << std::flush;
7192cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
720aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
721aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
72232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
72332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string error_msg;
72432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
72532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
726ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
72732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file == nullptr) {
72832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
72932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
73032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
73132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t fsize = oat_dex_file.FileSize();
73232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
73332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Some quick checks just in case
73432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
73532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Invalid dex file\n";
73632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
73732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
73832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
73932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Verify output directory exists
74032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!OS::DirectoryExists(options_.export_dex_location_)) {
74132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      // TODO: Extend OS::DirectoryExists if symlink support is required
74232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << options_.export_dex_location_ << " output directory not found or symlink\n";
74332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
74432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
74532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
74632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // Beautify path names
74732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
74832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
74932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
75032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
75132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string dex_orig_name;
75232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    size_t dex_orig_pos = dex_file_location.rfind('/');
75332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_pos == std::string::npos)
75432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location;
75532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    else
75632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
75732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
75832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // A more elegant approach to efficiently name user installed apps is welcome
75932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
76032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
76132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      size_t apk_orig_pos = dex_file_location.rfind('/');
76232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (apk_orig_pos != std::string::npos) {
76332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
76432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
76532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
76632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
76732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string out_dex_path(options_.export_dex_location_);
76832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.back() != '/') {
76932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      out_dex_path.append("/");
77032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
77132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append(dex_orig_name);
77232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    out_dex_path.append("_export.dex");
77332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (out_dex_path.length() > PATH_MAX) {
77432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
77532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
77632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
77732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
77832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file.get() == nullptr) {
77932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to open output dex file " << out_dex_path;
78032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
78132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
78232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
78332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (!file->WriteFully(dex_file->Begin(), fsize)) {
78432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Failed to write dex file";
78532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      file->Erase();
78632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
78732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
78832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
78932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (file->FlushCloseOrErase() != 0) {
79032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      os << "Flush and close failed";
79132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      return false;
79232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
79332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
79532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    os << std::flush;
79632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
79732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    return true;
79832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  }
79932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
800e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  static void SkipAllFields(ClassDataItemIterator& it) {
8010571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextStaticField()) {
8020571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
803aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
8040571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextInstanceField()) {
8050571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
806aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
807e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes  }
808aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
8098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatClass(VariableIndentationOutputStream* vios,
8108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                    const OatFile::OatClass& oat_class, const DexFile& dex_file,
81132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
8122cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
81332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    bool addr_found = false;
81413735955f39b3b304c37d2b2840663c131262c18Ian Rogers    const uint8_t* class_data = dex_file.GetClassData(class_def);
8152ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (class_data == nullptr) {  // empty class such as a marker interface?
8168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << std::flush;
8172cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      return success;
818e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
819e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    ClassDataItemIterator it(dex_file, class_data);
820e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    SkipAllFields(it);
8212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    uint32_t class_method_index = 0;
8220571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextDirectMethod()) {
8238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
8242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
82532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
8262cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
82832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
82932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
83032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
83132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
8322cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
8330571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
834aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
8350571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    while (it.HasNextVirtualMethod()) {
8368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
8372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                         it.GetMemberIndex(), it.GetMethodCodeItem(),
83832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                         it.GetRawMemberAccessFlags(), &addr_found)) {
8392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
8402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
84132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (addr_found) {
84232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *stop_analysis = true;
84332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
84432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
8452cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      class_method_index++;
8460571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers      it.Next();
847aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
8480571d357843c53e042f370f5f2c2e9aa3fe803a9Ian Rogers    DCHECK(!it.HasNext());
8498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
8502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
851aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
852e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
8532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kPrologueBytes = 16;
8542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8552cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
8562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
8572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
8588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  bool DumpOatMethod(VariableIndentationOutputStream* vios,
8598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                     const DexFile::ClassDef& class_def,
8608b2c0b9abc3f520495f4387ea040132ba85cae69Ian Rogers                     uint32_t class_method_index,
8612cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
8622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
86332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     uint32_t method_access_flags, bool* addr_found) {
8642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    bool success = true;
86532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
86632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    // TODO: Support regex
86732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
86832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (method_name.find(options_.method_filter_) == std::string::npos) {
8693fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray      return success;
8703fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray    }
8713fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray
87232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
8738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
8748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   class_method_index, pretty_method.c_str(),
8758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                   dex_method_idx);
87632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (options_.list_methods_) return success;
87732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
87832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
87932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
88032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
88132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_offset = oat_method.GetCodeOffset();
88232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    uint32_t code_size = oat_method.GetQuickCodeSize();
88332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    if (resolved_addr2instr_ != 0) {
88432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (resolved_addr2instr_ > code_offset + code_size) {
88532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return success;
88632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else {
88732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *addr_found = true;  // stop analyzing file at next iteration
88832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
88932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    }
89032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis
8918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    // Everything below is indented at least once.
8928f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(vios);
8938f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
8942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
8958f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "DEX CODE:\n";
8968f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
8978f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpDexCode(vios->Stream(), dex_file, code_item);
8982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
8992ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
900673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier    std::unique_ptr<StackHandleScope<1>> hs;
9012ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    std::unique_ptr<verifier::MethodVerifier> verifier;
9022ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (Runtime::Current() != nullptr) {
903673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      // We need to have the handle scope stay live until after the verifier since the verifier has
904673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      // a handle to the dex cache from hs.
905673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      hs.reset(new StackHandleScope<1>(Thread::Current()));
9068f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
9078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
908673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier      verifier.reset(DumpVerifier(vios, hs.get(),
9098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                  dex_method_idx, &dex_file, class_def, code_item,
9102ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                  method_access_flags));
9112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
9122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
9138f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatMethodOffsets ";
91432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9158f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method_offsets);
9162cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9178f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
9182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (oat_method_offsets_offset > oat_file_.Size()) {
9198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
9202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
9212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            oat_method_offsets_offset, oat_file_.Size());
9222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
9238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
9242cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
9252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
926a8b41aaa08283390606e6515d178ca3cc4c6ca41Andreas Gampe
9278f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
9292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
9302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (aligned_code_begin > oat_file_.Size()) {
9318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code offset 0x%08x is past end of file 0x%08zx.\n",
9338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       aligned_code_begin, oat_file_.Size());
9342cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
9352cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9368f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
9373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
9388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "gc_map: ";
93932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9408f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
9413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray      }
942957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
9438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
9442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (gc_map_offset > oat_file_.Size()) {
9458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                           "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
9478f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                           gc_map_offset, oat_file_.Size());
9482cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
94932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_gc_map_) {
9508f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent3(vios);
9518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpGcMap(vios->Stream(), oat_method, code_item);
9522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
9542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
9558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "OatQuickMethodHeader ";
9562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
9572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
9582cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
95932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9608f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", method_header);
9612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
9628f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
9632cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (method_header_offset > oat_file_.Size()) {
9648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf(
9652cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
9662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            method_header_offset, oat_file_.Size());
9672cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
9688f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << std::flush;
9692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
9702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9712cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
9738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "mapping_table: ";
97432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
9762cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
9788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
9792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (mapping_table_offset > oat_file_.Size()) {
9808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9818f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "mapping table offset 0x%08x is past end of file 0x%08zx. "
9828f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "mapping table offset was loaded from offset 0x%08x.\n",
9838f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       mapping_table_offset, oat_file_.Size(),
9848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetMappingTableOffsetOffset());
9852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
98632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_raw_mapping_table_) {
9878f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent3(vios);
9888f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpMappingTable(vios, oat_method);
9892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9902cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
9918f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "vmap_table: ";
99232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (options_.absolute_addresses_) {
9938f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
9942cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
9952cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
9968f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
9972cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (vmap_table_offset > oat_file_.Size()) {
9988f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
9998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset 0x%08x is past end of file 0x%08zx. "
10008f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "vmap table offset was loaded from offset 0x%08x.\n",
10018f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       vmap_table_offset, oat_file_.Size(),
10028f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       oat_method.GetVmapTableOffsetOffset());
10032cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
100432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      } else if (options_.dump_vmap_) {
10058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapData(vios, oat_method, code_item);
10062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
10072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
10082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
10098f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "QuickMethodFrameInfo\n";
10108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
10118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
10128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream()
10138f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
10148f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
10158f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
10168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
10178f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
10188f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
10198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "\n";
10202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
10212cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
10228f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // Based on spill masks from QuickMethodFrameInfo so placed
10238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // after it is dumped, but useful for understanding quick
10248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      // code, so dumped here.
10258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      ScopedIndentation indent2(vios);
10268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      DumpVregLocations(vios->Stream(), oat_method, code_item);
10272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
10282cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    {
10298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "CODE: ";
10302cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
10312cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      if (code_size_offset > oat_file_.Size()) {
10328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
10338f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("WARNING: "
10348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       "code size offset 0x%08x is past end of file 0x%08zx.",
10358f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset, oat_file_.Size());
10362cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        success = false;
10372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      } else {
10382cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        const void* code = oat_method.GetQuickCode();
10392cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
10402cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        uint64_t aligned_code_end = aligned_code_begin + code_size;
1041590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier
104232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        if (options_.absolute_addresses_) {
10438f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("%p ", code);
10442cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
10458f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
10468f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_offset,
10478f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size_offset,
10488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code_size,
10498f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                       code != nullptr ? "..." : "");
10502cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom
10518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(vios);
10522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (aligned_code_begin > oat_file_.Size()) {
10538f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("WARNING: "
10548f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         "start of code at 0x%08x is past end of file 0x%08zx.",
10558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         aligned_code_begin, oat_file_.Size());
10562cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
10572cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (aligned_code_end > oat_file_.Size()) {
10588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
10598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
10608f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
10618f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
10628f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              aligned_code_end, oat_file_.Size(),
10638f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
10642cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
106532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
10662cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
10678f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
10682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
10692cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
10702cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        } else if (code_size > kMaxCodeSize) {
10718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf(
10728f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "WARNING: "
10738f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size %d is bigger than max expected threshold of %d. "
10748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              "code size is 0x%08x loaded from offset 0x%08x.\n",
10758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, kMaxCodeSize,
10768f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              code_size, code_size_offset);
10772cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          success = false;
107832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis          if (options_.disassemble_code_) {
10792cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
10808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
10812cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
10822cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          }
108332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        } else if (options_.disassemble_code_) {
10848f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
10852cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
10862cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      }
10872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
10888f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    vios->Stream() << std::flush;
10892cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return success;
10903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
10913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
10923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
10933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (spill_mask == 0) {
10943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return;
10953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
10962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "(";
10973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    for (size_t i = 0; i < 32; i++) {
10983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if ((spill_mask & (1 << i)) != 0) {
10993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (is_float) {
11003a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "fr" << i;
11013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
11023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << "r" << i;
11033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
11043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        spill_mask ^= 1 << i;  // clear bit
11053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        if (spill_mask != 0) {
11063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          os << ", ";
11073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        } else {
11083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          break;
11093a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
11103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
11113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
11123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    os << ")";
11133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
11143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1115442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display data stored at the the vmap offset of an oat method.
11168f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpVmapData(VariableIndentationOutputStream* vios,
1117442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const OatFile::OatMethod& oat_method,
1118442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem* code_item) {
1119f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1120f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The optimizing compiler outputs its CodeInfo data in the vmap table.
1121442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const void* raw_code_info = oat_method.GetVmapTable();
1122442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_code_info != nullptr) {
1123442b46a087c389a91a0b51547ac9205058432364Roland Levillain        CodeInfo code_info(raw_code_info);
1124442b46a087c389a91a0b51547ac9205058432364Roland Levillain        DCHECK(code_item != nullptr);
11258f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
11268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpCodeInfo(vios, code_info, oat_method, *code_item);
1127442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
11280a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
11290a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // We don't encode the size in the table, so just emit that we have quickened
11300a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      // information.
11310a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      ScopedIndentation indent(vios);
11320a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray      vios->Stream() << "quickened data\n";
1133442b46a087c389a91a0b51547ac9205058432364Roland Levillain    } else {
1134442b46a087c389a91a0b51547ac9205058432364Roland Levillain      // Otherwise, display the vmap table.
1135442b46a087c389a91a0b51547ac9205058432364Roland Levillain      const uint8_t* raw_table = oat_method.GetVmapTable();
1136442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (raw_table != nullptr) {
1137442b46a087c389a91a0b51547ac9205058432364Roland Levillain        VmapTable vmap_table(raw_table);
11388f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpVmapTable(vios->Stream(), oat_method, vmap_table);
1139442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
114020d3eae6bd65bea5206e57acab8145956e36ab08Nicolas Geoffray    }
1141442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1142442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1143442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a CodeInfo object emitted by the optimizing compiler.
11448f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCodeInfo(VariableIndentationOutputStream* vios,
1145442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const CodeInfo& code_info,
1146f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                    const OatFile::OatMethod& oat_method,
1147442b46a087c389a91a0b51547ac9205058432364Roland Levillain                    const DexFile::CodeItem& code_item) {
11488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    code_info.Dump(vios,
1149f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   oat_method.GetCodeOffset(),
1150f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   code_item.registers_size_,
1151f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                   options_.dump_code_info_stack_maps_);
1152442b46a087c389a91a0b51547ac9205058432364Roland Levillain  }
1153442b46a087c389a91a0b51547ac9205058432364Roland Levillain
1154442b46a087c389a91a0b51547ac9205058432364Roland Levillain  // Display a vmap table.
1155442b46a087c389a91a0b51547ac9205058432364Roland Levillain  void DumpVmapTable(std::ostream& os,
1156442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const OatFile::OatMethod& oat_method,
1157442b46a087c389a91a0b51547ac9205058432364Roland Levillain                     const VmapTable& vmap_table) {
1158442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool first = true;
1159442b46a087c389a91a0b51547ac9205058432364Roland Levillain    bool processing_fp = false;
1160442b46a087c389a91a0b51547ac9205058432364Roland Levillain    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1161442b46a087c389a91a0b51547ac9205058432364Roland Levillain    for (size_t i = 0; i < vmap_table.Size(); i++) {
1162442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint16_t dex_reg = vmap_table[i];
1163442b46a087c389a91a0b51547ac9205058432364Roland Levillain      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1164442b46a087c389a91a0b51547ac9205058432364Roland Levillain                                                    processing_fp ? kFloatVReg : kIntVReg);
1165442b46a087c389a91a0b51547ac9205058432364Roland Levillain      os << (first ? "v" : ", v")  << dex_reg;
1166442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp) {
1167442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/r" << cpu_reg;
1168442b46a087c389a91a0b51547ac9205058432364Roland Levillain      } else {
1169442b46a087c389a91a0b51547ac9205058432364Roland Levillain        os << "/fr" << cpu_reg;
1170442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
1171442b46a087c389a91a0b51547ac9205058432364Roland Levillain      first = false;
1172442b46a087c389a91a0b51547ac9205058432364Roland Levillain      if (!processing_fp && dex_reg == 0xFFFF) {
1173442b46a087c389a91a0b51547ac9205058432364Roland Levillain        processing_fp = true;
1174442b46a087c389a91a0b51547ac9205058432364Roland Levillain        spill_mask = oat_method.GetFpSpillMask();
1175442b46a087c389a91a0b51547ac9205058432364Roland Levillain      }
11763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1177442b46a087c389a91a0b51547ac9205058432364Roland Levillain    os << "\n";
11783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
11793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1180faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1181faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru                         const DexFile::CodeItem* code_item) {
1182faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    if (code_item != nullptr) {
1183faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals_ins = code_item->registers_size_;
1184faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_ins = code_item->ins_size_;
1185faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_locals = num_locals_ins - num_ins;
1186faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      size_t num_outs = code_item->outs_size_;
1187faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1188faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "vr_stack_locations:";
1189faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1190faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        // For readability, delimit the different kinds of VRs.
1191faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (reg == num_locals_ins) {
1192faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tmethod*:";
1193faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == num_locals && num_ins > 0) {
1194faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tins:";
1195faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        } else if (reg == 0 && num_locals > 0) {
1196faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\tlocals:";
1197faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1198faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
119915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
120015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
120115b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
120215b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
120315b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
120415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
120515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1206faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << reg << "[sp + #" << offset << "]";
1207faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1208faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1209faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1210faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        if (out_reg == 0) {
1211faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru          os << "\n\touts:";
1212faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        }
1213faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1214faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1215faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru        os << " v" << out_reg << "[sp + #" << offset << "]";
1216faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      }
1217faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1218faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru      os << "\n";
1219faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru    }
1220faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru  }
1221faf9f0d53573025dc5ae5ff6c4412396030cf1daRazvan A Lupusoru
1222b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
12232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
12241809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    const uint8_t* raw_table = oat_method.GetVmapTable();
12252ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (raw_table != nullptr) {
1226b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      const VmapTable vmap_table(raw_table);
1227b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      uint32_t vmap_offset;
12281809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
12292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
12302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
12312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                       : oat_method.GetCoreSpillMask();
12322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1233b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      } else {
123415b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
123515b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            code_item,
123615b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetCoreSpillMask(),
123715b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFpSpillMask(),
123815b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            oat_method.GetFrameSizeInBytes(),
123915b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            reg,
124015b9d5274399736ac09705f0507df24fac4f00c1Nicolas Geoffray            GetInstructionSet());
1241b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        os << "[sp + #" << offset << "]";
1242b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
1243b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1244b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1245b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1246ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1247ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          const DexFile::CodeItem* code_item,
1248ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                          size_t num_regs, const uint8_t* reg_bitmap) {
1249ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    bool first = true;
1250ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    for (size_t reg = 0; reg < num_regs; reg++) {
1251ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1252ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        if (first) {
1253ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << "  v" << reg << " (";
1254ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1255ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1256ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          first = false;
1257ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        } else {
1258ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ", v" << reg << " (";
1259ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1260ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          os << ")";
1261ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        }
1262ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      }
1263ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1264ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (first) {
1265ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "No registers in GC map\n";
1266ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    } else {
1267ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      os << "\n";
1268ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    }
1269ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  }
1270b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1271b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers                 const DexFile::CodeItem* code_item) {
1272957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1273ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (gc_map_raw == nullptr) {
1274ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      return;  // No GC map.
12753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1276ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1277956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    NativePcOffsetToReferenceMap map(gc_map_raw);
1278956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1279956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1280956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes          map.GetNativePcOffset(entry);
1281956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      os << StringPrintf("%p", native_pc);
1282956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
12833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1284aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom  }
1285e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes
12868f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpMappingTable(VariableIndentationOutputStream* vios,
12878f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                        const OatFile::OatMethod& oat_method) {
1288ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1289ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (quick_code == nullptr) {
12902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      return;
12912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
12921809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
12931809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (table.TotalSize() != 0) {
12941809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.PcToDexSize() != 0) {
12951809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::PcToDexIterator It;
12968f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "suspend point mappings {\n";
12971809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
12988f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent1(vios);
12998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
13001809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
13018f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "}\n";
13021809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      }
13031809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      if (table.DexToPcSize() != 0) {
13041809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        typedef MappingTable::DexToPcIterator It;
13058f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "catch entry mappings {\n";
13061809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
13078f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent1(vios);
13088f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
13091809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
13108f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios->Stream() << "}\n";
13113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
13123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
13133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
13143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
13158f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
1316f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const OatFile::OatMethod& oat_method,
1317f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   const DexFile::CodeItem* code_item,
1318f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   size_t offset,
1319f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                   bool suspend_point_mapping) {
1320f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1321f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (suspend_point_mapping) {
13228f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent1(vios);
13238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
1324f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1325f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // The return value is not used in the case of a method compiled
1326f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      // with the optimizing compiler.
1327f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      return DexFile::kDexNoIndex;
1328f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else {
13298f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
1330f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1331f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1332f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
13331809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
13341809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers                               size_t offset, bool suspend_point_mapping) {
13351809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    MappingTable table(oat_method.GetMappingTable());
13361809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    if (suspend_point_mapping && table.PcToDexSize() > 0) {
13371809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::PcToDexIterator It;
13381809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
13391809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
13404b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
13411809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
13421809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        }
1343b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
13441809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
13451809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      typedef MappingTable::DexToPcIterator It;
13461809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
13471809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers        if (offset == cur.NativePcOffset()) {
13484b8c13ee44c4c959d7b8de9adff7ce6df48c31d0Brian Carlstrom          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
13491809a72a66d245ae598582d658b93a24ac3bf01eIan Rogers          return cur.DexPc();
1350b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1351b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
13523a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
13532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    return DexFile::kDexNoIndex;
1354b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
13553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
13562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
13572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1358957ca1cd025104fccb0b08928f955f9bdb4ab91cMathieu Chartier    const uint8_t* gc_map_raw = oat_method.GetGcMap();
13592ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (gc_map_raw != nullptr) {
1360b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      NativePcOffsetToReferenceMap map(gc_map_raw);
13612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (map.HasEntry(native_pc_offset)) {
1362b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        size_t num_regs = map.RegWidth() * 8;
13632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1364b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        bool first = true;
1365b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        for (size_t reg = 0; reg < num_regs; reg++) {
1366b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1367b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            if (first) {
13682bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              os << "GC map objects:  v" << reg << " (";
13692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1370b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1371b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              first = false;
1372b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            } else {
1373b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ", v" << reg << " (";
13742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1375b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers              os << ")";
1376b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers            }
1377b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          }
1378b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1379b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        if (!first) {
1380b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers          os << "\n";
1381b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        }
1382b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      }
13833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1384b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
13853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1386590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1387590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const OatFile::OatMethod& oat_method,
1388590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1389590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    DCHECK(verifier != nullptr);
13907b3ddd27c223fcce784314f78fda7f67dcb37730Ian Rogers    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
13912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    bool first = true;
13922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
13932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
13942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      if (kind != kUndefined) {
13952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (first) {
13962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << "VRegs:  v";
13972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          first = false;
13982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        } else {
13992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          os << ", v";
14002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
14012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << reg << " (";
14022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        switch (kind) {
14032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kImpreciseConstant:
14042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
14052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
14062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
14072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          case kConstant:
14082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            os << "Constant: " << kinds.at((reg * 2) + 1);
14092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
14102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          default:
14112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            DescribeVReg(os, oat_method, code_item, reg, kind);
14122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            break;
14132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
14142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << ")";
14152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
14162bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
14172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if (!first) {
14182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n";
14192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
14202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
14212bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
14222bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1423b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
14242ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (code_item != nullptr) {
1425b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      size_t i = 0;
1426b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      while (i < code_item->insns_size_in_code_units_) {
1427b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
142832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
142932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1430b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers        i += instruction->SizeInCodeUnits();
1431e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes      }
1432e3c845cdb5884e770287a5c0c65c8bb64733c388Elliott Hughes    }
1433b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1434b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
1435f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1436f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  // the optimizing compiler?
1437f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1438f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                                    const DexFile::CodeItem* code_item) {
1439f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // If the native GC map is null and the Dex `code_item` is not
1440f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // null, then this method has been compiled with the optimizing
1441f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // compiler.
14420a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() != nullptr &&
14430a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetGcMap() == nullptr &&
14440a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
14450a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  }
14460a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray
14470a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
14480a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  // the dextodex compiler?
14490a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray  static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
14500a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray                                                  const DexFile::CodeItem* code_item) {
14510a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // If the quick code is null, the Dex `code_item` is not
14520a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // null, and the vmap table is not null, then this method has been compiled
14530a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    // with the dextodex compiler.
14540a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray    return oat_method.GetQuickCode() == nullptr &&
14550a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           oat_method.GetVmapTable() != nullptr &&
14560a5cd12a30e1b02833bdd94f11b74ecf4835ecb3Nicolas Geoffray           code_item != nullptr;
1457f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1458f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
14598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
1460f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const OatFile::OatMethod& oat_method,
1461f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  const DexFile::CodeItem* code_item,
1462f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain                                  size_t offset) {
1463f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // This method is only relevant for oat methods compiled with the
1464f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // optimizing compiler.
1465f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1466f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
1467f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    // The optimizing compiler outputs its CodeInfo data in the vmap table.
1468f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    const void* raw_code_info = oat_method.GetVmapTable();
1469f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    if (raw_code_info != nullptr) {
1470f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      CodeInfo code_info(raw_code_info);
1471f677ebfd832c9c614fea5e6735725fec2f7a3f2aDavid Brazdil      StackMapEncoding encoding = code_info.ExtractEncoding();
1472f677ebfd832c9c614fea5e6735725fec2f7a3f2aDavid Brazdil      StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
1473f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      if (stack_map.IsValid()) {
14748f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
14758f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                       code_item->registers_size_);
1476f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      }
1477f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    }
1478f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  }
1479f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain
14808f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
1481673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier                                         StackHandleScope<1>* hs,
14828f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                                         uint32_t dex_method_idx,
14832ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile* dex_file,
14842ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::ClassDef& class_def,
14852ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         const DexFile::CodeItem* code_item,
14862ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe                                         uint32_t method_access_flags) {
14872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    if ((method_access_flags & kAccNative) == 0) {
14882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ScopedObjectAccess soa(Thread::Current());
1489d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier      Runtime* const runtime = Runtime::Current();
1490eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      Handle<mirror::DexCache> dex_cache(
1491d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier          hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1492d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier                                                                   runtime->GetLinearAlloc())));
149332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      DCHECK(options_.class_loader_ != nullptr);
1494e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      return verifier::MethodVerifier::VerifyMethodAndDump(
14958f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
14968f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          &class_def, code_item, nullptr, method_access_flags);
14972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    }
14982ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe
14992ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    return nullptr;
15002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  }
15012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
15028f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  void DumpCode(VariableIndentationOutputStream* vios,
15038f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko                verifier::MethodVerifier* verifier,
15042cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
15052cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom                bool bad_input, size_t code_size) {
1506ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const void* quick_code = oat_method.GetQuickCode();
1507ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers
15082cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    if (code_size == 0) {
15092cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      code_size = oat_method.GetQuickCodeSize();
15102cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    }
1511956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    if (code_size == 0 || quick_code == nullptr) {
15128f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko      vios->Stream() << "NO CODE!\n";
1513b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers      return;
1514956af0f0cb05422e38c1d22cbef309d16b8a1a12Elliott Hughes    } else {
1515ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1516ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      size_t offset = 0;
1517ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      while (offset < code_size) {
15182cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
15198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
15202cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        }
15218f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
15222cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        if (!bad_input) {
15238f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          uint32_t dex_pc =
15248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
15252cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom          if (dex_pc != DexFile::kDexNoIndex) {
15268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko            DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
15272cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            if (verifier != nullptr) {
15288f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
15292cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom            }
1530ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          }
15312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
15322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
1533b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers    }
1534b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers  }
1535b23a7729cf7855fa05345d03a4d84111d5ec7172Ian Rogers
15363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const OatFile& oat_file_;
15372cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
153832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const OatDumperOptions& options_;
153932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t resolved_addr2instr_;
1540a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier  InstructionSet instruction_set_;
1541ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  std::set<uintptr_t> offsets_;
15422cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  Disassembler* disassembler_;
1543aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom};
1544aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
15453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogersclass ImageDumper {
154627ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom public:
15473887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain  ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
15483887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain              const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
15492cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom      : os_(os),
15508f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        vios_(os),
15518f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent1_(&vios_),
15522cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_space_(image_space),
15532cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        image_header_(image_header),
15542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        oat_dumper_options_(oat_dumper_options) {}
155527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
155690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
15572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    std::ostream& os = *os_;
15588f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& indent_os = vios_.Stream();
15598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
15602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
15612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1562dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1563dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao
15642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
15652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1566e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1567e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
1568e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1569e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto section = static_cast<ImageHeader::ImageSections>(i);
1570e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1571e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
157231e8925781c2302f1d1a9b39e216ba415bfe0d7eMathieu Chartier
15732bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
15742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1575700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
15762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1577700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1578700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1579700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1580700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
1581700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
15822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers
1583a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1584a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light
158546774767fcf7780d1455e755729198648d08742eIgor Murashkin    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
158646774767fcf7780d1455e755729198648d08742eIgor Murashkin
15872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
15882bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1589e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_roots_descriptions_) ==
1590e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
15912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
15922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
15932bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        const char* image_root_description = image_roots_descriptions_[i];
15942dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
15958f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
15962bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        if (image_root_object->IsObjectArray()) {
15972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::ObjectArray<mirror::Object>* image_root_object_array
1598fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              = image_root_object->AsObjectArray<mirror::Object>();
15998f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          ScopedIndentation indent2(&vios_);
1600277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1601277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            mirror::Object* value = image_root_object_array->Get(j);
1602fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            size_t run = 0;
1603277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1604277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              if (value == image_root_object_array->Get(k)) {
1605fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                run++;
1606fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              } else {
1607fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers                break;
1608fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers              }
1609fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
1610fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            if (run == 0) {
16118f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d: ", j);
1612fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            } else {
16138f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << StringPrintf("%d to %zd: ", j, j + run);
1614277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe              j = j + run;
1615fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers            }
16162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            if (value != nullptr) {
16178f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              PrettyObjectValue(indent_os, value->GetClass(), value);
16182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            } else {
16198f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko              indent_os << j << ": null\n";
16202bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            }
1621d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
162234f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom        }
162334f426c49ac2de8cea70acef6b9ecdd8e62209d2Brian Carlstrom      }
16248f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    }
1625e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
16268f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    {
1627e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      os << "METHOD ROOTS\n";
1628e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_assert(arraysize(image_methods_descriptions_) ==
1629e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1630e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1631e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1632e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const char* description = image_methods_descriptions_[i];
1633e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        auto* image_method = image_header_.GetImageMethod(image_root);
16348f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        indent_os << StringPrintf("%s: %p\n", description, image_method);
1635e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
163627ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom    }
16372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
163827ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
1639f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    Runtime* const runtime = Runtime::Current();
1640f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    ClassLinker* class_linker = runtime->GetClassLinker();
16412ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string image_filename = image_space_.GetImageFilename();
16422ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
16432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OAT LOCATION: " << oat_location;
16442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
16458d31bbd3d6536de12bc20e3d29cfe03fe848f9daIan Rogers    std::string error_msg;
1646f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1647f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier        oat_location);
1648a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light    if (oat_file == nullptr) {
1649e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler      oat_file = OatFile::Open(oat_location, oat_location,
1650e5fed03772144595c0904faf3d6974cc55214c8cRichard Uhler                               nullptr, nullptr, false, nullptr,
1651b1d8c314b55bb2df2b2bb72a3daaf5db65b7ebc7Igor Murashkin                               &error_msg);
1652a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      if (oat_file == nullptr) {
1653a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light        os << "NOT FOUND: " << error_msg << "\n";
16542cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom        return false;
1655a59dd80f9f48cb750d329d4d4af2d99d72b484d1Alex Light      }
1656aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom    }
16572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
1658aded5f7ab991f3c1132851599d3bc60ff6707eedBrian Carlstrom
16593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.oat_file_bytes = oat_file->Size();
166078128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
166132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
16623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
166302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
16642ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      CHECK(oat_dex_file != nullptr);
166502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
166602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                                                         oat_dex_file->FileSize()));
166705f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers    }
166805f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
16692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "OBJECTS:\n" << std::flush;
1670b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier
1671dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    // Loop through the image space and dump its objects.
1672f9c6fc610b27887f832e453a0da1789187293408Mathieu Chartier    gc::Heap* heap = runtime->GetHeap();
167350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
1674357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    {
1675c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      {
1676c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1677c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier        heap->FlushAllocStack();
1678c22c59ef8513b4cbbfd25073d1afbf58196b522aMathieu Chartier      }
167990d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // Since FlushAllocStack() above resets the (active) allocation
168090d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // stack. Need to revoke the thread-local allocation stacks that
168190d706849a09590f150748ff97256ef718aca441Hiroshi Yamauchi      // point into it.
1682f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier      ScopedThreadSuspension sts(self, kNative);
16834f55e22630d99ca0edd9e951e5ee96b57bb9b980Mathieu Chartier      ScopedSuspendAll ssa(__FUNCTION__);
1684f1d666e1b48f8070ef1177fce156c08827f08eb8Mathieu Chartier      heap->RevokeAllThreadLocalAllocationStacks(self);
1685357e9be24c17a6bc2ae9fb53f25c73503116101dMathieu Chartier    }
16862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    {
1687e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      // Mark dex caches.
168805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      dex_caches_.clear();
1689e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      {
1690e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        ReaderMutexLock mu(self, *class_linker->DexLock());
169104302dbb106d590ff72c0dfecda23d85b6565059Hiroshi Yamauchi        for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
1692673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          mirror::DexCache* dex_cache =
169304302dbb106d590ff72c0dfecda23d85b6565059Hiroshi Yamauchi              down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
1694673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          if (dex_cache != nullptr) {
169505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            dex_caches_.insert(dex_cache);
1696673ed3d8aedc5462a47ded827c99f35d46525457Mathieu Chartier          }
1697e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        }
1698e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
16992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1700dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // Dump the normal objects before ArtMethods.
1701dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1702dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      indent_os << "\n";
1703dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // TODO: Dump fields.
1704dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      // Dump methods after.
1705dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      const auto& methods_section = image_header_.GetMethodsSection();
1706dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      const size_t pointer_size =
1707dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao          InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1708dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      DumpArtMethodVisitor visitor(this);
1709dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
17102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      // Dump the large objects separately.
1711bbd695c71e0bf518f582e84524e1cdeb3de3896cMathieu Chartier      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
17122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      indent_os << "\n";
1713b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
17142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "STATS:\n" << std::flush;
1715700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
17162ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() == nullptr) {
17172ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      LOG(WARNING) << "Failed to find image in " << image_filename;
17186f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
17192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (file.get() != nullptr) {
17202ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom      stats_.file_bytes = file->GetLength();
17216f277751ec5bfc0d7469b2cc7f7134e9032593f1Brian Carlstrom    }
17223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t header_bytes = sizeof(ImageHeader);
1723ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
1724e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1725e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto& method_section = image_header_.GetMethodsSection();
172605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    const auto& dex_cache_arrays_section = image_header_.GetImageSection(
172705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        ImageHeader::kSectionDexCacheArrays);
1728d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    const auto& intern_section = image_header_.GetImageSection(
1729d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier        ImageHeader::kSectionInternedStrings);
1730208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    const auto& class_table_section = image_header_.GetImageSection(
1731208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier        ImageHeader::kSectionClassTable);
1732ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1733ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
17343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    stats_.header_bytes = header_bytes;
1735ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1736ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Objects are kObjectAlignment-aligned.
1737ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1738ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    if (object_section.Offset() > header_bytes) {
1739ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe      stats_.alignment_bytes += object_section.Offset() - header_bytes;
1740ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    }
1741ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1742ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Field section is 4-byte aligned.
1743ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    constexpr size_t kFieldSectionAlignment = 4U;
1744ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_objects = object_section.Offset() + object_section.Size();
1745ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1746ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += field_section.Offset() - end_objects;
1747ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1748ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1749ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_fields = field_section.Offset() + field_section.Size();
1750ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(method_section.Offset(), 4);
1751ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += method_section.Offset() - end_fields;
1752ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1753ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1754ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_methods = method_section.Offset() + method_section.Size();
1755ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1756ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1757ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1758ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Intern table is 8-byte aligned.
1759ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1760ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1761ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += intern_section.Offset() - end_caches;
1762ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1763ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Add space between intern table and class table.
1764ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1765ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1766ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1767ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1768ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1769ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1770ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe    stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1771ace0dc1dd5480ad458e622085e51583653853fb9Andreas Gampe
1772e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.bitmap_bytes += bitmap_section.Size();
1773e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    stats_.art_field_bytes += field_section.Size();
1774cf36d493124d8048efa0bd6f67d817ce3cd6b725Vladimir Marko    stats_.art_method_bytes += method_section.Size();
177505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
1776d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    stats_.interned_strings_bytes += intern_section.Size();
1777208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    stats_.class_table_bytes += class_table_section.Size();
17788f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    stats_.Dump(os, indent_os);
17792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << "\n";
178027ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
17812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers    os << std::flush;
17823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
17832cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom    return oat_dumper_->Dump(os);
17843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
1785d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
17863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers private:
178754d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier  class DumpArtMethodVisitor : public ArtMethodVisitor {
178854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier   public:
178954d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
179054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
179154d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
179254d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      std::ostream& indent_os = image_dumper_->vios_.Stream();
179354d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
179454d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      image_dumper_->DumpMethod(method, image_dumper_, indent_os);
179554d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      indent_os << "\n";
179654d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    }
179754d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
179854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier   private:
179954d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    ImageDumper* const image_dumper_;
180054d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier  };
180154d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier
18022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
180390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
18042ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    CHECK(type != nullptr);
18052ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (value == nullptr) {
18062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1807d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (type->IsStringClass()) {
18082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::String* string = value->AsString();
18092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   String: %s\n", string,
181068b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
181164b6d145fa53b8dfb07a8fc2426af13f155d5a4dIan Rogers    } else if (type->IsClassClass()) {
18122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = value->AsClass();
18132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1814d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else {
18152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1816d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1817d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1818d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
1819c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
182090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
182161c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    os << StringPrintf("%s: ", field->GetName());
182208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers    switch (field->GetTypeAsPrimitiveType()) {
182308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimLong:
1824ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
182508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
182608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimDouble:
18272bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
182808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
182908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimFloat:
18302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
183108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
183208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimInt:
18332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
183408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
183508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimChar:
183637f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
183708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
183808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimShort:
183937f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
184008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
184108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimBoolean:
184237f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
184337f05ef45e0393de812d51261dc293240c17294dFred Shih            field->GetBoolean(obj));
184408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
184508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimByte:
184637f05ef45e0393de812d51261dc293240c17294dFred Shih        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
184708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
184808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      case Primitive::kPrimNot: {
184908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // Get the value, don't compute the type unless it is non-null as we don't want
185008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        // to cause class loading.
185108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        mirror::Object* value = field->GetObj(obj);
185208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        if (value == nullptr) {
185308f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
185450239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        } else {
185508f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          // Grab the field type without causing resolution.
1856daaf3265806eb2eadb2e03302bd68022fab5ca28Mathieu Chartier          mirror::Class* field_type = field->GetType<false>();
185708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          if (field_type != nullptr) {
185808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            PrettyObjectValue(os, field_type, value);
185908f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          } else {
186008f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers            os << StringPrintf("%p   %s\n", value,
186108f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
186208f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers          }
186350239c75041df9e12f9cbb018ccc08af6890e102Ian Rogers        }
186408f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
186548efc2b5c4b95ac507de5150cd315c6e1c96b381Ian Rogers      }
186608f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers      default:
186708f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
186808f1f50d6c2e8f247b8f5f19711d75a792851c7aIan Rogers        break;
1869d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1870d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1871d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
18722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
187390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
18742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* super = klass->GetSuperClass();
18752ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (super != nullptr) {
18762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      DumpFields(os, obj, super);
1877d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
187854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier    for (ArtField& field : klass->GetIFields()) {
187954d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      PrintField(os, &field, obj);
1880d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    }
1881d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers  }
1882d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers
18832dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  bool InDumpSpace(const mirror::Object* object) {
18843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return image_space_.Contains(object);
1885f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
18863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1887e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCodeBegin(ArtMethod* m)
188890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1889a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1890a7dd0386f35c0ba4aef3f5b16bc84c6f4e2fc702Mathieu Chartier        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
18916f3dbbadf4ce66982eb3d400e0a74cb73eb034f3Ian Rogers    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1892ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = oat_dumper_->GetQuickOatCode(m);
18933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
1894f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1895ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1896f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1897ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return quick_code;
18983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  }
18993a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
1900e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uint32_t GetQuickOatCodeSize(ArtMethod* m)
190190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1902ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1903ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    if (oat_code_begin == nullptr) {
1904f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom      return 0;
1905f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1906f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    return oat_code_begin[-1];
1907f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1908f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
1909e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  const void* GetQuickOatCodeEnd(ArtMethod* m)
191090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
1911ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
19122ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    if (oat_code_begin == nullptr) {
19132ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe      return nullptr;
1914f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom    }
1915ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    return oat_code_begin + GetQuickOatCodeSize(m);
1916f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom  }
1917f8bbb8448c733e9e3ad43aad69774c37888329b1Brian Carlstrom
191890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
19192ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(obj != nullptr);
19202ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe    DCHECK(arg != nullptr);
19213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
192278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    if (!state->InDumpSpace(obj)) {
192378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom      return;
192478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
1925916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
1926916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes = obj->SizeOf();
1927916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1928916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.object_bytes += object_bytes;
1929916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    state->stats_.alignment_bytes += alignment_bytes;
1930916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
19318f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    std::ostream& os = state->vios_.Stream();
19328f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
19332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* obj_class = obj->GetClass();
1934d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj_class->IsArrayClass()) {
19352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
19362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         obj->AsArray()->GetLength());
1937d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
19382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* klass = obj->AsClass();
19392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
19402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << klass->GetStatus() << ")\n";
1941d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj_class->IsStringClass()) {
19422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: java.lang.String %s\n", obj,
194368b56858367e29461ae290fd797443a1ef6d8005Ian Rogers                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
194478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    } else {
19452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
194678128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
19478f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    ScopedIndentation indent1(&state->vios_);
19488f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    DumpFields(os, obj, obj_class);
1949e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto image_pointer_size =
1950e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1951d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    if (obj->IsObjectArray()) {
1952e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      auto* obj_array = obj->AsObjectArray<mirror::Object>();
1953e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
19542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* value = obj_array->Get(i);
1955d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        size_t run = 0;
1956d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        for (int32_t j = i + 1; j < length; j++) {
1957d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          if (value == obj_array->Get(j)) {
1958d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            run++;
1959d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          } else {
1960d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers            break;
1961d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          }
1962d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1963d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        if (run == 0) {
19648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d: ", i);
196578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        } else {
19668f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko          os << StringPrintf("%d to %zd: ", i, i + run);
1967d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers          i = i + run;
196878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom        }
19692ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom        mirror::Class* value_class =
19702ed8deff799448e094fa7a7cb9cf3b718820f4c6Andreas Gampe            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
19718f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        PrettyObjectValue(os, value_class, value);
1972d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1973d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers    } else if (obj->IsClass()) {
1974c785344b87221f5e4e6473e5b762e4e61fe65dcfMathieu Chartier      mirror::Class* klass = obj->AsClass();
197554d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier      if (klass->NumStaticFields() != 0) {
19768f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        os << "STATICS:\n";
19778f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko        ScopedIndentation indent2(&state->vios_);
197854d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier        for (ArtField& field : klass->GetSFields()) {
197954d220eb9cc51215d75b9e0fe921b94bebbb3fd6Mathieu Chartier          PrintField(os, &field, field.GetDeclaringClass());
1980d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers        }
1981d5b32607798a46a905ba2d8d5bf7507cc970aa58Ian Rogers      }
1982e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
198305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      auto it = state->dex_caches_.find(obj);
198405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko      if (it != state->dex_caches_.end()) {
198505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        auto* dex_cache = down_cast<mirror::DexCache*>(obj);
1986e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& field_section = state->image_header_.GetImageSection(
1987e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            ImageHeader::kSectionArtFields);
1988e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        const auto& method_section = state->image_header_.GetMethodsSection();
198905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        size_t num_methods = dex_cache->NumResolvedMethods();
199005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        if (num_methods != 0u) {
199105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          os << "Methods (size=" << num_methods << "):";
199205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          ScopedIndentation indent2(&state->vios_);
199305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          auto* resolved_methods = dex_cache->GetResolvedMethods();
199405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
199505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
199605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            size_t run = 0;
199705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            for (size_t j = i + 1;
199805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
199905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           j,
200005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           image_pointer_size);
200105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                ++j, ++run) {}
200205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (run == 0) {
200305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd: ", i);
200405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
200505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd to %zd: ", i, i + run);
200605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              i = i + run;
200705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
200805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            std::string msg;
200905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (elem == nullptr) {
201005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "null";
201105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else if (method_section.Contains(
201205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
201305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
201405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
201505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "<not in method section>";
201605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
201705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            os << StringPrintf("%p   %s\n", elem, msg.c_str());
2018e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
201905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        }
202005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        size_t num_fields = dex_cache->NumResolvedFields();
202105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        if (num_fields != 0u) {
202205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          os << "Fields (size=" << num_fields << "):";
202305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          ScopedIndentation indent2(&state->vios_);
202405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          auto* resolved_fields = dex_cache->GetResolvedFields();
202505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
202605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
202705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            size_t run = 0;
202805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            for (size_t j = i + 1;
202905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
203005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           j,
203105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                                                           image_pointer_size);
203205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                ++j, ++run) {}
203305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (run == 0) {
203405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd: ", i);
203505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
203605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              os << StringPrintf("%zd to %zd: ", i, i + run);
203705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              i = i + run;
203805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
203905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            std::string msg;
204005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            if (elem == nullptr) {
204105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "null";
204205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else if (field_section.Contains(
204305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
204405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = PrettyField(reinterpret_cast<ArtField*>(elem));
204505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            } else {
204605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko              msg = "<not in field section>";
204705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            }
204805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko            os << StringPrintf("%p   %s\n", elem, msg.c_str());
2049e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          }
20503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
2051e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2052e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    }
2053e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::string temp;
2054e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
2055e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  }
20563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2057e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
205890443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
2059e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    DCHECK(method != nullptr);
2060e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    const auto image_pointer_size =
2061e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
20626bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray    const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
20636bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray    const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
2064524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray    OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2065524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray        reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2066e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    if (method->IsNative()) {
2067524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray      if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2068524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray        DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2069524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray        DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2070524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray      }
2071e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
2072e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
20736bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray      state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2074e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
2075e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2076e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
20776bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray      if (quick_oat_code_begin !=
20786bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray            method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
20796bc4374e3fa00e3ee5e832e1761c43e0b8a71558Nicolas Geoffray        indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
2080e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2081e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2082e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2083e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
2084e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    } else {
2085e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      const DexFile::CodeItem* code_item = method->GetCodeItem();
2086e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2087e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2088e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
2089e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      bool first_occurrence;
2090e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t gc_map_bytes = state->ComputeOatSize(
2091524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray          method_header->GetNativeGcMap(), &first_occurrence);
2092e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
2093e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.gc_map_bytes += gc_map_bytes;
2094e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2095e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
2096e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t pc_mapping_table_bytes = state->ComputeOatSize(
2097524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray          method_header->GetMappingTable(), &first_occurrence);
2098e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
2099e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2100e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2101e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
21026d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain      size_t vmap_table_bytes = 0u;
2103524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray      if (!method_header->IsOptimized()) {
21046d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        // Method compiled with the optimizing compiler have no vmap table.
21056d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        vmap_table_bytes = state->ComputeOatSize(
2106524e7ea8cd17bad17bd9f3e0ccbb19ad0d4d9c02Nicolas Geoffray            method_header->GetVmapTable(), &first_occurrence);
21076d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        if (first_occurrence) {
21086d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain          state->stats_.vmap_table_bytes += vmap_table_bytes;
21096d7f1796f3afafaaae4bf71088f92184adcff858Roland Levillain        }
2110e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2111e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
2112e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2113e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2114e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      if (first_occurrence) {
2115e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        state->stats_.managed_code_bytes += quick_oat_code_size;
2116e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        if (method->IsConstructor()) {
2117e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          if (method->IsStatic()) {
2118e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2119e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2120e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier            state->stats_.large_initializer_code_bytes += quick_oat_code_size;
21210d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          }
2122e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier        } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2123e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          state->stats_.large_method_code_bytes += quick_oat_code_size;
21243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
2125e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      }
2126e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
21273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21287617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      uint32_t method_access_flags = method->GetAccessFlags();
21297617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin
2130e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
21317617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin      indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
21327617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
21337617abdb402fd0419daa3eefb2ad059ccbb8b6dbIgor Murashkin                                method_access_flags);
21343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2135e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
213614632857428b7e37761e6e811c19021715a400f8Vladimir Marko          vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
21373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2138e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      double expansion =
2139e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2140e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      state->stats_.ComputeOutliers(total_size, expansion, method);
214178128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom    }
214278128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom  }
214327ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom
21443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  std::set<const void*> already_seen_;
21453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // Compute the size of the given data within the oat file and whether this is the first time
21463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  // this data has been requested
2147a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
21483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    if (already_seen_.count(oat_data) == 0) {
2149a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = true;
21503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      already_seen_.insert(oat_data);
21513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    } else {
2152a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      *first_occurrence = false;
21533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
21543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    return oat_dumper_->ComputeSize(oat_data);
215527ec961a1da540ba7f16c07a682585ab167317adBrian Carlstrom  }
2156916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2157916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom public:
2158916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  struct Stats {
21593a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t oat_file_bytes;
2160916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t file_bytes;
2161916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2162916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t header_bytes;
2163916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t object_bytes;
2164e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_field_bytes;
2165e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    size_t art_method_bytes;
216605792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko    size_t dex_cache_arrays_bytes;
2167d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier    size_t interned_strings_bytes;
2168208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier    size_t class_table_bytes;
216932327098e300ab66671b891a18bf669576fc896aMathieu Chartier    size_t bitmap_bytes;
2170916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t alignment_bytes;
2171916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2172916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_code_bytes;
21733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t managed_code_bytes_ignoring_deduplication;
2174916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t managed_to_native_code_bytes;
2175916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t native_to_managed_code_bytes;
21760d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t class_initializer_code_bytes;
21770d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_initializer_code_bytes;
21780d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    size_t large_method_code_bytes;
2179916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
21803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t gc_map_bytes;
2181916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t pc_mapping_table_bytes;
21823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    size_t vmap_table_bytes;
2183916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2184916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    size_t dex_instruction_bytes;
2185916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2186e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    std::vector<ArtMethod*> method_outlier;
21873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<size_t> method_outlier_size;
21883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    std::vector<double> method_outlier_expansion;
2189700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
21903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
21913887c468d731420e929e6ad3acf190d5431e94fcRoland Levillain    Stats()
21923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        : oat_file_bytes(0),
21933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          file_bytes(0),
2194916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          header_bytes(0),
2195916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          object_bytes(0),
2196e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_field_bytes(0),
2197e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier          art_method_bytes(0),
219805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko          dex_cache_arrays_bytes(0),
2199d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier          interned_strings_bytes(0),
2200208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier          class_table_bytes(0),
220132327098e300ab66671b891a18bf669576fc896aMathieu Chartier          bitmap_bytes(0),
2202916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          alignment_bytes(0),
2203916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_code_bytes(0),
22043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          managed_code_bytes_ignoring_deduplication(0),
2205916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          managed_to_native_code_bytes(0),
2206916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          native_to_managed_code_bytes(0),
22070d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          class_initializer_code_bytes(0),
22080d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_initializer_code_bytes(0),
22090d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers          large_method_code_bytes(0),
22103a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          gc_map_bytes(0),
2211916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          pc_mapping_table_bytes(0),
22123a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          vmap_table_bytes(0),
2213916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom          dex_instruction_bytes(0) {}
2214916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2215a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    struct SizeAndCount {
2216277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
2217a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t bytes;
2218a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      size_t count;
2219a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    };
2220a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2221a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    SizeAndCountTable sizes_and_counts;
2222a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes
2223277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe    void Update(const char* descriptor, size_t object_bytes_in) {
2224a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2225a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      if (it != sizes_and_counts.end()) {
2226277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        it->second.bytes += object_bytes_in;
2227a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes        it->second.count += 1;
2228a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      } else {
2229277ccbd200ea43590dfc06a93ae184a765327ad0Andreas Gampe        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
2230a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes      }
2231a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes    }
2232916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
22333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    double PercentOfOatBytes(size_t size) {
22343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
22353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
22363a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
2237916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfFileBytes(size_t size) {
2238916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2239916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2240916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2241916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    double PercentOfObjectBytes(size_t size) {
2242916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2243916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2244916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2245e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier    void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
22463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_size.push_back(total_size);
22473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier_expansion.push_back(expansion);
22483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      method_outlier.push_back(method);
22493a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
22503a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
225100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void DumpOutliers(std::ostream& os)
225290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier        SHARED_REQUIRES(Locks::mutator_lock_) {
22533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes = 0;
22543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_sizes_squared = 0;
22553a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion = 0;
22563a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t sum_of_expansion_squared = 0;
22573a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t n = method_outlier_size.size();
2258dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      if (n == 0) {
2259dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao        return;
2260dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      }
22613a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 0; i < n; i++) {
22623a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size = method_outlier_size[i];
22633a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes += cur_size;
22643a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_sizes_squared += cur_size * cur_size;
22653a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion = method_outlier_expansion[i];
22663a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion += cur_expansion;
22673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        sum_of_expansion_squared += cur_expansion * cur_expansion;
22683a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
22693a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_mean = sum_of_sizes / n;
22703a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
22713a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_mean = sum_of_expansion / n;
22723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      double expansion_variance =
22733a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
22743a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
22753a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose size is a certain number of standard deviations from the mean
22763a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t dumped_values = 0;
22773a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      size_t skipped_values = 0;
22783a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
22793a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        size_t cur_size_variance = i * i * size_variance;
22803a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
22813a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
22823a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          size_t cur_size = method_outlier_size[j];
22833a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_size > size_mean) {
22843a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_size - size_mean;
22853a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
22863a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_size_variance) {
22873a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
22883a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
22893a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
22903a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
22913a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
22923a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
22933a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
22943a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
22953a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
2296c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
22973a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
22983a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
22992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " requires storage of "
2300c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                    << PrettySize(cur_size) << "\n";
23013a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_size[j] = 0;  // don't consider this method again
23023a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
23033a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
23043a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
23053a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
23063a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
23073a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
23083a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
23092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2310c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with size > 1 standard deviation from the norm\n";
23113a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2312c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << std::flush;
23133a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
23143a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      // Dump methods whose expansion is a certain number of standard deviations from the mean
23153a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      dumped_values = 0;
23163a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      skipped_values = 0;
23173a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
23183a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        double cur_expansion_variance = i * i * expansion_variance;
23193a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        bool first = true;
23203a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        for (size_t j = 0; j < n; j++) {
23213a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          double cur_expansion = method_outlier_expansion[j];
23223a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          if (cur_expansion > expansion_mean) {
23233a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            size_t cur_var = cur_expansion - expansion_mean;
23243a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            cur_var = cur_var * cur_var;
23253a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            if (cur_var > cur_expansion_variance) {
23263a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              if (dumped_values > 20) {
23273a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (i == 1) {
23283a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  skipped_values++;
23293a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                } else {
23303a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  i = 2;  // jump to counting for 1 standard deviation
23313a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  break;
23323a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
23333a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              } else {
23343a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                if (first) {
23353a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  os << "\nLarge expansion methods (size > " << i
2336c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                      << " standard deviations the norm):\n";
23373a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                  first = false;
23383a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                }
23392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2340c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                   << cur_expansion << "\n";
23413a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                method_outlier_expansion[j] = 0.0;  // don't consider this method again
23423a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers                dumped_values++;
23433a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers              }
23443a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers            }
23453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers          }
23463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers        }
23473a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
23483a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      if (skipped_values > 0) {
23492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "... skipped " << skipped_values
2350c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes           << " methods with expansion > 1 standard deviation from the norm\n";
23513a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      }
2352c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
23533a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers    }
23543a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
23558f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko    void Dump(std::ostream& os, std::ostream& indent_os)
235690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier        SHARED_REQUIRES(Locks::mutator_lock_) {
23572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      {
23582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
23592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
236005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko        indent_os << StringPrintf("header_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
236105792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "object_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
236205792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "art_field_bytes        =  %8zd (%2.0f%% of art file bytes)\n"
236305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "art_method_bytes       =  %8zd (%2.0f%% of art file bytes)\n"
236405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "dex_cache_arrays_bytes =  %8zd (%2.0f%% of art file bytes)\n"
236505792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "interned_string_bytes  =  %8zd (%2.0f%% of art file bytes)\n"
2366208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                                  "class_table_bytes      =  %8zd (%2.0f%% of art file bytes)\n"
236705792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "bitmap_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
236805792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  "alignment_bytes        =  %8zd (%2.0f%% of art file bytes)\n\n",
23692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  header_bytes, PercentOfFileBytes(header_bytes),
23702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  object_bytes, PercentOfFileBytes(object_bytes),
2371e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_field_bytes, PercentOfFileBytes(art_field_bytes),
2372e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier                                  art_method_bytes, PercentOfFileBytes(art_method_bytes),
237305792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  dex_cache_arrays_bytes,
237405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko                                  PercentOfFileBytes(dex_cache_arrays_bytes),
2375d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  interned_strings_bytes,
2376d39645e22b8db1767cf64dc1200a9e4b2f939ed2Mathieu Chartier                                  PercentOfFileBytes(interned_strings_bytes),
2377208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                                  class_table_bytes, PercentOfFileBytes(class_table_bytes),
237832327098e300ab66671b891a18bf669576fc896aMathieu Chartier                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
23792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
23802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << std::flush;
2381208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier        CHECK_EQ(file_bytes,
2382208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2383208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2384208a5cb383dd9dcd3461f89b74af5df67dc8d794Mathieu Chartier                 bitmap_bytes + alignment_bytes);
23852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      }
2386916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
23872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "object_bytes breakdown:\n";
2388916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      size_t object_bytes_total = 0;
238902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const auto& sizes_and_count : sizes_and_counts) {
239002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        const std::string& descriptor(sizes_and_count.first);
239102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double average = static_cast<double>(sizes_and_count.second.bytes) /
239202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier            static_cast<double>(sizes_and_count.second.count);
239302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
23942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%32s %8zd bytes %6zd instances "
2395a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
239602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           descriptor.c_str(), sizes_and_count.second.bytes,
239702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           sizes_and_count.second.count, average, percent);
239802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        object_bytes_total += sizes_and_count.second.bytes;
2399916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      }
2400c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes      os << "\n" << std::flush;
2401916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom      CHECK_EQ(object_bytes, object_bytes_total);
2402916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
24032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("oat_file_bytes               = %8zd\n"
24042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
24052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
24062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
24072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
24082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
24092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
241005f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                         oat_file_bytes,
24112ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_code_bytes,
24122ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_code_bytes),
24132ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         managed_to_native_code_bytes,
24142ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(managed_to_native_code_bytes),
24152ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         native_to_managed_code_bytes,
24162ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(native_to_managed_code_bytes),
24172ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         class_initializer_code_bytes,
24182ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(class_initializer_code_bytes),
24192ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_initializer_code_bytes,
24202ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_initializer_code_bytes),
24212ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         large_method_code_bytes,
24222ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         PercentOfOatBytes(large_method_code_bytes))
24232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers            << "DexFile sizes:\n";
242402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
24252bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
242602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
242702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier                           PercentOfOatBytes(oat_dex_file_size.second));
242805f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers      }
242905f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers
24302bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
24312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
24322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
243305f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
243405f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
243505f28c6e00ecdb1da834acc8c29b4a7eba86d692Ian Rogers                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2436c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
2437916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
24382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
24392bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
24402ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                         static_cast<double>(managed_code_bytes) /
24412ec6520d57479d393bffa05defa1479b25ca8382Brian Carlstrom                             static_cast<double>(dex_instruction_bytes),
2442c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2443cf44e6f1ec194a906f4760a7c0395fdb0123d7aeElliott Hughes                             static_cast<double>(dex_instruction_bytes))
2444c073b075241640c94c27e661ed6f29ff5fcee5d8Elliott Hughes         << std::flush;
24453a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers
24463a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers      DumpOutliers(os);
2447916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom    }
2448916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom  } stats_;
2449916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom
2450916e74e45b60902af342a71bdbfb806ff29c6c2bBrian Carlstrom private:
24510d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  enum {
24520d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
24530d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
24540d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeConstructorDexBytes = 4000,
24550d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // Number of bytes for a method to be considered large. Based on the 4000 basic block
24560d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
24570d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers    kLargeMethodDexBytes = 16000
24580d2d3785961d8d542cba68a8e9126b01d3102f64Ian Rogers  };
24598f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
24608f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // For performance, use the *os_ directly for anything that doesn't need indentation
24618f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  // and prepare an indentation stream with default indentation 1.
24622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers  std::ostream* os_;
24638f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  VariableIndentationOutputStream vios_;
24648f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko  ScopedIndentation indent1_;
24658f1e08af6172781f91a17fce0a5a4183a9f70aa9Vladimir Marko
24661d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::space::ImageSpace& image_space_;
24673a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  const ImageHeader& image_header_;
24682cbaccb67e22c0b313a9785bfc65bcb4b25d0676Brian Carlstrom  std::unique_ptr<OatDumper> oat_dumper_;
2469df2bb1f36c0d755f0bdb2b1562e52669f3de4a09Andreas Gampe  OatDumperOptions* oat_dumper_options_;
247005792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko  std::set<mirror::Object*> dex_caches_;
2471d1bb4f6b7c8dda429f61937cd42f3a0b7367c271Elliott Hughes
24723a5c1ce3f11805a3382046f699c8fb1410a602b3Ian Rogers  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
247378128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom};
247478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
2475dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Haostatic int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
247600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Dumping the image, no explicit class loader.
24779865bde5d822f56c4732214c2005dfcaa41f94cfMathieu Chartier  ScopedNullHandle<mirror::ClassLoader> null_class_loader;
247800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
247900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
248000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
248100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  gc::Heap* heap = runtime->GetHeap();
2482dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2483dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  CHECK(!image_spaces.empty());
2484dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  for (gc::space::ImageSpace* image_space : image_spaces) {
2485dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    const ImageHeader& image_header = image_space->GetImageHeader();
2486dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    if (!image_header.IsValid()) {
2487dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2488dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      return EXIT_FAILURE;
2489dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    }
24903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2491dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    ImageDumper image_dumper(os, *image_space, image_header, options);
2492dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    if (!image_dumper.Dump()) {
2493dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao      return EXIT_FAILURE;
2494dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    }
2495dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
2496dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return EXIT_SUCCESS;
249778128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
249878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
249900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
250000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                              std::ostream* os) {
250100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
250200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
250300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Thread* self = Thread::Current();
250400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  CHECK(self != nullptr);
250500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need well-known-classes.
250600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  WellKnownClasses::Init(self->GetJniEnv());
250700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
250800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need to register dex files to get a working dex cache.
250900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ScopedObjectAccess soa(self);
251000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  ClassLinker* class_linker = runtime->GetClassLinker();
2511d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier  runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
2512ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier  std::vector<const DexFile*> class_path;
251300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
251400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    std::string error_msg;
2515ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
251600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    CHECK(dex_file != nullptr) << error_msg;
2517d57d454a11ac6f49eaa397ec14d6231e3a2727b7Mathieu Chartier    class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
2518ac8f4397f6978484fad6769526e9e3b3f7e7bd19Mathieu Chartier    class_path.push_back(dex_file);
251900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
252000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
252100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Need a class loader.
252200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Fake that we're a compiler.
2523966878d987cec1940fdfa8633fc79f8112320821Mathieu Chartier  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
252400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
252500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // Use the class loader while dumping.
252600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  StackHandleScope<1> scope(self);
252700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
252800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe      soa.Decode<mirror::ClassLoader*>(class_loader));
252900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &loader_handle;
253000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
253132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
253200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
253300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
253400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
253500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
253600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
253732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  CHECK(oat_file != nullptr && options != nullptr);
253800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  // No image = no class loader.
25399865bde5d822f56c4732214c2005dfcaa41f94cfMathieu Chartier  ScopedNullHandle<mirror::ClassLoader> null_class_loader;
254000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  options->class_loader_ = &null_class_loader;
254100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
254232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  OatDumper oat_dumper(*oat_file, *options);
254300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool success = oat_dumper.Dump(*os);
254400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
254500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
254600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
254700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampestatic int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
254800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe                   std::ostream* os) {
254900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
25503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2551b1d8c314b55bb2df2b2bb72a3daaf5db65b7ebc7Igor Murashkin                                    nullptr, &error_msg);
255200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
255300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
255400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
255500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
255600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
255700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (runtime != nullptr) {
255800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithRuntime(runtime, oat_file, options, os);
255900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  } else {
256000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return DumpOatWithoutRuntime(oat_file, options, os);
256100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
256200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
256300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25642fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbeckystatic int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
256500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string error_msg;
25663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2567b1d8c314b55bb2df2b2bb72a3daaf5db65b7ebc7Igor Murashkin                                    nullptr, &error_msg);
256800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  if (oat_file == nullptr) {
256900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
257000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
257100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
257200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25732d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  bool result;
25742d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
25752d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  // files for 64-bit code in the past.
25762d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
25772fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
25782d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    result = oat_symbolizer.Symbolize();
25792d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  } else {
25802fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
25812d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe    result = oat_symbolizer.Symbolize();
25822d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  }
25832d8614bf1ba44468c834d9d3d5562466637f63f2Andreas Gampe  if (!result) {
258400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    fprintf(stderr, "Failed to symbolize\n");
258500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    return EXIT_FAILURE;
258600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
258700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
258800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  return EXIT_SUCCESS;
258900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe}
259000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25913774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpArgs : public CmdlineArgs {
25923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin protected:
25933774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  using Base = CmdlineArgs;
259400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
25953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseCustom(const StringPiece& option,
25963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                                  std::string* error_msg) OVERRIDE {
25973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    {
25983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
25993774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      if (base_parse != kParseUnknownArgument) {
26003774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        return base_parse;
26013774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      }
260200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
260300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (option.starts_with("--oat-file=")) {
26053774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--oat-file=")).data();
26063774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--image=")) {
26073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      image_location_ = option.substr(strlen("--image=")).data();
26083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option =="--dump:raw_mapping_table") {
26093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_mapping_table_ = true;
26103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--dump:raw_gc_map") {
26113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_raw_gc_map_ = true;
26123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-dump:vmap") {
26133774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      dump_vmap_ = false;
2614f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain    } else if (option =="--dump:code_info_stack_maps") {
2615f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain      dump_code_info_stack_maps_ = true;
26163774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option == "--no-disassemble") {
26173774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      disassemble_code_ = false;
2618c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil    } else if (option =="--header-only") {
2619c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil      dump_header_only_ = true;
26203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--symbolize=")) {
26213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      oat_filename_ = option.substr(strlen("--symbolize=")).data();
26223774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      symbolize_ = true;
26232fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky    } else if (option.starts_with("--only-keep-debug")) {
26242fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      only_keep_debug_ = true;
262532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--class-filter=")) {
262632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      class_filter_ = option.substr(strlen("--class-filter=")).data();
26273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (option.starts_with("--method-filter=")) {
26283774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      method_filter_ = option.substr(strlen("--method-filter=")).data();
262932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-classes")) {
263032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_classes_ = true;
263132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--list-methods")) {
263232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      list_methods_ = true;
263332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--export-dex-to=")) {
263432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
263532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis    } else if (option.starts_with("--addr2instr=")) {
263632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
263732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        *error_msg = "Address conversion failed";
263832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        return kParseError;
263932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis      }
26403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else {
26413774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseUnknownArgument;
264200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
264300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26443774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
26453774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
26463774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26473774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
26483774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Infer boot image location from the image location if possible.
26493774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (boot_image_location_ == nullptr) {
26503774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      boot_image_location_ = image_location_;
265100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
265200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26533774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform the parent checks.
26543774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    ParseStatus parent_checks = Base::ParseChecks(error_msg);
26553774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (parent_checks != kParseOk) {
26563774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return parent_checks;
265700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
265800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
26593774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // Perform our own checks.
26603774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (image_location_ == nullptr && oat_filename_ == nullptr) {
26613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified";
26623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
26633774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
26643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      *error_msg = "Either --image or --oat-file must be specified but not both";
26653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return kParseError;
26663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    }
26673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return kParseOk;
26693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  }
26703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26713774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual std::string GetUsage() const {
26723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    std::string usage;
26733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26743774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=
26753774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "Usage: oatdump [options] ...\n"
26763774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
26773774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
26783774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
26793774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        // Either oat-file or image is required.
26803774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
26813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --oat-file=/system/framework/boot.oat\n"
26823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
26833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --image=<file.art>: specifies an input image location.\n"
26843774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --image=/system/framework/boot.art\n"
26853774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
26863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage += Base::GetUsage();
26883774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
26893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    usage +=  // Optional.
26903774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
26913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_mapping_table\n"
26923774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
269319510f02b011e545665f6219e6144c8e47aed5f0Mathieu Chartier        "  --dump:raw_gc_map enables dumping of the GC map.\n"
26943774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --dump:raw_gc_map\n"
26953774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
26963774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-dump:vmap may be used to disable vmap dumping.\n"
26973774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-dump:vmap\n"
26983774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2699f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2700f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "      Example: --dump:code_info_stack_maps\n"
2701f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        "\n"
27023774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --no-disassemble may be used to disable disassembly.\n"
27033774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --no-disassemble\n"
27043774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n"
2705c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "  --header-only may be used to print only the oat header.\n"
2706c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "      Example: --header-only\n"
2707c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        "\n"
270832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-classes may be used to list target file classes (can be used with filters).\n"
270932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes\n"
271032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-classes --class-filter=com.example.foo\n"
271132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
271232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --list-methods may be used to list target file methods (can be used with filters).\n"
271332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods\n"
271432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
271532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
271632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
271732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --symbolize=/system/framework/boot.oat\n"
271832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
27192fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "  --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
27202fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "      .rodata and .text sections are omitted in the output file to save space.\n"
27212fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "      Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
27222fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky        "\n"
272332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
272432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --class-filter=com.example.foo\n"
272532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
27263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
27273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "      Example: --method-filter=foo\n"
272832f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
272932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
273032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --export-dex-to=/data/local/tmp\n"
273132f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "\n"
273232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
273332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "                          address (e.g. PC from crash dump)\n"
273432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        "      Example: --addr2instr=0x00001a3b\n"
27353774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        "\n";
27363774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
27373774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return usage;
273800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
273900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
27403774335b08076117d6950cd472cdd59a167470b5Igor Murashkin public:
274100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* oat_filename_ = nullptr;
274232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* class_filter_ = "";
27433fcd22051f5ac12f0825204b534912af38f02c61Nicolas Geoffray  const char* method_filter_ = "";
274400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  const char* image_location_ = nullptr;
274500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  std::string elf_filename_prefix_;
274600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_mapping_table_ = false;
274700b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_raw_gc_map_ = false;
274800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool dump_vmap_ = true;
2749f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain  bool dump_code_info_stack_maps_ = false;
275000b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool disassemble_code_ = true;
275100b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  bool symbolize_ = false;
27522fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky  bool only_keep_debug_ = false;
275332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_classes_ = false;
275432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  bool list_methods_ = false;
2755c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil  bool dump_header_only_ = false;
275632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  uint32_t addr2instr_ = 0;
275732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis  const char* export_dex_location_ = nullptr;
275800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe};
275900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
27603774335b08076117d6950cd472cdd59a167470b5Igor Murashkinstruct OatdumpMain : public CmdlineMain<OatdumpArgs> {
27613774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool NeedsRuntime() OVERRIDE {
27623774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
276300b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
27643774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
27653774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    bool absolute_addresses = (args_->oat_filename_ == nullptr);
27663774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
27673774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
27683774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_mapping_table_,
27693774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_raw_gc_map_,
27703774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->dump_vmap_,
2771f2650d1f957b158496de8016bc43fb575e81d6bcRoland Levillain        args_->dump_code_info_stack_maps_,
27723774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        args_->disassemble_code_,
27733774335b08076117d6950cd472cdd59a167470b5Igor Murashkin        absolute_addresses,
277432f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->class_filter_,
277532f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->method_filter_,
277632f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_classes_,
277732f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->list_methods_,
2778c03d7b6bcac15ce8f829fee654b91a7e4583769dDavid Brazdil        args_->dump_header_only_,
277932f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->export_dex_location_,
278032f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis        args_->addr2instr_));
27813774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
27823774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
27833774335b08076117d6950cd472cdd59a167470b5Igor Murashkin          !args_->symbolize_;
278400b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
278500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
27863774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithoutRuntime() OVERRIDE {
27873774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
2788c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    CHECK(args_->oat_filename_ != nullptr);
27893774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2790d424d0856927a5d771644b0ef18cb48ef8c04817Mathieu Chartier    MemMap::Init();
27913774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
2792c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    if (args_->symbolize_) {
27932fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // ELF has special kind of section called SHT_NOBITS which allows us to create
27942fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // sections which exist but their data is omitted from the ELF file to save space.
27952fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // This is what "strip --only-keep-debug" does when it creates separate ELF file
27962fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      // with only debug data. We use it in similar way to exclude .rodata and .text.
27972fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      bool no_bits = args_->only_keep_debug_;
27982fdd03c982386e1d13aed9f1b51e1f0eee237971David Srbecky      return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
2799c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    } else {
2800c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe      return DumpOat(nullptr,
2801c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->oat_filename_,
280232f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
2803c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe                     args_->os_) == EXIT_SUCCESS;
2804c24f3990db5845691016b935df3d9382b6762f0fAndreas Gampe    }
280500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
280600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
28073774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  virtual bool ExecuteWithRuntime(Runtime* runtime) {
28083774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    CHECK(args_ != nullptr);
28093774335b08076117d6950cd472cdd59a167470b5Igor Murashkin
28103774335b08076117d6950cd472cdd59a167470b5Igor Murashkin    if (args_->oat_filename_ != nullptr) {
28113774335b08076117d6950cd472cdd59a167470b5Igor Murashkin      return DumpOat(runtime,
28123774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->oat_filename_,
281332f500daa2c04b1efe946c12c90502736e47d5fcAnestis Bechtsoudis                     oat_dumper_options_.get(),
28143774335b08076117d6950cd472cdd59a167470b5Igor Murashkin                     args_->os_) == EXIT_SUCCESS;
281500b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe    }
281600b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
2817dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
281800b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe  }
281900b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
28203774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
28213774335b08076117d6950cd472cdd59a167470b5Igor Murashkin};
282200b25f33737b281938347cca535beb77de6e8cd5Andreas Gampe
28237934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom}  // namespace art
282478128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom
282578128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstromint main(int argc, char** argv) {
28263774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  art::OatdumpMain main;
28273774335b08076117d6950cd472cdd59a167470b5Igor Murashkin  return main.Main(argc, argv);
282878128a63b2615744760b7f8ab83df9764a5d4a95Brian Carlstrom}
2829