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 <iomanip>
22 #include <iostream>
23 #include <map>
24 #include <set>
25 #include <string>
26 #include <unordered_map>
27 #include <unordered_set>
28 #include <vector>
29 
30 #include "android-base/logging.h"
31 #include "android-base/parseint.h"
32 #include "android-base/stringprintf.h"
33 #include "android-base/strings.h"
34 
35 #include "arch/instruction_set_features.h"
36 #include "art_field-inl.h"
37 #include "art_method-inl.h"
38 #include "base/bit_utils_iterator.h"
39 #include "base/indenter.h"
40 #include "base/os.h"
41 #include "base/safe_map.h"
42 #include "base/stats.h"
43 #include "base/stl_util.h"
44 #include "base/unix_file/fd_file.h"
45 #include "class_linker-inl.h"
46 #include "class_linker.h"
47 #include "class_root-inl.h"
48 #include "compiled_method.h"
49 #include "debug/debug_info.h"
50 #include "debug/elf_debug_writer.h"
51 #include "debug/method_debug_info.h"
52 #include "dex/art_dex_file_loader.h"
53 #include "dex/class_accessor-inl.h"
54 #include "dex/code_item_accessors-inl.h"
55 #include "dex/descriptors_names.h"
56 #include "dex/dex_file-inl.h"
57 #include "dex/dex_instruction-inl.h"
58 #include "dex/string_reference.h"
59 #include "dex/type_lookup_table.h"
60 #include "dexlayout.h"
61 #include "disassembler.h"
62 #include "elf/elf_builder.h"
63 #include "gc/accounting/space_bitmap-inl.h"
64 #include "gc/space/image_space.h"
65 #include "gc/space/large_object_space.h"
66 #include "gc/space/space-inl.h"
67 #include "image-inl.h"
68 #include "imtable-inl.h"
69 #include "index_bss_mapping.h"
70 #include "interpreter/unstarted_runtime.h"
71 #include "mirror/array-inl.h"
72 #include "mirror/class-inl.h"
73 #include "mirror/dex_cache-inl.h"
74 #include "mirror/object-inl.h"
75 #include "mirror/object_array-inl.h"
76 #include "oat.h"
77 #include "oat_file-inl.h"
78 #include "oat_file_manager.h"
79 #include "scoped_thread_state_change-inl.h"
80 #include "stack.h"
81 #include "stack_map.h"
82 #include "stream/buffered_output_stream.h"
83 #include "stream/file_output_stream.h"
84 #include "subtype_check.h"
85 #include "thread_list.h"
86 #include "vdex_file.h"
87 #include "verifier/method_verifier.h"
88 #include "verifier/verifier_deps.h"
89 #include "well_known_classes.h"
90 
91 #include <sys/stat.h>
92 #include "cmdline.h"
93 
94 namespace art {
95 
96 using android::base::StringPrintf;
97 
98 const char* image_methods_descriptions_[] = {
99   "kResolutionMethod",
100   "kImtConflictMethod",
101   "kImtUnimplementedMethod",
102   "kSaveAllCalleeSavesMethod",
103   "kSaveRefsOnlyMethod",
104   "kSaveRefsAndArgsMethod",
105   "kSaveEverythingMethod",
106   "kSaveEverythingMethodForClinit",
107   "kSaveEverythingMethodForSuspendCheck",
108 };
109 
110 const char* image_roots_descriptions_[] = {
111   "kDexCaches",
112   "kClassRoots",
113   "kSpecialRoots",
114 };
115 
116 // Map is so that we don't allocate multiple dex files for the same OatDexFile.
117 static std::map<const OatDexFile*, std::unique_ptr<const DexFile>> opened_dex_files;
118 
OpenDexFile(const OatDexFile * oat_dex_file,std::string * error_msg)119 const DexFile* OpenDexFile(const OatDexFile* oat_dex_file, std::string* error_msg) {
120   DCHECK(oat_dex_file != nullptr);
121   auto it = opened_dex_files.find(oat_dex_file);
122   if (it != opened_dex_files.end()) {
123     return it->second.get();
124   }
125   const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
126   opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
127   return ret;
128 }
129 
130 template <typename ElfTypes>
131 class OatSymbolizer final {
132  public:
OatSymbolizer(const OatFile * oat_file,const std::string & output_name,bool no_bits)133   OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
134       oat_file_(oat_file),
135       builder_(nullptr),
136       output_name_(output_name.empty() ? "symbolized.oat" : output_name),
137       no_bits_(no_bits) {
138   }
139 
Symbolize()140   bool Symbolize() {
141     const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
142     std::unique_ptr<const InstructionSetFeatures> features = InstructionSetFeatures::FromBitmap(
143         isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
144 
145     std::unique_ptr<File> elf_file(OS::CreateEmptyFile(output_name_.c_str()));
146     if (elf_file == nullptr) {
147       return false;
148     }
149     std::unique_ptr<BufferedOutputStream> output_stream =
150         std::make_unique<BufferedOutputStream>(
151             std::make_unique<FileOutputStream>(elf_file.get()));
152     builder_.reset(new ElfBuilder<ElfTypes>(isa, output_stream.get()));
153 
154     builder_->Start();
155 
156     auto* rodata = builder_->GetRoData();
157     auto* text = builder_->GetText();
158 
159     const uint8_t* rodata_begin = oat_file_->Begin();
160     const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
161     if (!no_bits_) {
162       rodata->Start();
163       rodata->WriteFully(rodata_begin, rodata_size);
164       rodata->End();
165     }
166 
167     const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
168     const size_t text_size = oat_file_->End() - text_begin;
169     if (!no_bits_) {
170       text->Start();
171       text->WriteFully(text_begin, text_size);
172       text->End();
173     }
174 
175     builder_->PrepareDynamicSection(elf_file->GetPath(),
176                                     rodata_size,
177                                     text_size,
178                                     oat_file_->DataBimgRelRoSize(),
179                                     oat_file_->BssSize(),
180                                     oat_file_->BssMethodsOffset(),
181                                     oat_file_->BssRootsOffset(),
182                                     oat_file_->VdexSize());
183     builder_->WriteDynamicSection();
184 
185     const OatHeader& oat_header = oat_file_->GetOatHeader();
186     #define DO_TRAMPOLINE(fn_name)                                                \
187       if (oat_header.Get ## fn_name ## Offset() != 0) {                           \
188         debug::MethodDebugInfo info = {};                                         \
189         info.custom_name = #fn_name;                                              \
190         info.isa = oat_header.GetInstructionSet();                                \
191         info.is_code_address_text_relative = true;                                \
192         size_t code_offset = oat_header.Get ## fn_name ## Offset();               \
193         code_offset -= CompiledCode::CodeDelta(oat_header.GetInstructionSet());   \
194         info.code_address = code_offset - oat_header.GetExecutableOffset();       \
195         info.code_size = 0;  /* The symbol lasts until the next symbol. */        \
196         method_debug_infos_.push_back(std::move(info));                           \
197       }
198     DO_TRAMPOLINE(JniDlsymLookupTrampoline);
199     DO_TRAMPOLINE(JniDlsymLookupCriticalTrampoline);
200     DO_TRAMPOLINE(QuickGenericJniTrampoline);
201     DO_TRAMPOLINE(QuickImtConflictTrampoline);
202     DO_TRAMPOLINE(QuickResolutionTrampoline);
203     DO_TRAMPOLINE(QuickToInterpreterBridge);
204     #undef DO_TRAMPOLINE
205 
206     Walk();
207 
208     // TODO: Try to symbolize link-time thunks?
209     // This would require disassembling all methods to find branches outside the method code.
210 
211     // TODO: Add symbols for dex bytecode in the .dex section.
212 
213     debug::DebugInfo debug_info{};
214     debug_info.compiled_methods = ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_);
215 
216     debug::WriteDebugInfo(builder_.get(), debug_info);
217 
218     builder_->End();
219 
220     bool ret_value = builder_->Good();
221 
222     builder_.reset();
223     output_stream.reset();
224 
225     if (elf_file->FlushCloseOrErase() != 0) {
226       return false;
227     }
228     elf_file.reset();
229 
230     return ret_value;
231   }
232 
Walk()233   void Walk() {
234     std::vector<const OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
235     for (size_t i = 0; i < oat_dex_files.size(); i++) {
236       const OatDexFile* oat_dex_file = oat_dex_files[i];
237       CHECK(oat_dex_file != nullptr);
238       WalkOatDexFile(oat_dex_file);
239     }
240   }
241 
WalkOatDexFile(const OatDexFile * oat_dex_file)242   void WalkOatDexFile(const OatDexFile* oat_dex_file) {
243     std::string error_msg;
244     const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
245     if (dex_file == nullptr) {
246       return;
247     }
248     for (size_t class_def_index = 0;
249         class_def_index < dex_file->NumClassDefs();
250         class_def_index++) {
251       const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
252       OatClassType type = oat_class.GetType();
253       switch (type) {
254         case kOatClassAllCompiled:
255         case kOatClassSomeCompiled:
256           WalkOatClass(oat_class, *dex_file, class_def_index);
257           break;
258 
259         case kOatClassNoneCompiled:
260         case kOatClassMax:
261           // Ignore.
262           break;
263       }
264     }
265   }
266 
WalkOatClass(const OatFile::OatClass & oat_class,const DexFile & dex_file,uint32_t class_def_index)267   void WalkOatClass(const OatFile::OatClass& oat_class,
268                     const DexFile& dex_file,
269                     uint32_t class_def_index) {
270     ClassAccessor accessor(dex_file, class_def_index);
271     // Note: even if this is an interface or a native class, we still have to walk it, as there
272     //       might be a static initializer.
273     uint32_t class_method_idx = 0;
274     for (const ClassAccessor::Method& method : accessor.GetMethods()) {
275       WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
276                     dex_file,
277                     class_def_index,
278                     method.GetIndex(),
279                     method.GetCodeItem(),
280                     method.GetAccessFlags());
281     }
282   }
283 
WalkOatMethod(const OatFile::OatMethod & oat_method,const DexFile & dex_file,uint32_t class_def_index,uint32_t dex_method_index,const dex::CodeItem * code_item,uint32_t method_access_flags)284   void WalkOatMethod(const OatFile::OatMethod& oat_method,
285                      const DexFile& dex_file,
286                      uint32_t class_def_index,
287                      uint32_t dex_method_index,
288                      const dex::CodeItem* code_item,
289                      uint32_t method_access_flags) {
290     if ((method_access_flags & kAccAbstract) != 0) {
291       // Abstract method, no code.
292       return;
293     }
294     const OatHeader& oat_header = oat_file_->GetOatHeader();
295     const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
296     if (method_header == nullptr || method_header->GetCodeSize() == 0) {
297       // No code.
298       return;
299     }
300 
301     uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
302     // Clear Thumb2 bit.
303     const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
304 
305     debug::MethodDebugInfo info = {};
306     DCHECK(info.custom_name.empty());
307     info.dex_file = &dex_file;
308     info.class_def_index = class_def_index;
309     info.dex_method_index = dex_method_index;
310     info.access_flags = method_access_flags;
311     info.code_item = code_item;
312     info.isa = oat_header.GetInstructionSet();
313     info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
314     info.is_native_debuggable = oat_header.IsNativeDebuggable();
315     info.is_optimized = method_header->IsOptimized();
316     info.is_code_address_text_relative = true;
317     info.code_address = reinterpret_cast<uintptr_t>(code_address);
318     info.code_size = method_header->GetCodeSize();
319     info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
320     info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
321     info.cfi = ArrayRef<uint8_t>();
322     method_debug_infos_.push_back(info);
323   }
324 
325  private:
326   const OatFile* oat_file_;
327   std::unique_ptr<ElfBuilder<ElfTypes>> builder_;
328   std::vector<debug::MethodDebugInfo> method_debug_infos_;
329   std::unordered_set<uint32_t> seen_offsets_;
330   const std::string output_name_;
331   bool no_bits_;
332 };
333 
334 class OatDumperOptions {
335  public:
OatDumperOptions(bool dump_vmap,bool dump_code_info_stack_maps,bool disassemble_code,bool absolute_addresses,const char * class_filter,const char * method_filter,bool list_classes,bool list_methods,bool dump_header_only,const char * export_dex_location,const char * app_image,const char * app_oat,uint32_t addr2instr)336   OatDumperOptions(bool dump_vmap,
337                    bool dump_code_info_stack_maps,
338                    bool disassemble_code,
339                    bool absolute_addresses,
340                    const char* class_filter,
341                    const char* method_filter,
342                    bool list_classes,
343                    bool list_methods,
344                    bool dump_header_only,
345                    const char* export_dex_location,
346                    const char* app_image,
347                    const char* app_oat,
348                    uint32_t addr2instr)
349     : dump_vmap_(dump_vmap),
350       dump_code_info_stack_maps_(dump_code_info_stack_maps),
351       disassemble_code_(disassemble_code),
352       absolute_addresses_(absolute_addresses),
353       class_filter_(class_filter),
354       method_filter_(method_filter),
355       list_classes_(list_classes),
356       list_methods_(list_methods),
357       dump_header_only_(dump_header_only),
358       export_dex_location_(export_dex_location),
359       app_image_(app_image),
360       app_oat_(app_oat),
361       addr2instr_(addr2instr),
362       class_loader_(nullptr) {}
363 
364   const bool dump_vmap_;
365   const bool dump_code_info_stack_maps_;
366   const bool disassemble_code_;
367   const bool absolute_addresses_;
368   const char* const class_filter_;
369   const char* const method_filter_;
370   const bool list_classes_;
371   const bool list_methods_;
372   const bool dump_header_only_;
373   const char* const export_dex_location_;
374   const char* const app_image_;
375   const char* const app_oat_;
376   uint32_t addr2instr_;
377   Handle<mirror::ClassLoader>* class_loader_;
378 };
379 
380 class OatDumper {
381  public:
OatDumper(const OatFile & oat_file,const OatDumperOptions & options)382   OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
383     : oat_file_(oat_file),
384       oat_dex_files_(oat_file.GetOatDexFiles()),
385       options_(options),
386       resolved_addr2instr_(0),
387       instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
388       disassembler_(Disassembler::Create(instruction_set_,
389                                          new DisassemblerOptions(
390                                              options_.absolute_addresses_,
391                                              oat_file.Begin(),
392                                              oat_file.End(),
393                                              /* can_read_literals_= */ true,
394                                              Is64BitInstructionSet(instruction_set_)
395                                                  ? &Thread::DumpThreadOffset<PointerSize::k64>
396                                                  : &Thread::DumpThreadOffset<PointerSize::k32>))) {
397     CHECK(options_.class_loader_ != nullptr);
398     CHECK(options_.class_filter_ != nullptr);
399     CHECK(options_.method_filter_ != nullptr);
400     AddAllOffsets();
401   }
402 
~OatDumper()403   ~OatDumper() {
404     delete disassembler_;
405   }
406 
GetInstructionSet()407   InstructionSet GetInstructionSet() {
408     return instruction_set_;
409   }
410 
411   using DexFileUniqV = std::vector<std::unique_ptr<const DexFile>>;
412 
Dump(std::ostream & os)413   bool Dump(std::ostream& os) {
414     bool success = true;
415     const OatHeader& oat_header = oat_file_.GetOatHeader();
416 
417     os << "MAGIC:\n";
418     os << oat_header.GetMagic() << "\n\n";
419 
420     os << "LOCATION:\n";
421     os << oat_file_.GetLocation() << "\n\n";
422 
423     os << "CHECKSUM:\n";
424     os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
425 
426     os << "INSTRUCTION SET:\n";
427     os << oat_header.GetInstructionSet() << "\n\n";
428 
429     {
430       std::unique_ptr<const InstructionSetFeatures> features(
431           InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
432                                              oat_header.GetInstructionSetFeaturesBitmap()));
433       os << "INSTRUCTION SET FEATURES:\n";
434       os << features->GetFeatureString() << "\n\n";
435     }
436 
437     os << "DEX FILE COUNT:\n";
438     os << oat_header.GetDexFileCount() << "\n\n";
439 
440 #define DUMP_OAT_HEADER_OFFSET(label, offset) \
441     os << label " OFFSET:\n"; \
442     os << StringPrintf("0x%08x", oat_header.offset()); \
443     if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
444       os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
445     } \
446     os << StringPrintf("\n\n");
447 
448     DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
449     DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP TRAMPOLINE",
450                            GetJniDlsymLookupTrampolineOffset);
451     DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP CRITICAL TRAMPOLINE",
452                            GetJniDlsymLookupCriticalTrampolineOffset);
453     DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
454                            GetQuickGenericJniTrampolineOffset);
455     DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
456                            GetQuickImtConflictTrampolineOffset);
457     DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
458                            GetQuickResolutionTrampolineOffset);
459     DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
460                            GetQuickToInterpreterBridgeOffset);
461 #undef DUMP_OAT_HEADER_OFFSET
462 
463     // Print the key-value store.
464     {
465       os << "KEY VALUE STORE:\n";
466       size_t index = 0;
467       const char* key;
468       const char* value;
469       while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
470         os << key << " = " << value << "\n";
471         index++;
472       }
473       os << "\n";
474     }
475 
476     if (options_.absolute_addresses_) {
477       os << "BEGIN:\n";
478       os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
479 
480       os << "END:\n";
481       os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
482     }
483 
484     os << "SIZE:\n";
485     os << oat_file_.Size() << "\n\n";
486 
487     os << std::flush;
488 
489     // If set, adjust relative address to be searched
490     if (options_.addr2instr_ != 0) {
491       resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
492       os << "SEARCH ADDRESS (executable offset + input):\n";
493       os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
494     }
495 
496     // Dump .data.bimg.rel.ro entries.
497     DumpDataBimgRelRoEntries(os);
498 
499     // Dump .bss summary, individual entries are dumped per dex file.
500     os << ".bss: ";
501     if (oat_file_.GetBssMethods().empty() && oat_file_.GetBssGcRoots().empty()) {
502       os << "empty.\n\n";
503     } else {
504       os << oat_file_.GetBssMethods().size() << " methods, ";
505       os << oat_file_.GetBssGcRoots().size() << " GC roots.\n\n";
506     }
507 
508     // Dumping the dex file overview is compact enough to do even if header only.
509     for (size_t i = 0; i < oat_dex_files_.size(); i++) {
510       const OatDexFile* oat_dex_file = oat_dex_files_[i];
511       CHECK(oat_dex_file != nullptr);
512       std::string error_msg;
513       const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
514       if (dex_file == nullptr) {
515         os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
516            << error_msg;
517         continue;
518       }
519 
520       const DexLayoutSections* const layout_sections = oat_dex_file->GetDexLayoutSections();
521       if (layout_sections != nullptr) {
522         os << "Layout data\n";
523         os << *layout_sections;
524         os << "\n";
525       }
526 
527       if (!options_.dump_header_only_) {
528         // Dump .bss entries.
529         DumpBssEntries(
530             os,
531             "ArtMethod",
532             oat_dex_file->GetMethodBssMapping(),
533             dex_file->NumMethodIds(),
534             static_cast<size_t>(GetInstructionSetPointerSize(instruction_set_)),
535             [=](uint32_t index) { return dex_file->PrettyMethod(index); });
536         DumpBssEntries(
537             os,
538             "Class",
539             oat_dex_file->GetTypeBssMapping(),
540             dex_file->NumTypeIds(),
541             sizeof(GcRoot<mirror::Class>),
542             [=](uint32_t index) { return dex_file->PrettyType(dex::TypeIndex(index)); });
543         DumpBssEntries(
544             os,
545             "String",
546             oat_dex_file->GetStringBssMapping(),
547             dex_file->NumStringIds(),
548             sizeof(GcRoot<mirror::Class>),
549             [=](uint32_t index) { return dex_file->StringDataByIdx(dex::StringIndex(index)); });
550       }
551     }
552 
553     if (!options_.dump_header_only_) {
554       VariableIndentationOutputStream vios(&os);
555       VdexFile::VerifierDepsHeader vdex_header = oat_file_.GetVdexFile()->GetVerifierDepsHeader();
556       if (vdex_header.IsValid()) {
557         std::string error_msg;
558         std::vector<const DexFile*> dex_files;
559         for (size_t i = 0; i < oat_dex_files_.size(); i++) {
560           const DexFile* dex_file = OpenDexFile(oat_dex_files_[i], &error_msg);
561           if (dex_file == nullptr) {
562             os << "Error opening dex file: " << error_msg << std::endl;
563             return false;
564           }
565           dex_files.push_back(dex_file);
566         }
567         verifier::VerifierDeps deps(dex_files, /*output_only=*/ false);
568         if (!deps.ParseStoredData(dex_files, oat_file_.GetVdexFile()->GetVerifierDepsData())) {
569           os << "Error parsing verifier dependencies." << std::endl;
570           return false;
571         }
572         deps.Dump(&vios);
573       } else {
574         os << "UNRECOGNIZED vdex file, magic "
575            << vdex_header.GetMagic()
576            << ", verifier deps version "
577            << vdex_header.GetVerifierDepsVersion()
578            << ", dex section version "
579            << vdex_header.GetDexSectionVersion()
580            << "\n";
581       }
582       for (size_t i = 0; i < oat_dex_files_.size(); i++) {
583         const OatDexFile* oat_dex_file = oat_dex_files_[i];
584         CHECK(oat_dex_file != nullptr);
585         if (!DumpOatDexFile(os, *oat_dex_file)) {
586           success = false;
587         }
588       }
589     }
590 
591     if (options_.export_dex_location_) {
592       std::string error_msg;
593       std::string vdex_filename = GetVdexFilename(oat_file_.GetLocation());
594       if (!OS::FileExists(vdex_filename.c_str())) {
595         os << "File " << vdex_filename.c_str() << " does not exist\n";
596         return false;
597       }
598 
599       DexFileUniqV vdex_dex_files;
600       std::unique_ptr<const VdexFile> vdex_file = OpenVdexUnquicken(vdex_filename,
601                                                                     &vdex_dex_files,
602                                                                     &error_msg);
603       if (vdex_file.get() == nullptr) {
604         os << "Failed to open vdex file: " << error_msg << "\n";
605         return false;
606       }
607       if (oat_dex_files_.size() != vdex_dex_files.size()) {
608         os << "Dex files number in Vdex file does not match Dex files number in Oat file: "
609            << vdex_dex_files.size() << " vs " << oat_dex_files_.size() << '\n';
610         return false;
611       }
612 
613       size_t i = 0;
614       for (const auto& vdex_dex_file : vdex_dex_files) {
615         const OatDexFile* oat_dex_file = oat_dex_files_[i];
616         CHECK(oat_dex_file != nullptr);
617         CHECK(vdex_dex_file != nullptr);
618 
619         // If a CompactDex file is detected within a Vdex container, DexLayout is used to convert
620         // back to a StandardDex file. Since the converted DexFile will most likely not reproduce
621         // the original input Dex file, the `update_checksum_` option is used to recompute the
622         // checksum. If the vdex container does not contain cdex resources (`used_dexlayout` is
623         // false), ExportDexFile() enforces a reproducible checksum verification.
624         if (vdex_dex_file->IsCompactDexFile()) {
625           Options options;
626           options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone;
627           options.update_checksum_ = true;
628           DexLayout dex_layout(options, /*info=*/ nullptr, /*out_file=*/ nullptr, /*header=*/ nullptr);
629           std::unique_ptr<art::DexContainer> dex_container;
630           bool result = dex_layout.ProcessDexFile(vdex_dex_file->GetLocation().c_str(),
631                                                   vdex_dex_file.get(),
632                                                   i,
633                                                   &dex_container,
634                                                   &error_msg);
635           if (!result) {
636             os << "DexLayout failed to process Dex file: " + error_msg;
637             success = false;
638             break;
639           }
640           DexContainer::Section* main_section = dex_container->GetMainSection();
641           CHECK_EQ(dex_container->GetDataSection()->Size(), 0u);
642 
643           const ArtDexFileLoader dex_file_loader;
644           std::unique_ptr<const DexFile> dex(dex_file_loader.Open(
645               main_section->Begin(),
646               main_section->Size(),
647               vdex_dex_file->GetLocation(),
648               vdex_file->GetLocationChecksum(i),
649               /*oat_dex_file=*/ nullptr,
650               /*verify=*/ false,
651               /*verify_checksum=*/ true,
652               &error_msg));
653           if (dex == nullptr) {
654             os << "Failed to load DexFile from layout container: " + error_msg;
655             success = false;
656             break;
657           }
658           if (dex->IsCompactDexFile()) {
659             os <<"CompactDex conversion to StandardDex failed";
660             success = false;
661             break;
662           }
663 
664           if (!ExportDexFile(os, *oat_dex_file, dex.get(), /*used_dexlayout=*/ true)) {
665             success = false;
666             break;
667           }
668         } else {
669           if (!ExportDexFile(os, *oat_dex_file, vdex_dex_file.get(), /*used_dexlayout=*/ false)) {
670             success = false;
671             break;
672           }
673         }
674         i++;
675       }
676     }
677 
678     {
679       os << "OAT FILE STATS:\n";
680       VariableIndentationOutputStream vios(&os);
681       stats_.AddBytes(oat_file_.Size());
682       DumpStats(vios, "OatFile", stats_, stats_.Value());
683     }
684 
685     os << std::flush;
686     return success;
687   }
688 
ComputeSize(const void * oat_data)689   size_t ComputeSize(const void* oat_data) {
690     if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
691         reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
692       return 0;  // Address not in oat file
693     }
694     uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
695                              reinterpret_cast<uintptr_t>(oat_file_.Begin());
696     auto it = offsets_.upper_bound(begin_offset);
697     CHECK(it != offsets_.end());
698     uintptr_t end_offset = *it;
699     return end_offset - begin_offset;
700   }
701 
GetOatInstructionSet()702   InstructionSet GetOatInstructionSet() {
703     return oat_file_.GetOatHeader().GetInstructionSet();
704   }
705 
GetQuickOatCode(ArtMethod * m)706   const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
707     for (size_t i = 0; i < oat_dex_files_.size(); i++) {
708       const OatDexFile* oat_dex_file = oat_dex_files_[i];
709       CHECK(oat_dex_file != nullptr);
710       std::string error_msg;
711       const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
712       if (dex_file == nullptr) {
713         LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
714             << "': " << error_msg;
715       } else {
716         const char* descriptor = m->GetDeclaringClassDescriptor();
717         const dex::ClassDef* class_def =
718             OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
719         if (class_def != nullptr) {
720           uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
721           const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
722           uint32_t oat_method_index;
723           if (m->IsStatic() || m->IsDirect()) {
724             // Simple case where the oat method index was stashed at load time.
725             oat_method_index = m->GetMethodIndex();
726           } else {
727             // Compute the oat_method_index by search for its position in the class def.
728             ClassAccessor accessor(*dex_file, *class_def);
729             oat_method_index = accessor.NumDirectMethods();
730             bool found_virtual = false;
731             for (ClassAccessor::Method dex_method : accessor.GetVirtualMethods()) {
732               // Check method index instead of identity in case of duplicate method definitions.
733               if (dex_method.GetIndex() == m->GetDexMethodIndex()) {
734                 found_virtual = true;
735                 break;
736               }
737               ++oat_method_index;
738             }
739             CHECK(found_virtual) << "Didn't find oat method index for virtual method: "
740                                  << dex_file->PrettyMethod(m->GetDexMethodIndex());
741           }
742           return oat_class.GetOatMethod(oat_method_index).GetQuickCode();
743         }
744       }
745     }
746     return nullptr;
747   }
748 
749   // Returns nullptr and updates error_msg if the Vdex file cannot be opened, otherwise all Dex
750   // files are fully unquickened and stored in dex_files
OpenVdexUnquicken(const std::string & vdex_filename,DexFileUniqV * dex_files,std::string * error_msg)751   std::unique_ptr<const VdexFile> OpenVdexUnquicken(const std::string& vdex_filename,
752                                                     /* out */ DexFileUniqV* dex_files,
753                                                     /* out */ std::string* error_msg) {
754     std::unique_ptr<const File> file(OS::OpenFileForReading(vdex_filename.c_str()));
755     if (file == nullptr) {
756       *error_msg = "Could not open file " + vdex_filename + " for reading.";
757       return nullptr;
758     }
759 
760     int64_t vdex_length = file->GetLength();
761     if (vdex_length == -1) {
762       *error_msg = "Could not read the length of file " + vdex_filename;
763       return nullptr;
764     }
765 
766     MemMap mmap = MemMap::MapFile(
767         file->GetLength(),
768         PROT_READ | PROT_WRITE,
769         MAP_PRIVATE,
770         file->Fd(),
771         /* start offset= */ 0,
772         /* low_4gb= */ false,
773         vdex_filename.c_str(),
774         error_msg);
775     if (!mmap.IsValid()) {
776       *error_msg = "Failed to mmap file " + vdex_filename + ": " + *error_msg;
777       return nullptr;
778     }
779 
780     std::unique_ptr<VdexFile> vdex_file(new VdexFile(std::move(mmap)));
781     if (!vdex_file->IsValid()) {
782       *error_msg = "Vdex file is not valid";
783       return nullptr;
784     }
785 
786     DexFileUniqV tmp_dex_files;
787     if (!vdex_file->OpenAllDexFiles(&tmp_dex_files, error_msg)) {
788       *error_msg = "Failed to open Dex files from Vdex: " + *error_msg;
789       return nullptr;
790     }
791 
792     vdex_file->Unquicken(MakeNonOwningPointerVector(tmp_dex_files),
793                          /* decompile_return_instruction= */ true);
794 
795     *dex_files = std::move(tmp_dex_files);
796     return vdex_file;
797   }
798 
AddStatsObject(const void * address)799   bool AddStatsObject(const void* address) {
800     return seen_stats_objects_.insert(address).second;  // Inserted new entry.
801   }
802 
DumpStats(VariableIndentationOutputStream & os,const std::string & name,const Stats & stats,double total)803   void DumpStats(VariableIndentationOutputStream& os,
804                  const std::string& name,
805                  const Stats& stats,
806                  double total) {
807     if (std::fabs(stats.Value()) > 0 || !stats.Children().empty()) {
808       double percent = 100.0 * stats.Value() / total;
809       os.Stream()
810           << std::setw(40 - os.GetIndentation()) << std::left << name << std::right << " "
811           << std::setw(8) << stats.Count() << " "
812           << std::setw(12) << std::fixed << std::setprecision(3) << stats.Value() / KB << "KB "
813           << std::setw(8) << std::fixed << std::setprecision(1) << percent << "%\n";
814 
815       // Sort all children by largest value first, than by name.
816       std::map<std::pair<double, std::string>, const Stats&> sorted_children;
817       for (const auto& it : stats.Children()) {
818         sorted_children.emplace(std::make_pair(-it.second.Value(), it.first), it.second);
819       }
820 
821       // Add "other" row to represent any amount not account for by the children.
822       Stats other;
823       other.AddBytes(stats.Value() - stats.SumChildrenValues(), stats.Count());
824       if (std::fabs(other.Value()) > 0 && !stats.Children().empty()) {
825         sorted_children.emplace(std::make_pair(-other.Value(), "(other)"), other);
826       }
827 
828       // Print the data.
829       ScopedIndentation indent1(&os);
830       for (const auto& it : sorted_children) {
831         DumpStats(os, it.first.second, it.second, total);
832       }
833     }
834   }
835 
836  private:
AddAllOffsets()837   void AddAllOffsets() {
838     // We don't know the length of the code for each method, but we need to know where to stop
839     // when disassembling. What we do know is that a region of code will be followed by some other
840     // region, so if we keep a sorted sequence of the start of each region, we can infer the length
841     // of a piece of code by using upper_bound to find the start of the next region.
842     for (size_t i = 0; i < oat_dex_files_.size(); i++) {
843       const OatDexFile* oat_dex_file = oat_dex_files_[i];
844       CHECK(oat_dex_file != nullptr);
845       std::string error_msg;
846       const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
847       if (dex_file == nullptr) {
848         LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
849             << "': " << error_msg;
850         continue;
851       }
852       offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
853       for (ClassAccessor accessor : dex_file->GetClasses()) {
854         const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(accessor.GetClassDefIndex());
855         for (uint32_t class_method_index = 0;
856             class_method_index < accessor.NumMethods();
857             ++class_method_index) {
858           AddOffsets(oat_class.GetOatMethod(class_method_index));
859         }
860       }
861     }
862 
863     // If the last thing in the file is code for a method, there won't be an offset for the "next"
864     // thing. Instead of having a special case in the upper_bound code, let's just add an entry
865     // for the end of the file.
866     offsets_.insert(oat_file_.Size());
867   }
868 
AlignCodeOffset(uint32_t maybe_thumb_offset)869   static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
870     return maybe_thumb_offset & ~0x1;  // TODO: Make this Thumb2 specific.
871   }
872 
AddOffsets(const OatFile::OatMethod & oat_method)873   void AddOffsets(const OatFile::OatMethod& oat_method) {
874     uint32_t code_offset = oat_method.GetCodeOffset();
875     if (oat_file_.GetOatHeader().GetInstructionSet() == InstructionSet::kThumb2) {
876       code_offset &= ~0x1;
877     }
878     offsets_.insert(code_offset);
879     offsets_.insert(oat_method.GetVmapTableOffset());
880   }
881 
DumpOatDexFile(std::ostream & os,const OatDexFile & oat_dex_file)882   bool DumpOatDexFile(std::ostream& os, const OatDexFile& oat_dex_file) {
883     bool success = true;
884     bool stop_analysis = false;
885     os << "OatDexFile:\n";
886     os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
887     os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
888 
889     const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
890     if (oat_dex_file.GetOatFile()->ContainsDexCode()) {
891       const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
892 
893       // Print data range of the dex file embedded inside the corresponding vdex file.
894       const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
895       uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
896       os << StringPrintf(
897           "dex-file: 0x%08x..0x%08x\n",
898           dex_offset,
899           dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
900     } else {
901       os << StringPrintf("dex-file not in VDEX file\n");
902     }
903 
904     // Create the dex file early. A lot of print-out things depend on it.
905     std::string error_msg;
906     const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
907     if (dex_file == nullptr) {
908       os << "NOT FOUND: " << error_msg << "\n\n";
909       os << std::flush;
910       return false;
911     }
912 
913     // Print lookup table, if it exists.
914     if (oat_dex_file.GetLookupTableData() != nullptr) {
915       uint32_t table_offset = dchecked_integral_cast<uint32_t>(
916           oat_dex_file.GetLookupTableData() - oat_file_begin);
917       uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
918       os << StringPrintf("type-table: 0x%08x..0x%08x\n",
919                          table_offset,
920                          table_offset + table_size - 1);
921     }
922 
923     VariableIndentationOutputStream vios(&os);
924     ScopedIndentation indent1(&vios);
925     for (ClassAccessor accessor : dex_file->GetClasses()) {
926       // TODO: Support regex
927       const char* descriptor = accessor.GetDescriptor();
928       if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
929         continue;
930       }
931 
932       const uint16_t class_def_index = accessor.GetClassDefIndex();
933       uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
934       const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
935       os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
936                          static_cast<ssize_t>(class_def_index),
937                          descriptor,
938                          oat_class_offset,
939                          accessor.GetClassIdx().index_)
940          << " (" << oat_class.GetStatus() << ")"
941          << " (" << oat_class.GetType() << ")\n";
942       // TODO: include bitmap here if type is kOatClassSomeCompiled?
943       if (options_.list_classes_) {
944         continue;
945       }
946       if (!DumpOatClass(&vios, oat_class, *dex_file, accessor, &stop_analysis)) {
947         success = false;
948       }
949       if (stop_analysis) {
950         os << std::flush;
951         return success;
952       }
953     }
954     os << "\n";
955     os << std::flush;
956     return success;
957   }
958 
959   // Backwards compatible Dex file export. If dex_file is nullptr (valid Vdex file not present) the
960   // Dex resource is extracted from the oat_dex_file and its checksum is repaired since it's not
961   // unquickened. Otherwise the dex_file has been fully unquickened and is expected to verify the
962   // original checksum.
ExportDexFile(std::ostream & os,const OatDexFile & oat_dex_file,const DexFile * dex_file,bool used_dexlayout)963   bool ExportDexFile(std::ostream& os,
964                      const OatDexFile& oat_dex_file,
965                      const DexFile* dex_file,
966                      bool used_dexlayout) {
967     std::string error_msg;
968     std::string dex_file_location = oat_dex_file.GetDexFileLocation();
969 
970     // If dex_file (from unquicken or dexlayout) is not available, the output DexFile size is the
971     // same as the one extracted from the Oat container (pre-oreo)
972     size_t fsize = dex_file == nullptr ? oat_dex_file.FileSize() : dex_file->Size();
973 
974     // Some quick checks just in case
975     if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
976       os << "Invalid dex file\n";
977       return false;
978     }
979 
980     if (dex_file == nullptr) {
981       // Exported bytecode is quickened (dex-to-dex transformations present)
982       dex_file = OpenDexFile(&oat_dex_file, &error_msg);
983       if (dex_file == nullptr) {
984         os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
985         return false;
986       }
987 
988       // Recompute checksum
989       reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
990           dex_file->CalculateChecksum();
991     } else {
992       // If dexlayout was used to convert CompactDex back to StandardDex, checksum will be updated
993       // due to `update_checksum_` option, otherwise we expect a reproducible checksum.
994       if (!used_dexlayout) {
995         // Vdex unquicken output should match original input bytecode
996         uint32_t orig_checksum =
997             reinterpret_cast<DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_;
998         if (orig_checksum != dex_file->CalculateChecksum()) {
999           os << "Unexpected checksum from unquicken dex file '" << dex_file_location << "'\n";
1000           return false;
1001         }
1002       }
1003     }
1004 
1005     // Verify output directory exists
1006     if (!OS::DirectoryExists(options_.export_dex_location_)) {
1007       // TODO: Extend OS::DirectoryExists if symlink support is required
1008       os << options_.export_dex_location_ << " output directory not found or symlink\n";
1009       return false;
1010     }
1011 
1012     // Beautify path names
1013     if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
1014       return false;
1015     }
1016 
1017     std::string dex_orig_name;
1018     size_t dex_orig_pos = dex_file_location.rfind('/');
1019     if (dex_orig_pos == std::string::npos)
1020       dex_orig_name = dex_file_location;
1021     else
1022       dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
1023 
1024     // A more elegant approach to efficiently name user installed apps is welcome
1025     if (dex_orig_name.size() == 8 &&
1026         dex_orig_name.compare("base.apk") == 0 &&
1027         dex_orig_pos != std::string::npos) {
1028       dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
1029       size_t apk_orig_pos = dex_file_location.rfind('/');
1030       if (apk_orig_pos != std::string::npos) {
1031         dex_orig_name = dex_file_location.substr(++apk_orig_pos);
1032       }
1033     }
1034 
1035     std::string out_dex_path(options_.export_dex_location_);
1036     if (out_dex_path.back() != '/') {
1037       out_dex_path.append("/");
1038     }
1039     out_dex_path.append(dex_orig_name);
1040     out_dex_path.append("_export.dex");
1041     if (out_dex_path.length() > PATH_MAX) {
1042       return false;
1043     }
1044 
1045     std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
1046     if (file.get() == nullptr) {
1047       os << "Failed to open output dex file " << out_dex_path;
1048       return false;
1049     }
1050 
1051     bool success = file->WriteFully(dex_file->Begin(), fsize);
1052     if (!success) {
1053       os << "Failed to write dex file";
1054       file->Erase();
1055       return false;
1056     }
1057 
1058     if (file->FlushCloseOrErase() != 0) {
1059       os << "Flush and close failed";
1060       return false;
1061     }
1062 
1063     os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
1064     os << std::flush;
1065 
1066     return true;
1067   }
1068 
DumpOatClass(VariableIndentationOutputStream * vios,const OatFile::OatClass & oat_class,const DexFile & dex_file,const ClassAccessor & class_accessor,bool * stop_analysis)1069   bool DumpOatClass(VariableIndentationOutputStream* vios,
1070                     const OatFile::OatClass& oat_class,
1071                     const DexFile& dex_file,
1072                     const ClassAccessor& class_accessor,
1073                     bool* stop_analysis) {
1074     bool success = true;
1075     bool addr_found = false;
1076     uint32_t class_method_index = 0;
1077     for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
1078       if (!DumpOatMethod(vios,
1079                          dex_file.GetClassDef(class_accessor.GetClassDefIndex()),
1080                          class_method_index,
1081                          oat_class,
1082                          dex_file,
1083                          method.GetIndex(),
1084                          method.GetCodeItem(),
1085                          method.GetAccessFlags(),
1086                          &addr_found)) {
1087         success = false;
1088       }
1089       if (addr_found) {
1090         *stop_analysis = true;
1091         return success;
1092       }
1093       class_method_index++;
1094     }
1095     vios->Stream() << std::flush;
1096     return success;
1097   }
1098 
1099   static constexpr uint32_t kPrologueBytes = 16;
1100 
1101   // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
1102   static constexpr uint32_t kMaxCodeSize = 100 * 1000;
1103 
DumpOatMethod(VariableIndentationOutputStream * vios,const dex::ClassDef & class_def,uint32_t class_method_index,const OatFile::OatClass & oat_class,const DexFile & dex_file,uint32_t dex_method_idx,const dex::CodeItem * code_item,uint32_t method_access_flags,bool * addr_found)1104   bool DumpOatMethod(VariableIndentationOutputStream* vios,
1105                      const dex::ClassDef& class_def,
1106                      uint32_t class_method_index,
1107                      const OatFile::OatClass& oat_class,
1108                      const DexFile& dex_file,
1109                      uint32_t dex_method_idx,
1110                      const dex::CodeItem* code_item,
1111                      uint32_t method_access_flags,
1112                      bool* addr_found) {
1113     bool success = true;
1114 
1115     CodeItemDataAccessor code_item_accessor(dex_file, code_item);
1116 
1117     // TODO: Support regex
1118     std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
1119     if (method_name.find(options_.method_filter_) == std::string::npos) {
1120       return success;
1121     }
1122 
1123     std::string pretty_method = dex_file.PrettyMethod(dex_method_idx, true);
1124     vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
1125                                    class_method_index, pretty_method.c_str(),
1126                                    dex_method_idx);
1127     if (options_.list_methods_) {
1128       return success;
1129     }
1130 
1131     uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
1132     const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
1133     const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
1134     uint32_t code_offset = oat_method.GetCodeOffset();
1135     uint32_t code_size = oat_method.GetQuickCodeSize();
1136     if (resolved_addr2instr_ != 0) {
1137       if (resolved_addr2instr_ > code_offset + code_size) {
1138         return success;
1139       } else {
1140         *addr_found = true;  // stop analyzing file at next iteration
1141       }
1142     }
1143 
1144     // Everything below is indented at least once.
1145     ScopedIndentation indent1(vios);
1146 
1147     {
1148       vios->Stream() << "DEX CODE:\n";
1149       ScopedIndentation indent2(vios);
1150       if (code_item_accessor.HasCodeItem()) {
1151         for (const DexInstructionPcPair& inst : code_item_accessor) {
1152           vios->Stream() << StringPrintf("0x%04x: ", inst.DexPc()) << inst->DumpHexLE(5)
1153                          << StringPrintf("\t| %s\n", inst->DumpString(&dex_file).c_str());
1154         }
1155       }
1156     }
1157 
1158     std::unique_ptr<StackHandleScope<1>> hs;
1159     std::unique_ptr<verifier::MethodVerifier> verifier;
1160     if (Runtime::Current() != nullptr) {
1161       // We need to have the handle scope stay live until after the verifier since the verifier has
1162       // a handle to the dex cache from hs.
1163       hs.reset(new StackHandleScope<1>(Thread::Current()));
1164       vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
1165       ScopedIndentation indent2(vios);
1166       verifier.reset(DumpVerifier(vios, hs.get(),
1167                                   dex_method_idx, &dex_file, class_def, code_item,
1168                                   method_access_flags));
1169     }
1170     {
1171       vios->Stream() << "OatMethodOffsets ";
1172       if (options_.absolute_addresses_) {
1173         vios->Stream() << StringPrintf("%p ", oat_method_offsets);
1174       }
1175       vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
1176       if (oat_method_offsets_offset > oat_file_.Size()) {
1177         vios->Stream() << StringPrintf(
1178             "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
1179             oat_method_offsets_offset, oat_file_.Size());
1180         // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
1181         vios->Stream() << std::flush;
1182         return false;
1183       }
1184 
1185       ScopedIndentation indent2(vios);
1186       vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
1187       uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
1188       if (aligned_code_begin > oat_file_.Size()) {
1189         vios->Stream() << StringPrintf("WARNING: "
1190                                        "code offset 0x%08x is past end of file 0x%08zx.\n",
1191                                        aligned_code_begin, oat_file_.Size());
1192         success = false;
1193       }
1194       vios->Stream() << "\n";
1195     }
1196     {
1197       vios->Stream() << "OatQuickMethodHeader ";
1198       uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1199       const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
1200       if (AddStatsObject(method_header)) {
1201         stats_.Child("QuickMethodHeader")->AddBytes(sizeof(*method_header));
1202       }
1203       if (options_.absolute_addresses_) {
1204         vios->Stream() << StringPrintf("%p ", method_header);
1205       }
1206       vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
1207       if (method_header_offset > oat_file_.Size()) {
1208         vios->Stream() << StringPrintf(
1209             "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1210             method_header_offset, oat_file_.Size());
1211         // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
1212         vios->Stream() << std::flush;
1213         return false;
1214       }
1215 
1216       ScopedIndentation indent2(vios);
1217       vios->Stream() << "vmap_table: ";
1218       if (options_.absolute_addresses_) {
1219         vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
1220       }
1221       uint32_t vmap_table_offset = method_header ==
1222           nullptr ? 0 : method_header->GetVmapTableOffset();
1223       vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
1224 
1225       size_t vmap_table_offset_limit =
1226           IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)
1227               ? oat_file_.GetVdexFile()->Size()
1228               : method_header->GetCode() - oat_file_.Begin();
1229       if (vmap_table_offset >= vmap_table_offset_limit) {
1230         vios->Stream() << StringPrintf("WARNING: "
1231                                        "vmap table offset 0x%08x is past end of file 0x%08zx. "
1232                                        "vmap table offset was loaded from offset 0x%08x.\n",
1233                                        vmap_table_offset,
1234                                        vmap_table_offset_limit,
1235                                        oat_method.GetVmapTableOffsetOffset());
1236         success = false;
1237       } else if (options_.dump_vmap_) {
1238         DumpVmapData(vios, oat_method, code_item_accessor);
1239       }
1240     }
1241     {
1242       vios->Stream() << "QuickMethodFrameInfo\n";
1243 
1244       ScopedIndentation indent2(vios);
1245       vios->Stream()
1246           << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1247       vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1248       DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1249       vios->Stream() << "\n";
1250       vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1251       DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1252       vios->Stream() << "\n";
1253     }
1254     {
1255       // Based on spill masks from QuickMethodFrameInfo so placed
1256       // after it is dumped, but useful for understanding quick
1257       // code, so dumped here.
1258       ScopedIndentation indent2(vios);
1259       DumpVregLocations(vios->Stream(), oat_method, code_item_accessor);
1260     }
1261     {
1262       vios->Stream() << "CODE: ";
1263       uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1264       if (code_size_offset > oat_file_.Size()) {
1265         ScopedIndentation indent2(vios);
1266         vios->Stream() << StringPrintf("WARNING: "
1267                                        "code size offset 0x%08x is past end of file 0x%08zx.",
1268                                        code_size_offset, oat_file_.Size());
1269         success = false;
1270       } else {
1271         const void* code = oat_method.GetQuickCode();
1272         uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1273         uint64_t aligned_code_end = aligned_code_begin + code_size;
1274         if (AddStatsObject(code)) {
1275           stats_.Child("Code")->AddBytes(code_size);
1276         }
1277 
1278         if (options_.absolute_addresses_) {
1279           vios->Stream() << StringPrintf("%p ", code);
1280         }
1281         vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1282                                        code_offset,
1283                                        code_size_offset,
1284                                        code_size,
1285                                        code != nullptr ? "..." : "");
1286 
1287         ScopedIndentation indent2(vios);
1288         if (aligned_code_begin > oat_file_.Size()) {
1289           vios->Stream() << StringPrintf("WARNING: "
1290                                          "start of code at 0x%08x is past end of file 0x%08zx.",
1291                                          aligned_code_begin, oat_file_.Size());
1292           success = false;
1293         } else if (aligned_code_end > oat_file_.Size()) {
1294           vios->Stream() << StringPrintf(
1295               "WARNING: "
1296               "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1297               "code size is 0x%08x loaded from offset 0x%08x.\n",
1298               aligned_code_end, oat_file_.Size(),
1299               code_size, code_size_offset);
1300           success = false;
1301           if (options_.disassemble_code_) {
1302             if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1303               DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1304             }
1305           }
1306         } else if (code_size > kMaxCodeSize) {
1307           vios->Stream() << StringPrintf(
1308               "WARNING: "
1309               "code size %d is bigger than max expected threshold of %d. "
1310               "code size is 0x%08x loaded from offset 0x%08x.\n",
1311               code_size, kMaxCodeSize,
1312               code_size, code_size_offset);
1313           success = false;
1314           if (options_.disassemble_code_) {
1315             if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
1316               DumpCode(vios, oat_method, code_item_accessor, true, kPrologueBytes);
1317             }
1318           }
1319         } else if (options_.disassemble_code_) {
1320           DumpCode(vios, oat_method, code_item_accessor, !success, 0);
1321         }
1322       }
1323     }
1324     vios->Stream() << std::flush;
1325     return success;
1326   }
1327 
DumpSpillMask(std::ostream & os,uint32_t spill_mask,bool is_float)1328   void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1329     if (spill_mask == 0) {
1330       return;
1331     }
1332     os << "(";
1333     for (size_t i = 0; i < 32; i++) {
1334       if ((spill_mask & (1 << i)) != 0) {
1335         if (is_float) {
1336           os << "fr" << i;
1337         } else {
1338           os << "r" << i;
1339         }
1340         spill_mask ^= 1 << i;  // clear bit
1341         if (spill_mask != 0) {
1342           os << ", ";
1343         } else {
1344           break;
1345         }
1346       }
1347     }
1348     os << ")";
1349   }
1350 
1351   // Display data stored at the the vmap offset of an oat method.
DumpVmapData(VariableIndentationOutputStream * vios,const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor)1352   void DumpVmapData(VariableIndentationOutputStream* vios,
1353                     const OatFile::OatMethod& oat_method,
1354                     const CodeItemDataAccessor& code_item_accessor) {
1355     if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item_accessor)) {
1356       // The optimizing compiler outputs its CodeInfo data in the vmap table.
1357       const uint8_t* raw_code_info = oat_method.GetVmapTable();
1358       if (raw_code_info != nullptr) {
1359         CodeInfo code_info(raw_code_info);
1360         DCHECK(code_item_accessor.HasCodeItem());
1361         ScopedIndentation indent1(vios);
1362         DumpCodeInfo(vios, code_info, oat_method);
1363       }
1364     } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item_accessor)) {
1365       // We don't encode the size in the table, so just emit that we have quickened
1366       // information.
1367       ScopedIndentation indent(vios);
1368       vios->Stream() << "quickened data\n";
1369     } else {
1370       // Otherwise, there is nothing to display.
1371     }
1372   }
1373 
1374   // Display a CodeInfo object emitted by the optimizing compiler.
DumpCodeInfo(VariableIndentationOutputStream * vios,const CodeInfo & code_info,const OatFile::OatMethod & oat_method)1375   void DumpCodeInfo(VariableIndentationOutputStream* vios,
1376                     const CodeInfo& code_info,
1377                     const OatFile::OatMethod& oat_method) {
1378     code_info.Dump(vios,
1379                    oat_method.GetCodeOffset(),
1380                    options_.dump_code_info_stack_maps_,
1381                    instruction_set_);
1382   }
1383 
GetOutVROffset(uint16_t out_num,InstructionSet isa)1384   static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
1385     // According to stack model, the first out is above the Method referernce.
1386     return static_cast<size_t>(InstructionSetPointerSize(isa)) + out_num * sizeof(uint32_t);
1387   }
1388 
GetVRegOffsetFromQuickCode(const CodeItemDataAccessor & code_item_accessor,uint32_t core_spills,uint32_t fp_spills,size_t frame_size,int reg,InstructionSet isa)1389   static uint32_t GetVRegOffsetFromQuickCode(const CodeItemDataAccessor& code_item_accessor,
1390                                              uint32_t core_spills,
1391                                              uint32_t fp_spills,
1392                                              size_t frame_size,
1393                                              int reg,
1394                                              InstructionSet isa) {
1395     PointerSize pointer_size = InstructionSetPointerSize(isa);
1396     if (kIsDebugBuild) {
1397       auto* runtime = Runtime::Current();
1398       if (runtime != nullptr) {
1399         CHECK_EQ(runtime->GetClassLinker()->GetImagePointerSize(), pointer_size);
1400       }
1401     }
1402     DCHECK_ALIGNED(frame_size, kStackAlignment);
1403     DCHECK_NE(reg, -1);
1404     int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
1405         + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
1406         + sizeof(uint32_t);  // Filler.
1407     int num_regs = code_item_accessor.RegistersSize() - code_item_accessor.InsSize();
1408     int temp_threshold = code_item_accessor.RegistersSize();
1409     const int max_num_special_temps = 1;
1410     if (reg == temp_threshold) {
1411       // The current method pointer corresponds to special location on stack.
1412       return 0;
1413     } else if (reg >= temp_threshold + max_num_special_temps) {
1414       /*
1415        * Special temporaries may have custom locations and the logic above deals with that.
1416        * However, non-special temporaries are placed relative to the outs.
1417        */
1418       int temps_start = code_item_accessor.OutsSize() * sizeof(uint32_t)
1419           + static_cast<size_t>(pointer_size) /* art method */;
1420       int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
1421       return temps_start + relative_offset;
1422     } else if (reg < num_regs) {
1423       int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
1424       return locals_start + (reg * sizeof(uint32_t));
1425     } else {
1426       // Handle ins.
1427       return frame_size + ((reg - num_regs) * sizeof(uint32_t))
1428           + static_cast<size_t>(pointer_size) /* art method */;
1429     }
1430   }
1431 
DumpVregLocations(std::ostream & os,const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor)1432   void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1433                          const CodeItemDataAccessor& code_item_accessor) {
1434     if (code_item_accessor.HasCodeItem()) {
1435       size_t num_locals_ins = code_item_accessor.RegistersSize();
1436       size_t num_ins = code_item_accessor.InsSize();
1437       size_t num_locals = num_locals_ins - num_ins;
1438       size_t num_outs = code_item_accessor.OutsSize();
1439 
1440       os << "vr_stack_locations:";
1441       for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1442         // For readability, delimit the different kinds of VRs.
1443         if (reg == num_locals_ins) {
1444           os << "\n\tmethod*:";
1445         } else if (reg == num_locals && num_ins > 0) {
1446           os << "\n\tins:";
1447         } else if (reg == 0 && num_locals > 0) {
1448           os << "\n\tlocals:";
1449         }
1450 
1451         uint32_t offset = GetVRegOffsetFromQuickCode(code_item_accessor,
1452                                                      oat_method.GetCoreSpillMask(),
1453                                                      oat_method.GetFpSpillMask(),
1454                                                      oat_method.GetFrameSizeInBytes(),
1455                                                      reg,
1456                                                      GetInstructionSet());
1457         os << " v" << reg << "[sp + #" << offset << "]";
1458       }
1459 
1460       for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1461         if (out_reg == 0) {
1462           os << "\n\touts:";
1463         }
1464 
1465         uint32_t offset = GetOutVROffset(out_reg, GetInstructionSet());
1466         os << " v" << out_reg << "[sp + #" << offset << "]";
1467       }
1468 
1469       os << "\n";
1470     }
1471   }
1472 
1473   // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1474   // the optimizing compiler?
IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor)1475   static bool IsMethodGeneratedByOptimizingCompiler(
1476       const OatFile::OatMethod& oat_method,
1477       const CodeItemDataAccessor& code_item_accessor) {
1478     // If the native GC map is null and the Dex `code_item` is not
1479     // null, then this method has been compiled with the optimizing
1480     // compiler.
1481     return oat_method.GetQuickCode() != nullptr &&
1482            oat_method.GetVmapTable() != nullptr &&
1483            code_item_accessor.HasCodeItem();
1484   }
1485 
1486   // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1487   // the dextodex compiler?
IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor)1488   static bool IsMethodGeneratedByDexToDexCompiler(
1489       const OatFile::OatMethod& oat_method,
1490       const CodeItemDataAccessor& code_item_accessor) {
1491     // If the quick code is null, the Dex `code_item` is not
1492     // null, and the vmap table is not null, then this method has been compiled
1493     // with the dextodex compiler.
1494     return oat_method.GetQuickCode() == nullptr &&
1495            oat_method.GetVmapTable() != nullptr &&
1496            code_item_accessor.HasCodeItem();
1497   }
1498 
DumpVerifier(VariableIndentationOutputStream * vios,StackHandleScope<1> * hs,uint32_t dex_method_idx,const DexFile * dex_file,const dex::ClassDef & class_def,const dex::CodeItem * code_item,uint32_t method_access_flags)1499   verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
1500                                          StackHandleScope<1>* hs,
1501                                          uint32_t dex_method_idx,
1502                                          const DexFile* dex_file,
1503                                          const dex::ClassDef& class_def,
1504                                          const dex::CodeItem* code_item,
1505                                          uint32_t method_access_flags) {
1506     if ((method_access_flags & kAccNative) == 0) {
1507       ScopedObjectAccess soa(Thread::Current());
1508       Runtime* const runtime = Runtime::Current();
1509       DCHECK(options_.class_loader_ != nullptr);
1510       Handle<mirror::DexCache> dex_cache = hs->NewHandle(
1511           runtime->GetClassLinker()->RegisterDexFile(*dex_file, options_.class_loader_->Get()));
1512       CHECK(dex_cache != nullptr);
1513       ArtMethod* method = runtime->GetClassLinker()->ResolveMethodWithoutInvokeType(
1514           dex_method_idx, dex_cache, *options_.class_loader_);
1515       if (method == nullptr) {
1516         soa.Self()->ClearException();
1517         return nullptr;
1518       }
1519       return verifier::MethodVerifier::VerifyMethodAndDump(
1520           soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1521           class_def, code_item, method, method_access_flags, /* api_level= */ 0);
1522     }
1523 
1524     return nullptr;
1525   }
1526 
1527   // The StackMapsHelper provides the stack maps in the native PC order.
1528   // For identical native PCs, the order from the CodeInfo is preserved.
1529   class StackMapsHelper {
1530    public:
StackMapsHelper(const uint8_t * raw_code_info,InstructionSet instruction_set)1531     explicit StackMapsHelper(const uint8_t* raw_code_info, InstructionSet instruction_set)
1532         : code_info_(raw_code_info),
1533           number_of_stack_maps_(code_info_.GetNumberOfStackMaps()),
1534           indexes_(),
1535           offset_(static_cast<uint32_t>(-1)),
1536           stack_map_index_(0u),
1537           instruction_set_(instruction_set) {
1538       if (number_of_stack_maps_ != 0u) {
1539         // Check if native PCs are ordered.
1540         bool ordered = true;
1541         StackMap last = code_info_.GetStackMapAt(0u);
1542         for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1543           StackMap current = code_info_.GetStackMapAt(i);
1544           if (last.GetNativePcOffset(instruction_set) >
1545               current.GetNativePcOffset(instruction_set)) {
1546             ordered = false;
1547             break;
1548           }
1549           last = current;
1550         }
1551         if (!ordered) {
1552           // Create indirection indexes for access in native PC order. We do not optimize
1553           // for the fact that there can currently be only two separately ordered ranges,
1554           // namely normal stack maps and catch-point stack maps.
1555           indexes_.resize(number_of_stack_maps_);
1556           std::iota(indexes_.begin(), indexes_.end(), 0u);
1557           std::sort(indexes_.begin(),
1558                     indexes_.end(),
1559                     [this](size_t lhs, size_t rhs) {
1560                       StackMap left = code_info_.GetStackMapAt(lhs);
1561                       uint32_t left_pc = left.GetNativePcOffset(instruction_set_);
1562                       StackMap right = code_info_.GetStackMapAt(rhs);
1563                       uint32_t right_pc = right.GetNativePcOffset(instruction_set_);
1564                       // If the PCs are the same, compare indexes to preserve the original order.
1565                       return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1566                     });
1567         }
1568         offset_ = GetStackMapAt(0).GetNativePcOffset(instruction_set_);
1569       }
1570     }
1571 
GetCodeInfo() const1572     const CodeInfo& GetCodeInfo() const {
1573       return code_info_;
1574     }
1575 
GetOffset() const1576     uint32_t GetOffset() const {
1577       return offset_;
1578     }
1579 
GetStackMap() const1580     StackMap GetStackMap() const {
1581       return GetStackMapAt(stack_map_index_);
1582     }
1583 
Next()1584     void Next() {
1585       ++stack_map_index_;
1586       offset_ = (stack_map_index_ == number_of_stack_maps_)
1587           ? static_cast<uint32_t>(-1)
1588           : GetStackMapAt(stack_map_index_).GetNativePcOffset(instruction_set_);
1589     }
1590 
1591    private:
GetStackMapAt(size_t i) const1592     StackMap GetStackMapAt(size_t i) const {
1593       if (!indexes_.empty()) {
1594         i = indexes_[i];
1595       }
1596       DCHECK_LT(i, number_of_stack_maps_);
1597       return code_info_.GetStackMapAt(i);
1598     }
1599 
1600     const CodeInfo code_info_;
1601     const size_t number_of_stack_maps_;
1602     dchecked_vector<size_t> indexes_;  // Used if stack map native PCs are not ordered.
1603     uint32_t offset_;
1604     size_t stack_map_index_;
1605     const InstructionSet instruction_set_;
1606   };
1607 
DumpCode(VariableIndentationOutputStream * vios,const OatFile::OatMethod & oat_method,const CodeItemDataAccessor & code_item_accessor,bool bad_input,size_t code_size)1608   void DumpCode(VariableIndentationOutputStream* vios,
1609                 const OatFile::OatMethod& oat_method,
1610                 const CodeItemDataAccessor& code_item_accessor,
1611                 bool bad_input, size_t code_size) {
1612     const void* quick_code = oat_method.GetQuickCode();
1613 
1614     if (code_size == 0) {
1615       code_size = oat_method.GetQuickCodeSize();
1616     }
1617     if (code_size == 0 || quick_code == nullptr) {
1618       vios->Stream() << "NO CODE!\n";
1619       return;
1620     } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method,
1621                                                                    code_item_accessor)) {
1622       // The optimizing compiler outputs its CodeInfo data in the vmap table.
1623       StackMapsHelper helper(oat_method.GetVmapTable(), instruction_set_);
1624       if (AddStatsObject(oat_method.GetVmapTable())) {
1625         helper.GetCodeInfo().CollectSizeStats(oat_method.GetVmapTable(), &stats_);
1626       }
1627       const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1628       size_t offset = 0;
1629       while (offset < code_size) {
1630         offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1631         if (offset == helper.GetOffset()) {
1632           ScopedIndentation indent1(vios);
1633           StackMap stack_map = helper.GetStackMap();
1634           DCHECK(stack_map.IsValid());
1635           stack_map.Dump(vios,
1636                          helper.GetCodeInfo(),
1637                          oat_method.GetCodeOffset(),
1638                          instruction_set_);
1639           do {
1640             helper.Next();
1641             // There may be multiple stack maps at a given PC. We display only the first one.
1642           } while (offset == helper.GetOffset());
1643         }
1644         DCHECK_LT(offset, helper.GetOffset());
1645       }
1646     } else {
1647       const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1648       size_t offset = 0;
1649       while (offset < code_size) {
1650         offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1651       }
1652     }
1653   }
1654 
GetBootImageLiveObjectsDataRange(gc::Heap * heap) const1655   std::pair<const uint8_t*, const uint8_t*> GetBootImageLiveObjectsDataRange(gc::Heap* heap) const
1656       REQUIRES_SHARED(Locks::mutator_lock_) {
1657     const std::vector<gc::space::ImageSpace*>& boot_image_spaces = heap->GetBootImageSpaces();
1658     const ImageHeader& main_header = boot_image_spaces[0]->GetImageHeader();
1659     ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects =
1660         ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast(
1661             main_header.GetImageRoot<kWithoutReadBarrier>(ImageHeader::kBootImageLiveObjects));
1662     DCHECK(boot_image_live_objects != nullptr);
1663     DCHECK(heap->ObjectIsInBootImageSpace(boot_image_live_objects));
1664     const uint8_t* boot_image_live_objects_address =
1665         reinterpret_cast<const uint8_t*>(boot_image_live_objects.Ptr());
1666     uint32_t begin_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(0).Uint32Value();
1667     uint32_t end_offset = mirror::ObjectArray<mirror::Object>::OffsetOfElement(
1668         boot_image_live_objects->GetLength()).Uint32Value();
1669     return std::make_pair(boot_image_live_objects_address + begin_offset,
1670                           boot_image_live_objects_address + end_offset);
1671   }
1672 
DumpDataBimgRelRoEntries(std::ostream & os)1673   void DumpDataBimgRelRoEntries(std::ostream& os) {
1674     os << ".data.bimg.rel.ro: ";
1675     if (oat_file_.GetBootImageRelocations().empty()) {
1676       os << "empty.\n\n";
1677       return;
1678     }
1679 
1680     os << oat_file_.GetBootImageRelocations().size() << " entries.\n";
1681     Runtime* runtime = Runtime::Current();
1682     if (runtime != nullptr && !runtime->GetHeap()->GetBootImageSpaces().empty()) {
1683       const std::vector<gc::space::ImageSpace*>& boot_image_spaces =
1684           runtime->GetHeap()->GetBootImageSpaces();
1685       ScopedObjectAccess soa(Thread::Current());
1686       auto live_objects = GetBootImageLiveObjectsDataRange(runtime->GetHeap());
1687       const uint8_t* live_objects_begin = live_objects.first;
1688       const uint8_t* live_objects_end = live_objects.second;
1689       for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1690         uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1691         uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1692         os << StringPrintf("  0x%x: 0x%08x", entry_offset, object_offset);
1693         uint8_t* address = boot_image_spaces[0]->Begin() + object_offset;
1694         bool found = false;
1695         for (gc::space::ImageSpace* space : boot_image_spaces) {
1696           uint64_t local_offset = address - space->Begin();
1697           if (local_offset < space->GetImageHeader().GetImageSize()) {
1698             if (space->GetImageHeader().GetObjectsSection().Contains(local_offset)) {
1699               if (address >= live_objects_begin && address < live_objects_end) {
1700                 size_t index =
1701                     (address - live_objects_begin) / sizeof(mirror::HeapReference<mirror::Object>);
1702                 os << StringPrintf("   0x%08x BootImageLiveObject[%zu]",
1703                                    object_offset,
1704                                    index);
1705               } else {
1706                 ObjPtr<mirror::Object> o = reinterpret_cast<mirror::Object*>(address);
1707                 if (o->IsString()) {
1708                   os << "   String: " << o->AsString()->ToModifiedUtf8();
1709                 } else if (o->IsClass()) {
1710                   os << "   Class: " << o->AsClass()->PrettyDescriptor();
1711                 } else {
1712                   os << StringPrintf("   0x%08x %s",
1713                                      object_offset,
1714                                      o->GetClass()->PrettyDescriptor().c_str());
1715                 }
1716               }
1717             } else if (space->GetImageHeader().GetMethodsSection().Contains(local_offset)) {
1718               ArtMethod* m = reinterpret_cast<ArtMethod*>(address);
1719               os << "   ArtMethod: " << m->PrettyMethod();
1720             } else {
1721               os << StringPrintf("   0x%08x <unexpected section in %s>",
1722                                  object_offset,
1723                                  space->GetImageFilename().c_str());
1724             }
1725             found = true;
1726             break;
1727           }
1728         }
1729         if (!found) {
1730           os << StringPrintf("   0x%08x <outside boot image spaces>", object_offset);
1731         }
1732         os << "\n";
1733       }
1734     } else {
1735       for (const uint32_t& object_offset : oat_file_.GetBootImageRelocations()) {
1736         uint32_t entry_index = &object_offset - oat_file_.GetBootImageRelocations().data();
1737         uint32_t entry_offset = entry_index * sizeof(oat_file_.GetBootImageRelocations()[0]);
1738         os << StringPrintf("  0x%x: 0x%08x\n", entry_offset, object_offset);
1739       }
1740     }
1741     os << "\n";
1742   }
1743 
1744   template <typename NameGetter>
DumpBssEntries(std::ostream & os,const char * slot_type,const IndexBssMapping * mapping,uint32_t number_of_indexes,size_t slot_size,NameGetter name)1745   void DumpBssEntries(std::ostream& os,
1746                       const char* slot_type,
1747                       const IndexBssMapping* mapping,
1748                       uint32_t number_of_indexes,
1749                       size_t slot_size,
1750                       NameGetter name) {
1751     os << ".bss mapping for " << slot_type << ": ";
1752     if (mapping == nullptr) {
1753       os << "empty.\n";
1754       return;
1755     }
1756     size_t index_bits = IndexBssMappingEntry::IndexBits(number_of_indexes);
1757     size_t num_valid_indexes = 0u;
1758     for (const IndexBssMappingEntry& entry : *mapping) {
1759       num_valid_indexes += 1u + POPCOUNT(entry.GetMask(index_bits));
1760     }
1761     os << mapping->size() << " entries for " << num_valid_indexes << " valid indexes.\n";
1762     os << std::hex;
1763     for (const IndexBssMappingEntry& entry : *mapping) {
1764       uint32_t index = entry.GetIndex(index_bits);
1765       uint32_t mask = entry.GetMask(index_bits);
1766       size_t bss_offset = entry.bss_offset - POPCOUNT(mask) * slot_size;
1767       for (uint32_t n : LowToHighBits(mask)) {
1768         size_t current_index = index - (32u - index_bits) + n;
1769         os << "  0x" << bss_offset << ": " << slot_type << ": " << name(current_index) << "\n";
1770         bss_offset += slot_size;
1771       }
1772       DCHECK_EQ(bss_offset, entry.bss_offset);
1773       os << "  0x" << bss_offset << ": " << slot_type << ": " << name(index) << "\n";
1774     }
1775     os << std::dec;
1776   }
1777 
1778   const OatFile& oat_file_;
1779   const std::vector<const OatDexFile*> oat_dex_files_;
1780   const OatDumperOptions& options_;
1781   uint32_t resolved_addr2instr_;
1782   const InstructionSet instruction_set_;
1783   std::set<uintptr_t> offsets_;
1784   Disassembler* disassembler_;
1785   Stats stats_;
1786   std::unordered_set<const void*> seen_stats_objects_;
1787 };
1788 
1789 class ImageDumper {
1790  public:
ImageDumper(std::ostream * os,gc::space::ImageSpace & image_space,const ImageHeader & image_header,OatDumperOptions * oat_dumper_options)1791   ImageDumper(std::ostream* os,
1792               gc::space::ImageSpace& image_space,
1793               const ImageHeader& image_header,
1794               OatDumperOptions* oat_dumper_options)
1795       : os_(os),
1796         vios_(os),
1797         indent1_(&vios_),
1798         image_space_(image_space),
1799         image_header_(image_header),
1800         oat_dumper_options_(oat_dumper_options) {}
1801 
Dump()1802   bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
1803     std::ostream& os = *os_;
1804     std::ostream& indent_os = vios_.Stream();
1805 
1806     os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
1807 
1808     os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1809 
1810     os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n";
1811     os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n";
1812     os << "IMAGE CHECKSUM: " << std::hex << image_header_.GetImageChecksum() << std::dec << "\n\n";
1813 
1814     os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum()) << "\n";
1815     os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n";
1816     os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n";
1817     os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n";
1818     os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
1819 
1820     os << "BOOT IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetBootImageBegin())
1821         << "\n";
1822     os << "BOOT IMAGE SIZE: " << image_header_.GetBootImageSize() << "\n\n";
1823 
1824     for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1825       auto section = static_cast<ImageHeader::ImageSections>(i);
1826       os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1827     }
1828 
1829     {
1830       os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots().Ptr()) << "\n";
1831       static_assert(arraysize(image_roots_descriptions_) ==
1832           static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
1833       DCHECK_LE(image_header_.GetImageRoots()->GetLength(), ImageHeader::kImageRootsMax);
1834       for (int32_t i = 0, size = image_header_.GetImageRoots()->GetLength(); i != size; ++i) {
1835         ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1836         const char* image_root_description = image_roots_descriptions_[i];
1837         ObjPtr<mirror::Object> image_root_object = image_header_.GetImageRoot(image_root);
1838         indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object.Ptr());
1839         if (image_root_object != nullptr && image_root_object->IsObjectArray()) {
1840           ObjPtr<mirror::ObjectArray<mirror::Object>> image_root_object_array
1841               = image_root_object->AsObjectArray<mirror::Object>();
1842           ScopedIndentation indent2(&vios_);
1843           for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1844             ObjPtr<mirror::Object> value = image_root_object_array->Get(j);
1845             size_t run = 0;
1846             for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1847               if (value == image_root_object_array->Get(k)) {
1848                 run++;
1849               } else {
1850                 break;
1851               }
1852             }
1853             if (run == 0) {
1854               indent_os << StringPrintf("%d: ", j);
1855             } else {
1856               indent_os << StringPrintf("%d to %zd: ", j, j + run);
1857               j = j + run;
1858             }
1859             if (value != nullptr) {
1860               PrettyObjectValue(indent_os, value->GetClass(), value);
1861             } else {
1862               indent_os << j << ": null\n";
1863             }
1864           }
1865         }
1866       }
1867     }
1868 
1869     {
1870       os << "METHOD ROOTS\n";
1871       static_assert(arraysize(image_methods_descriptions_) ==
1872           static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1873       for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1874         auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1875         const char* description = image_methods_descriptions_[i];
1876         auto* image_method = image_header_.GetImageMethod(image_root);
1877         indent_os << StringPrintf("%s: %p\n", description, image_method);
1878       }
1879     }
1880     os << "\n";
1881 
1882     Runtime* const runtime = Runtime::Current();
1883     ClassLinker* class_linker = runtime->GetClassLinker();
1884     std::string image_filename = image_space_.GetImageFilename();
1885     std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
1886     os << "OAT LOCATION: " << oat_location;
1887     os << "\n";
1888     std::string error_msg;
1889     const OatFile* oat_file = image_space_.GetOatFile();
1890     if (oat_file == nullptr) {
1891       oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1892     }
1893     if (oat_file == nullptr) {
1894       oat_file = OatFile::Open(/*zip_fd=*/ -1,
1895                                oat_location,
1896                                oat_location,
1897                                /*executable=*/ false,
1898                                /*low_4gb=*/ false,
1899                                &error_msg);
1900     }
1901     if (oat_file == nullptr) {
1902       os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1903       return EXIT_FAILURE;
1904     }
1905     os << "\n";
1906 
1907     stats_.oat_file_bytes = oat_file->Size();
1908 
1909     oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
1910 
1911     for (const OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
1912       CHECK(oat_dex_file != nullptr);
1913       stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1914                                                          oat_dex_file->FileSize()));
1915     }
1916 
1917     os << "OBJECTS:\n" << std::flush;
1918 
1919     // Loop through the image space and dump its objects.
1920     gc::Heap* heap = runtime->GetHeap();
1921     Thread* self = Thread::Current();
1922     {
1923       {
1924         WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1925         heap->FlushAllocStack();
1926       }
1927       // Since FlushAllocStack() above resets the (active) allocation
1928       // stack. Need to revoke the thread-local allocation stacks that
1929       // point into it.
1930       ScopedThreadSuspension sts(self, kNative);
1931       ScopedSuspendAll ssa(__FUNCTION__);
1932       heap->RevokeAllThreadLocalAllocationStacks(self);
1933     }
1934     {
1935       // Mark dex caches.
1936       dex_caches_.clear();
1937       {
1938         ReaderMutexLock mu(self, *Locks::dex_lock_);
1939         for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
1940           ObjPtr<mirror::DexCache> dex_cache =
1941               ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root));
1942           if (dex_cache != nullptr) {
1943             dex_caches_.insert(dex_cache.Ptr());
1944           }
1945         }
1946       }
1947       auto dump_visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
1948         DumpObject(obj);
1949       };
1950       ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
1951       // Dump the normal objects before ArtMethods.
1952       image_space_.GetLiveBitmap()->Walk(dump_visitor);
1953       indent_os << "\n";
1954       // TODO: Dump fields.
1955       // Dump methods after.
1956       image_header_.VisitPackedArtMethods([&](ArtMethod& method)
1957           REQUIRES_SHARED(Locks::mutator_lock_) {
1958         std::ostream& indent_os = vios_.Stream();
1959         indent_os << &method << " " << " ArtMethod: " << method.PrettyMethod() << "\n";
1960         DumpMethod(&method, indent_os);
1961         indent_os << "\n";
1962       },  image_space_.Begin(), image_header_.GetPointerSize());
1963       // Dump the large objects separately.
1964       heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(dump_visitor);
1965       indent_os << "\n";
1966     }
1967     os << "STATS:\n" << std::flush;
1968     std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
1969     size_t data_size = image_header_.GetDataSize();  // stored size in file.
1970     if (file == nullptr) {
1971       LOG(WARNING) << "Failed to find image in " << image_filename;
1972     } else {
1973       stats_.file_bytes = file->GetLength();
1974       // If the image is compressed, adjust to decompressed size.
1975       size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1976       if (!image_header_.HasCompressedBlock()) {
1977         DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1978       }
1979       stats_.file_bytes += uncompressed_size - data_size;
1980     }
1981     size_t header_bytes = sizeof(ImageHeader);
1982     const auto& object_section = image_header_.GetObjectsSection();
1983     const auto& field_section = image_header_.GetFieldsSection();
1984     const auto& method_section = image_header_.GetMethodsSection();
1985     const auto& dex_cache_arrays_section = image_header_.GetDexCacheArraysSection();
1986     const auto& intern_section = image_header_.GetInternedStringsSection();
1987     const auto& class_table_section = image_header_.GetClassTableSection();
1988     const auto& sro_section = image_header_.GetImageStringReferenceOffsetsSection();
1989     const auto& metadata_section = image_header_.GetMetadataSection();
1990     const auto& bitmap_section = image_header_.GetImageBitmapSection();
1991 
1992     stats_.header_bytes = header_bytes;
1993 
1994     // Objects are kObjectAlignment-aligned.
1995     // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1996     if (object_section.Offset() > header_bytes) {
1997       stats_.alignment_bytes += object_section.Offset() - header_bytes;
1998     }
1999 
2000     // Field section is 4-byte aligned.
2001     constexpr size_t kFieldSectionAlignment = 4U;
2002     uint32_t end_objects = object_section.Offset() + object_section.Size();
2003     CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
2004     stats_.alignment_bytes += field_section.Offset() - end_objects;
2005 
2006     // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
2007     uint32_t end_fields = field_section.Offset() + field_section.Size();
2008     CHECK_ALIGNED(method_section.Offset(), 4);
2009     stats_.alignment_bytes += method_section.Offset() - end_fields;
2010 
2011     // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
2012     uint32_t end_methods = method_section.Offset() + method_section.Size();
2013     CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
2014     stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
2015 
2016     // Intern table is 8-byte aligned.
2017     uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
2018     CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
2019     stats_.alignment_bytes += intern_section.Offset() - end_caches;
2020 
2021     // Add space between intern table and class table.
2022     uint32_t end_intern = intern_section.Offset() + intern_section.Size();
2023     stats_.alignment_bytes += class_table_section.Offset() - end_intern;
2024 
2025     // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
2026     const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
2027     CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
2028     stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
2029 
2030     stats_.bitmap_bytes += bitmap_section.Size();
2031     stats_.art_field_bytes += field_section.Size();
2032     stats_.art_method_bytes += method_section.Size();
2033     stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
2034     stats_.interned_strings_bytes += intern_section.Size();
2035     stats_.class_table_bytes += class_table_section.Size();
2036     stats_.sro_offset_bytes += sro_section.Size();
2037     stats_.metadata_bytes += metadata_section.Size();
2038 
2039     stats_.Dump(os, indent_os);
2040     os << "\n";
2041 
2042     os << std::flush;
2043 
2044     return oat_dumper_->Dump(os);
2045   }
2046 
2047  private:
PrettyObjectValue(std::ostream & os,ObjPtr<mirror::Class> type,ObjPtr<mirror::Object> value)2048   static void PrettyObjectValue(std::ostream& os,
2049                                 ObjPtr<mirror::Class> type,
2050                                 ObjPtr<mirror::Object> value)
2051       REQUIRES_SHARED(Locks::mutator_lock_) {
2052     CHECK(type != nullptr);
2053     if (value == nullptr) {
2054       os << StringPrintf("null   %s\n", type->PrettyDescriptor().c_str());
2055     } else if (type->IsStringClass()) {
2056       ObjPtr<mirror::String> string = value->AsString();
2057       os << StringPrintf("%p   String: %s\n",
2058                          string.Ptr(),
2059                          PrintableString(string->ToModifiedUtf8().c_str()).c_str());
2060     } else if (type->IsClassClass()) {
2061       ObjPtr<mirror::Class> klass = value->AsClass();
2062       os << StringPrintf("%p   Class: %s\n",
2063                          klass.Ptr(),
2064                          mirror::Class::PrettyDescriptor(klass).c_str());
2065     } else {
2066       os << StringPrintf("%p   %s\n", value.Ptr(), type->PrettyDescriptor().c_str());
2067     }
2068   }
2069 
PrintField(std::ostream & os,ArtField * field,ObjPtr<mirror::Object> obj)2070   static void PrintField(std::ostream& os, ArtField* field, ObjPtr<mirror::Object> obj)
2071       REQUIRES_SHARED(Locks::mutator_lock_) {
2072     os << StringPrintf("%s: ", field->GetName());
2073     switch (field->GetTypeAsPrimitiveType()) {
2074       case Primitive::kPrimLong:
2075         os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
2076         break;
2077       case Primitive::kPrimDouble:
2078         os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
2079         break;
2080       case Primitive::kPrimFloat:
2081         os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
2082         break;
2083       case Primitive::kPrimInt:
2084         os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
2085         break;
2086       case Primitive::kPrimChar:
2087         os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
2088         break;
2089       case Primitive::kPrimShort:
2090         os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
2091         break;
2092       case Primitive::kPrimBoolean:
2093         os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj) ? "true" : "false",
2094             field->GetBoolean(obj));
2095         break;
2096       case Primitive::kPrimByte:
2097         os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
2098         break;
2099       case Primitive::kPrimNot: {
2100         // Get the value, don't compute the type unless it is non-null as we don't want
2101         // to cause class loading.
2102         ObjPtr<mirror::Object> value = field->GetObj(obj);
2103         if (value == nullptr) {
2104           os << StringPrintf("null   %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2105         } else {
2106           // Grab the field type without causing resolution.
2107           ObjPtr<mirror::Class> field_type = field->LookupResolvedType();
2108           if (field_type != nullptr) {
2109             PrettyObjectValue(os, field_type, value);
2110           } else {
2111             os << StringPrintf("%p   %s\n",
2112                                value.Ptr(),
2113                                PrettyDescriptor(field->GetTypeDescriptor()).c_str());
2114           }
2115         }
2116         break;
2117       }
2118       default:
2119         os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
2120         break;
2121     }
2122   }
2123 
DumpFields(std::ostream & os,mirror::Object * obj,ObjPtr<mirror::Class> klass)2124   static void DumpFields(std::ostream& os, mirror::Object* obj, ObjPtr<mirror::Class> klass)
2125       REQUIRES_SHARED(Locks::mutator_lock_) {
2126     ObjPtr<mirror::Class> super = klass->GetSuperClass();
2127     if (super != nullptr) {
2128       DumpFields(os, obj, super);
2129     }
2130     for (ArtField& field : klass->GetIFields()) {
2131       PrintField(os, &field, obj);
2132     }
2133   }
2134 
InDumpSpace(const mirror::Object * object)2135   bool InDumpSpace(const mirror::Object* object) {
2136     return image_space_.Contains(object);
2137   }
2138 
GetQuickOatCodeBegin(ArtMethod * m)2139   const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
2140     const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
2141         image_header_.GetPointerSize());
2142     ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2143     if (class_linker->IsQuickResolutionStub(quick_code) ||
2144         class_linker->IsQuickToInterpreterBridge(quick_code) ||
2145         class_linker->IsQuickGenericJniStub(quick_code) ||
2146         class_linker->IsJniDlsymLookupStub(quick_code) ||
2147         class_linker->IsJniDlsymLookupCriticalStub(quick_code)) {
2148       quick_code = oat_dumper_->GetQuickOatCode(m);
2149     }
2150     if (oat_dumper_->GetInstructionSet() == InstructionSet::kThumb2) {
2151       quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
2152     }
2153     return quick_code;
2154   }
2155 
GetQuickOatCodeSize(ArtMethod * m)2156   uint32_t GetQuickOatCodeSize(ArtMethod* m)
2157       REQUIRES_SHARED(Locks::mutator_lock_) {
2158     const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
2159     if (oat_code_begin == nullptr) {
2160       return 0;
2161     }
2162     OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2163         reinterpret_cast<uintptr_t>(oat_code_begin) - sizeof(OatQuickMethodHeader));
2164     return method_header->GetCodeSize();
2165   }
2166 
GetQuickOatCodeEnd(ArtMethod * m)2167   const void* GetQuickOatCodeEnd(ArtMethod* m)
2168       REQUIRES_SHARED(Locks::mutator_lock_) {
2169     const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
2170     if (oat_code_begin == nullptr) {
2171       return nullptr;
2172     }
2173     return oat_code_begin + GetQuickOatCodeSize(m);
2174   }
2175 
DumpObject(mirror::Object * obj)2176   void DumpObject(mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) {
2177     DCHECK(obj != nullptr);
2178     if (!InDumpSpace(obj)) {
2179       return;
2180     }
2181 
2182     size_t object_bytes = obj->SizeOf();
2183     size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
2184     stats_.object_bytes += object_bytes;
2185     stats_.alignment_bytes += alignment_bytes;
2186 
2187     std::ostream& os = vios_.Stream();
2188 
2189     ObjPtr<mirror::Class> obj_class = obj->GetClass();
2190     if (obj_class->IsArrayClass()) {
2191       os << StringPrintf("%p: %s length:%d\n", obj, obj_class->PrettyDescriptor().c_str(),
2192                          obj->AsArray()->GetLength());
2193     } else if (obj->IsClass()) {
2194       ObjPtr<mirror::Class> klass = obj->AsClass();
2195       os << StringPrintf("%p: java.lang.Class \"%s\" (",
2196                          obj,
2197                          mirror::Class::PrettyDescriptor(klass).c_str())
2198          << klass->GetStatus() << ")\n";
2199     } else if (obj_class->IsStringClass()) {
2200       os << StringPrintf("%p: java.lang.String %s\n",
2201                          obj,
2202                          PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
2203     } else {
2204       os << StringPrintf("%p: %s\n", obj, obj_class->PrettyDescriptor().c_str());
2205     }
2206     ScopedIndentation indent1(&vios_);
2207     DumpFields(os, obj, obj_class);
2208     const PointerSize image_pointer_size = image_header_.GetPointerSize();
2209     if (obj->IsObjectArray()) {
2210       ObjPtr<mirror::ObjectArray<mirror::Object>> obj_array = obj->AsObjectArray<mirror::Object>();
2211       for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
2212         ObjPtr<mirror::Object> value = obj_array->Get(i);
2213         size_t run = 0;
2214         for (int32_t j = i + 1; j < length; j++) {
2215           if (value == obj_array->Get(j)) {
2216             run++;
2217           } else {
2218             break;
2219           }
2220         }
2221         if (run == 0) {
2222           os << StringPrintf("%d: ", i);
2223         } else {
2224           os << StringPrintf("%d to %zd: ", i, i + run);
2225           i = i + run;
2226         }
2227         ObjPtr<mirror::Class> value_class =
2228             (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
2229         PrettyObjectValue(os, value_class, value);
2230       }
2231     } else if (obj->IsClass()) {
2232       ObjPtr<mirror::Class> klass = obj->AsClass();
2233 
2234       if (kBitstringSubtypeCheckEnabled) {
2235         os << "SUBTYPE_CHECK_BITS: ";
2236         SubtypeCheck<ObjPtr<mirror::Class>>::Dump(klass, os);
2237         os << "\n";
2238       }
2239 
2240       if (klass->NumStaticFields() != 0) {
2241         os << "STATICS:\n";
2242         ScopedIndentation indent2(&vios_);
2243         for (ArtField& field : klass->GetSFields()) {
2244           PrintField(os, &field, field.GetDeclaringClass());
2245         }
2246       }
2247     } else {
2248       auto it = dex_caches_.find(obj);
2249       if (it != dex_caches_.end()) {
2250         auto* dex_cache = down_cast<mirror::DexCache*>(obj);
2251         const auto& field_section = image_header_.GetFieldsSection();
2252         const auto& method_section = image_header_.GetMethodsSection();
2253         size_t num_methods = dex_cache->NumResolvedMethods();
2254         if (num_methods != 0u) {
2255           os << "Methods (size=" << num_methods << "):\n";
2256           ScopedIndentation indent2(&vios_);
2257           mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
2258           for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
2259             ArtMethod* elem = mirror::DexCache::GetNativePairPtrSize(
2260                 resolved_methods, i, image_pointer_size).object;
2261             size_t run = 0;
2262             for (size_t j = i + 1;
2263                  j != length &&
2264                  elem == mirror::DexCache::GetNativePairPtrSize(
2265                      resolved_methods, j, image_pointer_size).object;
2266                  ++j) {
2267               ++run;
2268             }
2269             if (run == 0) {
2270               os << StringPrintf("%zd: ", i);
2271             } else {
2272               os << StringPrintf("%zd to %zd: ", i, i + run);
2273               i = i + run;
2274             }
2275             std::string msg;
2276             if (elem == nullptr) {
2277               msg = "null";
2278             } else if (method_section.Contains(
2279                 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
2280               msg = reinterpret_cast<ArtMethod*>(elem)->PrettyMethod();
2281             } else {
2282               msg = "<not in method section>";
2283             }
2284             os << StringPrintf("%p   %s\n", elem, msg.c_str());
2285           }
2286         }
2287         size_t num_fields = dex_cache->NumResolvedFields();
2288         if (num_fields != 0u) {
2289           os << "Fields (size=" << num_fields << "):\n";
2290           ScopedIndentation indent2(&vios_);
2291           auto* resolved_fields = dex_cache->GetResolvedFields();
2292           for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
2293             ArtField* elem = mirror::DexCache::GetNativePairPtrSize(
2294                 resolved_fields, i, image_pointer_size).object;
2295             size_t run = 0;
2296             for (size_t j = i + 1;
2297                  j != length &&
2298                  elem == mirror::DexCache::GetNativePairPtrSize(
2299                      resolved_fields, j, image_pointer_size).object;
2300                  ++j) {
2301               ++run;
2302             }
2303             if (run == 0) {
2304               os << StringPrintf("%zd: ", i);
2305             } else {
2306               os << StringPrintf("%zd to %zd: ", i, i + run);
2307               i = i + run;
2308             }
2309             std::string msg;
2310             if (elem == nullptr) {
2311               msg = "null";
2312             } else if (field_section.Contains(
2313                 reinterpret_cast<uint8_t*>(elem) - image_space_.Begin())) {
2314               msg = reinterpret_cast<ArtField*>(elem)->PrettyField();
2315             } else {
2316               msg = "<not in field section>";
2317             }
2318             os << StringPrintf("%p   %s\n", elem, msg.c_str());
2319           }
2320         }
2321         size_t num_types = dex_cache->NumResolvedTypes();
2322         if (num_types != 0u) {
2323           os << "Types (size=" << num_types << "):\n";
2324           ScopedIndentation indent2(&vios_);
2325           auto* resolved_types = dex_cache->GetResolvedTypes();
2326           for (size_t i = 0; i < num_types; ++i) {
2327             auto pair = resolved_types[i].load(std::memory_order_relaxed);
2328             size_t run = 0;
2329             for (size_t j = i + 1; j != num_types; ++j) {
2330               auto other_pair = resolved_types[j].load(std::memory_order_relaxed);
2331               if (pair.index != other_pair.index ||
2332                   pair.object.Read() != other_pair.object.Read()) {
2333                 break;
2334               }
2335               ++run;
2336             }
2337             if (run == 0) {
2338               os << StringPrintf("%zd: ", i);
2339             } else {
2340               os << StringPrintf("%zd to %zd: ", i, i + run);
2341               i = i + run;
2342             }
2343             std::string msg;
2344             auto* elem = pair.object.Read();
2345             if (elem == nullptr) {
2346               msg = "null";
2347             } else {
2348               msg = elem->PrettyClass();
2349             }
2350             os << StringPrintf("%p   %u %s\n", elem, pair.index, msg.c_str());
2351           }
2352         }
2353       }
2354     }
2355     std::string temp;
2356     stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
2357   }
2358 
DumpMethod(ArtMethod * method,std::ostream & indent_os)2359   void DumpMethod(ArtMethod* method, std::ostream& indent_os)
2360       REQUIRES_SHARED(Locks::mutator_lock_) {
2361     DCHECK(method != nullptr);
2362     const PointerSize pointer_size = image_header_.GetPointerSize();
2363     if (method->IsNative()) {
2364       const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2365       bool first_occurrence;
2366       uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2367       ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2368       if (first_occurrence) {
2369         stats_.native_to_managed_code_bytes += quick_oat_code_size;
2370       }
2371       if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
2372           image_header_.GetPointerSize())) {
2373         indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
2374       }
2375     } else if (method->IsAbstract() || method->IsClassInitializer()) {
2376       // Don't print information for these.
2377     } else if (method->IsRuntimeMethod()) {
2378       if (method == Runtime::Current()->GetResolutionMethod()) {
2379         const void* resolution_trampoline =
2380             method->GetEntryPointFromQuickCompiledCodePtrSize(image_header_.GetPointerSize());
2381         indent_os << StringPrintf("Resolution trampoline: %p\n", resolution_trampoline);
2382         const void* critical_native_resolution_trampoline =
2383             method->GetEntryPointFromJniPtrSize(image_header_.GetPointerSize());
2384         indent_os << StringPrintf("Resolution trampoline for @CriticalNative: %p\n",
2385                                   critical_native_resolution_trampoline);
2386       } else {
2387         ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
2388         if (table != nullptr) {
2389           indent_os << "IMT conflict table " << table << " method: ";
2390           for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
2391             indent_os << ArtMethod::PrettyMethod(table->GetImplementationMethod(i, pointer_size))
2392                       << " ";
2393           }
2394         }
2395       }
2396     } else {
2397       CodeItemDataAccessor code_item_accessor(method->DexInstructionData());
2398       size_t dex_instruction_bytes = code_item_accessor.InsnsSizeInCodeUnits() * 2;
2399       stats_.dex_instruction_bytes += dex_instruction_bytes;
2400 
2401       const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
2402       const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
2403 
2404       bool first_occurrence;
2405       size_t vmap_table_bytes = 0u;
2406       if (quick_oat_code_begin != nullptr) {
2407         OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2408             reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
2409         vmap_table_bytes = ComputeOatSize(method_header->GetOptimizedCodeInfoPtr(),
2410                                           &first_occurrence);
2411         if (first_occurrence) {
2412           stats_.vmap_table_bytes += vmap_table_bytes;
2413         }
2414       }
2415 
2416       uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2417       ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2418       if (first_occurrence) {
2419         stats_.managed_code_bytes += quick_oat_code_size;
2420         if (method->IsConstructor()) {
2421           if (method->IsStatic()) {
2422             stats_.class_initializer_code_bytes += quick_oat_code_size;
2423           } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2424             stats_.large_initializer_code_bytes += quick_oat_code_size;
2425           }
2426         } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2427           stats_.large_method_code_bytes += quick_oat_code_size;
2428         }
2429       }
2430       stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2431 
2432       uint32_t method_access_flags = method->GetAccessFlags();
2433 
2434       indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
2435       indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2436                                 dex_instruction_bytes,
2437                                 vmap_table_bytes,
2438                                 method_access_flags);
2439 
2440       size_t total_size = dex_instruction_bytes +
2441           vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
2442 
2443       double expansion =
2444       static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2445       stats_.ComputeOutliers(total_size, expansion, method);
2446     }
2447   }
2448 
2449   std::set<const void*> already_seen_;
2450   // Compute the size of the given data within the oat file and whether this is the first time
2451   // this data has been requested
ComputeOatSize(const void * oat_data,bool * first_occurrence)2452   size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
2453     if (already_seen_.count(oat_data) == 0) {
2454       *first_occurrence = true;
2455       already_seen_.insert(oat_data);
2456     } else {
2457       *first_occurrence = false;
2458     }
2459     return oat_dumper_->ComputeSize(oat_data);
2460   }
2461 
2462  public:
2463   struct Stats {
2464     size_t oat_file_bytes = 0u;
2465     size_t file_bytes = 0u;
2466 
2467     size_t header_bytes = 0u;
2468     size_t object_bytes = 0u;
2469     size_t art_field_bytes = 0u;
2470     size_t art_method_bytes = 0u;
2471     size_t dex_cache_arrays_bytes = 0u;
2472     size_t interned_strings_bytes = 0u;
2473     size_t class_table_bytes = 0u;
2474     size_t sro_offset_bytes = 0u;
2475     size_t metadata_bytes = 0u;
2476     size_t bitmap_bytes = 0u;
2477     size_t alignment_bytes = 0u;
2478 
2479     size_t managed_code_bytes = 0u;
2480     size_t managed_code_bytes_ignoring_deduplication = 0u;
2481     size_t native_to_managed_code_bytes = 0u;
2482     size_t class_initializer_code_bytes = 0u;
2483     size_t large_initializer_code_bytes = 0u;
2484     size_t large_method_code_bytes = 0u;
2485 
2486     size_t vmap_table_bytes = 0u;
2487 
2488     size_t dex_instruction_bytes = 0u;
2489 
2490     std::vector<ArtMethod*> method_outlier;
2491     std::vector<size_t> method_outlier_size;
2492     std::vector<double> method_outlier_expansion;
2493     std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
2494 
Statsart::ImageDumper::Stats2495     Stats() {}
2496 
2497     struct SizeAndCount {
SizeAndCountart::ImageDumper::Stats::SizeAndCount2498       SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
2499       size_t bytes;
2500       size_t count;
2501     };
2502     using SizeAndCountTable = SafeMap<std::string, SizeAndCount>;
2503     SizeAndCountTable sizes_and_counts;
2504 
Updateart::ImageDumper::Stats2505     void Update(const char* descriptor, size_t object_bytes_in) {
2506       SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2507       if (it != sizes_and_counts.end()) {
2508         it->second.bytes += object_bytes_in;
2509         it->second.count += 1;
2510       } else {
2511         sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
2512       }
2513     }
2514 
PercentOfOatBytesart::ImageDumper::Stats2515     double PercentOfOatBytes(size_t size) {
2516       return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2517     }
2518 
PercentOfFileBytesart::ImageDumper::Stats2519     double PercentOfFileBytes(size_t size) {
2520       return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2521     }
2522 
PercentOfObjectBytesart::ImageDumper::Stats2523     double PercentOfObjectBytes(size_t size) {
2524       return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2525     }
2526 
ComputeOutliersart::ImageDumper::Stats2527     void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
2528       method_outlier_size.push_back(total_size);
2529       method_outlier_expansion.push_back(expansion);
2530       method_outlier.push_back(method);
2531     }
2532 
DumpOutliersart::ImageDumper::Stats2533     void DumpOutliers(std::ostream& os)
2534         REQUIRES_SHARED(Locks::mutator_lock_) {
2535       size_t sum_of_sizes = 0;
2536       size_t sum_of_sizes_squared = 0;
2537       size_t sum_of_expansion = 0;
2538       size_t sum_of_expansion_squared = 0;
2539       size_t n = method_outlier_size.size();
2540       if (n <= 1) {
2541         return;
2542       }
2543       for (size_t i = 0; i < n; i++) {
2544         size_t cur_size = method_outlier_size[i];
2545         sum_of_sizes += cur_size;
2546         sum_of_sizes_squared += cur_size * cur_size;
2547         double cur_expansion = method_outlier_expansion[i];
2548         sum_of_expansion += cur_expansion;
2549         sum_of_expansion_squared += cur_expansion * cur_expansion;
2550       }
2551       size_t size_mean = sum_of_sizes / n;
2552       size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2553       double expansion_mean = sum_of_expansion / n;
2554       double expansion_variance =
2555           (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2556 
2557       // Dump methods whose size is a certain number of standard deviations from the mean
2558       size_t dumped_values = 0;
2559       size_t skipped_values = 0;
2560       for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
2561         size_t cur_size_variance = i * i * size_variance;
2562         bool first = true;
2563         for (size_t j = 0; j < n; j++) {
2564           size_t cur_size = method_outlier_size[j];
2565           if (cur_size > size_mean) {
2566             size_t cur_var = cur_size - size_mean;
2567             cur_var = cur_var * cur_var;
2568             if (cur_var > cur_size_variance) {
2569               if (dumped_values > 20) {
2570                 if (i == 1) {
2571                   skipped_values++;
2572                 } else {
2573                   i = 2;  // jump to counting for 1 standard deviation
2574                   break;
2575                 }
2576               } else {
2577                 if (first) {
2578                   os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
2579                   first = false;
2580                 }
2581                 os << ArtMethod::PrettyMethod(method_outlier[j]) << " requires storage of "
2582                     << PrettySize(cur_size) << "\n";
2583                 method_outlier_size[j] = 0;  // don't consider this method again
2584                 dumped_values++;
2585               }
2586             }
2587           }
2588         }
2589       }
2590       if (skipped_values > 0) {
2591         os << "... skipped " << skipped_values
2592            << " methods with size > 1 standard deviation from the norm\n";
2593       }
2594       os << std::flush;
2595 
2596       // Dump methods whose expansion is a certain number of standard deviations from the mean
2597       dumped_values = 0;
2598       skipped_values = 0;
2599       for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
2600         double cur_expansion_variance = i * i * expansion_variance;
2601         bool first = true;
2602         for (size_t j = 0; j < n; j++) {
2603           double cur_expansion = method_outlier_expansion[j];
2604           if (cur_expansion > expansion_mean) {
2605             size_t cur_var = cur_expansion - expansion_mean;
2606             cur_var = cur_var * cur_var;
2607             if (cur_var > cur_expansion_variance) {
2608               if (dumped_values > 20) {
2609                 if (i == 1) {
2610                   skipped_values++;
2611                 } else {
2612                   i = 2;  // jump to counting for 1 standard deviation
2613                   break;
2614                 }
2615               } else {
2616                 if (first) {
2617                   os << "\nLarge expansion methods (size > " << i
2618                       << " standard deviations the norm):\n";
2619                   first = false;
2620                 }
2621                 os << ArtMethod::PrettyMethod(method_outlier[j]) << " expanded code by "
2622                    << cur_expansion << "\n";
2623                 method_outlier_expansion[j] = 0.0;  // don't consider this method again
2624                 dumped_values++;
2625               }
2626             }
2627           }
2628         }
2629       }
2630       if (skipped_values > 0) {
2631         os << "... skipped " << skipped_values
2632            << " methods with expansion > 1 standard deviation from the norm\n";
2633       }
2634       os << "\n" << std::flush;
2635     }
2636 
Dumpart::ImageDumper::Stats2637     void Dump(std::ostream& os, std::ostream& indent_os)
2638         REQUIRES_SHARED(Locks::mutator_lock_) {
2639       {
2640         os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2641            << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
2642         indent_os << StringPrintf("header_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
2643                                   "object_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
2644                                   "art_field_bytes        =  %8zd (%2.0f%% of art file bytes)\n"
2645                                   "art_method_bytes       =  %8zd (%2.0f%% of art file bytes)\n"
2646                                   "dex_cache_arrays_bytes =  %8zd (%2.0f%% of art file bytes)\n"
2647                                   "interned_string_bytes  =  %8zd (%2.0f%% of art file bytes)\n"
2648                                   "class_table_bytes      =  %8zd (%2.0f%% of art file bytes)\n"
2649                                   "sro_bytes              =  %8zd (%2.0f%% of art file bytes)\n"
2650                                   "metadata_bytes         =  %8zd (%2.0f%% of art file bytes)\n"
2651                                   "bitmap_bytes           =  %8zd (%2.0f%% of art file bytes)\n"
2652                                   "alignment_bytes        =  %8zd (%2.0f%% of art file bytes)\n\n",
2653                                   header_bytes, PercentOfFileBytes(header_bytes),
2654                                   object_bytes, PercentOfFileBytes(object_bytes),
2655                                   art_field_bytes, PercentOfFileBytes(art_field_bytes),
2656                                   art_method_bytes, PercentOfFileBytes(art_method_bytes),
2657                                   dex_cache_arrays_bytes,
2658                                   PercentOfFileBytes(dex_cache_arrays_bytes),
2659                                   interned_strings_bytes,
2660                                   PercentOfFileBytes(interned_strings_bytes),
2661                                   class_table_bytes, PercentOfFileBytes(class_table_bytes),
2662                                   sro_offset_bytes, PercentOfFileBytes(sro_offset_bytes),
2663                                   metadata_bytes, PercentOfFileBytes(metadata_bytes),
2664                                   bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
2665                                   alignment_bytes, PercentOfFileBytes(alignment_bytes))
2666             << std::flush;
2667         CHECK_EQ(file_bytes,
2668                  header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2669                  dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2670                  sro_offset_bytes + metadata_bytes + bitmap_bytes + alignment_bytes);
2671       }
2672 
2673       os << "object_bytes breakdown:\n";
2674       size_t object_bytes_total = 0;
2675       for (const auto& sizes_and_count : sizes_and_counts) {
2676         const std::string& descriptor(sizes_and_count.first);
2677         double average = static_cast<double>(sizes_and_count.second.bytes) /
2678             static_cast<double>(sizes_and_count.second.count);
2679         double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
2680         os << StringPrintf("%32s %8zd bytes %6zd instances "
2681                            "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
2682                            descriptor.c_str(), sizes_and_count.second.bytes,
2683                            sizes_and_count.second.count, average, percent);
2684         object_bytes_total += sizes_and_count.second.bytes;
2685       }
2686       os << "\n" << std::flush;
2687       CHECK_EQ(object_bytes, object_bytes_total);
2688 
2689       os << StringPrintf("oat_file_bytes               = %8zd\n"
2690                          "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
2691                          "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2692                          "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2693                          "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2694                          "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
2695                          oat_file_bytes,
2696                          managed_code_bytes,
2697                          PercentOfOatBytes(managed_code_bytes),
2698                          native_to_managed_code_bytes,
2699                          PercentOfOatBytes(native_to_managed_code_bytes),
2700                          class_initializer_code_bytes,
2701                          PercentOfOatBytes(class_initializer_code_bytes),
2702                          large_initializer_code_bytes,
2703                          PercentOfOatBytes(large_initializer_code_bytes),
2704                          large_method_code_bytes,
2705                          PercentOfOatBytes(large_method_code_bytes))
2706             << "DexFile sizes:\n";
2707       for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
2708         os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
2709                            oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2710                            PercentOfOatBytes(oat_dex_file_size.second));
2711       }
2712 
2713       os << "\n" << StringPrintf("vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
2714                                  vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
2715          << std::flush;
2716 
2717       os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2718          << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
2719                          static_cast<double>(managed_code_bytes) /
2720                              static_cast<double>(dex_instruction_bytes),
2721                          static_cast<double>(managed_code_bytes_ignoring_deduplication) /
2722                              static_cast<double>(dex_instruction_bytes))
2723          << std::flush;
2724 
2725       DumpOutliers(os);
2726     }
2727   } stats_;
2728 
2729  private:
2730   enum {
2731     // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2732     // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2733     kLargeConstructorDexBytes = 4000,
2734     // Number of bytes for a method to be considered large. Based on the 4000 basic block
2735     // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2736     kLargeMethodDexBytes = 16000
2737   };
2738 
2739   // For performance, use the *os_ directly for anything that doesn't need indentation
2740   // and prepare an indentation stream with default indentation 1.
2741   std::ostream* os_;
2742   VariableIndentationOutputStream vios_;
2743   ScopedIndentation indent1_;
2744 
2745   gc::space::ImageSpace& image_space_;
2746   const ImageHeader& image_header_;
2747   std::unique_ptr<OatDumper> oat_dumper_;
2748   OatDumperOptions* oat_dumper_options_;
2749   std::set<mirror::Object*> dex_caches_;
2750 
2751   DISALLOW_COPY_AND_ASSIGN(ImageDumper);
2752 };
2753 
DumpImage(gc::space::ImageSpace * image_space,OatDumperOptions * options,std::ostream * os)2754 static int DumpImage(gc::space::ImageSpace* image_space,
2755                      OatDumperOptions* options,
2756                      std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
2757   const ImageHeader& image_header = image_space->GetImageHeader();
2758   if (!image_header.IsValid()) {
2759     LOG(ERROR) << "Invalid image header " << image_space->GetImageLocation();
2760     return EXIT_FAILURE;
2761   }
2762   ImageDumper image_dumper(os, *image_space, image_header, options);
2763   if (!image_dumper.Dump()) {
2764     return EXIT_FAILURE;
2765   }
2766   return EXIT_SUCCESS;
2767 }
2768 
DumpImages(Runtime * runtime,OatDumperOptions * options,std::ostream * os)2769 static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
2770   // Dumping the image, no explicit class loader.
2771   ScopedNullHandle<mirror::ClassLoader> null_class_loader;
2772   options->class_loader_ = &null_class_loader;
2773 
2774   ScopedObjectAccess soa(Thread::Current());
2775   if (options->app_image_ != nullptr) {
2776     if (options->app_oat_ == nullptr) {
2777       LOG(ERROR) << "Can not dump app image without app oat file";
2778       return EXIT_FAILURE;
2779     }
2780     // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2781     // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2782     // pointers into 32 bit pointer sized ArtMethods.
2783     std::string error_msg;
2784     std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
2785                                                     options->app_oat_,
2786                                                     options->app_oat_,
2787                                                     /*executable=*/ false,
2788                                                     /*low_4gb=*/ true,
2789                                                     &error_msg));
2790     if (oat_file == nullptr) {
2791       LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
2792       return EXIT_FAILURE;
2793     }
2794     std::unique_ptr<gc::space::ImageSpace> space(
2795         gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2796     if (space == nullptr) {
2797       LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2798                  << error_msg;
2799       return EXIT_FAILURE;
2800     }
2801     // Open dex files for the image.
2802     std::vector<std::unique_ptr<const DexFile>> dex_files;
2803     if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2804       LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2805                  << error_msg;
2806       return EXIT_FAILURE;
2807     }
2808     // Dump the actual image.
2809     int result = DumpImage(space.get(), options, os);
2810     if (result != EXIT_SUCCESS) {
2811       return result;
2812     }
2813     // Fall through to dump the boot images.
2814   }
2815 
2816   gc::Heap* heap = runtime->GetHeap();
2817   if (!heap->HasBootImageSpace()) {
2818     LOG(ERROR) << "No image spaces";
2819     return EXIT_FAILURE;
2820   }
2821   for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2822     int result = DumpImage(image_space, options, os);
2823     if (result != EXIT_SUCCESS) {
2824       return result;
2825     }
2826   }
2827   return EXIT_SUCCESS;
2828 }
2829 
InstallOatFile(Runtime * runtime,std::unique_ptr<OatFile> oat_file,std::vector<const DexFile * > * class_path)2830 static jobject InstallOatFile(Runtime* runtime,
2831                               std::unique_ptr<OatFile> oat_file,
2832                               std::vector<const DexFile*>* class_path)
2833     REQUIRES_SHARED(Locks::mutator_lock_) {
2834   Thread* self = Thread::Current();
2835   CHECK(self != nullptr);
2836   // Need well-known-classes.
2837   WellKnownClasses::Init(self->GetJniEnv());
2838 
2839   // Open dex files.
2840   OatFile* oat_file_ptr = oat_file.get();
2841   ClassLinker* class_linker = runtime->GetClassLinker();
2842   runtime->GetOatFileManager().RegisterOatFile(std::move(oat_file));
2843   for (const OatDexFile* odf : oat_file_ptr->GetOatDexFiles()) {
2844     std::string error_msg;
2845     const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
2846     CHECK(dex_file != nullptr) << error_msg;
2847     class_path->push_back(dex_file);
2848   }
2849 
2850   // Need a class loader. Fake that we're a compiler.
2851   // Note: this will run initializers through the unstarted runtime, so make sure it's
2852   //       initialized.
2853   interpreter::UnstartedRuntime::Initialize();
2854 
2855   jobject class_loader = class_linker->CreatePathClassLoader(self, *class_path);
2856 
2857   // Need to register dex files to get a working dex cache.
2858   for (const DexFile* dex_file : *class_path) {
2859     ObjPtr<mirror::DexCache> dex_cache = class_linker->RegisterDexFile(
2860         *dex_file, self->DecodeJObject(class_loader)->AsClassLoader());
2861     CHECK(dex_cache != nullptr);
2862   }
2863 
2864   return class_loader;
2865 }
2866 
DumpOatWithRuntime(Runtime * runtime,std::unique_ptr<OatFile> oat_file,OatDumperOptions * options,std::ostream * os)2867 static int DumpOatWithRuntime(Runtime* runtime,
2868                               std::unique_ptr<OatFile> oat_file,
2869                               OatDumperOptions* options,
2870                               std::ostream* os) {
2871   CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2872   ScopedObjectAccess soa(Thread::Current());
2873 
2874   OatFile* oat_file_ptr = oat_file.get();
2875   std::vector<const DexFile*> class_path;
2876   jobject class_loader = InstallOatFile(runtime, std::move(oat_file), &class_path);
2877 
2878   // Use the class loader while dumping.
2879   StackHandleScope<1> scope(soa.Self());
2880   Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2881       soa.Decode<mirror::ClassLoader>(class_loader));
2882   options->class_loader_ = &loader_handle;
2883 
2884   OatDumper oat_dumper(*oat_file_ptr, *options);
2885   bool success = oat_dumper.Dump(*os);
2886   return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2887 }
2888 
DumpOatWithoutRuntime(OatFile * oat_file,OatDumperOptions * options,std::ostream * os)2889 static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
2890   CHECK(oat_file != nullptr && options != nullptr);
2891   // No image = no class loader.
2892   ScopedNullHandle<mirror::ClassLoader> null_class_loader;
2893   options->class_loader_ = &null_class_loader;
2894 
2895   OatDumper oat_dumper(*oat_file, *options);
2896   bool success = oat_dumper.Dump(*os);
2897   return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2898 }
2899 
DumpOat(Runtime * runtime,const char * oat_filename,const char * dex_filename,OatDumperOptions * options,std::ostream * os)2900 static int DumpOat(Runtime* runtime,
2901                    const char* oat_filename,
2902                    const char* dex_filename,
2903                    OatDumperOptions* options,
2904                    std::ostream* os) {
2905   if (dex_filename == nullptr) {
2906     LOG(WARNING) << "No dex filename provided, "
2907                  << "oatdump might fail if the oat file does not contain the dex code.";
2908   }
2909   std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2910   ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2911                                             /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
2912   std::string error_msg;
2913   std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
2914                                                   oat_filename,
2915                                                   oat_filename,
2916                                                   /*executable=*/ false,
2917                                                   /*low_4gb=*/ false,
2918                                                   dex_filenames,
2919                                                   /*reservation=*/ nullptr,
2920                                                   &error_msg));
2921   if (oat_file == nullptr) {
2922     LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
2923     return EXIT_FAILURE;
2924   }
2925 
2926   if (runtime != nullptr) {
2927     return DumpOatWithRuntime(runtime, std::move(oat_file), options, os);
2928   } else {
2929     return DumpOatWithoutRuntime(oat_file.get(), options, os);
2930   }
2931 }
2932 
SymbolizeOat(const char * oat_filename,const char * dex_filename,std::string & output_name,bool no_bits)2933 static int SymbolizeOat(const char* oat_filename,
2934                         const char* dex_filename,
2935                         std::string& output_name,
2936                         bool no_bits) {
2937   std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2938   ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2939                                             /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
2940   std::string error_msg;
2941   std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
2942                                                   oat_filename,
2943                                                   oat_filename,
2944                                                   /*executable=*/ false,
2945                                                   /*low_4gb=*/ false,
2946                                                   dex_filenames,
2947                                                   /*reservation=*/ nullptr,
2948                                                   &error_msg));
2949   if (oat_file == nullptr) {
2950     LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
2951     return EXIT_FAILURE;
2952   }
2953 
2954   bool result;
2955   // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2956   // files for 64-bit code in the past.
2957   if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
2958     OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file.get(), output_name, no_bits);
2959     result = oat_symbolizer.Symbolize();
2960   } else {
2961     OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file.get(), output_name, no_bits);
2962     result = oat_symbolizer.Symbolize();
2963   }
2964   if (!result) {
2965     LOG(ERROR) << "Failed to symbolize";
2966     return EXIT_FAILURE;
2967   }
2968 
2969   return EXIT_SUCCESS;
2970 }
2971 
2972 class IMTDumper {
2973  public:
Dump(Runtime * runtime,const std::string & imt_file,bool dump_imt_stats,const char * oat_filename,const char * dex_filename)2974   static bool Dump(Runtime* runtime,
2975                    const std::string& imt_file,
2976                    bool dump_imt_stats,
2977                    const char* oat_filename,
2978                    const char* dex_filename) {
2979     Thread* self = Thread::Current();
2980 
2981     ScopedObjectAccess soa(self);
2982     StackHandleScope<1> scope(self);
2983     MutableHandle<mirror::ClassLoader> class_loader = scope.NewHandle<mirror::ClassLoader>(nullptr);
2984     std::vector<const DexFile*> class_path;
2985 
2986     if (oat_filename != nullptr) {
2987     std::string dex_filename_str((dex_filename != nullptr) ? dex_filename : "");
2988     ArrayRef<const std::string> dex_filenames(&dex_filename_str,
2989                                               /*size=*/ (dex_filename != nullptr) ? 1u : 0u);
2990       std::string error_msg;
2991       std::unique_ptr<OatFile> oat_file(OatFile::Open(/*zip_fd=*/ -1,
2992                                                       oat_filename,
2993                                                       oat_filename,
2994                                                       /*executable=*/ false,
2995                                                       /*low_4gb=*/false,
2996                                                       dex_filenames,
2997                                                       /*reservation=*/ nullptr,
2998                                                       &error_msg));
2999       if (oat_file == nullptr) {
3000         LOG(ERROR) << "Failed to open oat file from '" << oat_filename << "': " << error_msg;
3001         return false;
3002       }
3003 
3004       class_loader.Assign(soa.Decode<mirror::ClassLoader>(
3005           InstallOatFile(runtime, std::move(oat_file), &class_path)));
3006     } else {
3007       class_loader.Assign(nullptr);  // Boot classloader. Just here for explicit documentation.
3008       class_path = runtime->GetClassLinker()->GetBootClassPath();
3009     }
3010 
3011     if (!imt_file.empty()) {
3012       return DumpImt(runtime, imt_file, class_loader);
3013     }
3014 
3015     if (dump_imt_stats) {
3016       return DumpImtStats(runtime, class_path, class_loader);
3017     }
3018 
3019     LOG(FATAL) << "Should not reach here";
3020     UNREACHABLE();
3021   }
3022 
3023  private:
DumpImt(Runtime * runtime,const std::string & imt_file,Handle<mirror::ClassLoader> h_class_loader)3024   static bool DumpImt(Runtime* runtime,
3025                       const std::string& imt_file,
3026                       Handle<mirror::ClassLoader> h_class_loader)
3027       REQUIRES_SHARED(Locks::mutator_lock_) {
3028     std::vector<std::string> lines = ReadCommentedInputFromFile(imt_file);
3029     std::unordered_set<std::string> prepared;
3030 
3031     for (const std::string& line : lines) {
3032       // A line should be either a class descriptor, in which case we will dump the complete IMT,
3033       // or a class descriptor and an interface method, in which case we will lookup the method,
3034       // determine its IMT slot, and check the class' IMT.
3035       size_t first_space = line.find(' ');
3036       if (first_space == std::string::npos) {
3037         DumpIMTForClass(runtime, line, h_class_loader, &prepared);
3038       } else {
3039         DumpIMTForMethod(runtime,
3040                          line.substr(0, first_space),
3041                          line.substr(first_space + 1, std::string::npos),
3042                          h_class_loader,
3043                          &prepared);
3044       }
3045       std::cerr << std::endl;
3046     }
3047 
3048     return true;
3049   }
3050 
DumpImtStats(Runtime * runtime,const std::vector<const DexFile * > & dex_files,Handle<mirror::ClassLoader> h_class_loader)3051   static bool DumpImtStats(Runtime* runtime,
3052                            const std::vector<const DexFile*>& dex_files,
3053                            Handle<mirror::ClassLoader> h_class_loader)
3054       REQUIRES_SHARED(Locks::mutator_lock_) {
3055     size_t without_imt = 0;
3056     size_t with_imt = 0;
3057     std::map<size_t, size_t> histogram;
3058 
3059     ClassLinker* class_linker = runtime->GetClassLinker();
3060     const PointerSize pointer_size = class_linker->GetImagePointerSize();
3061     std::unordered_set<std::string> prepared;
3062 
3063     Thread* self = Thread::Current();
3064     StackHandleScope<1> scope(self);
3065     MutableHandle<mirror::Class> h_klass(scope.NewHandle<mirror::Class>(nullptr));
3066 
3067     for (const DexFile* dex_file : dex_files) {
3068       for (uint32_t class_def_index = 0;
3069            class_def_index != dex_file->NumClassDefs();
3070            ++class_def_index) {
3071         const dex::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
3072         const char* descriptor = dex_file->GetClassDescriptor(class_def);
3073         h_klass.Assign(class_linker->FindClass(self, descriptor, h_class_loader));
3074         if (h_klass == nullptr) {
3075           std::cerr << "Warning: could not load " << descriptor << std::endl;
3076           continue;
3077         }
3078 
3079         if (HasNoIMT(runtime, h_klass, pointer_size, &prepared)) {
3080           without_imt++;
3081           continue;
3082         }
3083 
3084         ImTable* im_table = PrepareAndGetImTable(runtime, h_klass, pointer_size, &prepared);
3085         if (im_table == nullptr) {
3086           // Should not happen, but accept.
3087           without_imt++;
3088           continue;
3089         }
3090 
3091         with_imt++;
3092         for (size_t imt_index = 0; imt_index != ImTable::kSize; ++imt_index) {
3093           ArtMethod* ptr = im_table->Get(imt_index, pointer_size);
3094           if (ptr->IsRuntimeMethod()) {
3095             if (ptr->IsImtUnimplementedMethod()) {
3096               histogram[0]++;
3097             } else {
3098               ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3099               histogram[current_table->NumEntries(pointer_size)]++;
3100             }
3101           } else {
3102             histogram[1]++;
3103           }
3104         }
3105       }
3106     }
3107 
3108     std::cerr << "IMT stats:"
3109               << std::endl << std::endl;
3110 
3111     std::cerr << "  " << with_imt << " classes with IMT."
3112               << std::endl << std::endl;
3113     std::cerr << "  " << without_imt << " classes without IMT (or copy from Object)."
3114               << std::endl << std::endl;
3115 
3116     double sum_one = 0;
3117     size_t count_one = 0;
3118 
3119     std::cerr << "  " << "IMT histogram" << std::endl;
3120     for (auto& bucket : histogram) {
3121       std::cerr << "    " << bucket.first << " " << bucket.second << std::endl;
3122       if (bucket.first > 0) {
3123         sum_one += bucket.second * bucket.first;
3124         count_one += bucket.second;
3125       }
3126     }
3127 
3128     double count_zero = count_one + histogram[0];
3129     std::cerr << "   Stats:" << std::endl;
3130     std::cerr << "     Average depth (including empty): " << (sum_one / count_zero) << std::endl;
3131     std::cerr << "     Average depth (excluding empty): " << (sum_one / count_one) << std::endl;
3132 
3133     return true;
3134   }
3135 
3136   // Return whether the given class has no IMT (or the one shared with java.lang.Object).
HasNoIMT(Runtime * runtime,Handle<mirror::Class> klass,const PointerSize pointer_size,std::unordered_set<std::string> * prepared)3137   static bool HasNoIMT(Runtime* runtime,
3138                        Handle<mirror::Class> klass,
3139                        const PointerSize pointer_size,
3140                        std::unordered_set<std::string>* prepared)
3141       REQUIRES_SHARED(Locks::mutator_lock_) {
3142     if (klass->IsObjectClass() || !klass->ShouldHaveImt()) {
3143       return true;
3144     }
3145 
3146     if (klass->GetImt(pointer_size) == nullptr) {
3147       PrepareClass(runtime, klass, prepared);
3148     }
3149 
3150     ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>();
3151     DCHECK(object_class->IsObjectClass());
3152 
3153     bool result = klass->GetImt(pointer_size) == object_class->GetImt(pointer_size);
3154 
3155     if (klass->GetIfTable()->Count() == 0) {
3156       DCHECK(result);
3157     }
3158 
3159     return result;
3160   }
3161 
PrintTable(ImtConflictTable * table,PointerSize pointer_size)3162   static void PrintTable(ImtConflictTable* table, PointerSize pointer_size)
3163       REQUIRES_SHARED(Locks::mutator_lock_) {
3164     if (table == nullptr) {
3165       std::cerr << "    <No IMT?>" << std::endl;
3166       return;
3167     }
3168     size_t table_index = 0;
3169     for (;;) {
3170       ArtMethod* ptr = table->GetInterfaceMethod(table_index, pointer_size);
3171       if (ptr == nullptr) {
3172         return;
3173       }
3174       table_index++;
3175       std::cerr << "    " << ptr->PrettyMethod(true) << std::endl;
3176     }
3177   }
3178 
PrepareAndGetImTable(Runtime * runtime,Thread * self,Handle<mirror::ClassLoader> h_loader,const std::string & class_name,const PointerSize pointer_size,ObjPtr<mirror::Class> * klass_out,std::unordered_set<std::string> * prepared)3179   static ImTable* PrepareAndGetImTable(Runtime* runtime,
3180                                        Thread* self,
3181                                        Handle<mirror::ClassLoader> h_loader,
3182                                        const std::string& class_name,
3183                                        const PointerSize pointer_size,
3184                                        /*out*/ ObjPtr<mirror::Class>* klass_out,
3185                                        /*inout*/ std::unordered_set<std::string>* prepared)
3186       REQUIRES_SHARED(Locks::mutator_lock_) {
3187     if (class_name.empty()) {
3188       return nullptr;
3189     }
3190 
3191     std::string descriptor;
3192     if (class_name[0] == 'L') {
3193       descriptor = class_name;
3194     } else {
3195       descriptor = DotToDescriptor(class_name.c_str());
3196     }
3197 
3198     ObjPtr<mirror::Class> klass =
3199         runtime->GetClassLinker()->FindClass(self, descriptor.c_str(), h_loader);
3200 
3201     if (klass == nullptr) {
3202       self->ClearException();
3203       std::cerr << "Did not find " <<  class_name << std::endl;
3204       *klass_out = nullptr;
3205       return nullptr;
3206     }
3207 
3208     StackHandleScope<1> scope(Thread::Current());
3209     Handle<mirror::Class> h_klass = scope.NewHandle<mirror::Class>(klass);
3210 
3211     ImTable* ret = PrepareAndGetImTable(runtime, h_klass, pointer_size, prepared);
3212     *klass_out = h_klass.Get();
3213     return ret;
3214   }
3215 
PrepareAndGetImTable(Runtime * runtime,Handle<mirror::Class> h_klass,const PointerSize pointer_size,std::unordered_set<std::string> * prepared)3216   static ImTable* PrepareAndGetImTable(Runtime* runtime,
3217                                        Handle<mirror::Class> h_klass,
3218                                        const PointerSize pointer_size,
3219                                        /*inout*/ std::unordered_set<std::string>* prepared)
3220       REQUIRES_SHARED(Locks::mutator_lock_) {
3221     PrepareClass(runtime, h_klass, prepared);
3222     return h_klass->GetImt(pointer_size);
3223   }
3224 
DumpIMTForClass(Runtime * runtime,const std::string & class_name,Handle<mirror::ClassLoader> h_loader,std::unordered_set<std::string> * prepared)3225   static void DumpIMTForClass(Runtime* runtime,
3226                               const std::string& class_name,
3227                               Handle<mirror::ClassLoader> h_loader,
3228                               std::unordered_set<std::string>* prepared)
3229       REQUIRES_SHARED(Locks::mutator_lock_) {
3230     const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3231     ObjPtr<mirror::Class> klass;
3232     ImTable* imt = PrepareAndGetImTable(runtime,
3233                                         Thread::Current(),
3234                                         h_loader,
3235                                         class_name,
3236                                         pointer_size,
3237                                         &klass,
3238                                         prepared);
3239     if (imt == nullptr) {
3240       return;
3241     }
3242 
3243     std::cerr << class_name << std::endl << " IMT:" << std::endl;
3244     for (size_t index = 0; index < ImTable::kSize; ++index) {
3245       std::cerr << "  " << index << ":" << std::endl;
3246       ArtMethod* ptr = imt->Get(index, pointer_size);
3247       if (ptr->IsRuntimeMethod()) {
3248         if (ptr->IsImtUnimplementedMethod()) {
3249           std::cerr << "    <empty>" << std::endl;
3250         } else {
3251           ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3252           PrintTable(current_table, pointer_size);
3253         }
3254       } else {
3255         std::cerr << "    " << ptr->PrettyMethod(true) << std::endl;
3256       }
3257     }
3258 
3259     std::cerr << " Interfaces:" << std::endl;
3260     // Run through iftable, find methods that slot here, see if they fit.
3261     ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
3262     for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3263       ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
3264       std::string iface_name;
3265       std::cerr << "  " << iface->GetDescriptor(&iface_name) << std::endl;
3266 
3267       for (ArtMethod& iface_method : iface->GetVirtualMethods(pointer_size)) {
3268         uint32_t class_hash, name_hash, signature_hash;
3269         ImTable::GetImtHashComponents(&iface_method, &class_hash, &name_hash, &signature_hash);
3270         uint32_t imt_slot = ImTable::GetImtIndex(&iface_method);
3271         std::cerr << "    " << iface_method.PrettyMethod(true)
3272             << " slot=" << imt_slot
3273             << std::hex
3274             << " class_hash=0x" << class_hash
3275             << " name_hash=0x" << name_hash
3276             << " signature_hash=0x" << signature_hash
3277             << std::dec
3278             << std::endl;
3279       }
3280     }
3281   }
3282 
DumpIMTForMethod(Runtime * runtime,const std::string & class_name,const std::string & method,Handle<mirror::ClassLoader> h_loader,std::unordered_set<std::string> * prepared)3283   static void DumpIMTForMethod(Runtime* runtime,
3284                                const std::string& class_name,
3285                                const std::string& method,
3286                                Handle<mirror::ClassLoader> h_loader,
3287                                /*inout*/ std::unordered_set<std::string>* prepared)
3288       REQUIRES_SHARED(Locks::mutator_lock_) {
3289     const PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
3290     ObjPtr<mirror::Class> klass;
3291     ImTable* imt = PrepareAndGetImTable(runtime,
3292                                         Thread::Current(),
3293                                         h_loader,
3294                                         class_name,
3295                                         pointer_size,
3296                                         &klass,
3297                                         prepared);
3298     if (imt == nullptr) {
3299       return;
3300     }
3301 
3302     std::cerr << class_name << " <" << method << ">" << std::endl;
3303     for (size_t index = 0; index < ImTable::kSize; ++index) {
3304       ArtMethod* ptr = imt->Get(index, pointer_size);
3305       if (ptr->IsRuntimeMethod()) {
3306         if (ptr->IsImtUnimplementedMethod()) {
3307           continue;
3308         }
3309 
3310         ImtConflictTable* current_table = ptr->GetImtConflictTable(pointer_size);
3311         if (current_table == nullptr) {
3312           continue;
3313         }
3314 
3315         size_t table_index = 0;
3316         for (;;) {
3317           ArtMethod* ptr2 = current_table->GetInterfaceMethod(table_index, pointer_size);
3318           if (ptr2 == nullptr) {
3319             break;
3320           }
3321           table_index++;
3322 
3323           std::string p_name = ptr2->PrettyMethod(true);
3324           if (android::base::StartsWith(p_name, method.c_str())) {
3325             std::cerr << "  Slot "
3326                       << index
3327                       << " ("
3328                       << current_table->NumEntries(pointer_size)
3329                       << ")"
3330                       << std::endl;
3331             PrintTable(current_table, pointer_size);
3332             return;
3333           }
3334         }
3335       } else {
3336         std::string p_name = ptr->PrettyMethod(true);
3337         if (android::base::StartsWith(p_name, method.c_str())) {
3338           std::cerr << "  Slot " << index << " (1)" << std::endl;
3339           std::cerr << "    " << p_name << std::endl;
3340         } else {
3341           // Run through iftable, find methods that slot here, see if they fit.
3342           ObjPtr<mirror::IfTable> if_table = klass->GetIfTable();
3343           for (size_t i = 0, num_interfaces = klass->GetIfTableCount(); i < num_interfaces; ++i) {
3344             ObjPtr<mirror::Class> iface = if_table->GetInterface(i);
3345             size_t num_methods = iface->NumDeclaredVirtualMethods();
3346             if (num_methods > 0) {
3347               for (ArtMethod& iface_method : iface->GetMethods(pointer_size)) {
3348                 if (ImTable::GetImtIndex(&iface_method) == index) {
3349                   std::string i_name = iface_method.PrettyMethod(true);
3350                   if (android::base::StartsWith(i_name, method.c_str())) {
3351                     std::cerr << "  Slot " << index << " (1)" << std::endl;
3352                     std::cerr << "    " << p_name << " (" << i_name << ")" << std::endl;
3353                   }
3354                 }
3355               }
3356             }
3357           }
3358         }
3359       }
3360     }
3361   }
3362 
3363   // Read lines from the given stream, dropping comments and empty lines
ReadCommentedInputStream(std::istream & in_stream)3364   static std::vector<std::string> ReadCommentedInputStream(std::istream& in_stream) {
3365     std::vector<std::string> output;
3366     while (in_stream.good()) {
3367       std::string dot;
3368       std::getline(in_stream, dot);
3369       if (android::base::StartsWith(dot, "#") || dot.empty()) {
3370         continue;
3371       }
3372       output.push_back(dot);
3373     }
3374     return output;
3375   }
3376 
3377   // Read lines from the given file, dropping comments and empty lines.
ReadCommentedInputFromFile(const std::string & input_filename)3378   static std::vector<std::string> ReadCommentedInputFromFile(const std::string& input_filename) {
3379     std::unique_ptr<std::ifstream> input_file(new std::ifstream(input_filename, std::ifstream::in));
3380     if (input_file.get() == nullptr) {
3381       LOG(ERROR) << "Failed to open input file " << input_filename;
3382       return std::vector<std::string>();
3383     }
3384     std::vector<std::string> result = ReadCommentedInputStream(*input_file);
3385     input_file->close();
3386     return result;
3387   }
3388 
3389   // Prepare a class, i.e., ensure it has a filled IMT. Will do so recursively for superclasses,
3390   // and note in the given set that the work was done.
PrepareClass(Runtime * runtime,Handle<mirror::Class> h_klass,std::unordered_set<std::string> * done)3391   static void PrepareClass(Runtime* runtime,
3392                            Handle<mirror::Class> h_klass,
3393                            /*inout*/ std::unordered_set<std::string>* done)
3394       REQUIRES_SHARED(Locks::mutator_lock_) {
3395     if (!h_klass->ShouldHaveImt()) {
3396       return;
3397     }
3398 
3399     std::string name;
3400     name = h_klass->GetDescriptor(&name);
3401 
3402     if (done->find(name) != done->end()) {
3403       return;
3404     }
3405     done->insert(name);
3406 
3407     if (h_klass->HasSuperClass()) {
3408       StackHandleScope<1> h(Thread::Current());
3409       PrepareClass(runtime, h.NewHandle<mirror::Class>(h_klass->GetSuperClass()), done);
3410     }
3411 
3412     if (!h_klass->IsTemp()) {
3413       runtime->GetClassLinker()->FillIMTAndConflictTables(h_klass.Get());
3414     }
3415   }
3416 };
3417 
3418 struct OatdumpArgs : public CmdlineArgs {
3419  protected:
3420   using Base = CmdlineArgs;
3421 
ParseCustomart::OatdumpArgs3422   ParseStatus ParseCustom(const char* raw_option,
3423                           size_t raw_option_length,
3424                           std::string* error_msg) override {
3425     DCHECK_EQ(strlen(raw_option), raw_option_length);
3426     {
3427       ParseStatus base_parse = Base::ParseCustom(raw_option, raw_option_length, error_msg);
3428       if (base_parse != kParseUnknownArgument) {
3429         return base_parse;
3430       }
3431     }
3432 
3433     std::string_view option(raw_option, raw_option_length);
3434     if (StartsWith(option, "--oat-file=")) {
3435       oat_filename_ = raw_option + strlen("--oat-file=");
3436     } else if (StartsWith(option, "--dex-file=")) {
3437       dex_filename_ = raw_option + strlen("--dex-file=");
3438     } else if (StartsWith(option, "--image=")) {
3439       image_location_ = raw_option + strlen("--image=");
3440     } else if (option == "--no-dump:vmap") {
3441       dump_vmap_ = false;
3442     } else if (option =="--dump:code_info_stack_maps") {
3443       dump_code_info_stack_maps_ = true;
3444     } else if (option == "--no-disassemble") {
3445       disassemble_code_ = false;
3446     } else if (option =="--header-only") {
3447       dump_header_only_ = true;
3448     } else if (StartsWith(option, "--symbolize=")) {
3449       oat_filename_ = raw_option + strlen("--symbolize=");
3450       symbolize_ = true;
3451     } else if (StartsWith(option, "--only-keep-debug")) {
3452       only_keep_debug_ = true;
3453     } else if (StartsWith(option, "--class-filter=")) {
3454       class_filter_ = raw_option + strlen("--class-filter=");
3455     } else if (StartsWith(option, "--method-filter=")) {
3456       method_filter_ = raw_option + strlen("--method-filter=");
3457     } else if (StartsWith(option, "--list-classes")) {
3458       list_classes_ = true;
3459     } else if (StartsWith(option, "--list-methods")) {
3460       list_methods_ = true;
3461     } else if (StartsWith(option, "--export-dex-to=")) {
3462       export_dex_location_ = raw_option + strlen("--export-dex-to=");
3463     } else if (StartsWith(option, "--addr2instr=")) {
3464       if (!android::base::ParseUint(raw_option + strlen("--addr2instr="), &addr2instr_)) {
3465         *error_msg = "Address conversion failed";
3466         return kParseError;
3467       }
3468     } else if (StartsWith(option, "--app-image=")) {
3469       app_image_ = raw_option + strlen("--app-image=");
3470     } else if (StartsWith(option, "--app-oat=")) {
3471       app_oat_ = raw_option + strlen("--app-oat=");
3472     } else if (StartsWith(option, "--dump-imt=")) {
3473       imt_dump_ = std::string(option.substr(strlen("--dump-imt=")));
3474     } else if (option == "--dump-imt-stats") {
3475       imt_stat_dump_ = true;
3476     } else {
3477       return kParseUnknownArgument;
3478     }
3479 
3480     return kParseOk;
3481   }
3482 
ParseChecksart::OatdumpArgs3483   ParseStatus ParseChecks(std::string* error_msg) override {
3484     // Infer boot image location from the image location if possible.
3485     if (boot_image_location_ == nullptr) {
3486       boot_image_location_ = image_location_;
3487     }
3488 
3489     // Perform the parent checks.
3490     ParseStatus parent_checks = Base::ParseChecks(error_msg);
3491     if (parent_checks != kParseOk) {
3492       return parent_checks;
3493     }
3494 
3495     // Perform our own checks.
3496     if (image_location_ == nullptr && oat_filename_ == nullptr) {
3497       *error_msg = "Either --image or --oat-file must be specified";
3498       return kParseError;
3499     } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
3500       *error_msg = "Either --image or --oat-file must be specified but not both";
3501       return kParseError;
3502     }
3503 
3504     return kParseOk;
3505   }
3506 
GetUsageart::OatdumpArgs3507   std::string GetUsage() const override {
3508     std::string usage;
3509 
3510     usage +=
3511         "Usage: oatdump [options] ...\n"
3512         "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
3513         "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
3514         "\n"
3515         // Either oat-file or image is required.
3516         "  --oat-file=<file.oat>: specifies an input oat filename.\n"
3517         "      Example: --oat-file=/system/framework/arm64/boot.oat\n"
3518         "\n"
3519         "  --image=<file.art>: specifies an input image location.\n"
3520         "      Example: --image=/system/framework/boot.art\n"
3521         "\n"
3522         "  --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
3523         " boot image (with --image) and app oat file (with --app-oat).\n"
3524         "      Example: --app-image=app.art\n"
3525         "\n"
3526         "  --app-oat=<file.odex>: specifies an input app oat.\n"
3527         "      Example: --app-oat=app.odex\n"
3528         "\n";
3529 
3530     usage += Base::GetUsage();
3531 
3532     usage +=  // Optional.
3533         "  --no-dump:vmap may be used to disable vmap dumping.\n"
3534         "      Example: --no-dump:vmap\n"
3535         "\n"
3536         "  --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
3537         "      Example: --dump:code_info_stack_maps\n"
3538         "\n"
3539         "  --no-disassemble may be used to disable disassembly.\n"
3540         "      Example: --no-disassemble\n"
3541         "\n"
3542         "  --header-only may be used to print only the oat header.\n"
3543         "      Example: --header-only\n"
3544         "\n"
3545         "  --list-classes may be used to list target file classes (can be used with filters).\n"
3546         "      Example: --list-classes\n"
3547         "      Example: --list-classes --class-filter=com.example.foo\n"
3548         "\n"
3549         "  --list-methods may be used to list target file methods (can be used with filters).\n"
3550         "      Example: --list-methods\n"
3551         "      Example: --list-methods --class-filter=com.example --method-filter=foo\n"
3552         "\n"
3553         "  --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
3554         "      Example: --symbolize=/system/framework/boot.oat\n"
3555         "\n"
3556         "  --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
3557         "      .rodata and .text sections are omitted in the output file to save space.\n"
3558         "      Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
3559         "\n"
3560         "  --class-filter=<class name>: only dumps classes that contain the filter.\n"
3561         "      Example: --class-filter=com.example.foo\n"
3562         "\n"
3563         "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
3564         "      Example: --method-filter=foo\n"
3565         "\n"
3566         "  --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
3567         "      Example: --export-dex-to=/data/local/tmp\n"
3568         "\n"
3569         "  --addr2instr=<address>: output matching method disassembled code from relative\n"
3570         "                          address (e.g. PC from crash dump)\n"
3571         "      Example: --addr2instr=0x00001a3b\n"
3572         "\n"
3573         "  --dump-imt=<file.txt>: output IMT collisions (if any) for the given receiver\n"
3574         "                         types and interface methods in the given file. The file\n"
3575         "                         is read line-wise, where each line should either be a class\n"
3576         "                         name or descriptor, or a class name/descriptor and a prefix\n"
3577         "                         of a complete method name (separated by a whitespace).\n"
3578         "      Example: --dump-imt=imt.txt\n"
3579         "\n"
3580         "  --dump-imt-stats: output IMT statistics for the given boot image\n"
3581         "      Example: --dump-imt-stats"
3582         "\n";
3583 
3584     return usage;
3585   }
3586 
3587  public:
3588   const char* oat_filename_ = nullptr;
3589   const char* dex_filename_ = nullptr;
3590   const char* class_filter_ = "";
3591   const char* method_filter_ = "";
3592   const char* image_location_ = nullptr;
3593   std::string elf_filename_prefix_;
3594   std::string imt_dump_;
3595   bool dump_vmap_ = true;
3596   bool dump_code_info_stack_maps_ = false;
3597   bool disassemble_code_ = true;
3598   bool symbolize_ = false;
3599   bool only_keep_debug_ = false;
3600   bool list_classes_ = false;
3601   bool list_methods_ = false;
3602   bool dump_header_only_ = false;
3603   bool imt_stat_dump_ = false;
3604   uint32_t addr2instr_ = 0;
3605   const char* export_dex_location_ = nullptr;
3606   const char* app_image_ = nullptr;
3607   const char* app_oat_ = nullptr;
3608 };
3609 
3610 struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
NeedsRuntimeart::OatdumpMain3611   bool NeedsRuntime() override {
3612     CHECK(args_ != nullptr);
3613 
3614     // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
3615     bool absolute_addresses = (args_->oat_filename_ == nullptr);
3616 
3617     oat_dumper_options_.reset(new OatDumperOptions(
3618         args_->dump_vmap_,
3619         args_->dump_code_info_stack_maps_,
3620         args_->disassemble_code_,
3621         absolute_addresses,
3622         args_->class_filter_,
3623         args_->method_filter_,
3624         args_->list_classes_,
3625         args_->list_methods_,
3626         args_->dump_header_only_,
3627         args_->export_dex_location_,
3628         args_->app_image_,
3629         args_->app_oat_,
3630         args_->addr2instr_));
3631 
3632     return (args_->boot_image_location_ != nullptr ||
3633             args_->image_location_ != nullptr ||
3634             !args_->imt_dump_.empty()) &&
3635           !args_->symbolize_;
3636   }
3637 
ExecuteWithoutRuntimeart::OatdumpMain3638   bool ExecuteWithoutRuntime() override {
3639     CHECK(args_ != nullptr);
3640     CHECK(args_->oat_filename_ != nullptr);
3641 
3642     MemMap::Init();
3643 
3644     if (args_->symbolize_) {
3645       // ELF has special kind of section called SHT_NOBITS which allows us to create
3646       // sections which exist but their data is omitted from the ELF file to save space.
3647       // This is what "strip --only-keep-debug" does when it creates separate ELF file
3648       // with only debug data. We use it in similar way to exclude .rodata and .text.
3649       bool no_bits = args_->only_keep_debug_;
3650       return SymbolizeOat(args_->oat_filename_, args_->dex_filename_, args_->output_name_, no_bits)
3651           == EXIT_SUCCESS;
3652     } else {
3653       return DumpOat(nullptr,
3654                      args_->oat_filename_,
3655                      args_->dex_filename_,
3656                      oat_dumper_options_.get(),
3657                      args_->os_) == EXIT_SUCCESS;
3658     }
3659   }
3660 
ExecuteWithRuntimeart::OatdumpMain3661   bool ExecuteWithRuntime(Runtime* runtime) override {
3662     CHECK(args_ != nullptr);
3663 
3664     if (!args_->imt_dump_.empty() || args_->imt_stat_dump_) {
3665       return IMTDumper::Dump(runtime,
3666                              args_->imt_dump_,
3667                              args_->imt_stat_dump_,
3668                              args_->oat_filename_,
3669                              args_->dex_filename_);
3670     }
3671 
3672     if (args_->oat_filename_ != nullptr) {
3673       return DumpOat(runtime,
3674                      args_->oat_filename_,
3675                      args_->dex_filename_,
3676                      oat_dumper_options_.get(),
3677                      args_->os_) == EXIT_SUCCESS;
3678     }
3679 
3680     return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
3681   }
3682 
3683   std::unique_ptr<OatDumperOptions> oat_dumper_options_;
3684 };
3685 
3686 }  // namespace art
3687 
main(int argc,char ** argv)3688 int main(int argc, char** argv) {
3689   // Output all logging to stderr.
3690   android::base::SetLogger(android::base::StderrLogger);
3691 
3692   art::OatdumpMain main;
3693   return main.Main(argc, argv);
3694 }
3695