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