oatdump.cc revision 9e49ab114a367203256f11545c42162a419ee880
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19
20#include <fstream>
21#include <iostream>
22#include <map>
23#include <set>
24#include <string>
25#include <unordered_map>
26#include <vector>
27
28#include "arch/instruction_set_features.h"
29#include "art_field-inl.h"
30#include "art_method-inl.h"
31#include "base/stl_util.h"
32#include "base/unix_file/fd_file.h"
33#include "class_linker.h"
34#include "class_linker-inl.h"
35#include "dex_file-inl.h"
36#include "dex_instruction.h"
37#include "disassembler.h"
38#include "elf_builder.h"
39#include "gc_map.h"
40#include "gc/space/image_space.h"
41#include "gc/space/large_object_space.h"
42#include "gc/space/space-inl.h"
43#include "image-inl.h"
44#include "indenter.h"
45#include "linker/buffered_output_stream.h"
46#include "linker/file_output_stream.h"
47#include "mapping_table.h"
48#include "mirror/array-inl.h"
49#include "mirror/class-inl.h"
50#include "mirror/dex_cache-inl.h"
51#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
53#include "oat.h"
54#include "oat_file-inl.h"
55#include "oat_file_manager.h"
56#include "os.h"
57#include "safe_map.h"
58#include "scoped_thread_state_change.h"
59#include "stack_map.h"
60#include "ScopedLocalRef.h"
61#include "thread_list.h"
62#include "verifier/dex_gc_map.h"
63#include "verifier/method_verifier.h"
64#include "vmap_table.h"
65#include "well_known_classes.h"
66
67#include <sys/stat.h>
68#include "cmdline.h"
69
70namespace art {
71
72const char* image_methods_descriptions_[] = {
73  "kResolutionMethod",
74  "kImtConflictMethod",
75  "kImtUnimplementedMethod",
76  "kCalleeSaveMethod",
77  "kRefsOnlySaveMethod",
78  "kRefsAndArgsSaveMethod",
79};
80
81const char* image_roots_descriptions_[] = {
82  "kDexCaches",
83  "kClassRoots",
84};
85
86// Map is so that we don't allocate multiple dex files for the same OatDexFile.
87static std::map<const OatFile::OatDexFile*,
88                std::unique_ptr<const DexFile>> opened_dex_files;
89
90const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
91  DCHECK(oat_dex_file != nullptr);
92  auto it = opened_dex_files.find(oat_dex_file);
93  if (it != opened_dex_files.end()) {
94    return it->second.get();
95  }
96  const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
97  opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
98  return ret;
99}
100
101class OatSymbolizer FINAL {
102 public:
103  OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
104      oat_file_(oat_file), builder_(nullptr),
105      output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
106  }
107
108  typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
109                                          uint32_t,
110                                          const OatFile::OatMethod&,
111                                          const DexFile&,
112                                          uint32_t,
113                                          const DexFile::CodeItem*,
114                                          uint32_t);
115
116  bool Symbolize() {
117    const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
118
119    File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
120    std::unique_ptr<BufferedOutputStream> output_stream(
121        MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
122    builder_.reset(new ElfBuilder<ElfTypes32>(isa, output_stream.get()));
123
124    builder_->Start();
125
126    auto* rodata = builder_->GetRoData();
127    auto* text = builder_->GetText();
128    auto* bss = builder_->GetBss();
129    auto* strtab = builder_->GetStrTab();
130    auto* symtab = builder_->GetSymTab();
131
132    rodata->Start();
133    const uint8_t* rodata_begin = oat_file_->Begin();
134    const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
135    rodata->WriteFully(rodata_begin, rodata_size);
136    rodata->End();
137
138    text->Start();
139    const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
140    const size_t text_size = oat_file_->End() - text_begin;
141    text->WriteFully(text_begin, text_size);
142    text->End();
143
144    if (oat_file_->BssSize() != 0) {
145      bss->WriteNoBitsSection(oat_file_->BssSize());
146    }
147
148    builder_->WriteDynamicSection(elf_file->GetPath());
149
150    Walk(&art::OatSymbolizer::RegisterForDedup);
151
152    NormalizeState();
153
154    strtab->Start();
155    strtab->Write("");  // strtab should start with empty string.
156    AddTrampolineSymbols();
157    Walk(&art::OatSymbolizer::AddSymbol);
158    strtab->End();
159
160    symtab->Start();
161    symtab->Write();
162    symtab->End();
163
164    builder_->End();
165
166    return builder_->Good();
167  }
168
169  void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
170    if (code_offset != 0) {
171      uint32_t name_offset = builder_->GetStrTab()->Write(name);
172      uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
173      // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
174      // the end of the section in case of the last symbol.
175      builder_->GetSymTab()->Add(name_offset, builder_->GetText(), symbol_value,
176          /* is_relative */ true, /* size */ 0, STB_GLOBAL, STT_FUNC);
177    }
178  }
179
180  void AddTrampolineSymbols() {
181    const OatHeader& oat_header = oat_file_->GetOatHeader();
182    AddTrampolineSymbol("interpreterToInterpreterBridge",
183                        oat_header.GetInterpreterToInterpreterBridgeOffset());
184    AddTrampolineSymbol("interpreterToCompiledCodeBridge",
185                        oat_header.GetInterpreterToCompiledCodeBridgeOffset());
186    AddTrampolineSymbol("jniDlsymLookup",
187                        oat_header.GetJniDlsymLookupOffset());
188    AddTrampolineSymbol("quickGenericJniTrampoline",
189                        oat_header.GetQuickGenericJniTrampolineOffset());
190    AddTrampolineSymbol("quickImtConflictTrampoline",
191                        oat_header.GetQuickImtConflictTrampolineOffset());
192    AddTrampolineSymbol("quickResolutionTrampoline",
193                        oat_header.GetQuickResolutionTrampolineOffset());
194    AddTrampolineSymbol("quickToInterpreterBridge",
195                        oat_header.GetQuickToInterpreterBridgeOffset());
196  }
197
198  void Walk(Callback callback) {
199    std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
200    for (size_t i = 0; i < oat_dex_files.size(); i++) {
201      const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
202      CHECK(oat_dex_file != nullptr);
203      WalkOatDexFile(oat_dex_file, callback);
204    }
205  }
206
207  void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
208    std::string error_msg;
209    const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
210    if (dex_file == nullptr) {
211      return;
212    }
213    for (size_t class_def_index = 0;
214        class_def_index < dex_file->NumClassDefs();
215        class_def_index++) {
216      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
217      const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
218      OatClassType type = oat_class.GetType();
219      switch (type) {
220        case kOatClassAllCompiled:
221        case kOatClassSomeCompiled:
222          WalkOatClass(oat_class, *dex_file, class_def, callback);
223          break;
224
225        case kOatClassNoneCompiled:
226        case kOatClassMax:
227          // Ignore.
228          break;
229      }
230    }
231  }
232
233  void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
234                    const DexFile::ClassDef& class_def, Callback callback) {
235    const uint8_t* class_data = dex_file.GetClassData(class_def);
236    if (class_data == nullptr) {  // empty class such as a marker interface?
237      return;
238    }
239    // Note: even if this is an interface or a native class, we still have to walk it, as there
240    //       might be a static initializer.
241    ClassDataItemIterator it(dex_file, class_data);
242    SkipAllFields(&it);
243    uint32_t class_method_idx = 0;
244    while (it.HasNextDirectMethod()) {
245      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
246      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
247                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
248      class_method_idx++;
249      it.Next();
250    }
251    while (it.HasNextVirtualMethod()) {
252      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
253      WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
254                    it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
255      class_method_idx++;
256      it.Next();
257    }
258    DCHECK(!it.HasNext());
259  }
260
261  void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
262                     const OatFile::OatMethod& oat_method, const DexFile& dex_file,
263                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
264                     uint32_t method_access_flags, Callback callback) {
265    if ((method_access_flags & kAccAbstract) != 0) {
266      // Abstract method, no code.
267      return;
268    }
269    if (oat_method.GetCodeOffset() == 0) {
270      // No code.
271      return;
272    }
273
274    (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
275                      method_access_flags);
276  }
277
278  void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
279                        uint32_t class_method_index ATTRIBUTE_UNUSED,
280                        const OatFile::OatMethod& oat_method,
281                        const DexFile& dex_file ATTRIBUTE_UNUSED,
282                        uint32_t dex_method_idx ATTRIBUTE_UNUSED,
283                        const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
284                        uint32_t method_access_flags ATTRIBUTE_UNUSED) {
285    state_[oat_method.GetCodeOffset()]++;
286  }
287
288  void NormalizeState() {
289    for (auto& x : state_) {
290      if (x.second == 1) {
291        state_[x.first] = 0;
292      }
293    }
294  }
295
296  enum class DedupState {  // private
297    kNotDeduplicated,
298    kDeduplicatedFirst,
299    kDeduplicatedOther
300  };
301  DedupState IsDuplicated(uint32_t offset) {
302    if (state_[offset] == 0) {
303      return DedupState::kNotDeduplicated;
304    }
305    if (state_[offset] == 1) {
306      return DedupState::kDeduplicatedOther;
307    }
308    state_[offset] = 1;
309    return DedupState::kDeduplicatedFirst;
310  }
311
312  void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
313                 uint32_t class_method_index ATTRIBUTE_UNUSED,
314                 const OatFile::OatMethod& oat_method,
315                 const DexFile& dex_file,
316                 uint32_t dex_method_idx,
317                 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
318                 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
319    DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
320    if (dedup != DedupState::kDeduplicatedOther) {
321      std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
322
323      if (dedup == DedupState::kDeduplicatedFirst) {
324        pretty_name = "[Dedup]" + pretty_name;
325      }
326
327      int name_offset = builder_->GetStrTab()->Write(pretty_name);
328      builder_->GetSymTab()->Add(name_offset, builder_->GetText(),
329          oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
330          true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
331    }
332  }
333
334 private:
335  static void SkipAllFields(ClassDataItemIterator* it) {
336    while (it->HasNextStaticField()) {
337      it->Next();
338    }
339    while (it->HasNextInstanceField()) {
340      it->Next();
341    }
342  }
343
344  const OatFile* oat_file_;
345  std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
346  std::unordered_map<uint32_t, uint32_t> state_;
347  const std::string output_name_;
348};
349
350class OatDumperOptions {
351 public:
352  OatDumperOptions(bool dump_raw_mapping_table,
353                   bool dump_raw_gc_map,
354                   bool dump_vmap,
355                   bool dump_code_info_stack_maps,
356                   bool disassemble_code,
357                   bool absolute_addresses,
358                   const char* class_filter,
359                   const char* method_filter,
360                   bool list_classes,
361                   bool list_methods,
362                   const char* export_dex_location,
363                   uint32_t addr2instr)
364    : dump_raw_mapping_table_(dump_raw_mapping_table),
365      dump_raw_gc_map_(dump_raw_gc_map),
366      dump_vmap_(dump_vmap),
367      dump_code_info_stack_maps_(dump_code_info_stack_maps),
368      disassemble_code_(disassemble_code),
369      absolute_addresses_(absolute_addresses),
370      class_filter_(class_filter),
371      method_filter_(method_filter),
372      list_classes_(list_classes),
373      list_methods_(list_methods),
374      export_dex_location_(export_dex_location),
375      addr2instr_(addr2instr),
376      class_loader_(nullptr) {}
377
378  const bool dump_raw_mapping_table_;
379  const bool dump_raw_gc_map_;
380  const bool dump_vmap_;
381  const bool dump_code_info_stack_maps_;
382  const bool disassemble_code_;
383  const bool absolute_addresses_;
384  const char* const class_filter_;
385  const char* const method_filter_;
386  const bool list_classes_;
387  const bool list_methods_;
388  const char* const export_dex_location_;
389  uint32_t addr2instr_;
390  Handle<mirror::ClassLoader>* class_loader_;
391};
392
393class OatDumper {
394 public:
395  OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
396    : oat_file_(oat_file),
397      oat_dex_files_(oat_file.GetOatDexFiles()),
398      options_(options),
399      resolved_addr2instr_(0),
400      instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
401      disassembler_(Disassembler::Create(instruction_set_,
402                                         new DisassemblerOptions(options_.absolute_addresses_,
403                                                                 oat_file.Begin(),
404                                                                 true /* can_read_literals_ */))) {
405    CHECK(options_.class_loader_ != nullptr);
406    CHECK(options_.class_filter_ != nullptr);
407    CHECK(options_.method_filter_ != nullptr);
408    AddAllOffsets();
409  }
410
411  ~OatDumper() {
412    delete disassembler_;
413  }
414
415  InstructionSet GetInstructionSet() {
416    return instruction_set_;
417  }
418
419  bool Dump(std::ostream& os) {
420    bool success = true;
421    const OatHeader& oat_header = oat_file_.GetOatHeader();
422
423    os << "MAGIC:\n";
424    os << oat_header.GetMagic() << "\n\n";
425
426    os << "LOCATION:\n";
427    os << oat_file_.GetLocation() << "\n\n";
428
429    os << "CHECKSUM:\n";
430    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
431
432    os << "INSTRUCTION SET:\n";
433    os << oat_header.GetInstructionSet() << "\n\n";
434
435    {
436      std::unique_ptr<const InstructionSetFeatures> features(
437          InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
438                                             oat_header.GetInstructionSetFeaturesBitmap()));
439      os << "INSTRUCTION SET FEATURES:\n";
440      os << features->GetFeatureString() << "\n\n";
441    }
442
443    os << "DEX FILE COUNT:\n";
444    os << oat_header.GetDexFileCount() << "\n\n";
445
446#define DUMP_OAT_HEADER_OFFSET(label, offset) \
447    os << label " OFFSET:\n"; \
448    os << StringPrintf("0x%08x", oat_header.offset()); \
449    if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
450      os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
451    } \
452    os << StringPrintf("\n\n");
453
454    DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
455    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
456                           GetInterpreterToInterpreterBridgeOffset);
457    DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
458                           GetInterpreterToCompiledCodeBridgeOffset);
459    DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
460                           GetJniDlsymLookupOffset);
461    DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
462                           GetQuickGenericJniTrampolineOffset);
463    DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
464                           GetQuickImtConflictTrampolineOffset);
465    DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
466                           GetQuickResolutionTrampolineOffset);
467    DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
468                           GetQuickToInterpreterBridgeOffset);
469#undef DUMP_OAT_HEADER_OFFSET
470
471    os << "IMAGE PATCH DELTA:\n";
472    os << StringPrintf("%d (0x%08x)\n\n",
473                       oat_header.GetImagePatchDelta(),
474                       oat_header.GetImagePatchDelta());
475
476    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
477    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
478
479    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
480    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
481
482    // Print the key-value store.
483    {
484      os << "KEY VALUE STORE:\n";
485      size_t index = 0;
486      const char* key;
487      const char* value;
488      while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
489        os << key << " = " << value << "\n";
490        index++;
491      }
492      os << "\n";
493    }
494
495    if (options_.absolute_addresses_) {
496      os << "BEGIN:\n";
497      os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
498
499      os << "END:\n";
500      os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
501    }
502
503    os << "SIZE:\n";
504    os << oat_file_.Size() << "\n\n";
505
506    os << std::flush;
507
508    // If set, adjust relative address to be searched
509    if (options_.addr2instr_ != 0) {
510      resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
511      os << "SEARCH ADDRESS (executable offset + input):\n";
512      os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
513    }
514
515    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
516      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
517      CHECK(oat_dex_file != nullptr);
518
519      // If file export selected skip file analysis
520      if (options_.export_dex_location_) {
521        if (!ExportDexFile(os, *oat_dex_file)) {
522          success = false;
523        }
524      } else {
525        if (!DumpOatDexFile(os, *oat_dex_file)) {
526          success = false;
527        }
528      }
529    }
530    os << std::flush;
531    return success;
532  }
533
534  size_t ComputeSize(const void* oat_data) {
535    if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
536        reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
537      return 0;  // Address not in oat file
538    }
539    uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
540                             reinterpret_cast<uintptr_t>(oat_file_.Begin());
541    auto it = offsets_.upper_bound(begin_offset);
542    CHECK(it != offsets_.end());
543    uintptr_t end_offset = *it;
544    return end_offset - begin_offset;
545  }
546
547  InstructionSet GetOatInstructionSet() {
548    return oat_file_.GetOatHeader().GetInstructionSet();
549  }
550
551  const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
552    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
553      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
554      CHECK(oat_dex_file != nullptr);
555      std::string error_msg;
556      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
557      if (dex_file == nullptr) {
558        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
559            << "': " << error_msg;
560      } else {
561        const char* descriptor = m->GetDeclaringClassDescriptor();
562        const DexFile::ClassDef* class_def =
563            dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
564        if (class_def != nullptr) {
565          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
566          const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
567          size_t method_index = m->GetMethodIndex();
568          return oat_class.GetOatMethod(method_index).GetQuickCode();
569        }
570      }
571    }
572    return nullptr;
573  }
574
575 private:
576  void AddAllOffsets() {
577    // We don't know the length of the code for each method, but we need to know where to stop
578    // when disassembling. What we do know is that a region of code will be followed by some other
579    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
580    // of a piece of code by using upper_bound to find the start of the next region.
581    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
582      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
583      CHECK(oat_dex_file != nullptr);
584      std::string error_msg;
585      const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
586      if (dex_file == nullptr) {
587        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
588            << "': " << error_msg;
589        continue;
590      }
591      offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
592      for (size_t class_def_index = 0;
593           class_def_index < dex_file->NumClassDefs();
594           class_def_index++) {
595        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
596        const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
597        const uint8_t* class_data = dex_file->GetClassData(class_def);
598        if (class_data != nullptr) {
599          ClassDataItemIterator it(*dex_file, class_data);
600          SkipAllFields(it);
601          uint32_t class_method_index = 0;
602          while (it.HasNextDirectMethod()) {
603            AddOffsets(oat_class.GetOatMethod(class_method_index++));
604            it.Next();
605          }
606          while (it.HasNextVirtualMethod()) {
607            AddOffsets(oat_class.GetOatMethod(class_method_index++));
608            it.Next();
609          }
610        }
611      }
612    }
613
614    // If the last thing in the file is code for a method, there won't be an offset for the "next"
615    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
616    // for the end of the file.
617    offsets_.insert(oat_file_.Size());
618  }
619
620  static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
621    return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
622  }
623
624  void AddOffsets(const OatFile::OatMethod& oat_method) {
625    uint32_t code_offset = oat_method.GetCodeOffset();
626    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
627      code_offset &= ~0x1;
628    }
629    offsets_.insert(code_offset);
630    offsets_.insert(oat_method.GetMappingTableOffset());
631    offsets_.insert(oat_method.GetVmapTableOffset());
632    offsets_.insert(oat_method.GetGcMapOffset());
633  }
634
635  bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
636    bool success = true;
637    bool stop_analysis = false;
638    os << "OatDexFile:\n";
639    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
640    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
641
642    // Create the verifier early.
643
644    std::string error_msg;
645    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
646    if (dex_file == nullptr) {
647      os << "NOT FOUND: " << error_msg << "\n\n";
648      os << std::flush;
649      return false;
650    }
651
652    VariableIndentationOutputStream vios(&os);
653    ScopedIndentation indent1(&vios);
654    for (size_t class_def_index = 0;
655         class_def_index < dex_file->NumClassDefs();
656         class_def_index++) {
657      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
658      const char* descriptor = dex_file->GetClassDescriptor(class_def);
659
660      // TODO: Support regex
661      if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
662        continue;
663      }
664
665      uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
666      const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
667      os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
668                         class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
669         << " (" << oat_class.GetStatus() << ")"
670         << " (" << oat_class.GetType() << ")\n";
671      // TODO: include bitmap here if type is kOatClassSomeCompiled?
672      if (options_.list_classes_) continue;
673      if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
674        success = false;
675      }
676      if (stop_analysis) {
677        os << std::flush;
678        return success;
679      }
680    }
681
682    os << std::flush;
683    return success;
684  }
685
686  bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
687    std::string error_msg;
688    std::string dex_file_location = oat_dex_file.GetDexFileLocation();
689
690    const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
691    if (dex_file == nullptr) {
692      os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
693      return false;
694    }
695    size_t fsize = oat_dex_file.FileSize();
696
697    // Some quick checks just in case
698    if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
699      os << "Invalid dex file\n";
700      return false;
701    }
702
703    // Verify output directory exists
704    if (!OS::DirectoryExists(options_.export_dex_location_)) {
705      // TODO: Extend OS::DirectoryExists if symlink support is required
706      os << options_.export_dex_location_ << " output directory not found or symlink\n";
707      return false;
708    }
709
710    // Beautify path names
711    if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
712      return false;
713    }
714
715    std::string dex_orig_name;
716    size_t dex_orig_pos = dex_file_location.rfind('/');
717    if (dex_orig_pos == std::string::npos)
718      dex_orig_name = dex_file_location;
719    else
720      dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
721
722    // A more elegant approach to efficiently name user installed apps is welcome
723    if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
724      dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
725      size_t apk_orig_pos = dex_file_location.rfind('/');
726      if (apk_orig_pos != std::string::npos) {
727        dex_orig_name = dex_file_location.substr(++apk_orig_pos);
728      }
729    }
730
731    std::string out_dex_path(options_.export_dex_location_);
732    if (out_dex_path.back() != '/') {
733      out_dex_path.append("/");
734    }
735    out_dex_path.append(dex_orig_name);
736    out_dex_path.append("_export.dex");
737    if (out_dex_path.length() > PATH_MAX) {
738      return false;
739    }
740
741    std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
742    if (file.get() == nullptr) {
743      os << "Failed to open output dex file " << out_dex_path;
744      return false;
745    }
746
747    if (!file->WriteFully(dex_file->Begin(), fsize)) {
748      os << "Failed to write dex file";
749      file->Erase();
750      return false;
751    }
752
753    if (file->FlushCloseOrErase() != 0) {
754      os << "Flush and close failed";
755      return false;
756    }
757
758    os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
759    os << std::flush;
760
761    return true;
762  }
763
764  static void SkipAllFields(ClassDataItemIterator& it) {
765    while (it.HasNextStaticField()) {
766      it.Next();
767    }
768    while (it.HasNextInstanceField()) {
769      it.Next();
770    }
771  }
772
773  bool DumpOatClass(VariableIndentationOutputStream* vios,
774                    const OatFile::OatClass& oat_class, const DexFile& dex_file,
775                    const DexFile::ClassDef& class_def, bool* stop_analysis) {
776    bool success = true;
777    bool addr_found = false;
778    const uint8_t* class_data = dex_file.GetClassData(class_def);
779    if (class_data == nullptr) {  // empty class such as a marker interface?
780      vios->Stream() << std::flush;
781      return success;
782    }
783    ClassDataItemIterator it(dex_file, class_data);
784    SkipAllFields(it);
785    uint32_t class_method_index = 0;
786    while (it.HasNextDirectMethod()) {
787      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
788                         it.GetMemberIndex(), it.GetMethodCodeItem(),
789                         it.GetRawMemberAccessFlags(), &addr_found)) {
790        success = false;
791      }
792      if (addr_found) {
793        *stop_analysis = true;
794        return success;
795      }
796      class_method_index++;
797      it.Next();
798    }
799    while (it.HasNextVirtualMethod()) {
800      if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
801                         it.GetMemberIndex(), it.GetMethodCodeItem(),
802                         it.GetRawMemberAccessFlags(), &addr_found)) {
803        success = false;
804      }
805      if (addr_found) {
806        *stop_analysis = true;
807        return success;
808      }
809      class_method_index++;
810      it.Next();
811    }
812    DCHECK(!it.HasNext());
813    vios->Stream() << std::flush;
814    return success;
815  }
816
817  static constexpr uint32_t kPrologueBytes = 16;
818
819  // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
820  static constexpr uint32_t kMaxCodeSize = 100 * 1000;
821
822  bool DumpOatMethod(VariableIndentationOutputStream* vios,
823                     const DexFile::ClassDef& class_def,
824                     uint32_t class_method_index,
825                     const OatFile::OatClass& oat_class, const DexFile& dex_file,
826                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
827                     uint32_t method_access_flags, bool* addr_found) {
828    bool success = true;
829
830    // TODO: Support regex
831    std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
832    if (method_name.find(options_.method_filter_) == std::string::npos) {
833      return success;
834    }
835
836    std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
837    vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
838                                   class_method_index, pretty_method.c_str(),
839                                   dex_method_idx);
840    if (options_.list_methods_) return success;
841
842    uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
843    const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
844    const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
845    uint32_t code_offset = oat_method.GetCodeOffset();
846    uint32_t code_size = oat_method.GetQuickCodeSize();
847    if (resolved_addr2instr_ != 0) {
848      if (resolved_addr2instr_ > code_offset + code_size) {
849        return success;
850      } else {
851        *addr_found = true;  // stop analyzing file at next iteration
852      }
853    }
854
855    // Everything below is indented at least once.
856    ScopedIndentation indent1(vios);
857
858    {
859      vios->Stream() << "DEX CODE:\n";
860      ScopedIndentation indent2(vios);
861      DumpDexCode(vios->Stream(), dex_file, code_item);
862    }
863
864    std::unique_ptr<StackHandleScope<1>> hs;
865    std::unique_ptr<verifier::MethodVerifier> verifier;
866    if (Runtime::Current() != nullptr) {
867      // We need to have the handle scope stay live until after the verifier since the verifier has
868      // a handle to the dex cache from hs.
869      hs.reset(new StackHandleScope<1>(Thread::Current()));
870      vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
871      ScopedIndentation indent2(vios);
872      verifier.reset(DumpVerifier(vios, hs.get(),
873                                  dex_method_idx, &dex_file, class_def, code_item,
874                                  method_access_flags));
875    }
876    {
877      vios->Stream() << "OatMethodOffsets ";
878      if (options_.absolute_addresses_) {
879        vios->Stream() << StringPrintf("%p ", oat_method_offsets);
880      }
881      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
882      if (oat_method_offsets_offset > oat_file_.Size()) {
883        vios->Stream() << StringPrintf(
884            "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
885            oat_method_offsets_offset, oat_file_.Size());
886        // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
887        vios->Stream() << std::flush;
888        return false;
889      }
890
891      ScopedIndentation indent2(vios);
892      vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
893      uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
894      if (aligned_code_begin > oat_file_.Size()) {
895        vios->Stream() << StringPrintf("WARNING: "
896                                       "code offset 0x%08x is past end of file 0x%08zx.\n",
897                                       aligned_code_begin, oat_file_.Size());
898        success = false;
899      }
900      vios->Stream() << "\n";
901
902      vios->Stream() << "gc_map: ";
903      if (options_.absolute_addresses_) {
904        vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
905      }
906      uint32_t gc_map_offset = oat_method.GetGcMapOffset();
907      vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
908      if (gc_map_offset > oat_file_.Size()) {
909        vios->Stream() << StringPrintf("WARNING: "
910                           "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
911                           gc_map_offset, oat_file_.Size());
912        success = false;
913      } else if (options_.dump_raw_gc_map_) {
914        ScopedIndentation indent3(vios);
915        DumpGcMap(vios->Stream(), oat_method, code_item);
916      }
917    }
918    {
919      vios->Stream() << "OatQuickMethodHeader ";
920      uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
921      const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
922
923      if (options_.absolute_addresses_) {
924        vios->Stream() << StringPrintf("%p ", method_header);
925      }
926      vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
927      if (method_header_offset > oat_file_.Size()) {
928        vios->Stream() << StringPrintf(
929            "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
930            method_header_offset, oat_file_.Size());
931        // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
932        vios->Stream() << std::flush;
933        return false;
934      }
935
936      ScopedIndentation indent2(vios);
937      vios->Stream() << "mapping_table: ";
938      if (options_.absolute_addresses_) {
939        vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
940      }
941      uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
942      vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
943      if (mapping_table_offset > oat_file_.Size()) {
944        vios->Stream() << StringPrintf("WARNING: "
945                                       "mapping table offset 0x%08x is past end of file 0x%08zx. "
946                                       "mapping table offset was loaded from offset 0x%08x.\n",
947                                       mapping_table_offset, oat_file_.Size(),
948                                       oat_method.GetMappingTableOffsetOffset());
949        success = false;
950      } else if (options_.dump_raw_mapping_table_) {
951        ScopedIndentation indent3(vios);
952        DumpMappingTable(vios, oat_method);
953      }
954
955      vios->Stream() << "vmap_table: ";
956      if (options_.absolute_addresses_) {
957        vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
958      }
959      uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
960      vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
961      if (vmap_table_offset > oat_file_.Size()) {
962        vios->Stream() << StringPrintf("WARNING: "
963                                       "vmap table offset 0x%08x is past end of file 0x%08zx. "
964                                       "vmap table offset was loaded from offset 0x%08x.\n",
965                                       vmap_table_offset, oat_file_.Size(),
966                                       oat_method.GetVmapTableOffsetOffset());
967        success = false;
968      } else if (options_.dump_vmap_) {
969        DumpVmapData(vios, oat_method, code_item);
970      }
971    }
972    {
973      vios->Stream() << "QuickMethodFrameInfo\n";
974
975      ScopedIndentation indent2(vios);
976      vios->Stream()
977          << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
978      vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
979      DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
980      vios->Stream() << "\n";
981      vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
982      DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
983      vios->Stream() << "\n";
984    }
985    {
986      // Based on spill masks from QuickMethodFrameInfo so placed
987      // after it is dumped, but useful for understanding quick
988      // code, so dumped here.
989      ScopedIndentation indent2(vios);
990      DumpVregLocations(vios->Stream(), oat_method, code_item);
991    }
992    {
993      vios->Stream() << "CODE: ";
994      uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
995      if (code_size_offset > oat_file_.Size()) {
996        ScopedIndentation indent2(vios);
997        vios->Stream() << StringPrintf("WARNING: "
998                                       "code size offset 0x%08x is past end of file 0x%08zx.",
999                                       code_size_offset, oat_file_.Size());
1000        success = false;
1001      } else {
1002        const void* code = oat_method.GetQuickCode();
1003        uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1004        uint64_t aligned_code_end = aligned_code_begin + code_size;
1005
1006        if (options_.absolute_addresses_) {
1007          vios->Stream() << StringPrintf("%p ", code);
1008        }
1009        vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1010                                       code_offset,
1011                                       code_size_offset,
1012                                       code_size,
1013                                       code != nullptr ? "..." : "");
1014
1015        ScopedIndentation indent2(vios);
1016        if (aligned_code_begin > oat_file_.Size()) {
1017          vios->Stream() << StringPrintf("WARNING: "
1018                                         "start of code at 0x%08x is past end of file 0x%08zx.",
1019                                         aligned_code_begin, oat_file_.Size());
1020          success = false;
1021        } else if (aligned_code_end > oat_file_.Size()) {
1022          vios->Stream() << StringPrintf(
1023              "WARNING: "
1024              "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1025              "code size is 0x%08x loaded from offset 0x%08x.\n",
1026              aligned_code_end, oat_file_.Size(),
1027              code_size, code_size_offset);
1028          success = false;
1029          if (options_.disassemble_code_) {
1030            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1031              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
1032            }
1033          }
1034        } else if (code_size > kMaxCodeSize) {
1035          vios->Stream() << StringPrintf(
1036              "WARNING: "
1037              "code size %d is bigger than max expected threshold of %d. "
1038              "code size is 0x%08x loaded from offset 0x%08x.\n",
1039              code_size, kMaxCodeSize,
1040              code_size, code_size_offset);
1041          success = false;
1042          if (options_.disassemble_code_) {
1043            if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1044              DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
1045            }
1046          }
1047        } else if (options_.disassemble_code_) {
1048          DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
1049        }
1050      }
1051    }
1052    vios->Stream() << std::flush;
1053    return success;
1054  }
1055
1056  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1057    if (spill_mask == 0) {
1058      return;
1059    }
1060    os << "(";
1061    for (size_t i = 0; i < 32; i++) {
1062      if ((spill_mask & (1 << i)) != 0) {
1063        if (is_float) {
1064          os << "fr" << i;
1065        } else {
1066          os << "r" << i;
1067        }
1068        spill_mask ^= 1 << i;  // clear bit
1069        if (spill_mask != 0) {
1070          os << ", ";
1071        } else {
1072          break;
1073        }
1074      }
1075    }
1076    os << ")";
1077  }
1078
1079  // Display data stored at the the vmap offset of an oat method.
1080  void DumpVmapData(VariableIndentationOutputStream* vios,
1081                    const OatFile::OatMethod& oat_method,
1082                    const DexFile::CodeItem* code_item) {
1083    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1084      // The optimizing compiler outputs its CodeInfo data in the vmap table.
1085      const void* raw_code_info = oat_method.GetVmapTable();
1086      if (raw_code_info != nullptr) {
1087        CodeInfo code_info(raw_code_info);
1088        DCHECK(code_item != nullptr);
1089        ScopedIndentation indent1(vios);
1090        DumpCodeInfo(vios, code_info, oat_method, *code_item);
1091      }
1092    } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1093      // We don't encode the size in the table, so just emit that we have quickened
1094      // information.
1095      ScopedIndentation indent(vios);
1096      vios->Stream() << "quickened data\n";
1097    } else {
1098      // Otherwise, display the vmap table.
1099      const uint8_t* raw_table = oat_method.GetVmapTable();
1100      if (raw_table != nullptr) {
1101        VmapTable vmap_table(raw_table);
1102        DumpVmapTable(vios->Stream(), oat_method, vmap_table);
1103      }
1104    }
1105  }
1106
1107  // Display a CodeInfo object emitted by the optimizing compiler.
1108  void DumpCodeInfo(VariableIndentationOutputStream* vios,
1109                    const CodeInfo& code_info,
1110                    const OatFile::OatMethod& oat_method,
1111                    const DexFile::CodeItem& code_item) {
1112    code_info.Dump(vios,
1113                   oat_method.GetCodeOffset(),
1114                   code_item.registers_size_,
1115                   options_.dump_code_info_stack_maps_);
1116  }
1117
1118  // Display a vmap table.
1119  void DumpVmapTable(std::ostream& os,
1120                     const OatFile::OatMethod& oat_method,
1121                     const VmapTable& vmap_table) {
1122    bool first = true;
1123    bool processing_fp = false;
1124    uint32_t spill_mask = oat_method.GetCoreSpillMask();
1125    for (size_t i = 0; i < vmap_table.Size(); i++) {
1126      uint16_t dex_reg = vmap_table[i];
1127      uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1128                                                    processing_fp ? kFloatVReg : kIntVReg);
1129      os << (first ? "v" : ", v")  << dex_reg;
1130      if (!processing_fp) {
1131        os << "/r" << cpu_reg;
1132      } else {
1133        os << "/fr" << cpu_reg;
1134      }
1135      first = false;
1136      if (!processing_fp && dex_reg == 0xFFFF) {
1137        processing_fp = true;
1138        spill_mask = oat_method.GetFpSpillMask();
1139      }
1140    }
1141    os << "\n";
1142  }
1143
1144  void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1145                         const DexFile::CodeItem* code_item) {
1146    if (code_item != nullptr) {
1147      size_t num_locals_ins = code_item->registers_size_;
1148      size_t num_ins = code_item->ins_size_;
1149      size_t num_locals = num_locals_ins - num_ins;
1150      size_t num_outs = code_item->outs_size_;
1151
1152      os << "vr_stack_locations:";
1153      for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1154        // For readability, delimit the different kinds of VRs.
1155        if (reg == num_locals_ins) {
1156          os << "\n\tmethod*:";
1157        } else if (reg == num_locals && num_ins > 0) {
1158          os << "\n\tins:";
1159        } else if (reg == 0 && num_locals > 0) {
1160          os << "\n\tlocals:";
1161        }
1162
1163        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1164            code_item,
1165            oat_method.GetCoreSpillMask(),
1166            oat_method.GetFpSpillMask(),
1167            oat_method.GetFrameSizeInBytes(),
1168            reg,
1169            GetInstructionSet());
1170        os << " v" << reg << "[sp + #" << offset << "]";
1171      }
1172
1173      for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1174        if (out_reg == 0) {
1175          os << "\n\touts:";
1176        }
1177
1178        uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1179        os << " v" << out_reg << "[sp + #" << offset << "]";
1180      }
1181
1182      os << "\n";
1183    }
1184  }
1185
1186  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
1187                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
1188    const uint8_t* raw_table = oat_method.GetVmapTable();
1189    if (raw_table != nullptr) {
1190      const VmapTable vmap_table(raw_table);
1191      uint32_t vmap_offset;
1192      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
1193        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1194        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1195                                       : oat_method.GetCoreSpillMask();
1196        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
1197      } else {
1198        uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1199            code_item,
1200            oat_method.GetCoreSpillMask(),
1201            oat_method.GetFpSpillMask(),
1202            oat_method.GetFrameSizeInBytes(),
1203            reg,
1204            GetInstructionSet());
1205        os << "[sp + #" << offset << "]";
1206      }
1207    }
1208  }
1209
1210  void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1211                          const DexFile::CodeItem* code_item,
1212                          size_t num_regs, const uint8_t* reg_bitmap) {
1213    bool first = true;
1214    for (size_t reg = 0; reg < num_regs; reg++) {
1215      if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1216        if (first) {
1217          os << "  v" << reg << " (";
1218          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1219          os << ")";
1220          first = false;
1221        } else {
1222          os << ", v" << reg << " (";
1223          DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1224          os << ")";
1225        }
1226      }
1227    }
1228    if (first) {
1229      os << "No registers in GC map\n";
1230    } else {
1231      os << "\n";
1232    }
1233  }
1234  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1235                 const DexFile::CodeItem* code_item) {
1236    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1237    if (gc_map_raw == nullptr) {
1238      return;  // No GC map.
1239    }
1240    const void* quick_code = oat_method.GetQuickCode();
1241    NativePcOffsetToReferenceMap map(gc_map_raw);
1242    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1243      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1244          map.GetNativePcOffset(entry);
1245      os << StringPrintf("%p", native_pc);
1246      DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
1247    }
1248  }
1249
1250  void DumpMappingTable(VariableIndentationOutputStream* vios,
1251                        const OatFile::OatMethod& oat_method) {
1252    const void* quick_code = oat_method.GetQuickCode();
1253    if (quick_code == nullptr) {
1254      return;
1255    }
1256    MappingTable table(oat_method.GetMappingTable());
1257    if (table.TotalSize() != 0) {
1258      if (table.PcToDexSize() != 0) {
1259        typedef MappingTable::PcToDexIterator It;
1260        vios->Stream() << "suspend point mappings {\n";
1261        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1262          ScopedIndentation indent1(vios);
1263          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1264        }
1265        vios->Stream() << "}\n";
1266      }
1267      if (table.DexToPcSize() != 0) {
1268        typedef MappingTable::DexToPcIterator It;
1269        vios->Stream() << "catch entry mappings {\n";
1270        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1271          ScopedIndentation indent1(vios);
1272          vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1273        }
1274        vios->Stream() << "}\n";
1275      }
1276    }
1277  }
1278
1279  uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
1280                                   const OatFile::OatMethod& oat_method,
1281                                   const DexFile::CodeItem* code_item,
1282                                   size_t offset,
1283                                   bool suspend_point_mapping) {
1284    if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1285      if (suspend_point_mapping) {
1286        ScopedIndentation indent1(vios);
1287        DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
1288      }
1289      // The return value is not used in the case of a method compiled
1290      // with the optimizing compiler.
1291      return DexFile::kDexNoIndex;
1292    } else {
1293      return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
1294    }
1295  }
1296
1297  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1298                               size_t offset, bool suspend_point_mapping) {
1299    MappingTable table(oat_method.GetMappingTable());
1300    if (suspend_point_mapping && table.PcToDexSize() > 0) {
1301      typedef MappingTable::PcToDexIterator It;
1302      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1303        if (offset == cur.NativePcOffset()) {
1304          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
1305          return cur.DexPc();
1306        }
1307      }
1308    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1309      typedef MappingTable::DexToPcIterator It;
1310      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1311        if (offset == cur.NativePcOffset()) {
1312          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
1313          return cur.DexPc();
1314        }
1315      }
1316    }
1317    return DexFile::kDexNoIndex;
1318  }
1319
1320  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1321                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
1322    const uint8_t* gc_map_raw = oat_method.GetGcMap();
1323    if (gc_map_raw != nullptr) {
1324      NativePcOffsetToReferenceMap map(gc_map_raw);
1325      if (map.HasEntry(native_pc_offset)) {
1326        size_t num_regs = map.RegWidth() * 8;
1327        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
1328        bool first = true;
1329        for (size_t reg = 0; reg < num_regs; reg++) {
1330          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1331            if (first) {
1332              os << "GC map objects:  v" << reg << " (";
1333              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1334              os << ")";
1335              first = false;
1336            } else {
1337              os << ", v" << reg << " (";
1338              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1339              os << ")";
1340            }
1341          }
1342        }
1343        if (!first) {
1344          os << "\n";
1345        }
1346      }
1347    }
1348  }
1349
1350  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1351                        const OatFile::OatMethod& oat_method,
1352                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1353    DCHECK(verifier != nullptr);
1354    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
1355    bool first = true;
1356    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1357      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1358      if (kind != kUndefined) {
1359        if (first) {
1360          os << "VRegs:  v";
1361          first = false;
1362        } else {
1363          os << ", v";
1364        }
1365        os << reg << " (";
1366        switch (kind) {
1367          case kImpreciseConstant:
1368            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1369            DescribeVReg(os, oat_method, code_item, reg, kind);
1370            break;
1371          case kConstant:
1372            os << "Constant: " << kinds.at((reg * 2) + 1);
1373            break;
1374          default:
1375            DescribeVReg(os, oat_method, code_item, reg, kind);
1376            break;
1377        }
1378        os << ")";
1379      }
1380    }
1381    if (!first) {
1382      os << "\n";
1383    }
1384  }
1385
1386
1387  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
1388    if (code_item != nullptr) {
1389      size_t i = 0;
1390      while (i < code_item->insns_size_in_code_units_) {
1391        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
1392        os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1393           << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
1394        i += instruction->SizeInCodeUnits();
1395      }
1396    }
1397  }
1398
1399  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1400  // the optimizing compiler?
1401  static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1402                                                    const DexFile::CodeItem* code_item) {
1403    // If the native GC map is null and the Dex `code_item` is not
1404    // null, then this method has been compiled with the optimizing
1405    // compiler.
1406    return oat_method.GetQuickCode() != nullptr &&
1407           oat_method.GetGcMap() == nullptr &&
1408           code_item != nullptr;
1409  }
1410
1411  // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1412  // the dextodex compiler?
1413  static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1414                                                  const DexFile::CodeItem* code_item) {
1415    // If the quick code is null, the Dex `code_item` is not
1416    // null, and the vmap table is not null, then this method has been compiled
1417    // with the dextodex compiler.
1418    return oat_method.GetQuickCode() == nullptr &&
1419           oat_method.GetVmapTable() != nullptr &&
1420           code_item != nullptr;
1421  }
1422
1423  void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
1424                                  const OatFile::OatMethod& oat_method,
1425                                  const DexFile::CodeItem* code_item,
1426                                  size_t offset) {
1427    // This method is only relevant for oat methods compiled with the
1428    // optimizing compiler.
1429    DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1430
1431    // The optimizing compiler outputs its CodeInfo data in the vmap table.
1432    const void* raw_code_info = oat_method.GetVmapTable();
1433    if (raw_code_info != nullptr) {
1434      CodeInfo code_info(raw_code_info);
1435      StackMapEncoding encoding = code_info.ExtractEncoding();
1436      StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
1437      if (stack_map.IsValid()) {
1438        stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1439                       code_item->registers_size_);
1440      }
1441    }
1442  }
1443
1444  verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
1445                                         StackHandleScope<1>* hs,
1446                                         uint32_t dex_method_idx,
1447                                         const DexFile* dex_file,
1448                                         const DexFile::ClassDef& class_def,
1449                                         const DexFile::CodeItem* code_item,
1450                                         uint32_t method_access_flags) {
1451    if ((method_access_flags & kAccNative) == 0) {
1452      ScopedObjectAccess soa(Thread::Current());
1453      Runtime* const runtime = Runtime::Current();
1454      Handle<mirror::DexCache> dex_cache(
1455          hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1456                                                                   runtime->GetLinearAlloc())));
1457      DCHECK(options_.class_loader_ != nullptr);
1458      return verifier::MethodVerifier::VerifyMethodAndDump(
1459          soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1460          &class_def, code_item, nullptr, method_access_flags);
1461    }
1462
1463    return nullptr;
1464  }
1465
1466  void DumpCode(VariableIndentationOutputStream* vios,
1467                verifier::MethodVerifier* verifier,
1468                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1469                bool bad_input, size_t code_size) {
1470    const void* quick_code = oat_method.GetQuickCode();
1471
1472    if (code_size == 0) {
1473      code_size = oat_method.GetQuickCodeSize();
1474    }
1475    if (code_size == 0 || quick_code == nullptr) {
1476      vios->Stream() << "NO CODE!\n";
1477      return;
1478    } else {
1479      const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1480      size_t offset = 0;
1481      while (offset < code_size) {
1482        if (!bad_input) {
1483          DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
1484        }
1485        offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1486        if (!bad_input) {
1487          uint32_t dex_pc =
1488              DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
1489          if (dex_pc != DexFile::kDexNoIndex) {
1490            DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
1491            if (verifier != nullptr) {
1492              DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
1493            }
1494          }
1495        }
1496      }
1497    }
1498  }
1499
1500  const OatFile& oat_file_;
1501  const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
1502  const OatDumperOptions& options_;
1503  uint32_t resolved_addr2instr_;
1504  InstructionSet instruction_set_;
1505  std::set<uintptr_t> offsets_;
1506  Disassembler* disassembler_;
1507};
1508
1509class ImageDumper {
1510 public:
1511  ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1512              const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1513      : os_(os),
1514        vios_(os),
1515        indent1_(&vios_),
1516        image_space_(image_space),
1517        image_header_(image_header),
1518        oat_dumper_options_(oat_dumper_options) {}
1519
1520  bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
1521    std::ostream& os = *os_;
1522    std::ostream& indent_os = vios_.Stream();
1523
1524    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
1525
1526    os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1527
1528    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
1529
1530    os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1531
1532    for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1533      auto section = static_cast<ImageHeader::ImageSections>(i);
1534      os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1535    }
1536
1537    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
1538
1539    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
1540
1541    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1542
1543    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1544
1545    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1546
1547    os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1548
1549    os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1550
1551    {
1552      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1553      static_assert(arraysize(image_roots_descriptions_) ==
1554          static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
1555      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1556        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1557        const char* image_root_description = image_roots_descriptions_[i];
1558        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
1559        indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1560        if (image_root_object->IsObjectArray()) {
1561          mirror::ObjectArray<mirror::Object>* image_root_object_array
1562              = image_root_object->AsObjectArray<mirror::Object>();
1563          ScopedIndentation indent2(&vios_);
1564          for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1565            mirror::Object* value = image_root_object_array->Get(j);
1566            size_t run = 0;
1567            for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1568              if (value == image_root_object_array->Get(k)) {
1569                run++;
1570              } else {
1571                break;
1572              }
1573            }
1574            if (run == 0) {
1575              indent_os << StringPrintf("%d: ", j);
1576            } else {
1577              indent_os << StringPrintf("%d to %zd: ", j, j + run);
1578              j = j + run;
1579            }
1580            if (value != nullptr) {
1581              PrettyObjectValue(indent_os, value->GetClass(), value);
1582            } else {
1583              indent_os << j << ": null\n";
1584            }
1585          }
1586        }
1587      }
1588    }
1589
1590    {
1591      os << "METHOD ROOTS\n";
1592      static_assert(arraysize(image_methods_descriptions_) ==
1593          static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1594      for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1595        auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1596        const char* description = image_methods_descriptions_[i];
1597        auto* image_method = image_header_.GetImageMethod(image_root);
1598        indent_os << StringPrintf("%s: %p\n", description, image_method);
1599      }
1600    }
1601    os << "\n";
1602
1603    Runtime* const runtime = Runtime::Current();
1604    ClassLinker* class_linker = runtime->GetClassLinker();
1605    std::string image_filename = image_space_.GetImageFilename();
1606    std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
1607    os << "OAT LOCATION: " << oat_location;
1608    os << "\n";
1609    std::string error_msg;
1610    const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1611        oat_location);
1612    if (oat_file == nullptr) {
1613      oat_file = OatFile::Open(oat_location, oat_location,
1614                               nullptr, nullptr, false, nullptr,
1615                               &error_msg);
1616      if (oat_file == nullptr) {
1617        os << "NOT FOUND: " << error_msg << "\n";
1618        return false;
1619      }
1620    }
1621    os << "\n";
1622
1623    stats_.oat_file_bytes = oat_file->Size();
1624
1625    oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
1626
1627    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
1628      CHECK(oat_dex_file != nullptr);
1629      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1630                                                         oat_dex_file->FileSize()));
1631    }
1632
1633    os << "OBJECTS:\n" << std::flush;
1634
1635    // Loop through the image space and dump its objects.
1636    gc::Heap* heap = runtime->GetHeap();
1637    Thread* self = Thread::Current();
1638    {
1639      {
1640        WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1641        heap->FlushAllocStack();
1642      }
1643      // Since FlushAllocStack() above resets the (active) allocation
1644      // stack. Need to revoke the thread-local allocation stacks that
1645      // point into it.
1646      ScopedThreadSuspension sts(self, kNative);
1647      ScopedSuspendAll ssa(__FUNCTION__);
1648      heap->RevokeAllThreadLocalAllocationStacks(self);
1649    }
1650    {
1651      // Mark dex caches.
1652      dex_caches_.clear();
1653      {
1654        ReaderMutexLock mu(self, *class_linker->DexLock());
1655        for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
1656          mirror::DexCache* dex_cache =
1657              down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
1658          if (dex_cache != nullptr) {
1659            dex_caches_.insert(dex_cache);
1660          }
1661        }
1662      }
1663      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1664      // Dump the normal objects before ArtMethods.
1665      image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1666      indent_os << "\n";
1667      // TODO: Dump fields.
1668      // Dump methods after.
1669      const auto& methods_section = image_header_.GetMethodsSection();
1670      const size_t pointer_size =
1671          InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1672      DumpArtMethodVisitor visitor(this);
1673      methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
1674      // Dump the large objects separately.
1675      heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1676      indent_os << "\n";
1677    }
1678    os << "STATS:\n" << std::flush;
1679    std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
1680    if (file.get() == nullptr) {
1681      LOG(WARNING) << "Failed to find image in " << image_filename;
1682    }
1683    if (file.get() != nullptr) {
1684      stats_.file_bytes = file->GetLength();
1685    }
1686    size_t header_bytes = sizeof(ImageHeader);
1687    const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
1688    const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1689    const auto& method_section = image_header_.GetMethodsSection();
1690    const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1691        ImageHeader::kSectionDexCacheArrays);
1692    const auto& intern_section = image_header_.GetImageSection(
1693        ImageHeader::kSectionInternedStrings);
1694    const auto& class_table_section = image_header_.GetImageSection(
1695        ImageHeader::kSectionClassTable);
1696    const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1697
1698    stats_.header_bytes = header_bytes;
1699
1700    // Objects are kObjectAlignment-aligned.
1701    // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1702    if (object_section.Offset() > header_bytes) {
1703      stats_.alignment_bytes += object_section.Offset() - header_bytes;
1704    }
1705
1706    // Field section is 4-byte aligned.
1707    constexpr size_t kFieldSectionAlignment = 4U;
1708    uint32_t end_objects = object_section.Offset() + object_section.Size();
1709    CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1710    stats_.alignment_bytes += field_section.Offset() - end_objects;
1711
1712    // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1713    uint32_t end_fields = field_section.Offset() + field_section.Size();
1714    CHECK_ALIGNED(method_section.Offset(), 4);
1715    stats_.alignment_bytes += method_section.Offset() - end_fields;
1716
1717    // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1718    uint32_t end_methods = method_section.Offset() + method_section.Size();
1719    CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1720    stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1721
1722    // Intern table is 8-byte aligned.
1723    uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1724    CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1725    stats_.alignment_bytes += intern_section.Offset() - end_caches;
1726
1727    // Add space between intern table and class table.
1728    uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1729    stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1730
1731    // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1732    uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1733    CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1734    stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1735
1736    stats_.bitmap_bytes += bitmap_section.Size();
1737    stats_.art_field_bytes += field_section.Size();
1738    stats_.art_method_bytes += method_section.Size();
1739    stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
1740    stats_.interned_strings_bytes += intern_section.Size();
1741    stats_.class_table_bytes += class_table_section.Size();
1742    stats_.Dump(os, indent_os);
1743    os << "\n";
1744
1745    os << std::flush;
1746
1747    return oat_dumper_->Dump(os);
1748  }
1749
1750 private:
1751  class DumpArtMethodVisitor : public ArtMethodVisitor {
1752   public:
1753    explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1754
1755    virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1756      std::ostream& indent_os = image_dumper_->vios_.Stream();
1757      indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1758      image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1759      indent_os << "\n";
1760    }
1761
1762   private:
1763    ImageDumper* const image_dumper_;
1764  };
1765
1766  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
1767      SHARED_REQUIRES(Locks::mutator_lock_) {
1768    CHECK(type != nullptr);
1769    if (value == nullptr) {
1770      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
1771    } else if (type->IsStringClass()) {
1772      mirror::String* string = value->AsString();
1773      os << StringPrintf("%p   String: %s\n", string,
1774                         PrintableString(string->ToModifiedUtf8().c_str()).c_str());
1775    } else if (type->IsClassClass()) {
1776      mirror::Class* klass = value->AsClass();
1777      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
1778    } else {
1779      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
1780    }
1781  }
1782
1783  static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
1784      SHARED_REQUIRES(Locks::mutator_lock_) {
1785    os << StringPrintf("%s: ", field->GetName());
1786    switch (field->GetTypeAsPrimitiveType()) {
1787      case Primitive::kPrimLong:
1788        os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
1789        break;
1790      case Primitive::kPrimDouble:
1791        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
1792        break;
1793      case Primitive::kPrimFloat:
1794        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
1795        break;
1796      case Primitive::kPrimInt:
1797        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
1798        break;
1799      case Primitive::kPrimChar:
1800        os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
1801        break;
1802      case Primitive::kPrimShort:
1803        os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
1804        break;
1805      case Primitive::kPrimBoolean:
1806        os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1807            field->GetBoolean(obj));
1808        break;
1809      case Primitive::kPrimByte:
1810        os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
1811        break;
1812      case Primitive::kPrimNot: {
1813        // Get the value, don't compute the type unless it is non-null as we don't want
1814        // to cause class loading.
1815        mirror::Object* value = field->GetObj(obj);
1816        if (value == nullptr) {
1817          os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1818        } else {
1819          // Grab the field type without causing resolution.
1820          mirror::Class* field_type = field->GetType<false>();
1821          if (field_type != nullptr) {
1822            PrettyObjectValue(os, field_type, value);
1823          } else {
1824            os << StringPrintf("%p   %s\n", value,
1825                               PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1826          }
1827        }
1828        break;
1829      }
1830      default:
1831        os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1832        break;
1833    }
1834  }
1835
1836  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
1837      SHARED_REQUIRES(Locks::mutator_lock_) {
1838    mirror::Class* super = klass->GetSuperClass();
1839    if (super != nullptr) {
1840      DumpFields(os, obj, super);
1841    }
1842    for (ArtField& field : klass->GetIFields()) {
1843      PrintField(os, &field, obj);
1844    }
1845  }
1846
1847  bool InDumpSpace(const mirror::Object* object) {
1848    return image_space_.Contains(object);
1849  }
1850
1851  const void* GetQuickOatCodeBegin(ArtMethod* m)
1852      SHARED_REQUIRES(Locks::mutator_lock_) {
1853    const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1854        InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
1855    if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
1856      quick_code = oat_dumper_->GetQuickOatCode(m);
1857    }
1858    if (oat_dumper_->GetInstructionSet() == kThumb2) {
1859      quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
1860    }
1861    return quick_code;
1862  }
1863
1864  uint32_t GetQuickOatCodeSize(ArtMethod* m)
1865      SHARED_REQUIRES(Locks::mutator_lock_) {
1866    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1867    if (oat_code_begin == nullptr) {
1868      return 0;
1869    }
1870    return oat_code_begin[-1];
1871  }
1872
1873  const void* GetQuickOatCodeEnd(ArtMethod* m)
1874      SHARED_REQUIRES(Locks::mutator_lock_) {
1875    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
1876    if (oat_code_begin == nullptr) {
1877      return nullptr;
1878    }
1879    return oat_code_begin + GetQuickOatCodeSize(m);
1880  }
1881
1882  static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
1883    DCHECK(obj != nullptr);
1884    DCHECK(arg != nullptr);
1885    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
1886    if (!state->InDumpSpace(obj)) {
1887      return;
1888    }
1889
1890    size_t object_bytes = obj->SizeOf();
1891    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1892    state->stats_.object_bytes += object_bytes;
1893    state->stats_.alignment_bytes += alignment_bytes;
1894
1895    std::ostream& os = state->vios_.Stream();
1896
1897    mirror::Class* obj_class = obj->GetClass();
1898    if (obj_class->IsArrayClass()) {
1899      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1900                         obj->AsArray()->GetLength());
1901    } else if (obj->IsClass()) {
1902      mirror::Class* klass = obj->AsClass();
1903      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1904         << klass->GetStatus() << ")\n";
1905    } else if (obj_class->IsStringClass()) {
1906      os << StringPrintf("%p: java.lang.String %s\n", obj,
1907                         PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
1908    } else {
1909      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
1910    }
1911    ScopedIndentation indent1(&state->vios_);
1912    DumpFields(os, obj, obj_class);
1913    const auto image_pointer_size =
1914        InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1915    if (obj->IsObjectArray()) {
1916      auto* obj_array = obj->AsObjectArray<mirror::Object>();
1917      for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
1918        mirror::Object* value = obj_array->Get(i);
1919        size_t run = 0;
1920        for (int32_t j = i + 1; j < length; j++) {
1921          if (value == obj_array->Get(j)) {
1922            run++;
1923          } else {
1924            break;
1925          }
1926        }
1927        if (run == 0) {
1928          os << StringPrintf("%d: ", i);
1929        } else {
1930          os << StringPrintf("%d to %zd: ", i, i + run);
1931          i = i + run;
1932        }
1933        mirror::Class* value_class =
1934            (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
1935        PrettyObjectValue(os, value_class, value);
1936      }
1937    } else if (obj->IsClass()) {
1938      mirror::Class* klass = obj->AsClass();
1939      if (klass->NumStaticFields() != 0) {
1940        os << "STATICS:\n";
1941        ScopedIndentation indent2(&state->vios_);
1942        for (ArtField& field : klass->GetSFields()) {
1943          PrintField(os, &field, field.GetDeclaringClass());
1944        }
1945      }
1946    } else {
1947      auto it = state->dex_caches_.find(obj);
1948      if (it != state->dex_caches_.end()) {
1949        auto* dex_cache = down_cast<mirror::DexCache*>(obj);
1950        const auto& field_section = state->image_header_.GetImageSection(
1951            ImageHeader::kSectionArtFields);
1952        const auto& method_section = state->image_header_.GetMethodsSection();
1953        size_t num_methods = dex_cache->NumResolvedMethods();
1954        if (num_methods != 0u) {
1955          os << "Methods (size=" << num_methods << "):";
1956          ScopedIndentation indent2(&state->vios_);
1957          auto* resolved_methods = dex_cache->GetResolvedMethods();
1958          for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1959            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1960            size_t run = 0;
1961            for (size_t j = i + 1;
1962                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1963                                                                           j,
1964                                                                           image_pointer_size);
1965                ++j, ++run) {}
1966            if (run == 0) {
1967              os << StringPrintf("%zd: ", i);
1968            } else {
1969              os << StringPrintf("%zd to %zd: ", i, i + run);
1970              i = i + run;
1971            }
1972            std::string msg;
1973            if (elem == nullptr) {
1974              msg = "null";
1975            } else if (method_section.Contains(
1976                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1977              msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1978            } else {
1979              msg = "<not in method section>";
1980            }
1981            os << StringPrintf("%p   %s\n", elem, msg.c_str());
1982          }
1983        }
1984        size_t num_fields = dex_cache->NumResolvedFields();
1985        if (num_fields != 0u) {
1986          os << "Fields (size=" << num_fields << "):";
1987          ScopedIndentation indent2(&state->vios_);
1988          auto* resolved_fields = dex_cache->GetResolvedFields();
1989          for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1990            auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1991            size_t run = 0;
1992            for (size_t j = i + 1;
1993                j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1994                                                                           j,
1995                                                                           image_pointer_size);
1996                ++j, ++run) {}
1997            if (run == 0) {
1998              os << StringPrintf("%zd: ", i);
1999            } else {
2000              os << StringPrintf("%zd to %zd: ", i, i + run);
2001              i = i + run;
2002            }
2003            std::string msg;
2004            if (elem == nullptr) {
2005              msg = "null";
2006            } else if (field_section.Contains(
2007                reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2008              msg = PrettyField(reinterpret_cast<ArtField*>(elem));
2009            } else {
2010              msg = "<not in field section>";
2011            }
2012            os << StringPrintf("%p   %s\n", elem, msg.c_str());
2013          }
2014        }
2015      }
2016    }
2017    std::string temp;
2018    state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
2019  }
2020
2021  void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
2022      SHARED_REQUIRES(Locks::mutator_lock_) {
2023    DCHECK(method != nullptr);
2024    const auto image_pointer_size =
2025        InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
2026    const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2027    const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
2028    OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2029        reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2030    if (method->IsNative()) {
2031      if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2032        DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2033        DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2034      }
2035      bool first_occurrence;
2036      uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2037      state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2038      if (first_occurrence) {
2039        state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2040      }
2041      if (quick_oat_code_begin !=
2042            method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
2043        indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
2044      }
2045    } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2046      method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2047      method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
2048    } else {
2049      const DexFile::CodeItem* code_item = method->GetCodeItem();
2050      size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2051      state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2052
2053      bool first_occurrence;
2054      size_t gc_map_bytes = state->ComputeOatSize(
2055          method_header->GetNativeGcMap(), &first_occurrence);
2056      if (first_occurrence) {
2057        state->stats_.gc_map_bytes += gc_map_bytes;
2058      }
2059
2060      size_t pc_mapping_table_bytes = state->ComputeOatSize(
2061          method_header->GetMappingTable(), &first_occurrence);
2062      if (first_occurrence) {
2063        state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2064      }
2065
2066      size_t vmap_table_bytes = 0u;
2067      if (!method_header->IsOptimized()) {
2068        // Method compiled with the optimizing compiler have no vmap table.
2069        vmap_table_bytes = state->ComputeOatSize(
2070            method_header->GetVmapTable(), &first_occurrence);
2071        if (first_occurrence) {
2072          state->stats_.vmap_table_bytes += vmap_table_bytes;
2073        }
2074      }
2075
2076      uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2077      state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2078      if (first_occurrence) {
2079        state->stats_.managed_code_bytes += quick_oat_code_size;
2080        if (method->IsConstructor()) {
2081          if (method->IsStatic()) {
2082            state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2083          } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2084            state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2085          }
2086        } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2087          state->stats_.large_method_code_bytes += quick_oat_code_size;
2088        }
2089      }
2090      state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2091
2092      uint32_t method_access_flags = method->GetAccessFlags();
2093
2094      indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
2095      indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2096                                dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2097                                method_access_flags);
2098
2099      size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
2100          vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
2101
2102      double expansion =
2103      static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2104      state->stats_.ComputeOutliers(total_size, expansion, method);
2105    }
2106  }
2107
2108  std::set<const void*> already_seen_;
2109  // Compute the size of the given data within the oat file and whether this is the first time
2110  // this data has been requested
2111  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
2112    if (already_seen_.count(oat_data) == 0) {
2113      *first_occurrence = true;
2114      already_seen_.insert(oat_data);
2115    } else {
2116      *first_occurrence = false;
2117    }
2118    return oat_dumper_->ComputeSize(oat_data);
2119  }
2120
2121 public:
2122  struct Stats {
2123    size_t oat_file_bytes;
2124    size_t file_bytes;
2125
2126    size_t header_bytes;
2127    size_t object_bytes;
2128    size_t art_field_bytes;
2129    size_t art_method_bytes;
2130    size_t dex_cache_arrays_bytes;
2131    size_t interned_strings_bytes;
2132    size_t class_table_bytes;
2133    size_t bitmap_bytes;
2134    size_t alignment_bytes;
2135
2136    size_t managed_code_bytes;
2137    size_t managed_code_bytes_ignoring_deduplication;
2138    size_t managed_to_native_code_bytes;
2139    size_t native_to_managed_code_bytes;
2140    size_t class_initializer_code_bytes;
2141    size_t large_initializer_code_bytes;
2142    size_t large_method_code_bytes;
2143
2144    size_t gc_map_bytes;
2145    size_t pc_mapping_table_bytes;
2146    size_t vmap_table_bytes;
2147
2148    size_t dex_instruction_bytes;
2149
2150    std::vector<ArtMethod*> method_outlier;
2151    std::vector<size_t> method_outlier_size;
2152    std::vector<double> method_outlier_expansion;
2153    std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
2154
2155    Stats()
2156        : oat_file_bytes(0),
2157          file_bytes(0),
2158          header_bytes(0),
2159          object_bytes(0),
2160          art_field_bytes(0),
2161          art_method_bytes(0),
2162          dex_cache_arrays_bytes(0),
2163          interned_strings_bytes(0),
2164          class_table_bytes(0),
2165          bitmap_bytes(0),
2166          alignment_bytes(0),
2167          managed_code_bytes(0),
2168          managed_code_bytes_ignoring_deduplication(0),
2169          managed_to_native_code_bytes(0),
2170          native_to_managed_code_bytes(0),
2171          class_initializer_code_bytes(0),
2172          large_initializer_code_bytes(0),
2173          large_method_code_bytes(0),
2174          gc_map_bytes(0),
2175          pc_mapping_table_bytes(0),
2176          vmap_table_bytes(0),
2177          dex_instruction_bytes(0) {}
2178
2179    struct SizeAndCount {
2180      SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
2181      size_t bytes;
2182      size_t count;
2183    };
2184    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2185    SizeAndCountTable sizes_and_counts;
2186
2187    void Update(const char* descriptor, size_t object_bytes_in) {
2188      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2189      if (it != sizes_and_counts.end()) {
2190        it->second.bytes += object_bytes_in;
2191        it->second.count += 1;
2192      } else {
2193        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
2194      }
2195    }
2196
2197    double PercentOfOatBytes(size_t size) {
2198      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2199    }
2200
2201    double PercentOfFileBytes(size_t size) {
2202      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2203    }
2204
2205    double PercentOfObjectBytes(size_t size) {
2206      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2207    }
2208
2209    void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
2210      method_outlier_size.push_back(total_size);
2211      method_outlier_expansion.push_back(expansion);
2212      method_outlier.push_back(method);
2213    }
2214
2215    void DumpOutliers(std::ostream& os)
2216        SHARED_REQUIRES(Locks::mutator_lock_) {
2217      size_t sum_of_sizes = 0;
2218      size_t sum_of_sizes_squared = 0;
2219      size_t sum_of_expansion = 0;
2220      size_t sum_of_expansion_squared = 0;
2221      size_t n = method_outlier_size.size();
2222      if (n == 0) {
2223        return;
2224      }
2225      for (size_t i = 0; i < n; i++) {
2226        size_t cur_size = method_outlier_size[i];
2227        sum_of_sizes += cur_size;
2228        sum_of_sizes_squared += cur_size * cur_size;
2229        double cur_expansion = method_outlier_expansion[i];
2230        sum_of_expansion += cur_expansion;
2231        sum_of_expansion_squared += cur_expansion * cur_expansion;
2232      }
2233      size_t size_mean = sum_of_sizes / n;
2234      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2235      double expansion_mean = sum_of_expansion / n;
2236      double expansion_variance =
2237          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2238
2239      // Dump methods whose size is a certain number of standard deviations from the mean
2240      size_t dumped_values = 0;
2241      size_t skipped_values = 0;
2242      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
2243        size_t cur_size_variance = i * i * size_variance;
2244        bool first = true;
2245        for (size_t j = 0; j < n; j++) {
2246          size_t cur_size = method_outlier_size[j];
2247          if (cur_size > size_mean) {
2248            size_t cur_var = cur_size - size_mean;
2249            cur_var = cur_var * cur_var;
2250            if (cur_var > cur_size_variance) {
2251              if (dumped_values > 20) {
2252                if (i == 1) {
2253                  skipped_values++;
2254                } else {
2255                  i = 2;  // jump to counting for 1 standard deviation
2256                  break;
2257                }
2258              } else {
2259                if (first) {
2260                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
2261                  first = false;
2262                }
2263                os << PrettyMethod(method_outlier[j]) << " requires storage of "
2264                    << PrettySize(cur_size) << "\n";
2265                method_outlier_size[j] = 0;  // don't consider this method again
2266                dumped_values++;
2267              }
2268            }
2269          }
2270        }
2271      }
2272      if (skipped_values > 0) {
2273        os << "... skipped " << skipped_values
2274           << " methods with size > 1 standard deviation from the norm\n";
2275      }
2276      os << std::flush;
2277
2278      // Dump methods whose expansion is a certain number of standard deviations from the mean
2279      dumped_values = 0;
2280      skipped_values = 0;
2281      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
2282        double cur_expansion_variance = i * i * expansion_variance;
2283        bool first = true;
2284        for (size_t j = 0; j < n; j++) {
2285          double cur_expansion = method_outlier_expansion[j];
2286          if (cur_expansion > expansion_mean) {
2287            size_t cur_var = cur_expansion - expansion_mean;
2288            cur_var = cur_var * cur_var;
2289            if (cur_var > cur_expansion_variance) {
2290              if (dumped_values > 20) {
2291                if (i == 1) {
2292                  skipped_values++;
2293                } else {
2294                  i = 2;  // jump to counting for 1 standard deviation
2295                  break;
2296                }
2297              } else {
2298                if (first) {
2299                  os << "\nLarge expansion methods (size > " << i
2300                      << " standard deviations the norm):\n";
2301                  first = false;
2302                }
2303                os << PrettyMethod(method_outlier[j]) << " expanded code by "
2304                   << cur_expansion << "\n";
2305                method_outlier_expansion[j] = 0.0;  // don't consider this method again
2306                dumped_values++;
2307              }
2308            }
2309          }
2310        }
2311      }
2312      if (skipped_values > 0) {
2313        os << "... skipped " << skipped_values
2314           << " methods with expansion > 1 standard deviation from the norm\n";
2315      }
2316      os << "\n" << std::flush;
2317    }
2318
2319    void Dump(std::ostream& os, std::ostream& indent_os)
2320        SHARED_REQUIRES(Locks::mutator_lock_) {
2321      {
2322        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2323           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
2324        indent_os << StringPrintf("header_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
2325                                  "object_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
2326                                  "art_field_bytes        =  %8zd (%2.0f%% of art file bytes)\n"
2327                                  "art_method_bytes       =  %8zd (%2.0f%% of art file bytes)\n"
2328                                  "dex_cache_arrays_bytes =  %8zd (%2.0f%% of art file bytes)\n"
2329                                  "interned_string_bytes  =  %8zd (%2.0f%% of art file bytes)\n"
2330                                  "class_table_bytes      =  %8zd (%2.0f%% of art file bytes)\n"
2331                                  "bitmap_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
2332                                  "alignment_bytes        =  %8zd (%2.0f%% of art file bytes)\n\n",
2333                                  header_bytes, PercentOfFileBytes(header_bytes),
2334                                  object_bytes, PercentOfFileBytes(object_bytes),
2335                                  art_field_bytes, PercentOfFileBytes(art_field_bytes),
2336                                  art_method_bytes, PercentOfFileBytes(art_method_bytes),
2337                                  dex_cache_arrays_bytes,
2338                                  PercentOfFileBytes(dex_cache_arrays_bytes),
2339                                  interned_strings_bytes,
2340                                  PercentOfFileBytes(interned_strings_bytes),
2341                                  class_table_bytes, PercentOfFileBytes(class_table_bytes),
2342                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
2343                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
2344            << std::flush;
2345        CHECK_EQ(file_bytes,
2346                 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2347                 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2348                 bitmap_bytes + alignment_bytes);
2349      }
2350
2351      os << "object_bytes breakdown:\n";
2352      size_t object_bytes_total = 0;
2353      for (const auto& sizes_and_count : sizes_and_counts) {
2354        const std::string& descriptor(sizes_and_count.first);
2355        double average = static_cast<double>(sizes_and_count.second.bytes) /
2356            static_cast<double>(sizes_and_count.second.count);
2357        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
2358        os << StringPrintf("%32s %8zd bytes %6zd instances "
2359                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
2360                           descriptor.c_str(), sizes_and_count.second.bytes,
2361                           sizes_and_count.second.count, average, percent);
2362        object_bytes_total += sizes_and_count.second.bytes;
2363      }
2364      os << "\n" << std::flush;
2365      CHECK_EQ(object_bytes, object_bytes_total);
2366
2367      os << StringPrintf("oat_file_bytes               = %8zd\n"
2368                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
2369                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2370                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2371                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2372                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2373                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
2374                         oat_file_bytes,
2375                         managed_code_bytes,
2376                         PercentOfOatBytes(managed_code_bytes),
2377                         managed_to_native_code_bytes,
2378                         PercentOfOatBytes(managed_to_native_code_bytes),
2379                         native_to_managed_code_bytes,
2380                         PercentOfOatBytes(native_to_managed_code_bytes),
2381                         class_initializer_code_bytes,
2382                         PercentOfOatBytes(class_initializer_code_bytes),
2383                         large_initializer_code_bytes,
2384                         PercentOfOatBytes(large_initializer_code_bytes),
2385                         large_method_code_bytes,
2386                         PercentOfOatBytes(large_method_code_bytes))
2387            << "DexFile sizes:\n";
2388      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
2389        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
2390                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2391                           PercentOfOatBytes(oat_dex_file_size.second));
2392      }
2393
2394      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
2395                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2396                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
2397                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2398                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2399                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2400         << std::flush;
2401
2402      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2403         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
2404                         static_cast<double>(managed_code_bytes) /
2405                             static_cast<double>(dex_instruction_bytes),
2406                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2407                             static_cast<double>(dex_instruction_bytes))
2408         << std::flush;
2409
2410      DumpOutliers(os);
2411    }
2412  } stats_;
2413
2414 private:
2415  enum {
2416    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2417    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2418    kLargeConstructorDexBytes = 4000,
2419    // Number of bytes for a method to be considered large. Based on the 4000 basic block
2420    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2421    kLargeMethodDexBytes = 16000
2422  };
2423
2424  // For performance, use the *os_ directly for anything that doesn't need indentation
2425  // and prepare an indentation stream with default indentation 1.
2426  std::ostream* os_;
2427  VariableIndentationOutputStream vios_;
2428  ScopedIndentation indent1_;
2429
2430  gc::space::ImageSpace& image_space_;
2431  const ImageHeader& image_header_;
2432  std::unique_ptr<OatDumper> oat_dumper_;
2433  OatDumperOptions* oat_dumper_options_;
2434  std::set<mirror::Object*> dex_caches_;
2435
2436  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
2437};
2438
2439static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
2440  // Dumping the image, no explicit class loader.
2441  ScopedNullHandle<mirror::ClassLoader> null_class_loader;
2442  options->class_loader_ = &null_class_loader;
2443
2444  ScopedObjectAccess soa(Thread::Current());
2445  gc::Heap* heap = runtime->GetHeap();
2446  std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2447  CHECK(!image_spaces.empty());
2448  for (gc::space::ImageSpace* image_space : image_spaces) {
2449    const ImageHeader& image_header = image_space->GetImageHeader();
2450    if (!image_header.IsValid()) {
2451      fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2452      return EXIT_FAILURE;
2453    }
2454
2455    ImageDumper image_dumper(os, *image_space, image_header, options);
2456    if (!image_dumper.Dump()) {
2457      return EXIT_FAILURE;
2458    }
2459  }
2460  return EXIT_SUCCESS;
2461}
2462
2463static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2464                              std::ostream* os) {
2465  CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2466
2467  Thread* self = Thread::Current();
2468  CHECK(self != nullptr);
2469  // Need well-known-classes.
2470  WellKnownClasses::Init(self->GetJniEnv());
2471
2472  // Need to register dex files to get a working dex cache.
2473  ScopedObjectAccess soa(self);
2474  ClassLinker* class_linker = runtime->GetClassLinker();
2475  runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
2476  std::vector<const DexFile*> class_path;
2477  for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2478    std::string error_msg;
2479    const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
2480    CHECK(dex_file != nullptr) << error_msg;
2481    class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
2482    class_path.push_back(dex_file);
2483  }
2484
2485  // Need a class loader.
2486  // Fake that we're a compiler.
2487  jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
2488
2489  // Use the class loader while dumping.
2490  StackHandleScope<1> scope(self);
2491  Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2492      soa.Decode<mirror::ClassLoader*>(class_loader));
2493  options->class_loader_ = &loader_handle;
2494
2495  OatDumper oat_dumper(*oat_file, *options);
2496  bool success = oat_dumper.Dump(*os);
2497  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2498}
2499
2500static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2501  CHECK(oat_file != nullptr && options != nullptr);
2502  // No image = no class loader.
2503  ScopedNullHandle<mirror::ClassLoader> null_class_loader;
2504  options->class_loader_ = &null_class_loader;
2505
2506  OatDumper oat_dumper(*oat_file, *options);
2507  bool success = oat_dumper.Dump(*os);
2508  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2509}
2510
2511static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2512                   std::ostream* os) {
2513  std::string error_msg;
2514  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2515                                    nullptr, &error_msg);
2516  if (oat_file == nullptr) {
2517    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2518    return EXIT_FAILURE;
2519  }
2520
2521  if (runtime != nullptr) {
2522    return DumpOatWithRuntime(runtime, oat_file, options, os);
2523  } else {
2524    return DumpOatWithoutRuntime(oat_file, options, os);
2525  }
2526}
2527
2528static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2529  std::string error_msg;
2530  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
2531                                    nullptr, &error_msg);
2532  if (oat_file == nullptr) {
2533    fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2534    return EXIT_FAILURE;
2535  }
2536
2537  OatSymbolizer oat_symbolizer(oat_file, output_name);
2538  if (!oat_symbolizer.Symbolize()) {
2539    fprintf(stderr, "Failed to symbolize\n");
2540    return EXIT_FAILURE;
2541  }
2542
2543  return EXIT_SUCCESS;
2544}
2545
2546struct OatdumpArgs : public CmdlineArgs {
2547 protected:
2548  using Base = CmdlineArgs;
2549
2550  virtual ParseStatus ParseCustom(const StringPiece& option,
2551                                  std::string* error_msg) OVERRIDE {
2552    {
2553      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2554      if (base_parse != kParseUnknownArgument) {
2555        return base_parse;
2556      }
2557    }
2558
2559    if (option.starts_with("--oat-file=")) {
2560      oat_filename_ = option.substr(strlen("--oat-file=")).data();
2561    } else if (option.starts_with("--image=")) {
2562      image_location_ = option.substr(strlen("--image=")).data();
2563    } else if (option =="--dump:raw_mapping_table") {
2564      dump_raw_mapping_table_ = true;
2565    } else if (option == "--dump:raw_gc_map") {
2566      dump_raw_gc_map_ = true;
2567    } else if (option == "--no-dump:vmap") {
2568      dump_vmap_ = false;
2569    } else if (option =="--dump:code_info_stack_maps") {
2570      dump_code_info_stack_maps_ = true;
2571    } else if (option == "--no-disassemble") {
2572      disassemble_code_ = false;
2573    } else if (option.starts_with("--symbolize=")) {
2574      oat_filename_ = option.substr(strlen("--symbolize=")).data();
2575      symbolize_ = true;
2576    } else if (option.starts_with("--class-filter=")) {
2577      class_filter_ = option.substr(strlen("--class-filter=")).data();
2578    } else if (option.starts_with("--method-filter=")) {
2579      method_filter_ = option.substr(strlen("--method-filter=")).data();
2580    } else if (option.starts_with("--list-classes")) {
2581      list_classes_ = true;
2582    } else if (option.starts_with("--list-methods")) {
2583      list_methods_ = true;
2584    } else if (option.starts_with("--export-dex-to=")) {
2585      export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2586    } else if (option.starts_with("--addr2instr=")) {
2587      if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2588        *error_msg = "Address conversion failed";
2589        return kParseError;
2590      }
2591    } else {
2592      return kParseUnknownArgument;
2593    }
2594
2595    return kParseOk;
2596  }
2597
2598  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2599    // Infer boot image location from the image location if possible.
2600    if (boot_image_location_ == nullptr) {
2601      boot_image_location_ = image_location_;
2602    }
2603
2604    // Perform the parent checks.
2605    ParseStatus parent_checks = Base::ParseChecks(error_msg);
2606    if (parent_checks != kParseOk) {
2607      return parent_checks;
2608    }
2609
2610    // Perform our own checks.
2611    if (image_location_ == nullptr && oat_filename_ == nullptr) {
2612      *error_msg = "Either --image or --oat-file must be specified";
2613      return kParseError;
2614    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2615      *error_msg = "Either --image or --oat-file must be specified but not both";
2616      return kParseError;
2617    }
2618
2619    return kParseOk;
2620  }
2621
2622  virtual std::string GetUsage() const {
2623    std::string usage;
2624
2625    usage +=
2626        "Usage: oatdump [options] ...\n"
2627        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2628        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
2629        "\n"
2630        // Either oat-file or image is required.
2631        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
2632        "      Example: --oat-file=/system/framework/boot.oat\n"
2633        "\n"
2634        "  --image=<file.art>: specifies an input image location.\n"
2635        "      Example: --image=/system/framework/boot.art\n"
2636        "\n";
2637
2638    usage += Base::GetUsage();
2639
2640    usage +=  // Optional.
2641        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
2642        "      Example: --dump:raw_mapping_table\n"
2643        "\n"
2644        "  --dump:raw_gc_map enables dumping of the GC map.\n"
2645        "      Example: --dump:raw_gc_map\n"
2646        "\n"
2647        "  --no-dump:vmap may be used to disable vmap dumping.\n"
2648        "      Example: --no-dump:vmap\n"
2649        "\n"
2650        "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2651        "      Example: --dump:code_info_stack_maps\n"
2652        "\n"
2653        "  --no-disassemble may be used to disable disassembly.\n"
2654        "      Example: --no-disassemble\n"
2655        "\n"
2656        "  --list-classes may be used to list target file classes (can be used with filters).\n"
2657        "      Example: --list-classes\n"
2658        "      Example: --list-classes --class-filter=com.example.foo\n"
2659        "\n"
2660        "  --list-methods may be used to list target file methods (can be used with filters).\n"
2661        "      Example: --list-methods\n"
2662        "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2663        "\n"
2664        "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2665        "      Example: --symbolize=/system/framework/boot.oat\n"
2666        "\n"
2667        "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
2668        "      Example: --class-filter=com.example.foo\n"
2669        "\n"
2670        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
2671        "      Example: --method-filter=foo\n"
2672        "\n"
2673        "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2674        "      Example: --export-dex-to=/data/local/tmp\n"
2675        "\n"
2676        "  --addr2instr=<address>: output matching method disassembled code from relative\n"
2677        "                          address (e.g. PC from crash dump)\n"
2678        "      Example: --addr2instr=0x00001a3b\n"
2679        "\n";
2680
2681    return usage;
2682  }
2683
2684 public:
2685  const char* oat_filename_ = nullptr;
2686  const char* class_filter_ = "";
2687  const char* method_filter_ = "";
2688  const char* image_location_ = nullptr;
2689  std::string elf_filename_prefix_;
2690  bool dump_raw_mapping_table_ = false;
2691  bool dump_raw_gc_map_ = false;
2692  bool dump_vmap_ = true;
2693  bool dump_code_info_stack_maps_ = false;
2694  bool disassemble_code_ = true;
2695  bool symbolize_ = false;
2696  bool list_classes_ = false;
2697  bool list_methods_ = false;
2698  uint32_t addr2instr_ = 0;
2699  const char* export_dex_location_ = nullptr;
2700};
2701
2702struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2703  virtual bool NeedsRuntime() OVERRIDE {
2704    CHECK(args_ != nullptr);
2705
2706    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2707    bool absolute_addresses = (args_->oat_filename_ == nullptr);
2708
2709    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2710        args_->dump_raw_mapping_table_,
2711        args_->dump_raw_gc_map_,
2712        args_->dump_vmap_,
2713        args_->dump_code_info_stack_maps_,
2714        args_->disassemble_code_,
2715        absolute_addresses,
2716        args_->class_filter_,
2717        args_->method_filter_,
2718        args_->list_classes_,
2719        args_->list_methods_,
2720        args_->export_dex_location_,
2721        args_->addr2instr_));
2722
2723    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2724          !args_->symbolize_;
2725  }
2726
2727  virtual bool ExecuteWithoutRuntime() OVERRIDE {
2728    CHECK(args_ != nullptr);
2729    CHECK(args_->oat_filename_ != nullptr);
2730
2731    MemMap::Init();
2732
2733    if (args_->symbolize_) {
2734      return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2735    } else {
2736      return DumpOat(nullptr,
2737                     args_->oat_filename_,
2738                     oat_dumper_options_.get(),
2739                     args_->os_) == EXIT_SUCCESS;
2740    }
2741  }
2742
2743  virtual bool ExecuteWithRuntime(Runtime* runtime) {
2744    CHECK(args_ != nullptr);
2745
2746    if (args_->oat_filename_ != nullptr) {
2747      return DumpOat(runtime,
2748                     args_->oat_filename_,
2749                     oat_dumper_options_.get(),
2750                     args_->os_) == EXIT_SUCCESS;
2751    }
2752
2753    return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
2754  }
2755
2756  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2757};
2758
2759}  // namespace art
2760
2761int main(int argc, char** argv) {
2762  art::OatdumpMain main;
2763  return main.Main(argc, argv);
2764}
2765