oatdump.cc revision 590fee9e8972f872301c2d16a575d579ee564bee
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19
20#include <fstream>
21#include <iostream>
22#include <string>
23#include <vector>
24
25#include "base/stringpiece.h"
26#include "base/unix_file/fd_file.h"
27#include "class_linker.h"
28#include "class_linker-inl.h"
29#include "dex_file-inl.h"
30#include "dex_instruction.h"
31#include "disassembler.h"
32#include "gc_map.h"
33#include "gc/space/image_space.h"
34#include "gc/space/large_object_space.h"
35#include "gc/space/space-inl.h"
36#include "image.h"
37#include "indenter.h"
38#include "mapping_table.h"
39#include "mirror/art_field-inl.h"
40#include "mirror/art_method-inl.h"
41#include "mirror/array-inl.h"
42#include "mirror/class-inl.h"
43#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
45#include "oat.h"
46#include "object_utils.h"
47#include "os.h"
48#include "runtime.h"
49#include "safe_map.h"
50#include "scoped_thread_state_change.h"
51#include "verifier/method_verifier.h"
52#include "vmap_table.h"
53
54namespace art {
55
56static void usage() {
57  fprintf(stderr,
58          "Usage: oatdump [options] ...\n"
59          "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n"
60          "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
61          "\n");
62  fprintf(stderr,
63          "  --oat-file=<file.oat>: specifies an input oat filename.\n"
64          "      Example: --oat-file=/system/framework/boot.oat\n"
65          "\n");
66  fprintf(stderr,
67          "  --image=<file.art>: specifies an input image filename.\n"
68          "      Example: --image=/system/framework/boot.art\n"
69          "\n");
70  fprintf(stderr,
71          "  --boot-image=<file.art>: provide the image file for the boot class path.\n"
72          "      Example: --boot-image=/system/framework/boot.art\n"
73          "\n");
74  fprintf(stderr,
75          "  --host-prefix may be used to translate host paths to target paths during\n"
76          "      cross compilation.\n"
77          "      Example: --host-prefix=out/target/product/crespo\n"
78          "      Default: $ANDROID_PRODUCT_OUT\n"
79          "\n");
80  fprintf(stderr,
81          "  --output=<file> may be used to send the output to a file.\n"
82          "      Example: --output=/tmp/oatdump.txt\n"
83          "\n");
84  exit(EXIT_FAILURE);
85}
86
87const char* image_roots_descriptions_[] = {
88  "kResolutionMethod",
89  "kImtConflictMethod",
90  "kDefaultImt",
91  "kCalleeSaveMethod",
92  "kRefsOnlySaveMethod",
93  "kRefsAndArgsSaveMethod",
94  "kOatLocation",
95  "kDexCaches",
96  "kClassRoots",
97};
98
99class OatDumper {
100 public:
101  explicit OatDumper(const std::string& host_prefix, const OatFile& oat_file)
102    : host_prefix_(host_prefix),
103      oat_file_(oat_file),
104      oat_dex_files_(oat_file.GetOatDexFiles()),
105      disassembler_(Disassembler::Create(oat_file_.GetOatHeader().GetInstructionSet())) {
106    AddAllOffsets();
107  }
108
109  void Dump(std::ostream& os) {
110    const OatHeader& oat_header = oat_file_.GetOatHeader();
111
112    os << "MAGIC:\n";
113    os << oat_header.GetMagic() << "\n\n";
114
115    os << "CHECKSUM:\n";
116    os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
117
118    os << "INSTRUCTION SET:\n";
119    os << oat_header.GetInstructionSet() << "\n\n";
120
121    os << "INSTRUCTION SET FEATURES:\n";
122    os << oat_header.GetInstructionSetFeatures().GetFeatureString() << "\n\n";
123
124    os << "DEX FILE COUNT:\n";
125    os << oat_header.GetDexFileCount() << "\n\n";
126
127    os << "EXECUTABLE OFFSET:\n";
128    os << StringPrintf("0x%08x\n\n", oat_header.GetExecutableOffset());
129
130    os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
131    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
132
133    os << "IMAGE FILE LOCATION OAT BEGIN:\n";
134    os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
135
136    os << "IMAGE FILE LOCATION:\n";
137    const std::string image_file_location(oat_header.GetImageFileLocation());
138    os << image_file_location;
139    if (!image_file_location.empty() && !host_prefix_.empty()) {
140      os << " (" << host_prefix_ << image_file_location << ")";
141    }
142    os << "\n\n";
143
144    os << "BEGIN:\n";
145    os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
146
147    os << "END:\n";
148    os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
149
150    os << std::flush;
151
152    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
153      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
154      CHECK(oat_dex_file != NULL);
155      DumpOatDexFile(os, *oat_dex_file);
156    }
157  }
158
159  size_t ComputeSize(const void* oat_data) {
160    if (reinterpret_cast<const byte*>(oat_data) < oat_file_.Begin() ||
161        reinterpret_cast<const byte*>(oat_data) > oat_file_.End()) {
162      return 0;  // Address not in oat file
163    }
164    uint32_t begin_offset = reinterpret_cast<size_t>(oat_data) -
165                            reinterpret_cast<size_t>(oat_file_.Begin());
166    typedef std::set<uint32_t>::iterator It;
167    It it = offsets_.upper_bound(begin_offset);
168    CHECK(it != offsets_.end());
169    uint32_t end_offset = *it;
170    return end_offset - begin_offset;
171  }
172
173  InstructionSet GetInstructionSet() {
174    return oat_file_.GetOatHeader().GetInstructionSet();
175  }
176
177  const void* GetOatCode(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
178    MethodHelper mh(m);
179    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
180      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
181      CHECK(oat_dex_file != nullptr);
182      std::string error_msg;
183      UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
184      if (dex_file.get() == nullptr) {
185        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
186            << "': " << error_msg;
187      } else {
188        const DexFile::ClassDef* class_def =
189            dex_file->FindClassDef(mh.GetDeclaringClassDescriptor());
190        if (class_def != NULL) {
191          uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
192          const OatFile::OatClass* oat_class = oat_dex_file->GetOatClass(class_def_index);
193          CHECK(oat_class != NULL);
194          size_t method_index = m->GetMethodIndex();
195          return oat_class->GetOatMethod(method_index).GetCode();
196        }
197      }
198    }
199    return NULL;
200  }
201
202 private:
203  void AddAllOffsets() {
204    // We don't know the length of the code for each method, but we need to know where to stop
205    // when disassembling. What we do know is that a region of code will be followed by some other
206    // region, so if we keep a sorted sequence of the start of each region, we can infer the length
207    // of a piece of code by using upper_bound to find the start of the next region.
208    for (size_t i = 0; i < oat_dex_files_.size(); i++) {
209      const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
210      CHECK(oat_dex_file != NULL);
211      std::string error_msg;
212      UniquePtr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
213      if (dex_file.get() == nullptr) {
214        LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
215            << "': " << error_msg;
216        continue;
217      }
218      offsets_.insert(reinterpret_cast<uint32_t>(&dex_file->GetHeader()));
219      for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
220        const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
221        UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file->GetOatClass(class_def_index));
222        const byte* class_data = dex_file->GetClassData(class_def);
223        if (class_data != NULL) {
224          ClassDataItemIterator it(*dex_file, class_data);
225          SkipAllFields(it);
226          uint32_t class_method_index = 0;
227          while (it.HasNextDirectMethod()) {
228            AddOffsets(oat_class->GetOatMethod(class_method_index++));
229            it.Next();
230          }
231          while (it.HasNextVirtualMethod()) {
232            AddOffsets(oat_class->GetOatMethod(class_method_index++));
233            it.Next();
234          }
235        }
236      }
237    }
238
239    // If the last thing in the file is code for a method, there won't be an offset for the "next"
240    // thing. Instead of having a special case in the upper_bound code, let's just add an entry
241    // for the end of the file.
242    offsets_.insert(static_cast<uint32_t>(oat_file_.Size()));
243  }
244
245  void AddOffsets(const OatFile::OatMethod& oat_method) {
246    uint32_t code_offset = oat_method.GetCodeOffset();
247    if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
248      code_offset &= ~0x1;
249    }
250    offsets_.insert(code_offset);
251    offsets_.insert(oat_method.GetMappingTableOffset());
252    offsets_.insert(oat_method.GetVmapTableOffset());
253    offsets_.insert(oat_method.GetNativeGcMapOffset());
254  }
255
256  void DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
257    os << "OAT DEX FILE:\n";
258    os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
259    os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
260
261    // Create the verifier early.
262
263    std::string error_msg;
264    UniquePtr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
265    if (dex_file.get() == NULL) {
266      os << "NOT FOUND: " << error_msg << "\n\n";
267      return;
268    }
269    for (size_t class_def_index = 0; class_def_index < dex_file->NumClassDefs(); class_def_index++) {
270      const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
271      const char* descriptor = dex_file->GetClassDescriptor(class_def);
272      UniquePtr<const OatFile::OatClass> oat_class(oat_dex_file.GetOatClass(class_def_index));
273      CHECK(oat_class.get() != NULL);
274      os << StringPrintf("%zd: %s (type_idx=%d)", class_def_index, descriptor, class_def.class_idx_)
275         << " (" << oat_class->GetStatus() << ")"
276         << " (" << oat_class->GetType() << ")\n";
277      // TODO: include bitmap here if type is kOatClassBitmap?
278      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
279      std::ostream indented_os(&indent_filter);
280      DumpOatClass(indented_os, *oat_class.get(), *(dex_file.get()), class_def);
281    }
282
283    os << std::flush;
284  }
285
286  static void SkipAllFields(ClassDataItemIterator& it) {
287    while (it.HasNextStaticField()) {
288      it.Next();
289    }
290    while (it.HasNextInstanceField()) {
291      it.Next();
292    }
293  }
294
295  void DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
296                    const DexFile::ClassDef& class_def) {
297    const byte* class_data = dex_file.GetClassData(class_def);
298    if (class_data == NULL) {  // empty class such as a marker interface?
299      return;
300    }
301    ClassDataItemIterator it(dex_file, class_data);
302    SkipAllFields(it);
303    uint32_t class_method_idx = 0;
304    while (it.HasNextDirectMethod()) {
305      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
306      DumpOatMethod(os, class_def, class_method_idx, oat_method, dex_file,
307                    it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
308      class_method_idx++;
309      it.Next();
310    }
311    while (it.HasNextVirtualMethod()) {
312      const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
313      DumpOatMethod(os, class_def, class_method_idx, oat_method, dex_file,
314                    it.GetMemberIndex(), it.GetMethodCodeItem(), it.GetMemberAccessFlags());
315      class_method_idx++;
316      it.Next();
317    }
318    DCHECK(!it.HasNext());
319    os << std::flush;
320  }
321
322  void DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
323                     uint32_t class_method_index,
324                     const OatFile::OatMethod& oat_method, const DexFile& dex_file,
325                     uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
326                     uint32_t method_access_flags) {
327    os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
328                       class_method_index, PrettyMethod(dex_method_idx, dex_file, true).c_str(),
329                       dex_method_idx);
330    Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
331    std::ostream indent1_os(&indent1_filter);
332    {
333      indent1_os << "DEX CODE:\n";
334      Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
335      std::ostream indent2_os(&indent2_filter);
336      DumpDexCode(indent2_os, dex_file, code_item);
337    }
338    if (Runtime::Current() != NULL) {
339      indent1_os << "VERIFIER TYPE ANALYSIS:\n";
340      Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
341      std::ostream indent2_os(&indent2_filter);
342      DumpVerifier(indent2_os, dex_method_idx, &dex_file, class_def, code_item,
343                   method_access_flags);
344    }
345    {
346      indent1_os << "OAT DATA:\n";
347      Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
348      std::ostream indent2_os(&indent2_filter);
349
350      indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
351      indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
352      DumpSpillMask(indent2_os, oat_method.GetCoreSpillMask(), false);
353      indent2_os << StringPrintf("\nfp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
354      DumpSpillMask(indent2_os, oat_method.GetFpSpillMask(), true);
355      indent2_os << StringPrintf("\nvmap_table: %p (offset=0x%08x)\n",
356                                 oat_method.GetVmapTable(), oat_method.GetVmapTableOffset());
357      DumpVmap(indent2_os, oat_method);
358      indent2_os << StringPrintf("mapping_table: %p (offset=0x%08x)\n",
359                                 oat_method.GetMappingTable(), oat_method.GetMappingTableOffset());
360      const bool kDumpRawMappingTable = false;
361      if (kDumpRawMappingTable) {
362        Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count);
363        std::ostream indent3_os(&indent3_filter);
364        DumpMappingTable(indent3_os, oat_method);
365      }
366      indent2_os << StringPrintf("gc_map: %p (offset=0x%08x)\n",
367                                 oat_method.GetNativeGcMap(), oat_method.GetNativeGcMapOffset());
368      const bool kDumpRawGcMap = false;
369      if (kDumpRawGcMap) {
370        Indenter indent3_filter(indent2_os.rdbuf(), kIndentChar, kIndentBy1Count);
371        std::ostream indent3_os(&indent3_filter);
372        DumpGcMap(indent3_os, oat_method, code_item);
373      }
374    }
375    {
376      indent1_os << StringPrintf("CODE: %p (offset=0x%08x size=%d)%s\n",
377                                 oat_method.GetCode(),
378                                 oat_method.GetCodeOffset(),
379                                 oat_method.GetCodeSize(),
380                                 oat_method.GetCode() != NULL ? "..." : "");
381      Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
382      std::ostream indent2_os(&indent2_filter);
383
384      Runtime* runtime = Runtime::Current();
385      if (runtime != nullptr) {
386        ScopedObjectAccess soa(Thread::Current());
387        SirtRef<mirror::DexCache> dex_cache(
388            soa.Self(), runtime->GetClassLinker()->FindDexCache(dex_file));
389        SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
390        verifier::MethodVerifier verifier(&dex_file, &dex_cache, &class_loader, &class_def, code_item,
391                                          dex_method_idx, nullptr, method_access_flags, true, true);
392        verifier.Verify();
393        DumpCode(indent2_os, &verifier, oat_method, code_item);
394      } else {
395        DumpCode(indent2_os, nullptr, oat_method, code_item);
396      }
397    }
398  }
399
400  void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
401    if (spill_mask == 0) {
402      return;
403    }
404    os << "(";
405    for (size_t i = 0; i < 32; i++) {
406      if ((spill_mask & (1 << i)) != 0) {
407        if (is_float) {
408          os << "fr" << i;
409        } else {
410          os << "r" << i;
411        }
412        spill_mask ^= 1 << i;  // clear bit
413        if (spill_mask != 0) {
414          os << ", ";
415        } else {
416          break;
417        }
418      }
419    }
420    os << ")";
421  }
422
423  void DumpVmap(std::ostream& os, const OatFile::OatMethod& oat_method) {
424    const uint8_t* raw_table = oat_method.GetVmapTable();
425    if (raw_table != NULL) {
426      const VmapTable vmap_table(raw_table);
427      bool first = true;
428      bool processing_fp = false;
429      uint32_t spill_mask = oat_method.GetCoreSpillMask();
430      for (size_t i = 0; i < vmap_table.Size(); i++) {
431        uint16_t dex_reg = vmap_table[i];
432        uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
433                                                      processing_fp ? kFloatVReg : kIntVReg);
434        os << (first ? "v" : ", v")  << dex_reg;
435        if (!processing_fp) {
436          os << "/r" << cpu_reg;
437        } else {
438          os << "/fr" << cpu_reg;
439        }
440        first = false;
441        if (!processing_fp && dex_reg == 0xFFFF) {
442          processing_fp = true;
443          spill_mask = oat_method.GetFpSpillMask();
444        }
445      }
446      os << "\n";
447    }
448  }
449
450  void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
451                    const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
452    const uint8_t* raw_table = oat_method.GetVmapTable();
453    if (raw_table != NULL) {
454      const VmapTable vmap_table(raw_table);
455      uint32_t vmap_offset;
456      if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
457        bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
458        uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
459                                       : oat_method.GetCoreSpillMask();
460        os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
461      } else {
462        uint32_t offset = StackVisitor::GetVRegOffset(code_item, oat_method.GetCoreSpillMask(),
463                                                      oat_method.GetFpSpillMask(),
464                                                      oat_method.GetFrameSizeInBytes(), reg);
465        os << "[sp + #" << offset << "]";
466      }
467    }
468  }
469
470  void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
471                 const DexFile::CodeItem* code_item) {
472    const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
473    if (gc_map_raw == NULL) {
474      return;
475    }
476    NativePcOffsetToReferenceMap map(gc_map_raw);
477    const void* code = oat_method.GetCode();
478    for (size_t entry = 0; entry < map.NumEntries(); entry++) {
479      const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code) +
480                                 map.GetNativePcOffset(entry);
481      os << StringPrintf("%p", native_pc);
482      size_t num_regs = map.RegWidth() * 8;
483      const uint8_t* reg_bitmap = map.GetBitMap(entry);
484      bool first = true;
485      for (size_t reg = 0; reg < num_regs; reg++) {
486        if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
487          if (first) {
488            os << "  v" << reg << " (";
489            DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
490            os << ")";
491            first = false;
492          } else {
493            os << ", v" << reg << " (";
494            DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
495            os << ")";
496          }
497        }
498      }
499      os << "\n";
500    }
501  }
502
503  void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
504    const void* code = oat_method.GetCode();
505    if (code == NULL) {
506      return;
507    }
508    MappingTable table(oat_method.GetMappingTable());
509    if (table.TotalSize() != 0) {
510      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
511      std::ostream indent_os(&indent_filter);
512      if (table.PcToDexSize() != 0) {
513        typedef MappingTable::PcToDexIterator It;
514        os << "suspend point mappings {\n";
515        for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
516          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
517        }
518        os << "}\n";
519      }
520      if (table.DexToPcSize() != 0) {
521        typedef MappingTable::DexToPcIterator It;
522        os << "catch entry mappings {\n";
523        for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
524          indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
525        }
526        os << "}\n";
527      }
528    }
529  }
530
531  uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
532                               size_t offset, bool suspend_point_mapping) {
533    MappingTable table(oat_method.GetMappingTable());
534    if (suspend_point_mapping && table.PcToDexSize() > 0) {
535      typedef MappingTable::PcToDexIterator It;
536      for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
537        if (offset == cur.NativePcOffset()) {
538          os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
539          return cur.DexPc();
540        }
541      }
542    } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
543      typedef MappingTable::DexToPcIterator It;
544      for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
545        if (offset == cur.NativePcOffset()) {
546          os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
547          return cur.DexPc();
548        }
549      }
550    }
551    return DexFile::kDexNoIndex;
552  }
553
554  void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
555                                 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
556    const uint8_t* gc_map_raw = oat_method.GetNativeGcMap();
557    if (gc_map_raw != NULL) {
558      NativePcOffsetToReferenceMap map(gc_map_raw);
559      if (map.HasEntry(native_pc_offset)) {
560        size_t num_regs = map.RegWidth() * 8;
561        const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
562        bool first = true;
563        for (size_t reg = 0; reg < num_regs; reg++) {
564          if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
565            if (first) {
566              os << "GC map objects:  v" << reg << " (";
567              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
568              os << ")";
569              first = false;
570            } else {
571              os << ", v" << reg << " (";
572              DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
573              os << ")";
574            }
575          }
576        }
577        if (!first) {
578          os << "\n";
579        }
580      }
581    }
582  }
583
584  void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
585                        const OatFile::OatMethod& oat_method,
586                        const DexFile::CodeItem* code_item, uint32_t dex_pc) {
587    DCHECK(verifier != nullptr);
588    std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
589    bool first = true;
590    for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
591      VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
592      if (kind != kUndefined) {
593        if (first) {
594          os << "VRegs:  v";
595          first = false;
596        } else {
597          os << ", v";
598        }
599        os << reg << " (";
600        switch (kind) {
601          case kImpreciseConstant:
602            os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
603            DescribeVReg(os, oat_method, code_item, reg, kind);
604            break;
605          case kConstant:
606            os << "Constant: " << kinds.at((reg * 2) + 1);
607            break;
608          default:
609            DescribeVReg(os, oat_method, code_item, reg, kind);
610            break;
611        }
612        os << ")";
613      }
614    }
615    if (!first) {
616      os << "\n";
617    }
618  }
619
620
621  void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
622    if (code_item != NULL) {
623      size_t i = 0;
624      while (i < code_item->insns_size_in_code_units_) {
625        const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
626        os << StringPrintf("0x%04zx: %s\n", i, instruction->DumpString(&dex_file).c_str());
627        i += instruction->SizeInCodeUnits();
628      }
629    }
630  }
631
632  void DumpVerifier(std::ostream& os, uint32_t dex_method_idx, const DexFile* dex_file,
633                    const DexFile::ClassDef& class_def, const DexFile::CodeItem* code_item,
634                    uint32_t method_access_flags) {
635    if ((method_access_flags & kAccNative) == 0) {
636      ScopedObjectAccess soa(Thread::Current());
637      SirtRef<mirror::DexCache> dex_cache(soa.Self(), Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file));
638      SirtRef<mirror::ClassLoader> class_loader(soa.Self(), nullptr);
639      verifier::MethodVerifier::VerifyMethodAndDump(os, dex_method_idx, dex_file, dex_cache,
640                                                    class_loader, &class_def, code_item, NULL,
641                                                    method_access_flags);
642    }
643  }
644
645  void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
646                const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item) {
647    const void* code = oat_method.GetCode();
648    size_t code_size = oat_method.GetCodeSize();
649    if (code == NULL || code_size == 0) {
650      os << "NO CODE!\n";
651      return;
652    }
653    const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(code);
654    size_t offset = 0;
655    while (offset < code_size) {
656      DumpMappingAtOffset(os, oat_method, offset, false);
657      offset += disassembler_->Dump(os, native_pc + offset);
658      uint32_t dex_pc = DumpMappingAtOffset(os, oat_method, offset, true);
659      if (dex_pc != DexFile::kDexNoIndex) {
660        DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
661        if (verifier != nullptr) {
662          DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
663        }
664      }
665    }
666  }
667
668  const std::string host_prefix_;
669  const OatFile& oat_file_;
670  std::vector<const OatFile::OatDexFile*> oat_dex_files_;
671  std::set<uint32_t> offsets_;
672  UniquePtr<Disassembler> disassembler_;
673};
674
675class ImageDumper {
676 public:
677  explicit ImageDumper(std::ostream* os, const std::string& image_filename,
678                       const std::string& host_prefix, gc::space::ImageSpace& image_space,
679                       const ImageHeader& image_header)
680      : os_(os), image_filename_(image_filename), host_prefix_(host_prefix),
681        image_space_(image_space), image_header_(image_header) {}
682
683  void Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
684    std::ostream& os = *os_;
685    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
686
687    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
688
689    os << "IMAGE BITMAP OFFSET: " << reinterpret_cast<void*>(image_header_.GetImageBitmapOffset())
690       << " SIZE: " << reinterpret_cast<void*>(image_header_.GetImageBitmapSize()) << "\n\n";
691
692    os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
693
694    os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
695
696    os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
697
698    os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
699
700    os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
701
702    {
703      os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
704      Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
705      std::ostream indent1_os(&indent1_filter);
706      CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax));
707      for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
708        ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
709        const char* image_root_description = image_roots_descriptions_[i];
710        mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
711        indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
712        if (image_root_object->IsObjectArray()) {
713          Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
714          std::ostream indent2_os(&indent2_filter);
715          mirror::ObjectArray<mirror::Object>* image_root_object_array
716              = image_root_object->AsObjectArray<mirror::Object>();
717          for (int i = 0; i < image_root_object_array->GetLength(); i++) {
718            mirror::Object* value = image_root_object_array->Get(i);
719            size_t run = 0;
720            for (int32_t j = i + 1; j < image_root_object_array->GetLength(); j++) {
721              if (value == image_root_object_array->Get(j)) {
722                run++;
723              } else {
724                break;
725              }
726            }
727            if (run == 0) {
728              indent2_os << StringPrintf("%d: ", i);
729            } else {
730              indent2_os << StringPrintf("%d to %zd: ", i, i + run);
731              i = i + run;
732            }
733            if (value != NULL) {
734              PrettyObjectValue(indent2_os, value->GetClass(), value);
735            } else {
736              indent2_os << i << ": null\n";
737            }
738          }
739        }
740      }
741    }
742    os << "\n";
743
744    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
745    mirror::Object* oat_location_object = image_header_.GetImageRoot(ImageHeader::kOatLocation);
746    std::string oat_location(oat_location_object->AsString()->ToModifiedUtf8());
747    os << "OAT LOCATION: " << oat_location;
748    if (!host_prefix_.empty()) {
749      oat_location = host_prefix_ + oat_location;
750      os << " (" << oat_location << ")";
751    }
752    os << "\n";
753    std::string error_msg;
754    const OatFile* oat_file = class_linker->FindOatFileFromOatLocation(oat_location, &error_msg);
755    if (oat_file == NULL) {
756      os << "NOT FOUND: " << error_msg << "\n";
757      return;
758    }
759    os << "\n";
760
761    stats_.oat_file_bytes = oat_file->Size();
762
763    oat_dumper_.reset(new OatDumper(host_prefix_, *oat_file));
764
765    for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
766      CHECK(oat_dex_file != NULL);
767      stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
768                                                         oat_dex_file->FileSize()));
769    }
770
771    os << "OBJECTS:\n" << std::flush;
772
773    // Loop through all the image spaces and dump their objects.
774    gc::Heap* heap = Runtime::Current()->GetHeap();
775    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
776    Thread* self = Thread::Current();
777    {
778      WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
779      heap->FlushAllocStack();
780    }
781    {
782      std::ostream* saved_os = os_;
783      Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
784      std::ostream indent_os(&indent_filter);
785      os_ = &indent_os;
786      ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
787      for (const auto& space : spaces) {
788        if (space->IsImageSpace()) {
789          gc::space::ImageSpace* image_space = space->AsImageSpace();
790          image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
791          indent_os << "\n";
792        }
793      }
794      // Dump the large objects separately.
795      heap->GetLargeObjectsSpace()->GetLiveObjects()->Walk(ImageDumper::Callback, this);
796      indent_os << "\n";
797      os_ = saved_os;
798    }
799    os << "STATS:\n" << std::flush;
800    UniquePtr<File> file(OS::OpenFileForReading(image_filename_.c_str()));
801    if (file.get() == NULL) {
802      std::string cache_location(GetDalvikCacheFilenameOrDie(image_filename_.c_str()));
803      file.reset(OS::OpenFileForReading(cache_location.c_str()));
804      if (file.get() == NULL) {
805          LOG(WARNING) << "Failed to find image in " << image_filename_
806                       << " and " << cache_location;
807      }
808    }
809    if (file.get() != NULL) {
810        stats_.file_bytes = file->GetLength();
811    }
812    size_t header_bytes = sizeof(ImageHeader);
813    stats_.header_bytes = header_bytes;
814    size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
815    stats_.alignment_bytes += alignment_bytes;
816    stats_.alignment_bytes += image_header_.GetImageBitmapOffset() - image_header_.GetImageSize();
817    stats_.bitmap_bytes += image_header_.GetImageBitmapSize();
818    stats_.Dump(os);
819    os << "\n";
820
821    os << std::flush;
822
823    oat_dumper_->Dump(os);
824  }
825
826 private:
827  static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
828      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
829    CHECK(type != NULL);
830    if (value == NULL) {
831      os << StringPrintf("null   %s\n", PrettyDescriptor(type).c_str());
832    } else if (type->IsStringClass()) {
833      mirror::String* string = value->AsString();
834      os << StringPrintf("%p   String: %s\n", string,
835                         PrintableString(string->ToModifiedUtf8()).c_str());
836    } else if (type->IsClassClass()) {
837      mirror::Class* klass = value->AsClass();
838      os << StringPrintf("%p   Class: %s\n", klass, PrettyDescriptor(klass).c_str());
839    } else if (type->IsArtFieldClass()) {
840      mirror::ArtField* field = value->AsArtField();
841      os << StringPrintf("%p   Field: %s\n", field, PrettyField(field).c_str());
842    } else if (type->IsArtMethodClass()) {
843      mirror::ArtMethod* method = value->AsArtMethod();
844      os << StringPrintf("%p   Method: %s\n", method, PrettyMethod(method).c_str());
845    } else {
846      os << StringPrintf("%p   %s\n", value, PrettyDescriptor(type).c_str());
847    }
848  }
849
850  static void PrintField(std::ostream& os, mirror::ArtField* field, mirror::Object* obj)
851      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
852    FieldHelper fh(field);
853    const char* descriptor = fh.GetTypeDescriptor();
854    os << StringPrintf("%s: ", fh.GetName());
855    if (descriptor[0] != 'L' && descriptor[0] != '[') {
856      mirror::Class* type = fh.GetType();
857      if (type->IsPrimitiveLong()) {
858        os << StringPrintf("%lld (0x%llx)\n", field->Get64(obj), field->Get64(obj));
859      } else if (type->IsPrimitiveDouble()) {
860        os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
861      } else if (type->IsPrimitiveFloat()) {
862        os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
863      } else {
864        DCHECK(type->IsPrimitive());
865        os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
866      }
867    } else {
868      // Get the value, don't compute the type unless it is non-null as we don't want
869      // to cause class loading.
870      mirror::Object* value = field->GetObj(obj);
871      if (value == NULL) {
872        os << StringPrintf("null   %s\n", PrettyDescriptor(descriptor).c_str());
873      } else {
874        // Grab the field type without causing resolution.
875        mirror::Class* field_type = fh.GetType(false);
876        if (field_type != NULL) {
877          PrettyObjectValue(os, field_type, value);
878        } else {
879          os << StringPrintf("%p   %s\n", value, PrettyDescriptor(descriptor).c_str());
880        }
881      }
882    }
883  }
884
885  static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
886      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
887    mirror::Class* super = klass->GetSuperClass();
888    if (super != NULL) {
889      DumpFields(os, obj, super);
890    }
891    mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields();
892    if (fields != NULL) {
893      for (int32_t i = 0; i < fields->GetLength(); i++) {
894        mirror::ArtField* field = fields->Get(i);
895        PrintField(os, field, obj);
896      }
897    }
898  }
899
900  bool InDumpSpace(const mirror::Object* object) {
901    return image_space_.Contains(object);
902  }
903
904  const void* GetOatCodeBegin(mirror::ArtMethod* m)
905      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
906    const void* code = m->GetEntryPointFromCompiledCode();
907    if (code == GetResolutionTrampoline(Runtime::Current()->GetClassLinker())) {
908      code = oat_dumper_->GetOatCode(m);
909    }
910    if (oat_dumper_->GetInstructionSet() == kThumb2) {
911      code = reinterpret_cast<void*>(reinterpret_cast<uint32_t>(code) & ~0x1);
912    }
913    return code;
914  }
915
916  uint32_t GetOatCodeSize(mirror::ArtMethod* m)
917      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
918    const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetOatCodeBegin(m));
919    if (oat_code_begin == NULL) {
920      return 0;
921    }
922    return oat_code_begin[-1];
923  }
924
925  const void* GetOatCodeEnd(mirror::ArtMethod* m)
926      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
927    const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetOatCodeBegin(m));
928    if (oat_code_begin == NULL) {
929      return NULL;
930    }
931    return oat_code_begin + GetOatCodeSize(m);
932  }
933
934  static void Callback(mirror::Object* obj, void* arg)
935      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
936    DCHECK(obj != NULL);
937    DCHECK(arg != NULL);
938    ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
939    if (!state->InDumpSpace(obj)) {
940      return;
941    }
942
943    size_t object_bytes = obj->SizeOf();
944    size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
945    state->stats_.object_bytes += object_bytes;
946    state->stats_.alignment_bytes += alignment_bytes;
947
948    std::ostream& os = *state->os_;
949    mirror::Class* obj_class = obj->GetClass();
950    if (obj_class->IsArrayClass()) {
951      os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
952                         obj->AsArray()->GetLength());
953    } else if (obj->IsClass()) {
954      mirror::Class* klass = obj->AsClass();
955      os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
956         << klass->GetStatus() << ")\n";
957    } else if (obj->IsArtField()) {
958      os << StringPrintf("%p: java.lang.reflect.ArtField %s\n", obj,
959                         PrettyField(obj->AsArtField()).c_str());
960    } else if (obj->IsArtMethod()) {
961      os << StringPrintf("%p: java.lang.reflect.ArtMethod %s\n", obj,
962                         PrettyMethod(obj->AsArtMethod()).c_str());
963    } else if (obj_class->IsStringClass()) {
964      os << StringPrintf("%p: java.lang.String %s\n", obj,
965                         PrintableString(obj->AsString()->ToModifiedUtf8()).c_str());
966    } else {
967      os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
968    }
969    Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
970    std::ostream indent_os(&indent_filter);
971    DumpFields(indent_os, obj, obj_class);
972    if (obj->IsObjectArray()) {
973      mirror::ObjectArray<mirror::Object>* obj_array = obj->AsObjectArray<mirror::Object>();
974      int32_t length = obj_array->GetLength();
975      for (int32_t i = 0; i < length; i++) {
976        mirror::Object* value = obj_array->Get(i);
977        size_t run = 0;
978        for (int32_t j = i + 1; j < length; j++) {
979          if (value == obj_array->Get(j)) {
980            run++;
981          } else {
982            break;
983          }
984        }
985        if (run == 0) {
986          indent_os << StringPrintf("%d: ", i);
987        } else {
988          indent_os << StringPrintf("%d to %zd: ", i, i + run);
989          i = i + run;
990        }
991        mirror::Class* value_class = value == NULL ? obj_class->GetComponentType() : value->GetClass();
992        PrettyObjectValue(indent_os, value_class, value);
993      }
994    } else if (obj->IsClass()) {
995      mirror::ObjectArray<mirror::ArtField>* sfields = obj->AsClass()->GetSFields();
996      if (sfields != NULL) {
997        indent_os << "STATICS:\n";
998        Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
999        std::ostream indent2_os(&indent2_filter);
1000        for (int32_t i = 0; i < sfields->GetLength(); i++) {
1001          mirror::ArtField* field = sfields->Get(i);
1002          PrintField(indent2_os, field, field->GetDeclaringClass());
1003        }
1004      }
1005    } else if (obj->IsArtMethod()) {
1006      mirror::ArtMethod* method = obj->AsArtMethod();
1007      if (method->IsNative()) {
1008        DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
1009        DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
1010        bool first_occurrence;
1011        const void* oat_code = state->GetOatCodeBegin(method);
1012        uint32_t oat_code_size = state->GetOatCodeSize(method);
1013        state->ComputeOatSize(oat_code, &first_occurrence);
1014        if (first_occurrence) {
1015          state->stats_.native_to_managed_code_bytes += oat_code_size;
1016        }
1017        if (oat_code != method->GetEntryPointFromCompiledCode()) {
1018          indent_os << StringPrintf("OAT CODE: %p\n", oat_code);
1019        }
1020      } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1021          method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1022          MethodHelper(method).IsClassInitializer()) {
1023        DCHECK(method->GetNativeGcMap() == NULL) << PrettyMethod(method);
1024        DCHECK(method->GetMappingTable() == NULL) << PrettyMethod(method);
1025      } else {
1026        // TODO: we check there is a GC map here, we may not have a GC map if the code is pointing
1027        //       to the quick/portable to interpreter bridge.
1028        CHECK(method->GetNativeGcMap() != NULL) << PrettyMethod(method);
1029
1030        const DexFile::CodeItem* code_item = MethodHelper(method).GetCodeItem();
1031        size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1032        state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1033
1034        bool first_occurrence;
1035        size_t gc_map_bytes = state->ComputeOatSize(method->GetNativeGcMap(), &first_occurrence);
1036        if (first_occurrence) {
1037          state->stats_.gc_map_bytes += gc_map_bytes;
1038        }
1039
1040        size_t pc_mapping_table_bytes =
1041            state->ComputeOatSize(method->GetMappingTable(), &first_occurrence);
1042        if (first_occurrence) {
1043          state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1044        }
1045
1046        size_t vmap_table_bytes =
1047            state->ComputeOatSize(method->GetVmapTable(), &first_occurrence);
1048        if (first_occurrence) {
1049          state->stats_.vmap_table_bytes += vmap_table_bytes;
1050        }
1051
1052        const void* oat_code_begin = state->GetOatCodeBegin(method);
1053        const void* oat_code_end = state->GetOatCodeEnd(method);
1054        uint32_t oat_code_size = state->GetOatCodeSize(method);
1055        state->ComputeOatSize(oat_code_begin, &first_occurrence);
1056        if (first_occurrence) {
1057          state->stats_.managed_code_bytes += oat_code_size;
1058          if (method->IsConstructor()) {
1059            if (method->IsStatic()) {
1060              state->stats_.class_initializer_code_bytes += oat_code_size;
1061            } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1062              state->stats_.large_initializer_code_bytes += oat_code_size;
1063            }
1064          } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1065            state->stats_.large_method_code_bytes += oat_code_size;
1066          }
1067        }
1068        state->stats_.managed_code_bytes_ignoring_deduplication += oat_code_size;
1069
1070        indent_os << StringPrintf("OAT CODE: %p-%p\n", oat_code_begin, oat_code_end);
1071        indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1072                                  dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
1073
1074        size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1075            vmap_table_bytes + oat_code_size + object_bytes;
1076
1077        double expansion =
1078            static_cast<double>(oat_code_size) / static_cast<double>(dex_instruction_bytes);
1079        state->stats_.ComputeOutliers(total_size, expansion, method);
1080      }
1081    }
1082    state->stats_.Update(ClassHelper(obj_class).GetDescriptor(), object_bytes);
1083  }
1084
1085  std::set<const void*> already_seen_;
1086  // Compute the size of the given data within the oat file and whether this is the first time
1087  // this data has been requested
1088  size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
1089    if (already_seen_.count(oat_data) == 0) {
1090      *first_occurrence = true;
1091      already_seen_.insert(oat_data);
1092    } else {
1093      *first_occurrence = false;
1094    }
1095    return oat_dumper_->ComputeSize(oat_data);
1096  }
1097
1098 public:
1099  struct Stats {
1100    size_t oat_file_bytes;
1101    size_t file_bytes;
1102
1103    size_t header_bytes;
1104    size_t object_bytes;
1105    size_t bitmap_bytes;
1106    size_t alignment_bytes;
1107
1108    size_t managed_code_bytes;
1109    size_t managed_code_bytes_ignoring_deduplication;
1110    size_t managed_to_native_code_bytes;
1111    size_t native_to_managed_code_bytes;
1112    size_t class_initializer_code_bytes;
1113    size_t large_initializer_code_bytes;
1114    size_t large_method_code_bytes;
1115
1116    size_t gc_map_bytes;
1117    size_t pc_mapping_table_bytes;
1118    size_t vmap_table_bytes;
1119
1120    size_t dex_instruction_bytes;
1121
1122    std::vector<mirror::ArtMethod*> method_outlier;
1123    std::vector<size_t> method_outlier_size;
1124    std::vector<double> method_outlier_expansion;
1125    std::vector<std::pair<std::string, size_t> > oat_dex_file_sizes;
1126
1127    explicit Stats()
1128        : oat_file_bytes(0),
1129          file_bytes(0),
1130          header_bytes(0),
1131          object_bytes(0),
1132          bitmap_bytes(0),
1133          alignment_bytes(0),
1134          managed_code_bytes(0),
1135          managed_code_bytes_ignoring_deduplication(0),
1136          managed_to_native_code_bytes(0),
1137          native_to_managed_code_bytes(0),
1138          class_initializer_code_bytes(0),
1139          large_initializer_code_bytes(0),
1140          large_method_code_bytes(0),
1141          gc_map_bytes(0),
1142          pc_mapping_table_bytes(0),
1143          vmap_table_bytes(0),
1144          dex_instruction_bytes(0) {}
1145
1146    struct SizeAndCount {
1147      SizeAndCount(size_t bytes, size_t count) : bytes(bytes), count(count) {}
1148      size_t bytes;
1149      size_t count;
1150    };
1151    typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1152    SizeAndCountTable sizes_and_counts;
1153
1154    void Update(const char* descriptor, size_t object_bytes) {
1155      SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1156      if (it != sizes_and_counts.end()) {
1157        it->second.bytes += object_bytes;
1158        it->second.count += 1;
1159      } else {
1160        sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes, 1));
1161      }
1162    }
1163
1164    double PercentOfOatBytes(size_t size) {
1165      return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
1166    }
1167
1168    double PercentOfFileBytes(size_t size) {
1169      return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
1170    }
1171
1172    double PercentOfObjectBytes(size_t size) {
1173      return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
1174    }
1175
1176    void ComputeOutliers(size_t total_size, double expansion, mirror::ArtMethod* method) {
1177      method_outlier_size.push_back(total_size);
1178      method_outlier_expansion.push_back(expansion);
1179      method_outlier.push_back(method);
1180    }
1181
1182    void DumpOutliers(std::ostream& os)
1183        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1184      size_t sum_of_sizes = 0;
1185      size_t sum_of_sizes_squared = 0;
1186      size_t sum_of_expansion = 0;
1187      size_t sum_of_expansion_squared = 0;
1188      size_t n = method_outlier_size.size();
1189      for (size_t i = 0; i < n; i++) {
1190        size_t cur_size = method_outlier_size[i];
1191        sum_of_sizes += cur_size;
1192        sum_of_sizes_squared += cur_size * cur_size;
1193        double cur_expansion = method_outlier_expansion[i];
1194        sum_of_expansion += cur_expansion;
1195        sum_of_expansion_squared += cur_expansion * cur_expansion;
1196      }
1197      size_t size_mean = sum_of_sizes / n;
1198      size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
1199      double expansion_mean = sum_of_expansion / n;
1200      double expansion_variance =
1201          (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
1202
1203      // Dump methods whose size is a certain number of standard deviations from the mean
1204      size_t dumped_values = 0;
1205      size_t skipped_values = 0;
1206      for (size_t i = 100; i > 0; i--) {  // i is the current number of standard deviations
1207        size_t cur_size_variance = i * i * size_variance;
1208        bool first = true;
1209        for (size_t j = 0; j < n; j++) {
1210          size_t cur_size = method_outlier_size[j];
1211          if (cur_size > size_mean) {
1212            size_t cur_var = cur_size - size_mean;
1213            cur_var = cur_var * cur_var;
1214            if (cur_var > cur_size_variance) {
1215              if (dumped_values > 20) {
1216                if (i == 1) {
1217                  skipped_values++;
1218                } else {
1219                  i = 2;  // jump to counting for 1 standard deviation
1220                  break;
1221                }
1222              } else {
1223                if (first) {
1224                  os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
1225                  first = false;
1226                }
1227                os << PrettyMethod(method_outlier[j]) << " requires storage of "
1228                    << PrettySize(cur_size) << "\n";
1229                method_outlier_size[j] = 0;  // don't consider this method again
1230                dumped_values++;
1231              }
1232            }
1233          }
1234        }
1235      }
1236      if (skipped_values > 0) {
1237        os << "... skipped " << skipped_values
1238           << " methods with size > 1 standard deviation from the norm\n";
1239      }
1240      os << std::flush;
1241
1242      // Dump methods whose expansion is a certain number of standard deviations from the mean
1243      dumped_values = 0;
1244      skipped_values = 0;
1245      for (size_t i = 10; i > 0; i--) {  // i is the current number of standard deviations
1246        double cur_expansion_variance = i * i * expansion_variance;
1247        bool first = true;
1248        for (size_t j = 0; j < n; j++) {
1249          double cur_expansion = method_outlier_expansion[j];
1250          if (cur_expansion > expansion_mean) {
1251            size_t cur_var = cur_expansion - expansion_mean;
1252            cur_var = cur_var * cur_var;
1253            if (cur_var > cur_expansion_variance) {
1254              if (dumped_values > 20) {
1255                if (i == 1) {
1256                  skipped_values++;
1257                } else {
1258                  i = 2;  // jump to counting for 1 standard deviation
1259                  break;
1260                }
1261              } else {
1262                if (first) {
1263                  os << "\nLarge expansion methods (size > " << i
1264                      << " standard deviations the norm):\n";
1265                  first = false;
1266                }
1267                os << PrettyMethod(method_outlier[j]) << " expanded code by "
1268                   << cur_expansion << "\n";
1269                method_outlier_expansion[j] = 0.0;  // don't consider this method again
1270                dumped_values++;
1271              }
1272            }
1273          }
1274        }
1275      }
1276      if (skipped_values > 0) {
1277        os << "... skipped " << skipped_values
1278           << " methods with expansion > 1 standard deviation from the norm\n";
1279      }
1280      os << "\n" << std::flush;
1281    }
1282
1283    void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1284      {
1285        os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
1286           << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
1287        Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1288        std::ostream indent_os(&indent_filter);
1289        indent_os << StringPrintf("header_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
1290                                  "object_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
1291                                  "bitmap_bytes    =  %8zd (%2.0f%% of art file bytes)\n"
1292                                  "alignment_bytes =  %8zd (%2.0f%% of art file bytes)\n\n",
1293                                  header_bytes, PercentOfFileBytes(header_bytes),
1294                                  object_bytes, PercentOfFileBytes(object_bytes),
1295                                  bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
1296                                  alignment_bytes, PercentOfFileBytes(alignment_bytes))
1297            << std::flush;
1298        CHECK_EQ(file_bytes, bitmap_bytes + header_bytes + object_bytes + alignment_bytes);
1299      }
1300
1301      os << "object_bytes breakdown:\n";
1302      size_t object_bytes_total = 0;
1303      for (const auto& sizes_and_count : sizes_and_counts) {
1304        const std::string& descriptor(sizes_and_count.first);
1305        double average = static_cast<double>(sizes_and_count.second.bytes) /
1306            static_cast<double>(sizes_and_count.second.count);
1307        double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
1308        os << StringPrintf("%32s %8zd bytes %6zd instances "
1309                           "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
1310                           descriptor.c_str(), sizes_and_count.second.bytes,
1311                           sizes_and_count.second.count, average, percent);
1312        object_bytes_total += sizes_and_count.second.bytes;
1313      }
1314      os << "\n" << std::flush;
1315      CHECK_EQ(object_bytes, object_bytes_total);
1316
1317      os << StringPrintf("oat_file_bytes               = %8zd\n"
1318                         "managed_code_bytes           = %8zd (%2.0f%% of oat file bytes)\n"
1319                         "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1320                         "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
1321                         "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1322                         "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
1323                         "large_method_code_bytes      = %8zd (%2.0f%% of oat file bytes)\n\n",
1324                         oat_file_bytes,
1325                         managed_code_bytes, PercentOfOatBytes(managed_code_bytes),
1326                         managed_to_native_code_bytes, PercentOfOatBytes(managed_to_native_code_bytes),
1327                         native_to_managed_code_bytes, PercentOfOatBytes(native_to_managed_code_bytes),
1328                         class_initializer_code_bytes, PercentOfOatBytes(class_initializer_code_bytes),
1329                         large_initializer_code_bytes, PercentOfOatBytes(large_initializer_code_bytes),
1330                         large_method_code_bytes, PercentOfOatBytes(large_method_code_bytes))
1331            << "DexFile sizes:\n";
1332      for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
1333        os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
1334                           oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
1335                           PercentOfOatBytes(oat_dex_file_size.second));
1336      }
1337
1338      os << "\n" << StringPrintf("gc_map_bytes           = %7zd (%2.0f%% of oat file bytes)\n"
1339                                 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
1340                                 "vmap_table_bytes       = %7zd (%2.0f%% of oat file bytes)\n\n",
1341                                 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
1342                                 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
1343                                 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
1344         << std::flush;
1345
1346      os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
1347         << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
1348                         static_cast<double>(managed_code_bytes) / static_cast<double>(dex_instruction_bytes),
1349                         static_cast<double>(managed_code_bytes_ignoring_deduplication) /
1350                             static_cast<double>(dex_instruction_bytes))
1351         << std::flush;
1352
1353      DumpOutliers(os);
1354    }
1355  } stats_;
1356
1357 private:
1358  enum {
1359    // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
1360    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1361    kLargeConstructorDexBytes = 4000,
1362    // Number of bytes for a method to be considered large. Based on the 4000 basic block
1363    // threshold, we assume 2 bytes per instruction and 2 instructions per block.
1364    kLargeMethodDexBytes = 16000
1365  };
1366  UniquePtr<OatDumper> oat_dumper_;
1367  std::ostream* os_;
1368  const std::string image_filename_;
1369  const std::string host_prefix_;
1370  gc::space::ImageSpace& image_space_;
1371  const ImageHeader& image_header_;
1372
1373  DISALLOW_COPY_AND_ASSIGN(ImageDumper);
1374};
1375
1376static int oatdump(int argc, char** argv) {
1377  InitLogging(argv);
1378
1379  // Skip over argv[0].
1380  argv++;
1381  argc--;
1382
1383  if (argc == 0) {
1384    fprintf(stderr, "No arguments specified\n");
1385    usage();
1386  }
1387
1388  const char* oat_filename = NULL;
1389  const char* image_filename = NULL;
1390  const char* boot_image_filename = NULL;
1391  std::string elf_filename_prefix;
1392  UniquePtr<std::string> host_prefix;
1393  std::ostream* os = &std::cout;
1394  UniquePtr<std::ofstream> out;
1395
1396  for (int i = 0; i < argc; i++) {
1397    const StringPiece option(argv[i]);
1398    if (option.starts_with("--oat-file=")) {
1399      oat_filename = option.substr(strlen("--oat-file=")).data();
1400    } else if (option.starts_with("--image=")) {
1401      image_filename = option.substr(strlen("--image=")).data();
1402    } else if (option.starts_with("--boot-image=")) {
1403      boot_image_filename = option.substr(strlen("--boot-image=")).data();
1404    } else if (option.starts_with("--host-prefix=")) {
1405      host_prefix.reset(new std::string(option.substr(strlen("--host-prefix=")).data()));
1406    } else if (option.starts_with("--output=")) {
1407      const char* filename = option.substr(strlen("--output=")).data();
1408      out.reset(new std::ofstream(filename));
1409      if (!out->good()) {
1410        fprintf(stderr, "Failed to open output filename %s\n", filename);
1411        usage();
1412      }
1413      os = out.get();
1414    } else {
1415      fprintf(stderr, "Unknown argument %s\n", option.data());
1416      usage();
1417    }
1418  }
1419
1420  if (image_filename == NULL && oat_filename == NULL) {
1421    fprintf(stderr, "Either --image or --oat must be specified\n");
1422    return EXIT_FAILURE;
1423  }
1424
1425  if (image_filename != NULL && oat_filename != NULL) {
1426    fprintf(stderr, "Either --image or --oat must be specified but not both\n");
1427    return EXIT_FAILURE;
1428  }
1429
1430  if (host_prefix.get() == NULL) {
1431    const char* android_product_out = getenv("ANDROID_PRODUCT_OUT");
1432    if (android_product_out != NULL) {
1433        host_prefix.reset(new std::string(android_product_out));
1434    } else {
1435        host_prefix.reset(new std::string(""));
1436    }
1437  }
1438
1439  if (oat_filename != NULL) {
1440    std::string error_msg;
1441    OatFile* oat_file =
1442        OatFile::Open(oat_filename, oat_filename, NULL, false, &error_msg);
1443    if (oat_file == NULL) {
1444      fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
1445      return EXIT_FAILURE;
1446    }
1447    OatDumper oat_dumper(*host_prefix.get(), *oat_file);
1448    oat_dumper.Dump(*os);
1449    return EXIT_SUCCESS;
1450  }
1451
1452  Runtime::Options options;
1453  std::string image_option;
1454  std::string oat_option;
1455  std::string boot_image_option;
1456  std::string boot_oat_option;
1457
1458  // We are more like a compiler than a run-time. We don't want to execute code.
1459  options.push_back(std::make_pair("compiler", reinterpret_cast<void*>(NULL)));
1460
1461  if (boot_image_filename != NULL) {
1462    boot_image_option += "-Ximage:";
1463    boot_image_option += boot_image_filename;
1464    options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL)));
1465  }
1466  if (image_filename != NULL) {
1467    image_option += "-Ximage:";
1468    image_option += image_filename;
1469    options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL)));
1470  }
1471
1472  if (!host_prefix->empty()) {
1473    options.push_back(std::make_pair("host-prefix", host_prefix->c_str()));
1474  }
1475
1476  if (!Runtime::Create(options, false)) {
1477    fprintf(stderr, "Failed to create runtime\n");
1478    return EXIT_FAILURE;
1479  }
1480  UniquePtr<Runtime> runtime(Runtime::Current());
1481  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
1482  // give it away now and then switch to a more managable ScopedObjectAccess.
1483  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
1484  ScopedObjectAccess soa(Thread::Current());
1485
1486  gc::Heap* heap = Runtime::Current()->GetHeap();
1487  gc::space::ImageSpace* image_space = heap->GetImageSpace();
1488  CHECK(image_space != NULL);
1489  const ImageHeader& image_header = image_space->GetImageHeader();
1490  if (!image_header.IsValid()) {
1491    fprintf(stderr, "Invalid image header %s\n", image_filename);
1492    return EXIT_FAILURE;
1493  }
1494  ImageDumper image_dumper(os, image_filename, *host_prefix.get(), *image_space, image_header);
1495  image_dumper.Dump();
1496  return EXIT_SUCCESS;
1497}
1498
1499}  // namespace art
1500
1501int main(int argc, char** argv) {
1502  return art::oatdump(argc, argv);
1503}
1504