blob: d8ac581a99618948f30cc6131540ccdc0aacabdb [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
David Srbecky5d950762016-03-07 20:47:29 +000035#include "debug/elf_debug_writer.h"
36#include "debug/method_debug_info.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070037#include "dex_file-inl.h"
Christina Wadsworthbc233ac2016-06-20 15:01:32 -070038#include "dex_instruction-inl.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080039#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070040#include "elf_builder.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070041#include "gc/space/image_space.h"
42#include "gc/space/large_object_space.h"
43#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080044#include "image-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080045#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000046#include "linker/buffered_output_stream.h"
47#include "linker/file_output_stream.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/array-inl.h"
49#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010050#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080056#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070057#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058#include "scoped_thread_state_change.h"
Nicolas Geoffray9c055782016-07-14 09:24:30 +000059#include "ScopedLocalRef.h"
Mathieu Chartierdc00f182016-07-14 10:10:44 -070060#include "stack_map.h"
61#include "string_reference.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080062#include "thread_list.h"
Andreas Gampe2ba88952016-04-29 17:52:07 -070063#include "type_lookup_table.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080064#include "verifier/method_verifier.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070065#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Igor Murashkin37743352014-11-13 14:38:00 -080067#include <sys/stat.h>
68#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070069
Igor Murashkin37743352014-11-13 14:38:00 -080070namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Mathieu Chartiere401d142015-04-22 13:56:20 -070072const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080073 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070074 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070075 "kImtUnimplementedMethod",
Vladimir Markofd36f1f2016-08-03 18:49:58 +010076 "kSaveAllCalleeSavesMethod",
77 "kSaveRefsOnlyMethod",
78 "kSaveRefsAndArgsMethod",
Vladimir Marko952dbb12016-07-28 12:01:51 +010079 "kSaveEverythingMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070080};
81
82const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070083 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070084 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070085};
86
Mathieu Chartierac8f4392015-08-27 13:54:20 -070087// Map is so that we don't allocate multiple dex files for the same OatDexFile.
88static std::map<const OatFile::OatDexFile*,
89 std::unique_ptr<const DexFile>> opened_dex_files;
90
91const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
92 DCHECK(oat_dex_file != nullptr);
93 auto it = opened_dex_files.find(oat_dex_file);
94 if (it != opened_dex_files.end()) {
95 return it->second.get();
96 }
97 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
98 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
99 return ret;
100}
101
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800102template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100103class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700104 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000105 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
106 oat_file_(oat_file),
107 builder_(nullptr),
108 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
109 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700110 }
111
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700112 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000113 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000114 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
115 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000116
117 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
118 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000119 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800120 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000121
122 builder_->Start();
123
124 auto* rodata = builder_->GetRoData();
125 auto* text = builder_->GetText();
126 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000127
David Srbecky6d8c8f02015-10-26 10:57:09 +0000128 const uint8_t* rodata_begin = oat_file_->Begin();
129 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000130 if (no_bits_) {
131 rodata->WriteNoBitsSection(rodata_size);
132 } else {
133 rodata->Start();
134 rodata->WriteFully(rodata_begin, rodata_size);
135 rodata->End();
136 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000137
David Srbecky6d8c8f02015-10-26 10:57:09 +0000138 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
139 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000140 if (no_bits_) {
141 text->WriteNoBitsSection(text_size);
142 } else {
143 text->Start();
144 text->WriteFully(text_begin, text_size);
145 text->End();
146 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000147
148 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000149 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000150 }
151
Douglas Leung316a2182015-09-17 15:26:25 -0700152 if (isa == kMips || isa == kMips64) {
153 builder_->WriteMIPSabiflagsSection();
154 }
Vladimir Marko944da602016-02-19 12:27:55 +0000155 builder_->PrepareDynamicSection(
156 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
157 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700158
David Srbecky5d950762016-03-07 20:47:29 +0000159 Walk();
160 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
161 method_debug_infos_.push_back(trampoline);
162 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700163
David Srbecky5d950762016-03-07 20:47:29 +0000164 debug::WriteDebugInfo(builder_.get(),
165 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
166 dwarf::DW_DEBUG_FRAME_FORMAT,
167 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700168
David Srbecky6d8c8f02015-10-26 10:57:09 +0000169 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700170
Vladimir Marko10c13562015-11-25 14:33:36 +0000171 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172 }
173
David Srbecky5d950762016-03-07 20:47:29 +0000174 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700175 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
176 for (size_t i = 0; i < oat_dex_files.size(); i++) {
177 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700178 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000179 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700180 }
181 }
182
David Srbecky5d950762016-03-07 20:47:29 +0000183 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700184 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700185 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
186 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700187 return;
188 }
189 for (size_t class_def_index = 0;
190 class_def_index < dex_file->NumClassDefs();
191 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700192 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
193 OatClassType type = oat_class.GetType();
194 switch (type) {
195 case kOatClassAllCompiled:
196 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000197 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700198 break;
199
200 case kOatClassNoneCompiled:
201 case kOatClassMax:
202 // Ignore.
203 break;
204 }
205 }
206 }
207
David Srbecky5d950762016-03-07 20:47:29 +0000208 void WalkOatClass(const OatFile::OatClass& oat_class,
209 const DexFile& dex_file,
210 uint32_t class_def_index) {
211 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700212 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700213 if (class_data == nullptr) { // empty class such as a marker interface?
214 return;
215 }
216 // Note: even if this is an interface or a native class, we still have to walk it, as there
217 // might be a static initializer.
218 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700219 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000220 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
221 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
222 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
223 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
224 dex_file,
225 class_def_index,
226 it.GetMemberIndex(),
227 it.GetMethodCodeItem(),
228 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700229 }
230 DCHECK(!it.HasNext());
231 }
232
David Srbecky5d950762016-03-07 20:47:29 +0000233 void WalkOatMethod(const OatFile::OatMethod& oat_method,
234 const DexFile& dex_file,
235 uint32_t class_def_index,
236 uint32_t dex_method_index,
237 const DexFile::CodeItem* code_item,
238 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700239 if ((method_access_flags & kAccAbstract) != 0) {
240 // Abstract method, no code.
241 return;
242 }
David Srbecky5d950762016-03-07 20:47:29 +0000243 const OatHeader& oat_header = oat_file_->GetOatHeader();
244 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
245 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 // No code.
247 return;
248 }
249
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100250 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
251 // Clear Thumb2 bit.
252 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
253
David Srbecky5d950762016-03-07 20:47:29 +0000254 debug::MethodDebugInfo info = debug::MethodDebugInfo();
255 info.trampoline_name = nullptr;
256 info.dex_file = &dex_file;
257 info.class_def_index = class_def_index;
258 info.dex_method_index = dex_method_index;
259 info.access_flags = method_access_flags;
260 info.code_item = code_item;
261 info.isa = oat_header.GetInstructionSet();
262 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
263 info.is_native_debuggable = oat_header.IsNativeDebuggable();
264 info.is_optimized = method_header->IsOptimized();
265 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100266 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000267 info.code_size = method_header->GetCodeSize();
268 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
269 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
270 info.cfi = ArrayRef<uint8_t>();
271 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 }
273
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700274 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700275 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800276 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000277 std::vector<debug::MethodDebugInfo> method_debug_infos_;
278 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700279 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000280 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281};
282
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700283class OatDumperOptions {
284 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100285 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100286 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700287 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700288 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800289 const char* class_filter,
290 const char* method_filter,
291 bool list_classes,
292 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000293 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800294 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800295 const char* app_image,
296 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800297 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100298 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100299 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700300 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700301 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800302 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000303 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800304 list_classes_(list_classes),
305 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000306 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800307 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800308 app_image_(app_image),
309 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800310 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800311 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700312
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700313 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100314 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700315 const bool disassemble_code_;
316 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800317 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000318 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800319 const bool list_classes_;
320 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000321 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800322 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800323 const char* const app_image_;
324 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800325 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700326 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700327};
328
Elliott Hughese3c845c2012-02-28 17:23:01 -0800329class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700330 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100331 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000332 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800333 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700334 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800335 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800336 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
337 disassembler_(Disassembler::Create(instruction_set_,
Andreas Gampe372f3a32016-08-19 10:49:06 -0700338 new DisassemblerOptions(
339 options_.absolute_addresses_,
340 oat_file.Begin(),
341 oat_file.End(),
342 true /* can_read_literals_ */,
343 Is64BitInstructionSet(instruction_set_)
344 ? &Thread::DumpThreadOffset<PointerSize::k64>
345 : &Thread::DumpThreadOffset<PointerSize::k32>))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800346 CHECK(options_.class_loader_ != nullptr);
347 CHECK(options_.class_filter_ != nullptr);
348 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800349 AddAllOffsets();
350 }
351
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700353 delete disassembler_;
354 }
355
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800356 InstructionSet GetInstructionSet() {
357 return instruction_set_;
358 }
359
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360 bool Dump(std::ostream& os) {
361 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800362 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700363
364 os << "MAGIC:\n";
365 os << oat_header.GetMagic() << "\n\n";
366
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800367 os << "LOCATION:\n";
368 os << oat_file_.GetLocation() << "\n\n";
369
Brian Carlstromaded5f72011-10-07 17:15:04 -0700370 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800371 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700372
Elliott Hughesa72ec822012-03-05 17:12:22 -0800373 os << "INSTRUCTION SET:\n";
374 os << oat_header.GetInstructionSet() << "\n\n";
375
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700376 {
377 std::unique_ptr<const InstructionSetFeatures> features(
378 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
379 oat_header.GetInstructionSetFeaturesBitmap()));
380 os << "INSTRUCTION SET FEATURES:\n";
381 os << features->GetFeatureString() << "\n\n";
382 }
Dave Allison70202782013-10-22 17:52:19 -0700383
Brian Carlstromaded5f72011-10-07 17:15:04 -0700384 os << "DEX FILE COUNT:\n";
385 os << oat_header.GetDexFileCount() << "\n\n";
386
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800387#define DUMP_OAT_HEADER_OFFSET(label, offset) \
388 os << label " OFFSET:\n"; \
389 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800390 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800391 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
392 } \
393 os << StringPrintf("\n\n");
394
395 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
396 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
397 GetInterpreterToInterpreterBridgeOffset);
398 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
399 GetInterpreterToCompiledCodeBridgeOffset);
400 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
401 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800402 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
403 GetQuickGenericJniTrampolineOffset);
404 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
405 GetQuickImtConflictTrampolineOffset);
406 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
407 GetQuickResolutionTrampolineOffset);
408 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
409 GetQuickToInterpreterBridgeOffset);
410#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700411
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700412 os << "IMAGE PATCH DELTA:\n";
413 os << StringPrintf("%d (0x%08x)\n\n",
414 oat_header.GetImagePatchDelta(),
415 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700416
Brian Carlstrom28db0122012-10-18 16:20:41 -0700417 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
418 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
419
420 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800421 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700422
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700423 // Print the key-value store.
424 {
425 os << "KEY VALUE STORE:\n";
426 size_t index = 0;
427 const char* key;
428 const char* value;
429 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
430 os << key << " = " << value << "\n";
431 index++;
432 }
433 os << "\n";
434 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700435
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800436 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700437 os << "BEGIN:\n";
438 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700439
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700440 os << "END:\n";
441 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
442 }
443
444 os << "SIZE:\n";
445 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700446
447 os << std::flush;
448
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800449 // If set, adjust relative address to be searched
450 if (options_.addr2instr_ != 0) {
451 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
452 os << "SEARCH ADDRESS (executable offset + input):\n";
453 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
454 }
455
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700456 // Dumping the dex file overview is compact enough to do even if header only.
457 DexFileData cumulative;
458 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
459 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
460 CHECK(oat_dex_file != nullptr);
461 std::string error_msg;
462 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
463 if (dex_file == nullptr) {
464 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
465 << error_msg;
466 continue;
467 }
468 DexFileData data(*dex_file);
469 os << "Dex file data for " << dex_file->GetLocation() << "\n";
470 data.Dump(os);
471 os << "\n";
472 cumulative.Add(data);
473 }
474 os << "Cumulative dex file data\n";
475 cumulative.Dump(os);
476 os << "\n";
477
David Brazdilc03d7b62016-03-02 12:18:03 +0000478 if (!options_.dump_header_only_) {
479 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
480 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
481 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800482
David Brazdilc03d7b62016-03-02 12:18:03 +0000483 // If file export selected skip file analysis
484 if (options_.export_dex_location_) {
485 if (!ExportDexFile(os, *oat_dex_file)) {
486 success = false;
487 }
488 } else {
489 if (!DumpOatDexFile(os, *oat_dex_file)) {
490 success = false;
491 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800492 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700493 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700494 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000495
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700496 os << std::flush;
497 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700498 }
499
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800500 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700501 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
502 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 return 0; // Address not in oat file
504 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800505 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
506 reinterpret_cast<uintptr_t>(oat_file_.Begin());
507 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800508 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800509 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 return end_offset - begin_offset;
511 }
512
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800513 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700514 return oat_file_.GetOatHeader().GetInstructionSet();
515 }
516
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700517 const void* GetQuickOatCode(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
519 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700520 CHECK(oat_dex_file != nullptr);
521 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700522 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
523 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700524 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
525 << "': " << error_msg;
526 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800527 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700528 const DexFile::ClassDef* class_def =
David Sehr9aa352e2016-09-15 18:13:52 -0700529 OatDexFile::FindClassDef(*dex_file, descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700530 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700531 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100532 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800533 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100534 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 }
536 }
537 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700538 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800539 }
540
Brian Carlstromaded5f72011-10-07 17:15:04 -0700541 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800543 // We don't know the length of the code for each method, but we need to know where to stop
544 // when disassembling. What we do know is that a region of code will be followed by some other
545 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
546 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800547 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
548 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700549 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700550 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700551 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
552 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700553 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
554 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800555 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800556 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800557 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800558 for (size_t class_def_index = 0;
559 class_def_index < dex_file->NumClassDefs();
560 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800561 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100562 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700563 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700564 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800565 ClassDataItemIterator it(*dex_file, class_data);
566 SkipAllFields(it);
567 uint32_t class_method_index = 0;
568 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100569 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800570 it.Next();
571 }
572 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100573 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800574 it.Next();
575 }
576 }
577 }
578 }
579
580 // If the last thing in the file is code for a method, there won't be an offset for the "next"
581 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
582 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800583 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800584 }
585
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700586 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
587 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
588 }
589
Elliott Hughese3c845c2012-02-28 17:23:01 -0800590 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800591 uint32_t code_offset = oat_method.GetCodeOffset();
592 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
593 code_offset &= ~0x1;
594 }
595 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800596 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800597 }
598
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700599 // Dex file data, may be for multiple different dex files.
600 class DexFileData {
601 public:
602 DexFileData() {}
603
604 explicit DexFileData(const DexFile& dex_file)
605 : num_string_ids_(dex_file.NumStringIds()),
606 num_method_ids_(dex_file.NumMethodIds()),
607 num_field_ids_(dex_file.NumFieldIds()),
608 num_type_ids_(dex_file.NumTypeIds()),
609 num_class_defs_(dex_file.NumClassDefs()) {
610 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
611 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
612 WalkClass(dex_file, class_def);
613 }
614 }
615
616 void Add(const DexFileData& other) {
617 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
618 num_string_ids_from_code_ += other.num_string_ids_from_code_;
619 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
620 dex_code_bytes_ += other.dex_code_bytes_;
621 num_string_ids_ += other.num_string_ids_;
622 num_method_ids_ += other.num_method_ids_;
623 num_field_ids_ += other.num_field_ids_;
624 num_type_ids_ += other.num_type_ids_;
625 num_class_defs_ += other.num_class_defs_;
626 }
627
628 void Dump(std::ostream& os) {
629 os << "Num string ids: " << num_string_ids_ << "\n";
630 os << "Num method ids: " << num_method_ids_ << "\n";
631 os << "Num field ids: " << num_field_ids_ << "\n";
632 os << "Num type ids: " << num_type_ids_ << "\n";
633 os << "Num class defs: " << num_class_defs_ << "\n";
634 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
635 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
636 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
637 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
638 }
639
640 private:
641 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
642 const uint8_t* class_data = dex_file.GetClassData(class_def);
643 if (class_data == nullptr) { // empty class such as a marker interface?
644 return;
645 }
646 ClassDataItemIterator it(dex_file, class_data);
647 SkipAllFields(it);
648 while (it.HasNextDirectMethod()) {
649 WalkCodeItem(dex_file, it.GetMethodCodeItem());
650 it.Next();
651 }
652 while (it.HasNextVirtualMethod()) {
653 WalkCodeItem(dex_file, it.GetMethodCodeItem());
654 it.Next();
655 }
656 DCHECK(!it.HasNext());
657 }
658
659 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
660 if (code_item == nullptr) {
661 return;
662 }
663 const size_t code_item_size = code_item->insns_size_in_code_units_;
664 const uint16_t* code_ptr = code_item->insns_;
665 const uint16_t* code_end = code_item->insns_ + code_item_size;
666
667 // If we inserted a new dex code item pointer, add to total code bytes.
668 if (dex_code_item_ptrs_.insert(code_ptr).second) {
669 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
670 }
671
672 while (code_ptr < code_end) {
673 const Instruction* inst = Instruction::At(code_ptr);
674 switch (inst->Opcode()) {
675 case Instruction::CONST_STRING: {
676 const uint32_t string_index = inst->VRegB_21c();
677 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
678 ++num_string_ids_from_code_;
679 break;
680 }
681 case Instruction::CONST_STRING_JUMBO: {
682 const uint32_t string_index = inst->VRegB_31c();
683 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
684 ++num_string_ids_from_code_;
685 break;
686 }
687 default:
688 break;
689 }
690
691 code_ptr += inst->SizeInCodeUnits();
692 }
693 }
694
695 // Unique string ids loaded from dex code.
696 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
697
698 // Total string ids loaded from dex code.
699 size_t num_string_ids_from_code_ = 0;
700
701 // Unique code pointers.
702 std::set<const void*> dex_code_item_ptrs_;
703
704 // Total "unique" dex code bytes.
705 size_t dex_code_bytes_ = 0;
706
707 // Other dex ids.
708 size_t num_string_ids_ = 0;
709 size_t num_method_ids_ = 0;
710 size_t num_field_ids_ = 0;
711 size_t num_type_ids_ = 0;
712 size_t num_class_defs_ = 0;
713 };
714
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700715 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
716 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800717 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700718 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800719 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800720 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700721
Andreas Gampe2ba88952016-04-29 17:52:07 -0700722 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100723 const uint8_t* const vdex_file_begin = oat_dex_file.GetOatFile()->DexBegin();
724
725 // Print data range of the dex file embedded inside the corresponding vdex file.
Andreas Gampe2ba88952016-04-29 17:52:07 -0700726 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
David Brazdil7b49e6c2016-09-01 11:06:18 +0100727 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - vdex_file_begin);
Andreas Gampe2ba88952016-04-29 17:52:07 -0700728 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
729 dex_offset,
730 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700731
Andreas Gampe2ba88952016-04-29 17:52:07 -0700732 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700733 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700734 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
735 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700736 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700737 os << std::flush;
738 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700739 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100740
Andreas Gampe2ba88952016-04-29 17:52:07 -0700741 // Print lookup table, if it exists.
742 if (oat_dex_file.GetLookupTableData() != nullptr) {
743 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
744 oat_dex_file.GetLookupTableData() - oat_file_begin);
David Sehr9aa352e2016-09-15 18:13:52 -0700745 uint32_t table_size = TypeLookupTable::RawDataLength(dex_file->NumClassDefs());
Andreas Gampe2ba88952016-04-29 17:52:07 -0700746 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
747 table_offset,
748 table_offset + table_size - 1);
749 }
750
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100751 VariableIndentationOutputStream vios(&os);
752 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800753 for (size_t class_def_index = 0;
754 class_def_index < dex_file->NumClassDefs();
755 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700756 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
757 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800758
759 // TODO: Support regex
760 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
761 continue;
762 }
763
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700764 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100765 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700766 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
767 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100768 << " (" << oat_class.GetStatus() << ")"
769 << " (" << oat_class.GetType() << ")\n";
770 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700771 if (options_.list_classes_) {
772 continue;
773 }
774 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700775 success = false;
776 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800777 if (stop_analysis) {
778 os << std::flush;
779 return success;
780 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700781 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700782 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700783 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700784 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700785 }
786
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800787 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
788 std::string error_msg;
789 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
790
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700791 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800792 if (dex_file == nullptr) {
793 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
794 return false;
795 }
796 size_t fsize = oat_dex_file.FileSize();
797
798 // Some quick checks just in case
799 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
800 os << "Invalid dex file\n";
801 return false;
802 }
803
804 // Verify output directory exists
805 if (!OS::DirectoryExists(options_.export_dex_location_)) {
806 // TODO: Extend OS::DirectoryExists if symlink support is required
807 os << options_.export_dex_location_ << " output directory not found or symlink\n";
808 return false;
809 }
810
811 // Beautify path names
812 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
813 return false;
814 }
815
816 std::string dex_orig_name;
817 size_t dex_orig_pos = dex_file_location.rfind('/');
818 if (dex_orig_pos == std::string::npos)
819 dex_orig_name = dex_file_location;
820 else
821 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
822
823 // A more elegant approach to efficiently name user installed apps is welcome
824 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
825 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
826 size_t apk_orig_pos = dex_file_location.rfind('/');
827 if (apk_orig_pos != std::string::npos) {
828 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
829 }
830 }
831
832 std::string out_dex_path(options_.export_dex_location_);
833 if (out_dex_path.back() != '/') {
834 out_dex_path.append("/");
835 }
836 out_dex_path.append(dex_orig_name);
837 out_dex_path.append("_export.dex");
838 if (out_dex_path.length() > PATH_MAX) {
839 return false;
840 }
841
842 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
843 if (file.get() == nullptr) {
844 os << "Failed to open output dex file " << out_dex_path;
845 return false;
846 }
847
848 if (!file->WriteFully(dex_file->Begin(), fsize)) {
849 os << "Failed to write dex file";
850 file->Erase();
851 return false;
852 }
853
854 if (file->FlushCloseOrErase() != 0) {
855 os << "Flush and close failed";
856 return false;
857 }
858
859 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
860 os << std::flush;
861
862 return true;
863 }
864
Elliott Hughese3c845c2012-02-28 17:23:01 -0800865 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700866 while (it.HasNextStaticField()) {
867 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700868 }
Ian Rogers0571d352011-11-03 19:51:38 -0700869 while (it.HasNextInstanceField()) {
870 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700871 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800872 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700873
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100874 bool DumpOatClass(VariableIndentationOutputStream* vios,
875 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700876 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700877 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800878 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700879 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700880 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700882 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800883 }
884 ClassDataItemIterator it(dex_file, class_data);
885 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700886 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700887 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100888 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700889 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700890 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700891 success = false;
892 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800893 if (addr_found) {
894 *stop_analysis = true;
895 return success;
896 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700897 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700898 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700899 }
Ian Rogers0571d352011-11-03 19:51:38 -0700900 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100901 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700902 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700903 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700904 success = false;
905 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800906 if (addr_found) {
907 *stop_analysis = true;
908 return success;
909 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700911 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700912 }
Ian Rogers0571d352011-11-03 19:51:38 -0700913 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100914 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700916 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800917
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700918 static constexpr uint32_t kPrologueBytes = 16;
919
920 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
921 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
922
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100923 bool DumpOatMethod(VariableIndentationOutputStream* vios,
924 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700925 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700926 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800927 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700928 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700929 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800930
931 // TODO: Support regex
932 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
933 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000934 return success;
935 }
936
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800937 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100938 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
939 class_method_index, pretty_method.c_str(),
940 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800941 if (options_.list_methods_) return success;
942
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800943 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
944 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
945 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
946 uint32_t code_offset = oat_method.GetCodeOffset();
947 uint32_t code_size = oat_method.GetQuickCodeSize();
948 if (resolved_addr2instr_ != 0) {
949 if (resolved_addr2instr_ > code_offset + code_size) {
950 return success;
951 } else {
952 *addr_found = true; // stop analyzing file at next iteration
953 }
954 }
955
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100956 // Everything below is indented at least once.
957 ScopedIndentation indent1(vios);
958
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800959 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100960 vios->Stream() << "DEX CODE:\n";
961 ScopedIndentation indent2(vios);
962 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700963 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700964
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700965 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700966 std::unique_ptr<verifier::MethodVerifier> verifier;
967 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700968 // We need to have the handle scope stay live until after the verifier since the verifier has
969 // a handle to the dex cache from hs.
970 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100971 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
972 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700973 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100974 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700975 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700976 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800977 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100978 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800979 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100980 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100981 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100982 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700983 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100984 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700985 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
986 oat_method_offsets_offset, oat_file_.Size());
987 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100988 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700989 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800990 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700991
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100992 ScopedIndentation indent2(vios);
993 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700994 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
995 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100996 vios->Stream() << StringPrintf("WARNING: "
997 "code offset 0x%08x is past end of file 0x%08zx.\n",
998 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700999 success = false;
1000 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001001 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001002 }
1003 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001004 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001005 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
1006 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001007
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001008 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001009 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001010 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001011 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001012 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001013 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001014 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1015 method_header_offset, oat_file_.Size());
1016 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001017 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001018 return false;
1019 }
1020
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001021 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001023 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001024 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001025 }
1026 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001027 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001028 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001029 vios->Stream() << StringPrintf("WARNING: "
1030 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1031 "vmap table offset was loaded from offset 0x%08x.\n",
1032 vmap_table_offset, oat_file_.Size(),
1033 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001034 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001035 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001036 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001037 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001038 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001039 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001040 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001041
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001042 ScopedIndentation indent2(vios);
1043 vios->Stream()
1044 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1045 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1046 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1047 vios->Stream() << "\n";
1048 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1049 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1050 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001051 }
1052 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001053 // Based on spill masks from QuickMethodFrameInfo so placed
1054 // after it is dumped, but useful for understanding quick
1055 // code, so dumped here.
1056 ScopedIndentation indent2(vios);
1057 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001058 }
1059 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001060 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001061 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1062 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001063 ScopedIndentation indent2(vios);
1064 vios->Stream() << StringPrintf("WARNING: "
1065 "code size offset 0x%08x is past end of file 0x%08zx.",
1066 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001067 success = false;
1068 } else {
1069 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001070 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1071 uint64_t aligned_code_end = aligned_code_begin + code_size;
1072
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001073 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001074 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001075 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001076 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1077 code_offset,
1078 code_size_offset,
1079 code_size,
1080 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001081
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001082 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001083 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001084 vios->Stream() << StringPrintf("WARNING: "
1085 "start of code at 0x%08x is past end of file 0x%08zx.",
1086 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001087 success = false;
1088 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001089 vios->Stream() << StringPrintf(
1090 "WARNING: "
1091 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1092 "code size is 0x%08x loaded from offset 0x%08x.\n",
1093 aligned_code_end, oat_file_.Size(),
1094 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001095 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001096 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001097 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001098 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001099 }
1100 }
1101 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001102 vios->Stream() << StringPrintf(
1103 "WARNING: "
1104 "code size %d is bigger than max expected threshold of %d. "
1105 "code size is 0x%08x loaded from offset 0x%08x.\n",
1106 code_size, kMaxCodeSize,
1107 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001108 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001109 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001110 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001111 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001112 }
1113 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001114 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001115 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001116 }
1117 }
1118 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001119 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001120 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001121 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001122
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001123 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1124 if (spill_mask == 0) {
1125 return;
1126 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001127 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001128 for (size_t i = 0; i < 32; i++) {
1129 if ((spill_mask & (1 << i)) != 0) {
1130 if (is_float) {
1131 os << "fr" << i;
1132 } else {
1133 os << "r" << i;
1134 }
1135 spill_mask ^= 1 << i; // clear bit
1136 if (spill_mask != 0) {
1137 os << ", ";
1138 } else {
1139 break;
1140 }
1141 }
1142 }
1143 os << ")";
1144 }
1145
Roland Levillain442b46a2015-02-18 16:54:21 +00001146 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001147 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001148 const OatFile::OatMethod& oat_method,
1149 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001150 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1151 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001152 const void* raw_code_info = oat_method.GetVmapTable();
1153 if (raw_code_info != nullptr) {
1154 CodeInfo code_info(raw_code_info);
1155 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001156 ScopedIndentation indent1(vios);
1157 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001158 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001159 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1160 // We don't encode the size in the table, so just emit that we have quickened
1161 // information.
1162 ScopedIndentation indent(vios);
1163 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001164 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001165 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001166 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001167 }
1168
1169 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001170 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001171 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001172 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001173 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001174 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001175 oat_method.GetCodeOffset(),
1176 code_item.registers_size_,
1177 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001178 }
1179
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001180 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1181 const DexFile::CodeItem* code_item) {
1182 if (code_item != nullptr) {
1183 size_t num_locals_ins = code_item->registers_size_;
1184 size_t num_ins = code_item->ins_size_;
1185 size_t num_locals = num_locals_ins - num_ins;
1186 size_t num_outs = code_item->outs_size_;
1187
1188 os << "vr_stack_locations:";
1189 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1190 // For readability, delimit the different kinds of VRs.
1191 if (reg == num_locals_ins) {
1192 os << "\n\tmethod*:";
1193 } else if (reg == num_locals && num_ins > 0) {
1194 os << "\n\tins:";
1195 } else if (reg == 0 && num_locals > 0) {
1196 os << "\n\tlocals:";
1197 }
1198
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001199 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1200 code_item,
1201 oat_method.GetCoreSpillMask(),
1202 oat_method.GetFpSpillMask(),
1203 oat_method.GetFrameSizeInBytes(),
1204 reg,
1205 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001206 os << " v" << reg << "[sp + #" << offset << "]";
1207 }
1208
1209 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1210 if (out_reg == 0) {
1211 os << "\n\touts:";
1212 }
1213
1214 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1215 os << " v" << out_reg << "[sp + #" << offset << "]";
1216 }
1217
1218 os << "\n";
1219 }
1220 }
1221
Ian Rogersb23a7722012-10-09 16:54:26 -07001222 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001223 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001224 size_t i = 0;
1225 while (i < code_item->insns_size_in_code_units_) {
1226 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001227 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1228 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001229 i += instruction->SizeInCodeUnits();
1230 }
1231 }
1232 }
1233
Roland Levillainf2650d12015-05-28 14:53:28 +01001234 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1235 // the optimizing compiler?
1236 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1237 const DexFile::CodeItem* code_item) {
1238 // If the native GC map is null and the Dex `code_item` is not
1239 // null, then this method has been compiled with the optimizing
1240 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001241 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001242 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001243 code_item != nullptr;
1244 }
1245
1246 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1247 // the dextodex compiler?
1248 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1249 const DexFile::CodeItem* code_item) {
1250 // If the quick code is null, the Dex `code_item` is not
1251 // null, and the vmap table is not null, then this method has been compiled
1252 // with the dextodex compiler.
1253 return oat_method.GetQuickCode() == nullptr &&
1254 oat_method.GetVmapTable() != nullptr &&
1255 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001256 }
1257
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001258 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001259 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001260 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001261 const DexFile* dex_file,
1262 const DexFile::ClassDef& class_def,
1263 const DexFile::CodeItem* code_item,
1264 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001265 if ((method_access_flags & kAccNative) == 0) {
1266 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001267 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001268 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001269 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001270 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001271 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001272 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
David Brazdil15fc7292016-09-02 14:13:18 +01001273 class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001274 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001275
1276 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001277 }
1278
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001279 // The StackMapsHelper provides the stack maps in the native PC order.
1280 // For identical native PCs, the order from the CodeInfo is preserved.
1281 class StackMapsHelper {
1282 public:
1283 explicit StackMapsHelper(const uint8_t* raw_code_info)
1284 : code_info_(raw_code_info),
1285 encoding_(code_info_.ExtractEncoding()),
1286 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1287 indexes_(),
1288 offset_(static_cast<size_t>(-1)),
1289 stack_map_index_(0u) {
1290 if (number_of_stack_maps_ != 0u) {
1291 // Check if native PCs are ordered.
1292 bool ordered = true;
1293 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1294 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1295 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1296 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1297 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1298 ordered = false;
1299 break;
1300 }
1301 last = current;
1302 }
1303 if (!ordered) {
1304 // Create indirection indexes for access in native PC order. We do not optimize
1305 // for the fact that there can currently be only two separately ordered ranges,
1306 // namely normal stack maps and catch-point stack maps.
1307 indexes_.resize(number_of_stack_maps_);
1308 std::iota(indexes_.begin(), indexes_.end(), 0u);
1309 std::sort(indexes_.begin(),
1310 indexes_.end(),
1311 [this](size_t lhs, size_t rhs) {
1312 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1313 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1314 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1315 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1316 // If the PCs are the same, compare indexes to preserve the original order.
1317 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1318 });
1319 }
1320 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1321 }
1322 }
1323
1324 const CodeInfo& GetCodeInfo() const {
1325 return code_info_;
1326 }
1327
1328 const CodeInfoEncoding& GetEncoding() const {
1329 return encoding_;
1330 }
1331
1332 size_t GetOffset() const {
1333 return offset_;
1334 }
1335
1336 StackMap GetStackMap() const {
1337 return GetStackMapAt(stack_map_index_);
1338 }
1339
1340 void Next() {
1341 ++stack_map_index_;
1342 offset_ = (stack_map_index_ == number_of_stack_maps_)
1343 ? static_cast<size_t>(-1)
1344 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1345 }
1346
1347 private:
1348 StackMap GetStackMapAt(size_t i) const {
1349 if (!indexes_.empty()) {
1350 i = indexes_[i];
1351 }
1352 DCHECK_LT(i, number_of_stack_maps_);
1353 return code_info_.GetStackMapAt(i, encoding_);
1354 }
1355
1356 const CodeInfo code_info_;
1357 const CodeInfoEncoding encoding_;
1358 const size_t number_of_stack_maps_;
1359 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1360 size_t offset_;
1361 size_t stack_map_index_;
1362 };
1363
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001364 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001365 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1366 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001367 const void* quick_code = oat_method.GetQuickCode();
1368
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001369 if (code_size == 0) {
1370 code_size = oat_method.GetQuickCodeSize();
1371 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001372 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001373 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001374 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001375 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1376 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1377 StackMapsHelper helper(oat_method.GetVmapTable());
1378 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1379 size_t offset = 0;
1380 while (offset < code_size) {
1381 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1382 if (offset == helper.GetOffset()) {
1383 ScopedIndentation indent1(vios);
1384 StackMap stack_map = helper.GetStackMap();
1385 DCHECK(stack_map.IsValid());
1386 stack_map.Dump(vios,
1387 helper.GetCodeInfo(),
1388 helper.GetEncoding(),
1389 oat_method.GetCodeOffset(),
1390 code_item->registers_size_);
1391 do {
1392 helper.Next();
1393 // There may be multiple stack maps at a given PC. We display only the first one.
1394 } while (offset == helper.GetOffset());
1395 }
1396 DCHECK_LT(offset, helper.GetOffset());
1397 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001398 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001399 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1400 size_t offset = 0;
1401 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001402 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001403 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001404 }
1405 }
1406
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001407 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001408 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001409 const OatDumperOptions& options_;
1410 uint32_t resolved_addr2instr_;
Andreas Gampe372f3a32016-08-19 10:49:06 -07001411 const InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001412 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001413 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001414};
1415
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001416class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001417 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001418 ImageDumper(std::ostream* os,
1419 gc::space::ImageSpace& image_space,
1420 const ImageHeader& image_header,
1421 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001422 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001423 vios_(os),
1424 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001425 image_space_(image_space),
1426 image_header_(image_header),
1427 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001428
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001429 bool Dump() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001431 std::ostream& indent_os = vios_.Stream();
1432
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001433 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001434
Jeff Haodcdc85b2015-12-04 14:06:18 -08001435 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1436
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001437 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001438
Mathieu Chartiere401d142015-04-22 13:56:20 -07001439 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1440
1441 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1442 auto section = static_cast<ImageHeader::ImageSections>(i);
1443 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1444 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001445
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001447
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001448 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001449
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001450 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1451
1452 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1453
1454 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001455
Alex Lighta59dd802014-07-02 16:28:08 -07001456 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1457
Igor Murashkin46774762014-10-22 11:37:02 -07001458 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1459
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001460 {
1461 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001462 static_assert(arraysize(image_roots_descriptions_) ==
1463 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1465 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1466 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001467 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001468 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001469 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001470 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001471 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001472 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001473 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1474 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001475 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001476 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1477 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001478 run++;
1479 } else {
1480 break;
1481 }
1482 }
1483 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001484 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001485 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001486 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001487 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001488 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001489 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001490 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001492 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001493 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001494 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001495 }
1496 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001497 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001498
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001499 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001500 os << "METHOD ROOTS\n";
1501 static_assert(arraysize(image_methods_descriptions_) ==
1502 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1503 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1504 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1505 const char* description = image_methods_descriptions_[i];
1506 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001507 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001508 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001509 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001510 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001511
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001512 Runtime* const runtime = Runtime::Current();
1513 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001514 std::string image_filename = image_space_.GetImageFilename();
1515 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001516 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001517 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001518 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001519 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001520 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001521 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1522 }
1523 if (oat_file == nullptr) {
1524 oat_file = OatFile::Open(oat_location,
1525 oat_location,
1526 nullptr,
1527 nullptr,
1528 false,
1529 /*low_4gb*/false,
1530 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001531 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001532 }
1533 if (oat_file == nullptr) {
1534 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1535 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001536 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001537 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001538
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001539 stats_.oat_file_bytes = oat_file->Size();
1540
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001541 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001542
Mathieu Chartier02e25112013-08-14 16:14:24 -07001543 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001544 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001545 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1546 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001547 }
1548
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001550
Jeff Haodcdc85b2015-12-04 14:06:18 -08001551 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001552 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001553 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001554 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001555 {
1556 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1557 heap->FlushAllocStack();
1558 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001559 // Since FlushAllocStack() above resets the (active) allocation
1560 // stack. Need to revoke the thread-local allocation stacks that
1561 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001562 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001563 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001564 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001565 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001566 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001567 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001568 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001569 {
1570 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001571 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001572 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001573 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001574 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001575 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001576 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001577 }
1578 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001579 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001580 // Dump the normal objects before ArtMethods.
1581 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1582 indent_os << "\n";
1583 // TODO: Dump fields.
1584 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001585 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001586 image_header_.VisitPackedArtMethods(&visitor,
1587 image_space_.Begin(),
1588 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001589 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001590 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001591 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001592 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001593 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001594 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001595 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1596 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001597 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001598 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001599 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001600 // If the image is compressed, adjust to decompressed size.
1601 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1602 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1603 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1604 }
1605 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001606 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001607 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001608 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001609 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1610 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001611 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1612 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001613 const auto& intern_section = image_header_.GetImageSection(
1614 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001615 const auto& class_table_section = image_header_.GetImageSection(
1616 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001617 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1618
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001619 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001620
1621 // Objects are kObjectAlignment-aligned.
1622 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1623 if (object_section.Offset() > header_bytes) {
1624 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1625 }
1626
1627 // Field section is 4-byte aligned.
1628 constexpr size_t kFieldSectionAlignment = 4U;
1629 uint32_t end_objects = object_section.Offset() + object_section.Size();
1630 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1631 stats_.alignment_bytes += field_section.Offset() - end_objects;
1632
1633 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1634 uint32_t end_fields = field_section.Offset() + field_section.Size();
1635 CHECK_ALIGNED(method_section.Offset(), 4);
1636 stats_.alignment_bytes += method_section.Offset() - end_fields;
1637
1638 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1639 uint32_t end_methods = method_section.Offset() + method_section.Size();
1640 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1641 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1642
1643 // Intern table is 8-byte aligned.
1644 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1645 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1646 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1647
1648 // Add space between intern table and class table.
1649 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1650 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1651
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001652 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1653 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001654 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001655 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001656
Mathieu Chartiere401d142015-04-22 13:56:20 -07001657 stats_.bitmap_bytes += bitmap_section.Size();
1658 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001659 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001660 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001661 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001662 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001663 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001664 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001665
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001666 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001668 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001669 }
1670
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001671 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001672 class DumpArtMethodVisitor : public ArtMethodVisitor {
1673 public:
1674 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1675
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001676 virtual void Visit(ArtMethod* method) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001677 std::ostream& indent_os = image_dumper_->vios_.Stream();
1678 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001679 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001680 indent_os << "\n";
1681 }
1682
1683 private:
1684 ImageDumper* const image_dumper_;
1685 };
1686
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001687 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001688 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001689 CHECK(type != nullptr);
1690 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001691 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001692 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001693 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001694 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001695 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001696 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001697 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001698 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001699 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001700 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001701 }
1702 }
1703
Mathieu Chartierc7853442015-03-27 14:35:38 -07001704 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001705 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001706 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001707 switch (field->GetTypeAsPrimitiveType()) {
1708 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001709 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001710 break;
1711 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001712 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001713 break;
1714 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001715 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001716 break;
1717 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001718 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001719 break;
1720 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001721 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001722 break;
1723 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001724 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001725 break;
1726 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001727 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1728 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001729 break;
1730 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001731 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001732 break;
1733 case Primitive::kPrimNot: {
1734 // Get the value, don't compute the type unless it is non-null as we don't want
1735 // to cause class loading.
1736 mirror::Object* value = field->GetObj(obj);
1737 if (value == nullptr) {
1738 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001739 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001740 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001741 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001742 if (field_type != nullptr) {
1743 PrettyObjectValue(os, field_type, value);
1744 } else {
1745 os << StringPrintf("%p %s\n", value,
1746 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1747 }
Ian Rogers50239c72013-06-17 14:53:22 -07001748 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001749 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001750 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001751 default:
1752 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1753 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001754 }
1755 }
1756
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001757 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001758 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001759 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001760 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001761 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001762 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001763 for (ArtField& field : klass->GetIFields()) {
1764 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001765 }
1766 }
1767
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001768 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001769 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001770 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001771
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001772 const void* GetQuickOatCodeBegin(ArtMethod* m) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001773 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001774 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001775 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001776 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001777 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001778 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001779 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001780 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001781 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001782 }
1783
Mathieu Chartiere401d142015-04-22 13:56:20 -07001784 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001785 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001786 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1787 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001788 return 0;
1789 }
1790 return oat_code_begin[-1];
1791 }
1792
Mathieu Chartiere401d142015-04-22 13:56:20 -07001793 const void* GetQuickOatCodeEnd(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001794 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001795 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001796 if (oat_code_begin == nullptr) {
1797 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001798 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001799 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001800 }
1801
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001802 static void Callback(mirror::Object* obj, void* arg) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001803 DCHECK(obj != nullptr);
1804 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001805 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001806 if (!state->InDumpSpace(obj)) {
1807 return;
1808 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001809
1810 size_t object_bytes = obj->SizeOf();
1811 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1812 state->stats_.object_bytes += object_bytes;
1813 state->stats_.alignment_bytes += alignment_bytes;
1814
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001815 std::ostream& os = state->vios_.Stream();
1816
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001817 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001818 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001819 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1820 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001821 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001822 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001823 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1824 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001825 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001826 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001827 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001828 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001829 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001830 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001831 ScopedIndentation indent1(&state->vios_);
1832 DumpFields(os, obj, obj_class);
Andreas Gampe542451c2016-07-26 09:02:02 -07001833 const PointerSize image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001834 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001835 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1836 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001837 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001838 size_t run = 0;
1839 for (int32_t j = i + 1; j < length; j++) {
1840 if (value == obj_array->Get(j)) {
1841 run++;
1842 } else {
1843 break;
1844 }
1845 }
1846 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001847 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001848 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001849 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001850 i = i + run;
1851 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001852 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001853 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001854 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001855 }
1856 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001857 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001858 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001859 os << "STATICS:\n";
1860 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001861 for (ArtField& field : klass->GetSFields()) {
1862 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001863 }
1864 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001865 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001866 auto it = state->dex_caches_.find(obj);
1867 if (it != state->dex_caches_.end()) {
1868 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001869 const auto& field_section = state->image_header_.GetImageSection(
1870 ImageHeader::kSectionArtFields);
1871 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001872 size_t num_methods = dex_cache->NumResolvedMethods();
1873 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001874 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001875 ScopedIndentation indent2(&state->vios_);
1876 auto* resolved_methods = dex_cache->GetResolvedMethods();
1877 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001878 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1879 i,
1880 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001881 size_t run = 0;
1882 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001883 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1884 j,
1885 image_pointer_size);
1886 ++j) {
1887 ++run;
1888 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001889 if (run == 0) {
1890 os << StringPrintf("%zd: ", i);
1891 } else {
1892 os << StringPrintf("%zd to %zd: ", i, i + run);
1893 i = i + run;
1894 }
1895 std::string msg;
1896 if (elem == nullptr) {
1897 msg = "null";
1898 } else if (method_section.Contains(
1899 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1900 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1901 } else {
1902 msg = "<not in method section>";
1903 }
1904 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001905 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001906 }
1907 size_t num_fields = dex_cache->NumResolvedFields();
1908 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001909 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001910 ScopedIndentation indent2(&state->vios_);
1911 auto* resolved_fields = dex_cache->GetResolvedFields();
1912 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001913 auto* elem = mirror::DexCache::GetElementPtrSize(
1914 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001915 size_t run = 0;
1916 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001917 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1918 j,
1919 image_pointer_size);
1920 ++j) {
1921 ++run;
1922 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001923 if (run == 0) {
1924 os << StringPrintf("%zd: ", i);
1925 } else {
1926 os << StringPrintf("%zd to %zd: ", i, i + run);
1927 i = i + run;
1928 }
1929 std::string msg;
1930 if (elem == nullptr) {
1931 msg = "null";
1932 } else if (field_section.Contains(
1933 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1934 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1935 } else {
1936 msg = "<not in field section>";
1937 }
1938 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001939 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001940 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001941 size_t num_types = dex_cache->NumResolvedTypes();
1942 if (num_types != 0u) {
1943 os << "Types (size=" << num_types << "):\n";
1944 ScopedIndentation indent2(&state->vios_);
1945 auto* resolved_types = dex_cache->GetResolvedTypes();
1946 for (size_t i = 0; i < num_types; ++i) {
1947 auto* elem = resolved_types[i].Read();
1948 size_t run = 0;
1949 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1950 ++run;
1951 }
1952 if (run == 0) {
1953 os << StringPrintf("%zd: ", i);
1954 } else {
1955 os << StringPrintf("%zd to %zd: ", i, i + run);
1956 i = i + run;
1957 }
1958 std::string msg;
1959 if (elem == nullptr) {
1960 msg = "null";
1961 } else {
1962 msg = PrettyClass(elem);
1963 }
1964 os << StringPrintf("%p %s\n", elem, msg.c_str());
1965 }
1966 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001967 }
1968 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001969 std::string temp;
1970 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001971 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001972
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001973 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001974 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001975 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001976 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1977 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Andreas Gampe542451c2016-07-26 09:02:02 -07001978 const PointerSize pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001979 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1980 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001981 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001982 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001983 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1984 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001985 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001986 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001987 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001988 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
1989 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001990 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001991 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001992 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00001993 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001994 } else if (method->IsRuntimeMethod()) {
1995 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
1996 if (table != nullptr) {
1997 indent_os << "IMT conflict table " << table << " method: ";
1998 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
1999 indent_os << PrettyMethod(table->GetImplementationMethod(i, pointer_size)) << " ";
2000 }
2001 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002002 } else {
2003 const DexFile::CodeItem* code_item = method->GetCodeItem();
2004 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002005 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002006
2007 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002008 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002009 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002010 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002011 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002012 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002013 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002014 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015 }
2016
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002017 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2018 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002019 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002020 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002021 if (method->IsConstructor()) {
2022 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002023 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002025 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 }
2027 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002028 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002029 }
2030 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002031 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002032
Igor Murashkin7617abd2015-07-10 18:27:47 -07002033 uint32_t method_access_flags = method->GetAccessFlags();
2034
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002036 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2037 dex_instruction_bytes,
2038 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002039 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002040
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002041 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002042 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043
2044 double expansion =
2045 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002046 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002047 }
2048 }
2049
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002050 std::set<const void*> already_seen_;
2051 // Compute the size of the given data within the oat file and whether this is the first time
2052 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002053 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002054 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002055 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002056 already_seen_.insert(oat_data);
2057 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002058 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002059 }
2060 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002061 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002062
2063 public:
2064 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002065 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002066 size_t file_bytes;
2067
2068 size_t header_bytes;
2069 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002070 size_t art_field_bytes;
2071 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002072 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002073 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002074 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002075 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002076 size_t alignment_bytes;
2077
2078 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002079 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002080 size_t managed_to_native_code_bytes;
2081 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002082 size_t class_initializer_code_bytes;
2083 size_t large_initializer_code_bytes;
2084 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002085
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002086 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002087
2088 size_t dex_instruction_bytes;
2089
Mathieu Chartiere401d142015-04-22 13:56:20 -07002090 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002091 std::vector<size_t> method_outlier_size;
2092 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002093 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002094
Roland Levillain3887c462015-08-12 18:15:42 +01002095 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002096 : oat_file_bytes(0),
2097 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002098 header_bytes(0),
2099 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002100 art_field_bytes(0),
2101 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002102 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002103 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002104 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002105 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002106 alignment_bytes(0),
2107 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002108 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002109 managed_to_native_code_bytes(0),
2110 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002111 class_initializer_code_bytes(0),
2112 large_initializer_code_bytes(0),
2113 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002114 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002115 dex_instruction_bytes(0) {}
2116
Elliott Hughesa0e18062012-04-13 15:59:59 -07002117 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002118 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002119 size_t bytes;
2120 size_t count;
2121 };
2122 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2123 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002124
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002125 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002126 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2127 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002128 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002129 it->second.count += 1;
2130 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002131 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002132 }
2133 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002134
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002135 double PercentOfOatBytes(size_t size) {
2136 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2137 }
2138
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002139 double PercentOfFileBytes(size_t size) {
2140 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2141 }
2142
2143 double PercentOfObjectBytes(size_t size) {
2144 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2145 }
2146
Mathieu Chartiere401d142015-04-22 13:56:20 -07002147 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002148 method_outlier_size.push_back(total_size);
2149 method_outlier_expansion.push_back(expansion);
2150 method_outlier.push_back(method);
2151 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002152
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002153 void DumpOutliers(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002154 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002155 size_t sum_of_sizes = 0;
2156 size_t sum_of_sizes_squared = 0;
2157 size_t sum_of_expansion = 0;
2158 size_t sum_of_expansion_squared = 0;
2159 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002160 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002161 return;
2162 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002163 for (size_t i = 0; i < n; i++) {
2164 size_t cur_size = method_outlier_size[i];
2165 sum_of_sizes += cur_size;
2166 sum_of_sizes_squared += cur_size * cur_size;
2167 double cur_expansion = method_outlier_expansion[i];
2168 sum_of_expansion += cur_expansion;
2169 sum_of_expansion_squared += cur_expansion * cur_expansion;
2170 }
2171 size_t size_mean = sum_of_sizes / n;
2172 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2173 double expansion_mean = sum_of_expansion / n;
2174 double expansion_variance =
2175 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2176
2177 // Dump methods whose size is a certain number of standard deviations from the mean
2178 size_t dumped_values = 0;
2179 size_t skipped_values = 0;
2180 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2181 size_t cur_size_variance = i * i * size_variance;
2182 bool first = true;
2183 for (size_t j = 0; j < n; j++) {
2184 size_t cur_size = method_outlier_size[j];
2185 if (cur_size > size_mean) {
2186 size_t cur_var = cur_size - size_mean;
2187 cur_var = cur_var * cur_var;
2188 if (cur_var > cur_size_variance) {
2189 if (dumped_values > 20) {
2190 if (i == 1) {
2191 skipped_values++;
2192 } else {
2193 i = 2; // jump to counting for 1 standard deviation
2194 break;
2195 }
2196 } else {
2197 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002198 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002199 first = false;
2200 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002201 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002202 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002203 method_outlier_size[j] = 0; // don't consider this method again
2204 dumped_values++;
2205 }
2206 }
2207 }
2208 }
2209 }
2210 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002211 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002212 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002213 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002214 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002215
2216 // Dump methods whose expansion is a certain number of standard deviations from the mean
2217 dumped_values = 0;
2218 skipped_values = 0;
2219 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2220 double cur_expansion_variance = i * i * expansion_variance;
2221 bool first = true;
2222 for (size_t j = 0; j < n; j++) {
2223 double cur_expansion = method_outlier_expansion[j];
2224 if (cur_expansion > expansion_mean) {
2225 size_t cur_var = cur_expansion - expansion_mean;
2226 cur_var = cur_var * cur_var;
2227 if (cur_var > cur_expansion_variance) {
2228 if (dumped_values > 20) {
2229 if (i == 1) {
2230 skipped_values++;
2231 } else {
2232 i = 2; // jump to counting for 1 standard deviation
2233 break;
2234 }
2235 } else {
2236 if (first) {
2237 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002238 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002239 first = false;
2240 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002241 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002242 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002243 method_outlier_expansion[j] = 0.0; // don't consider this method again
2244 dumped_values++;
2245 }
2246 }
2247 }
2248 }
2249 }
2250 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002251 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002252 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002253 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002254 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002255 }
2256
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002257 void Dump(std::ostream& os, std::ostream& indent_os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002258 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002259 {
2260 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2261 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002262 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2263 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2264 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2265 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2266 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2267 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002268 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002269 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2270 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002271 header_bytes, PercentOfFileBytes(header_bytes),
2272 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002273 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2274 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002275 dex_cache_arrays_bytes,
2276 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002277 interned_strings_bytes,
2278 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002279 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002280 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2282 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002283 CHECK_EQ(file_bytes,
2284 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2285 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2286 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002287 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002288
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002289 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002290 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002291 for (const auto& sizes_and_count : sizes_and_counts) {
2292 const std::string& descriptor(sizes_and_count.first);
2293 double average = static_cast<double>(sizes_and_count.second.bytes) /
2294 static_cast<double>(sizes_and_count.second.count);
2295 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002296 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002297 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002298 descriptor.c_str(), sizes_and_count.second.bytes,
2299 sizes_and_count.second.count, average, percent);
2300 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002301 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002302 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002303 CHECK_EQ(object_bytes, object_bytes_total);
2304
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002305 os << StringPrintf("oat_file_bytes = %8zd\n"
2306 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2307 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2308 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2309 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2310 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2311 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002312 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002313 managed_code_bytes,
2314 PercentOfOatBytes(managed_code_bytes),
2315 managed_to_native_code_bytes,
2316 PercentOfOatBytes(managed_to_native_code_bytes),
2317 native_to_managed_code_bytes,
2318 PercentOfOatBytes(native_to_managed_code_bytes),
2319 class_initializer_code_bytes,
2320 PercentOfOatBytes(class_initializer_code_bytes),
2321 large_initializer_code_bytes,
2322 PercentOfOatBytes(large_initializer_code_bytes),
2323 large_method_code_bytes,
2324 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002326 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002327 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002328 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2329 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002330 }
2331
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002332 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002333 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002334 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002335
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002336 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2337 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002338 static_cast<double>(managed_code_bytes) /
2339 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002340 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002341 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002342 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002343
2344 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002345 }
2346 } stats_;
2347
2348 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002349 enum {
2350 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2351 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2352 kLargeConstructorDexBytes = 4000,
2353 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2354 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2355 kLargeMethodDexBytes = 16000
2356 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002357
2358 // For performance, use the *os_ directly for anything that doesn't need indentation
2359 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002361 VariableIndentationOutputStream vios_;
2362 ScopedIndentation indent1_;
2363
Ian Rogers1d54e732013-05-02 21:10:01 -07002364 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002365 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002366 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002367 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002368 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002369
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002370 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002371};
2372
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002373static int DumpImage(gc::space::ImageSpace* image_space,
2374 OatDumperOptions* options,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002375 std::ostream* os) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002376 const ImageHeader& image_header = image_space->GetImageHeader();
2377 if (!image_header.IsValid()) {
2378 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2379 return EXIT_FAILURE;
2380 }
2381 ImageDumper image_dumper(os, *image_space, image_header, options);
2382 if (!image_dumper.Dump()) {
2383 return EXIT_FAILURE;
2384 }
2385 return EXIT_SUCCESS;
2386}
2387
2388static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002389 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002390 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002391 options->class_loader_ = &null_class_loader;
2392
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002393 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002394 if (options->app_image_ != nullptr) {
2395 if (options->app_oat_ == nullptr) {
2396 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002397 return EXIT_FAILURE;
2398 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002399 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2400 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2401 // pointers into 32 bit pointer sized ArtMethods.
2402 std::string error_msg;
2403 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2404 options->app_oat_,
2405 nullptr,
2406 nullptr,
2407 false,
2408 /*low_4gb*/true,
2409 nullptr,
2410 &error_msg));
2411 if (oat_file == nullptr) {
2412 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002413 return EXIT_FAILURE;
2414 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002415 std::unique_ptr<gc::space::ImageSpace> space(
2416 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2417 if (space == nullptr) {
2418 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2419 << error_msg;
2420 }
2421 // Open dex files for the image.
2422 std::vector<std::unique_ptr<const DexFile>> dex_files;
2423 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2424 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2425 << error_msg;
2426 }
2427 // Dump the actual image.
2428 int result = DumpImage(space.get(), options, os);
2429 if (result != EXIT_SUCCESS) {
2430 return result;
2431 }
2432 // Fall through to dump the boot images.
2433 }
2434
2435 gc::Heap* heap = runtime->GetHeap();
2436 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2437 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2438 int result = DumpImage(image_space, options, os);
2439 if (result != EXIT_SUCCESS) {
2440 return result;
2441 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002442 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002443 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002444}
2445
Andreas Gampe00b25f32014-09-17 21:49:05 -07002446static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2447 std::ostream* os) {
2448 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2449
2450 Thread* self = Thread::Current();
2451 CHECK(self != nullptr);
2452 // Need well-known-classes.
2453 WellKnownClasses::Init(self->GetJniEnv());
2454
2455 // Need to register dex files to get a working dex cache.
2456 ScopedObjectAccess soa(self);
2457 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002458 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002459 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002460 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2461 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002462 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002463 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002464 class_linker->RegisterDexFile(*dex_file, nullptr);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002465 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002466 }
2467
2468 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002469 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002470 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002471
2472 // Use the class loader while dumping.
2473 StackHandleScope<1> scope(self);
2474 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2475 soa.Decode<mirror::ClassLoader*>(class_loader));
2476 options->class_loader_ = &loader_handle;
2477
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002478 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002479 bool success = oat_dumper.Dump(*os);
2480 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2481}
2482
2483static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002484 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002485 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002486 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002487 options->class_loader_ = &null_class_loader;
2488
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002489 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002490 bool success = oat_dumper.Dump(*os);
2491 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2492}
2493
2494static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2495 std::ostream* os) {
2496 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002497 OatFile* oat_file = OatFile::Open(oat_filename,
2498 oat_filename,
2499 nullptr,
2500 nullptr,
2501 false,
2502 /*low_4gb*/false,
2503 nullptr,
2504 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002505 if (oat_file == nullptr) {
2506 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2507 return EXIT_FAILURE;
2508 }
2509
2510 if (runtime != nullptr) {
2511 return DumpOatWithRuntime(runtime, oat_file, options, os);
2512 } else {
2513 return DumpOatWithoutRuntime(oat_file, options, os);
2514 }
2515}
2516
David Srbecky2fdd03c2016-03-10 15:32:37 +00002517static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002518 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002519 OatFile* oat_file = OatFile::Open(oat_filename,
2520 oat_filename,
2521 nullptr,
2522 nullptr,
2523 false,
2524 /*low_4gb*/false,
2525 nullptr,
2526 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002527 if (oat_file == nullptr) {
2528 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2529 return EXIT_FAILURE;
2530 }
2531
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002532 bool result;
2533 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2534 // files for 64-bit code in the past.
2535 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002536 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002537 result = oat_symbolizer.Symbolize();
2538 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002539 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002540 result = oat_symbolizer.Symbolize();
2541 }
2542 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002543 fprintf(stderr, "Failed to symbolize\n");
2544 return EXIT_FAILURE;
2545 }
2546
2547 return EXIT_SUCCESS;
2548}
2549
Igor Murashkin37743352014-11-13 14:38:00 -08002550struct OatdumpArgs : public CmdlineArgs {
2551 protected:
2552 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002553
Igor Murashkin37743352014-11-13 14:38:00 -08002554 virtual ParseStatus ParseCustom(const StringPiece& option,
2555 std::string* error_msg) OVERRIDE {
2556 {
2557 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2558 if (base_parse != kParseUnknownArgument) {
2559 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002560 }
2561 }
2562
Igor Murashkin37743352014-11-13 14:38:00 -08002563 if (option.starts_with("--oat-file=")) {
2564 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2565 } else if (option.starts_with("--image=")) {
2566 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002567 } else if (option == "--no-dump:vmap") {
2568 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002569 } else if (option =="--dump:code_info_stack_maps") {
2570 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002571 } else if (option == "--no-disassemble") {
2572 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002573 } else if (option =="--header-only") {
2574 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002575 } else if (option.starts_with("--symbolize=")) {
2576 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2577 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002578 } else if (option.starts_with("--only-keep-debug")) {
2579 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002580 } else if (option.starts_with("--class-filter=")) {
2581 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002582 } else if (option.starts_with("--method-filter=")) {
2583 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002584 } else if (option.starts_with("--list-classes")) {
2585 list_classes_ = true;
2586 } else if (option.starts_with("--list-methods")) {
2587 list_methods_ = true;
2588 } else if (option.starts_with("--export-dex-to=")) {
2589 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2590 } else if (option.starts_with("--addr2instr=")) {
2591 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2592 *error_msg = "Address conversion failed";
2593 return kParseError;
2594 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002595 } else if (option.starts_with("--app-image=")) {
2596 app_image_ = option.substr(strlen("--app-image=")).data();
2597 } else if (option.starts_with("--app-oat=")) {
2598 app_oat_ = option.substr(strlen("--app-oat=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002599 } else {
2600 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002601 }
2602
Igor Murashkin37743352014-11-13 14:38:00 -08002603 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002604 }
2605
Igor Murashkin37743352014-11-13 14:38:00 -08002606 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2607 // Infer boot image location from the image location if possible.
2608 if (boot_image_location_ == nullptr) {
2609 boot_image_location_ = image_location_;
2610 }
2611
2612 // Perform the parent checks.
2613 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2614 if (parent_checks != kParseOk) {
2615 return parent_checks;
2616 }
2617
2618 // Perform our own checks.
2619 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2620 *error_msg = "Either --image or --oat-file must be specified";
2621 return kParseError;
2622 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2623 *error_msg = "Either --image or --oat-file must be specified but not both";
2624 return kParseError;
2625 }
2626
2627 return kParseOk;
2628 }
2629
2630 virtual std::string GetUsage() const {
2631 std::string usage;
2632
2633 usage +=
2634 "Usage: oatdump [options] ...\n"
2635 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2636 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2637 "\n"
2638 // Either oat-file or image is required.
2639 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2640 " Example: --oat-file=/system/framework/boot.oat\n"
2641 "\n"
2642 " --image=<file.art>: specifies an input image location.\n"
2643 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002644 "\n"
2645 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
2646 " boot image and app oat file.\n"
2647 " Example: --app-image=app.art\n"
2648 "\n"
2649 " --app-oat=<file.odex>: specifies an input app oat.\n"
2650 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002651 "\n";
2652
2653 usage += Base::GetUsage();
2654
2655 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08002656 " --no-dump:vmap may be used to disable vmap dumping.\n"
2657 " Example: --no-dump:vmap\n"
2658 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002659 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2660 " Example: --dump:code_info_stack_maps\n"
2661 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002662 " --no-disassemble may be used to disable disassembly.\n"
2663 " Example: --no-disassemble\n"
2664 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002665 " --header-only may be used to print only the oat header.\n"
2666 " Example: --header-only\n"
2667 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002668 " --list-classes may be used to list target file classes (can be used with filters).\n"
2669 " Example: --list-classes\n"
2670 " Example: --list-classes --class-filter=com.example.foo\n"
2671 "\n"
2672 " --list-methods may be used to list target file methods (can be used with filters).\n"
2673 " Example: --list-methods\n"
2674 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2675 "\n"
2676 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2677 " Example: --symbolize=/system/framework/boot.oat\n"
2678 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00002679 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
2680 " .rodata and .text sections are omitted in the output file to save space.\n"
2681 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
2682 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002683 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2684 " Example: --class-filter=com.example.foo\n"
2685 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002686 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2687 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002688 "\n"
2689 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2690 " Example: --export-dex-to=/data/local/tmp\n"
2691 "\n"
2692 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2693 " address (e.g. PC from crash dump)\n"
2694 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002695 "\n";
2696
2697 return usage;
2698 }
2699
2700 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002701 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002702 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002703 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002704 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002705 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002706 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002707 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002708 bool disassemble_code_ = true;
2709 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002710 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002711 bool list_classes_ = false;
2712 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002713 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002714 uint32_t addr2instr_ = 0;
2715 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002716 const char* app_image_ = nullptr;
2717 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002718};
2719
Igor Murashkin37743352014-11-13 14:38:00 -08002720struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2721 virtual bool NeedsRuntime() OVERRIDE {
2722 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002723
Igor Murashkin37743352014-11-13 14:38:00 -08002724 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2725 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2726
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002727 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08002728 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002729 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002730 args_->disassemble_code_,
2731 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002732 args_->class_filter_,
2733 args_->method_filter_,
2734 args_->list_classes_,
2735 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002736 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002737 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002738 args_->app_image_,
2739 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002740 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002741
2742 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2743 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002744 }
2745
Igor Murashkin37743352014-11-13 14:38:00 -08002746 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2747 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002748 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002749
Mathieu Chartierd424d082014-10-15 10:31:46 -07002750 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002751
Andreas Gampec24f3992014-12-17 20:40:11 -08002752 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002753 // ELF has special kind of section called SHT_NOBITS which allows us to create
2754 // sections which exist but their data is omitted from the ELF file to save space.
2755 // This is what "strip --only-keep-debug" does when it creates separate ELF file
2756 // with only debug data. We use it in similar way to exclude .rodata and .text.
2757 bool no_bits = args_->only_keep_debug_;
2758 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08002759 } else {
2760 return DumpOat(nullptr,
2761 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002762 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002763 args_->os_) == EXIT_SUCCESS;
2764 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002765 }
2766
Igor Murashkin37743352014-11-13 14:38:00 -08002767 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2768 CHECK(args_ != nullptr);
2769
2770 if (args_->oat_filename_ != nullptr) {
2771 return DumpOat(runtime,
2772 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002773 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002774 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002775 }
Igor Murashkin37743352014-11-13 14:38:00 -08002776
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002777 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002778 }
2779
Igor Murashkin37743352014-11-13 14:38:00 -08002780 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2781};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002782
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002783} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002784
2785int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002786 art::OatdumpMain main;
2787 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002788}