blob: 64349b5bc3c0ba04b8448cff467a2e05463d4d9f [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",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070077 "kRefsOnlySaveMethod",
78 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070079};
80
81const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Mathieu Chartierac8f4392015-08-27 13:54:20 -070086// Map is so that we don't allocate multiple dex files for the same OatDexFile.
87static std::map<const OatFile::OatDexFile*,
88 std::unique_ptr<const DexFile>> opened_dex_files;
89
90const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
91 DCHECK(oat_dex_file != nullptr);
92 auto it = opened_dex_files.find(oat_dex_file);
93 if (it != opened_dex_files.end()) {
94 return it->second.get();
95 }
96 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
97 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
98 return ret;
99}
100
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800101template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100102class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700103 public:
David Srbecky2fdd03c2016-03-10 15:32:37 +0000104 OatSymbolizer(const OatFile* oat_file, const std::string& output_name, bool no_bits) :
105 oat_file_(oat_file),
106 builder_(nullptr),
107 output_name_(output_name.empty() ? "symbolized.oat" : output_name),
108 no_bits_(no_bits) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700109 }
110
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700111 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000112 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000113 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
114 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000115
116 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
117 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000118 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800119 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000120
121 builder_->Start();
122
123 auto* rodata = builder_->GetRoData();
124 auto* text = builder_->GetText();
125 auto* bss = builder_->GetBss();
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126
David Srbecky6d8c8f02015-10-26 10:57:09 +0000127 const uint8_t* rodata_begin = oat_file_->Begin();
128 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
David Srbecky2fdd03c2016-03-10 15:32:37 +0000129 if (no_bits_) {
130 rodata->WriteNoBitsSection(rodata_size);
131 } else {
132 rodata->Start();
133 rodata->WriteFully(rodata_begin, rodata_size);
134 rodata->End();
135 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000136
David Srbecky6d8c8f02015-10-26 10:57:09 +0000137 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
138 const size_t text_size = oat_file_->End() - text_begin;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000139 if (no_bits_) {
140 text->WriteNoBitsSection(text_size);
141 } else {
142 text->Start();
143 text->WriteFully(text_begin, text_size);
144 text->End();
145 }
David Srbecky6d8c8f02015-10-26 10:57:09 +0000146
147 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000148 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149 }
150
Douglas Leung316a2182015-09-17 15:26:25 -0700151 if (isa == kMips || isa == kMips64) {
152 builder_->WriteMIPSabiflagsSection();
153 }
Vladimir Marko944da602016-02-19 12:27:55 +0000154 builder_->PrepareDynamicSection(
155 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
156 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700157
David Srbecky5d950762016-03-07 20:47:29 +0000158 Walk();
159 for (const auto& trampoline : debug::MakeTrampolineInfos(oat_file_->GetOatHeader())) {
160 method_debug_infos_.push_back(trampoline);
161 }
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700162
David Srbecky5d950762016-03-07 20:47:29 +0000163 debug::WriteDebugInfo(builder_.get(),
164 ArrayRef<const debug::MethodDebugInfo>(method_debug_infos_),
165 dwarf::DW_DEBUG_FRAME_FORMAT,
166 true /* write_oat_patches */);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700167
David Srbecky6d8c8f02015-10-26 10:57:09 +0000168 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700169
Vladimir Marko10c13562015-11-25 14:33:36 +0000170 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700171 }
172
David Srbecky5d950762016-03-07 20:47:29 +0000173 void Walk() {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700174 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
175 for (size_t i = 0; i < oat_dex_files.size(); i++) {
176 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700177 CHECK(oat_dex_file != nullptr);
David Srbecky5d950762016-03-07 20:47:29 +0000178 WalkOatDexFile(oat_dex_file);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700179 }
180 }
181
David Srbecky5d950762016-03-07 20:47:29 +0000182 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700183 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700184 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
185 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700186 return;
187 }
188 for (size_t class_def_index = 0;
189 class_def_index < dex_file->NumClassDefs();
190 class_def_index++) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700191 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
192 OatClassType type = oat_class.GetType();
193 switch (type) {
194 case kOatClassAllCompiled:
195 case kOatClassSomeCompiled:
David Srbecky5d950762016-03-07 20:47:29 +0000196 WalkOatClass(oat_class, *dex_file, class_def_index);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700197 break;
198
199 case kOatClassNoneCompiled:
200 case kOatClassMax:
201 // Ignore.
202 break;
203 }
204 }
205 }
206
David Srbecky5d950762016-03-07 20:47:29 +0000207 void WalkOatClass(const OatFile::OatClass& oat_class,
208 const DexFile& dex_file,
209 uint32_t class_def_index) {
210 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700211 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700212 if (class_data == nullptr) { // empty class such as a marker interface?
213 return;
214 }
215 // Note: even if this is an interface or a native class, we still have to walk it, as there
216 // might be a static initializer.
217 ClassDataItemIterator it(dex_file, class_data);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700218 uint32_t class_method_idx = 0;
David Srbecky5d950762016-03-07 20:47:29 +0000219 for (; it.HasNextStaticField(); it.Next()) { /* skip */ }
220 for (; it.HasNextInstanceField(); it.Next()) { /* skip */ }
221 for (; it.HasNextDirectMethod() || it.HasNextVirtualMethod(); it.Next()) {
222 WalkOatMethod(oat_class.GetOatMethod(class_method_idx++),
223 dex_file,
224 class_def_index,
225 it.GetMemberIndex(),
226 it.GetMethodCodeItem(),
227 it.GetMethodAccessFlags());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700228 }
229 DCHECK(!it.HasNext());
230 }
231
David Srbecky5d950762016-03-07 20:47:29 +0000232 void WalkOatMethod(const OatFile::OatMethod& oat_method,
233 const DexFile& dex_file,
234 uint32_t class_def_index,
235 uint32_t dex_method_index,
236 const DexFile::CodeItem* code_item,
237 uint32_t method_access_flags) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238 if ((method_access_flags & kAccAbstract) != 0) {
239 // Abstract method, no code.
240 return;
241 }
David Srbecky5d950762016-03-07 20:47:29 +0000242 const OatHeader& oat_header = oat_file_->GetOatHeader();
243 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
244 if (method_header == nullptr || method_header->GetCodeSize() == 0) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700245 // No code.
246 return;
247 }
248
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100249 uint32_t entry_point = oat_method.GetCodeOffset() - oat_header.GetExecutableOffset();
250 // Clear Thumb2 bit.
251 const void* code_address = EntryPointToCodePointer(reinterpret_cast<void*>(entry_point));
252
David Srbecky5d950762016-03-07 20:47:29 +0000253 debug::MethodDebugInfo info = debug::MethodDebugInfo();
254 info.trampoline_name = nullptr;
255 info.dex_file = &dex_file;
256 info.class_def_index = class_def_index;
257 info.dex_method_index = dex_method_index;
258 info.access_flags = method_access_flags;
259 info.code_item = code_item;
260 info.isa = oat_header.GetInstructionSet();
261 info.deduped = !seen_offsets_.insert(oat_method.GetCodeOffset()).second;
262 info.is_native_debuggable = oat_header.IsNativeDebuggable();
263 info.is_optimized = method_header->IsOptimized();
264 info.is_code_address_text_relative = true;
David Srbeckya1b4c5f2016-03-31 18:17:59 +0100265 info.code_address = reinterpret_cast<uintptr_t>(code_address);
David Srbecky5d950762016-03-07 20:47:29 +0000266 info.code_size = method_header->GetCodeSize();
267 info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
268 info.code_info = info.is_optimized ? method_header->GetOptimizedCodeInfoPtr() : nullptr;
269 info.cfi = ArrayRef<uint8_t>();
270 method_debug_infos_.push_back(info);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700271 }
272
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700273 private:
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700274 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800275 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
David Srbecky5d950762016-03-07 20:47:29 +0000276 std::vector<debug::MethodDebugInfo> method_debug_infos_;
277 std::unordered_set<uint32_t> seen_offsets_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700278 const std::string output_name_;
David Srbecky2fdd03c2016-03-10 15:32:37 +0000279 bool no_bits_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700280};
281
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700282class OatDumperOptions {
283 public:
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100284 OatDumperOptions(bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100285 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700286 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700287 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800288 const char* class_filter,
289 const char* method_filter,
290 bool list_classes,
291 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000292 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800293 const char* export_dex_location,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800294 const char* app_image,
295 const char* app_oat,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800296 uint32_t addr2instr)
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100297 : dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100298 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700299 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700300 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800301 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000302 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800303 list_classes_(list_classes),
304 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000305 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800306 export_dex_location_(export_dex_location),
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800307 app_image_(app_image),
308 app_oat_(app_oat),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800309 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800310 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700311
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700312 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100313 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700314 const bool disassemble_code_;
315 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800316 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000317 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800318 const bool list_classes_;
319 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000320 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800321 const char* const export_dex_location_;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800322 const char* const app_image_;
323 const char* const app_oat_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700325 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700326};
327
Elliott Hughese3c845c2012-02-28 17:23:01 -0800328class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700329 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100330 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000331 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800332 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700333 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800334 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800335 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
336 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800337 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000338 oat_file.Begin(),
Aart Bikd3059e72016-05-11 10:30:47 -0700339 oat_file.End(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100340 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800341 CHECK(options_.class_loader_ != nullptr);
342 CHECK(options_.class_filter_ != nullptr);
343 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800344 AddAllOffsets();
345 }
346
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700347 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700348 delete disassembler_;
349 }
350
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800351 InstructionSet GetInstructionSet() {
352 return instruction_set_;
353 }
354
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700355 bool Dump(std::ostream& os) {
356 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800357 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700358
359 os << "MAGIC:\n";
360 os << oat_header.GetMagic() << "\n\n";
361
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800362 os << "LOCATION:\n";
363 os << oat_file_.GetLocation() << "\n\n";
364
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800366 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700367
Elliott Hughesa72ec822012-03-05 17:12:22 -0800368 os << "INSTRUCTION SET:\n";
369 os << oat_header.GetInstructionSet() << "\n\n";
370
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700371 {
372 std::unique_ptr<const InstructionSetFeatures> features(
373 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
374 oat_header.GetInstructionSetFeaturesBitmap()));
375 os << "INSTRUCTION SET FEATURES:\n";
376 os << features->GetFeatureString() << "\n\n";
377 }
Dave Allison70202782013-10-22 17:52:19 -0700378
Brian Carlstromaded5f72011-10-07 17:15:04 -0700379 os << "DEX FILE COUNT:\n";
380 os << oat_header.GetDexFileCount() << "\n\n";
381
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800382#define DUMP_OAT_HEADER_OFFSET(label, offset) \
383 os << label " OFFSET:\n"; \
384 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800385 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800386 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
387 } \
388 os << StringPrintf("\n\n");
389
390 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
391 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
392 GetInterpreterToInterpreterBridgeOffset);
393 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
394 GetInterpreterToCompiledCodeBridgeOffset);
395 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
396 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800397 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
398 GetQuickGenericJniTrampolineOffset);
399 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
400 GetQuickImtConflictTrampolineOffset);
401 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
402 GetQuickResolutionTrampolineOffset);
403 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
404 GetQuickToInterpreterBridgeOffset);
405#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700406
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700407 os << "IMAGE PATCH DELTA:\n";
408 os << StringPrintf("%d (0x%08x)\n\n",
409 oat_header.GetImagePatchDelta(),
410 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700411
Brian Carlstrom28db0122012-10-18 16:20:41 -0700412 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
413 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
414
415 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800416 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700417
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700418 // Print the key-value store.
419 {
420 os << "KEY VALUE STORE:\n";
421 size_t index = 0;
422 const char* key;
423 const char* value;
424 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
425 os << key << " = " << value << "\n";
426 index++;
427 }
428 os << "\n";
429 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700430
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800431 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700432 os << "BEGIN:\n";
433 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700434
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700435 os << "END:\n";
436 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
437 }
438
439 os << "SIZE:\n";
440 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700441
442 os << std::flush;
443
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800444 // If set, adjust relative address to be searched
445 if (options_.addr2instr_ != 0) {
446 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
447 os << "SEARCH ADDRESS (executable offset + input):\n";
448 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
449 }
450
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700451 // Dumping the dex file overview is compact enough to do even if header only.
452 DexFileData cumulative;
453 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
454 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
455 CHECK(oat_dex_file != nullptr);
456 std::string error_msg;
457 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
458 if (dex_file == nullptr) {
459 os << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation() << "': "
460 << error_msg;
461 continue;
462 }
463 DexFileData data(*dex_file);
464 os << "Dex file data for " << dex_file->GetLocation() << "\n";
465 data.Dump(os);
466 os << "\n";
467 cumulative.Add(data);
468 }
469 os << "Cumulative dex file data\n";
470 cumulative.Dump(os);
471 os << "\n";
472
David Brazdilc03d7b62016-03-02 12:18:03 +0000473 if (!options_.dump_header_only_) {
474 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
475 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
476 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800477
David Brazdilc03d7b62016-03-02 12:18:03 +0000478 // If file export selected skip file analysis
479 if (options_.export_dex_location_) {
480 if (!ExportDexFile(os, *oat_dex_file)) {
481 success = false;
482 }
483 } else {
484 if (!DumpOatDexFile(os, *oat_dex_file)) {
485 success = false;
486 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800487 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700488 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700489 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000490
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700491 os << std::flush;
492 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700493 }
494
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800495 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700496 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
497 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800498 return 0; // Address not in oat file
499 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800500 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
501 reinterpret_cast<uintptr_t>(oat_file_.Begin());
502 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800504 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 return end_offset - begin_offset;
506 }
507
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800508 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700509 return oat_file_.GetOatHeader().GetInstructionSet();
510 }
511
Mathieu Chartier90443472015-07-16 20:32:27 -0700512 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
514 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700515 CHECK(oat_dex_file != nullptr);
516 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700517 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
518 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700519 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
520 << "': " << error_msg;
521 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800522 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700523 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800524 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700525 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700526 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100527 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800528 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100529 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800530 }
531 }
532 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700533 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800534 }
535
Brian Carlstromaded5f72011-10-07 17:15:04 -0700536 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800538 // We don't know the length of the code for each method, but we need to know where to stop
539 // when disassembling. What we do know is that a region of code will be followed by some other
540 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
541 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
543 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700544 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700545 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700546 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
547 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700548 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
549 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800551 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800552 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800553 for (size_t class_def_index = 0;
554 class_def_index < dex_file->NumClassDefs();
555 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800556 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100557 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700558 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700559 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800560 ClassDataItemIterator it(*dex_file, class_data);
561 SkipAllFields(it);
562 uint32_t class_method_index = 0;
563 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100564 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800565 it.Next();
566 }
567 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100568 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 it.Next();
570 }
571 }
572 }
573 }
574
575 // If the last thing in the file is code for a method, there won't be an offset for the "next"
576 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
577 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800578 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800579 }
580
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700581 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
582 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
583 }
584
Elliott Hughese3c845c2012-02-28 17:23:01 -0800585 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800586 uint32_t code_offset = oat_method.GetCodeOffset();
587 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
588 code_offset &= ~0x1;
589 }
590 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800591 offsets_.insert(oat_method.GetVmapTableOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800592 }
593
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700594 // Dex file data, may be for multiple different dex files.
595 class DexFileData {
596 public:
597 DexFileData() {}
598
599 explicit DexFileData(const DexFile& dex_file)
600 : num_string_ids_(dex_file.NumStringIds()),
601 num_method_ids_(dex_file.NumMethodIds()),
602 num_field_ids_(dex_file.NumFieldIds()),
603 num_type_ids_(dex_file.NumTypeIds()),
604 num_class_defs_(dex_file.NumClassDefs()) {
605 for (size_t class_def_index = 0; class_def_index < num_class_defs_; ++class_def_index) {
606 const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_index);
607 WalkClass(dex_file, class_def);
608 }
609 }
610
611 void Add(const DexFileData& other) {
612 AddAll(unique_string_ids_from_code_, other.unique_string_ids_from_code_);
613 num_string_ids_from_code_ += other.num_string_ids_from_code_;
614 AddAll(dex_code_item_ptrs_, other.dex_code_item_ptrs_);
615 dex_code_bytes_ += other.dex_code_bytes_;
616 num_string_ids_ += other.num_string_ids_;
617 num_method_ids_ += other.num_method_ids_;
618 num_field_ids_ += other.num_field_ids_;
619 num_type_ids_ += other.num_type_ids_;
620 num_class_defs_ += other.num_class_defs_;
621 }
622
623 void Dump(std::ostream& os) {
624 os << "Num string ids: " << num_string_ids_ << "\n";
625 os << "Num method ids: " << num_method_ids_ << "\n";
626 os << "Num field ids: " << num_field_ids_ << "\n";
627 os << "Num type ids: " << num_type_ids_ << "\n";
628 os << "Num class defs: " << num_class_defs_ << "\n";
629 os << "Unique strings loaded from dex code: " << unique_string_ids_from_code_.size() << "\n";
630 os << "Total strings loaded from dex code: " << num_string_ids_from_code_ << "\n";
631 os << "Number of unique dex code items: " << dex_code_item_ptrs_.size() << "\n";
632 os << "Total number of dex code bytes: " << dex_code_bytes_ << "\n";
633 }
634
635 private:
636 void WalkClass(const DexFile& dex_file, const DexFile::ClassDef& class_def) {
637 const uint8_t* class_data = dex_file.GetClassData(class_def);
638 if (class_data == nullptr) { // empty class such as a marker interface?
639 return;
640 }
641 ClassDataItemIterator it(dex_file, class_data);
642 SkipAllFields(it);
643 while (it.HasNextDirectMethod()) {
644 WalkCodeItem(dex_file, it.GetMethodCodeItem());
645 it.Next();
646 }
647 while (it.HasNextVirtualMethod()) {
648 WalkCodeItem(dex_file, it.GetMethodCodeItem());
649 it.Next();
650 }
651 DCHECK(!it.HasNext());
652 }
653
654 void WalkCodeItem(const DexFile& dex_file, const DexFile::CodeItem* code_item) {
655 if (code_item == nullptr) {
656 return;
657 }
658 const size_t code_item_size = code_item->insns_size_in_code_units_;
659 const uint16_t* code_ptr = code_item->insns_;
660 const uint16_t* code_end = code_item->insns_ + code_item_size;
661
662 // If we inserted a new dex code item pointer, add to total code bytes.
663 if (dex_code_item_ptrs_.insert(code_ptr).second) {
664 dex_code_bytes_ += code_item_size * sizeof(code_ptr[0]);
665 }
666
667 while (code_ptr < code_end) {
668 const Instruction* inst = Instruction::At(code_ptr);
669 switch (inst->Opcode()) {
670 case Instruction::CONST_STRING: {
671 const uint32_t string_index = inst->VRegB_21c();
672 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
673 ++num_string_ids_from_code_;
674 break;
675 }
676 case Instruction::CONST_STRING_JUMBO: {
677 const uint32_t string_index = inst->VRegB_31c();
678 unique_string_ids_from_code_.insert(StringReference(&dex_file, string_index));
679 ++num_string_ids_from_code_;
680 break;
681 }
682 default:
683 break;
684 }
685
686 code_ptr += inst->SizeInCodeUnits();
687 }
688 }
689
690 // Unique string ids loaded from dex code.
691 std::set<StringReference, StringReferenceComparator> unique_string_ids_from_code_;
692
693 // Total string ids loaded from dex code.
694 size_t num_string_ids_from_code_ = 0;
695
696 // Unique code pointers.
697 std::set<const void*> dex_code_item_ptrs_;
698
699 // Total "unique" dex code bytes.
700 size_t dex_code_bytes_ = 0;
701
702 // Other dex ids.
703 size_t num_string_ids_ = 0;
704 size_t num_method_ids_ = 0;
705 size_t num_field_ids_ = 0;
706 size_t num_type_ids_ = 0;
707 size_t num_class_defs_ = 0;
708 };
709
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700710 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
711 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800712 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700713 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800714 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800715 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700716
Andreas Gampe2ba88952016-04-29 17:52:07 -0700717 // Print embedded dex file data range.
718 const uint8_t* const oat_file_begin = oat_dex_file.GetOatFile()->Begin();
719 const uint8_t* const dex_file_pointer = oat_dex_file.GetDexFilePointer();
720 uint32_t dex_offset = dchecked_integral_cast<uint32_t>(dex_file_pointer - oat_file_begin);
721 os << StringPrintf("dex-file: 0x%08x..0x%08x\n",
722 dex_offset,
723 dchecked_integral_cast<uint32_t>(dex_offset + oat_dex_file.FileSize() - 1));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700724
Andreas Gampe2ba88952016-04-29 17:52:07 -0700725 // Create the dex file early. A lot of print-out things depend on it.
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700726 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700727 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
728 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700729 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700730 os << std::flush;
731 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700732 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100733
Andreas Gampe2ba88952016-04-29 17:52:07 -0700734 // Print lookup table, if it exists.
735 if (oat_dex_file.GetLookupTableData() != nullptr) {
736 uint32_t table_offset = dchecked_integral_cast<uint32_t>(
737 oat_dex_file.GetLookupTableData() - oat_file_begin);
738 uint32_t table_size = TypeLookupTable::RawDataLength(*dex_file);
739 os << StringPrintf("type-table: 0x%08x..0x%08x\n",
740 table_offset,
741 table_offset + table_size - 1);
742 }
743
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100744 VariableIndentationOutputStream vios(&os);
745 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800746 for (size_t class_def_index = 0;
747 class_def_index < dex_file->NumClassDefs();
748 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700749 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
750 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800751
752 // TODO: Support regex
753 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
754 continue;
755 }
756
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700757 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100758 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700759 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
760 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100761 << " (" << oat_class.GetStatus() << ")"
762 << " (" << oat_class.GetType() << ")\n";
763 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700764 if (options_.list_classes_) {
765 continue;
766 }
767 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700768 success = false;
769 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800770 if (stop_analysis) {
771 os << std::flush;
772 return success;
773 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700774 }
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700775 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700776 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700777 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700778 }
779
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800780 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
781 std::string error_msg;
782 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
783
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700784 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800785 if (dex_file == nullptr) {
786 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
787 return false;
788 }
789 size_t fsize = oat_dex_file.FileSize();
790
791 // Some quick checks just in case
792 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
793 os << "Invalid dex file\n";
794 return false;
795 }
796
797 // Verify output directory exists
798 if (!OS::DirectoryExists(options_.export_dex_location_)) {
799 // TODO: Extend OS::DirectoryExists if symlink support is required
800 os << options_.export_dex_location_ << " output directory not found or symlink\n";
801 return false;
802 }
803
804 // Beautify path names
805 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
806 return false;
807 }
808
809 std::string dex_orig_name;
810 size_t dex_orig_pos = dex_file_location.rfind('/');
811 if (dex_orig_pos == std::string::npos)
812 dex_orig_name = dex_file_location;
813 else
814 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
815
816 // A more elegant approach to efficiently name user installed apps is welcome
817 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
818 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
819 size_t apk_orig_pos = dex_file_location.rfind('/');
820 if (apk_orig_pos != std::string::npos) {
821 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
822 }
823 }
824
825 std::string out_dex_path(options_.export_dex_location_);
826 if (out_dex_path.back() != '/') {
827 out_dex_path.append("/");
828 }
829 out_dex_path.append(dex_orig_name);
830 out_dex_path.append("_export.dex");
831 if (out_dex_path.length() > PATH_MAX) {
832 return false;
833 }
834
835 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
836 if (file.get() == nullptr) {
837 os << "Failed to open output dex file " << out_dex_path;
838 return false;
839 }
840
841 if (!file->WriteFully(dex_file->Begin(), fsize)) {
842 os << "Failed to write dex file";
843 file->Erase();
844 return false;
845 }
846
847 if (file->FlushCloseOrErase() != 0) {
848 os << "Flush and close failed";
849 return false;
850 }
851
852 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
853 os << std::flush;
854
855 return true;
856 }
857
Elliott Hughese3c845c2012-02-28 17:23:01 -0800858 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700859 while (it.HasNextStaticField()) {
860 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700861 }
Ian Rogers0571d352011-11-03 19:51:38 -0700862 while (it.HasNextInstanceField()) {
863 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700864 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800865 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700866
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100867 bool DumpOatClass(VariableIndentationOutputStream* vios,
868 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700869 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700870 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800871 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700872 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700873 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100874 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700875 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800876 }
877 ClassDataItemIterator it(dex_file, class_data);
878 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700879 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700880 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700882 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700883 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700884 success = false;
885 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800886 if (addr_found) {
887 *stop_analysis = true;
888 return success;
889 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700890 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700891 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700892 }
Ian Rogers0571d352011-11-03 19:51:38 -0700893 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100894 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 it.GetMemberIndex(), it.GetMethodCodeItem(),
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700896 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700897 success = false;
898 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800899 if (addr_found) {
900 *stop_analysis = true;
901 return success;
902 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700904 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700905 }
Ian Rogers0571d352011-11-03 19:51:38 -0700906 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100907 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700909 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800910
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700911 static constexpr uint32_t kPrologueBytes = 16;
912
913 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
914 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
915
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100916 bool DumpOatMethod(VariableIndentationOutputStream* vios,
917 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700918 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800920 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Mathieu Chartierdc00f182016-07-14 10:10:44 -0700921 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700922 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800923
924 // TODO: Support regex
925 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
926 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000927 return success;
928 }
929
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800930 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100931 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
932 class_method_index, pretty_method.c_str(),
933 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800934 if (options_.list_methods_) return success;
935
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800936 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
937 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
938 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
939 uint32_t code_offset = oat_method.GetCodeOffset();
940 uint32_t code_size = oat_method.GetQuickCodeSize();
941 if (resolved_addr2instr_ != 0) {
942 if (resolved_addr2instr_ > code_offset + code_size) {
943 return success;
944 } else {
945 *addr_found = true; // stop analyzing file at next iteration
946 }
947 }
948
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100949 // Everything below is indented at least once.
950 ScopedIndentation indent1(vios);
951
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800952 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100953 vios->Stream() << "DEX CODE:\n";
954 ScopedIndentation indent2(vios);
955 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700956 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700957
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700958 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700959 std::unique_ptr<verifier::MethodVerifier> verifier;
960 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700961 // We need to have the handle scope stay live until after the verifier since the verifier has
962 // a handle to the dex cache from hs.
963 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100964 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
965 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700966 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100967 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700968 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700969 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800970 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100971 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800972 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100973 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100974 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100975 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700976 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100977 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700978 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
979 oat_method_offsets_offset, oat_file_.Size());
980 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100981 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700982 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800983 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700984
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100985 ScopedIndentation indent2(vios);
986 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
988 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100989 vios->Stream() << StringPrintf("WARNING: "
990 "code offset 0x%08x is past end of file 0x%08zx.\n",
991 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700992 success = false;
993 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100994 vios->Stream() << "\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800995 }
996 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100997 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700998 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
999 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001000
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001001 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001002 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001003 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001004 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001005 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001006 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001007 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
1008 method_header_offset, oat_file_.Size());
1009 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001010 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001011 return false;
1012 }
1013
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001014 ScopedIndentation indent2(vios);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001015 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001016 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001017 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001018 }
1019 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001020 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001021 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 vios->Stream() << StringPrintf("WARNING: "
1023 "vmap table offset 0x%08x is past end of file 0x%08zx. "
1024 "vmap table offset was loaded from offset 0x%08x.\n",
1025 vmap_table_offset, oat_file_.Size(),
1026 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001027 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001028 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001029 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001030 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001031 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001032 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001033 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001034
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001035 ScopedIndentation indent2(vios);
1036 vios->Stream()
1037 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1038 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1039 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1040 vios->Stream() << "\n";
1041 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1042 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1043 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001044 }
1045 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001046 // Based on spill masks from QuickMethodFrameInfo so placed
1047 // after it is dumped, but useful for understanding quick
1048 // code, so dumped here.
1049 ScopedIndentation indent2(vios);
1050 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001051 }
1052 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001053 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001054 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1055 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001056 ScopedIndentation indent2(vios);
1057 vios->Stream() << StringPrintf("WARNING: "
1058 "code size offset 0x%08x is past end of file 0x%08zx.",
1059 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001060 success = false;
1061 } else {
1062 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001063 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1064 uint64_t aligned_code_end = aligned_code_begin + code_size;
1065
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001066 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001067 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001068 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001069 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1070 code_offset,
1071 code_size_offset,
1072 code_size,
1073 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001074
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001075 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001076 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001077 vios->Stream() << StringPrintf("WARNING: "
1078 "start of code at 0x%08x is past end of file 0x%08zx.",
1079 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001080 success = false;
1081 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001082 vios->Stream() << StringPrintf(
1083 "WARNING: "
1084 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1085 "code size is 0x%08x loaded from offset 0x%08x.\n",
1086 aligned_code_end, oat_file_.Size(),
1087 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001088 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001089 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001090 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001091 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001092 }
1093 }
1094 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001095 vios->Stream() << StringPrintf(
1096 "WARNING: "
1097 "code size %d is bigger than max expected threshold of %d. "
1098 "code size is 0x%08x loaded from offset 0x%08x.\n",
1099 code_size, kMaxCodeSize,
1100 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001101 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001102 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001103 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001104 DumpCode(vios, oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001105 }
1106 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001107 } else if (options_.disassemble_code_) {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001108 DumpCode(vios, oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001109 }
1110 }
1111 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001112 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001113 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001114 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001115
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001116 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1117 if (spill_mask == 0) {
1118 return;
1119 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001120 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001121 for (size_t i = 0; i < 32; i++) {
1122 if ((spill_mask & (1 << i)) != 0) {
1123 if (is_float) {
1124 os << "fr" << i;
1125 } else {
1126 os << "r" << i;
1127 }
1128 spill_mask ^= 1 << i; // clear bit
1129 if (spill_mask != 0) {
1130 os << ", ";
1131 } else {
1132 break;
1133 }
1134 }
1135 }
1136 os << ")";
1137 }
1138
Roland Levillain442b46a2015-02-18 16:54:21 +00001139 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001140 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001141 const OatFile::OatMethod& oat_method,
1142 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001143 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1144 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001145 const void* raw_code_info = oat_method.GetVmapTable();
1146 if (raw_code_info != nullptr) {
1147 CodeInfo code_info(raw_code_info);
1148 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001149 ScopedIndentation indent1(vios);
1150 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001151 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001152 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1153 // We don't encode the size in the table, so just emit that we have quickened
1154 // information.
1155 ScopedIndentation indent(vios);
1156 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001157 } else {
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001158 // Otherwise, there is nothing to display.
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001159 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001160 }
1161
1162 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001163 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001164 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001165 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001166 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001167 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001168 oat_method.GetCodeOffset(),
1169 code_item.registers_size_,
1170 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001171 }
1172
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001173 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1174 const DexFile::CodeItem* code_item) {
1175 if (code_item != nullptr) {
1176 size_t num_locals_ins = code_item->registers_size_;
1177 size_t num_ins = code_item->ins_size_;
1178 size_t num_locals = num_locals_ins - num_ins;
1179 size_t num_outs = code_item->outs_size_;
1180
1181 os << "vr_stack_locations:";
1182 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1183 // For readability, delimit the different kinds of VRs.
1184 if (reg == num_locals_ins) {
1185 os << "\n\tmethod*:";
1186 } else if (reg == num_locals && num_ins > 0) {
1187 os << "\n\tins:";
1188 } else if (reg == 0 && num_locals > 0) {
1189 os << "\n\tlocals:";
1190 }
1191
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001192 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1193 code_item,
1194 oat_method.GetCoreSpillMask(),
1195 oat_method.GetFpSpillMask(),
1196 oat_method.GetFrameSizeInBytes(),
1197 reg,
1198 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001199 os << " v" << reg << "[sp + #" << offset << "]";
1200 }
1201
1202 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1203 if (out_reg == 0) {
1204 os << "\n\touts:";
1205 }
1206
1207 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1208 os << " v" << out_reg << "[sp + #" << offset << "]";
1209 }
1210
1211 os << "\n";
1212 }
1213 }
1214
Ian Rogersb23a7722012-10-09 16:54:26 -07001215 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001216 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001217 size_t i = 0;
1218 while (i < code_item->insns_size_in_code_units_) {
1219 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001220 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1221 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001222 i += instruction->SizeInCodeUnits();
1223 }
1224 }
1225 }
1226
Roland Levillainf2650d12015-05-28 14:53:28 +01001227 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1228 // the optimizing compiler?
1229 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1230 const DexFile::CodeItem* code_item) {
1231 // If the native GC map is null and the Dex `code_item` is not
1232 // null, then this method has been compiled with the optimizing
1233 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001234 return oat_method.GetQuickCode() != nullptr &&
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01001235 oat_method.GetVmapTable() != nullptr &&
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001236 code_item != nullptr;
1237 }
1238
1239 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1240 // the dextodex compiler?
1241 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1242 const DexFile::CodeItem* code_item) {
1243 // If the quick code is null, the Dex `code_item` is not
1244 // null, and the vmap table is not null, then this method has been compiled
1245 // with the dextodex compiler.
1246 return oat_method.GetQuickCode() == nullptr &&
1247 oat_method.GetVmapTable() != nullptr &&
1248 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001249 }
1250
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001251 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001252 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001253 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001254 const DexFile* dex_file,
1255 const DexFile::ClassDef& class_def,
1256 const DexFile::CodeItem* code_item,
1257 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001258 if ((method_access_flags & kAccNative) == 0) {
1259 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001260 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001261 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierf284d442016-06-02 11:48:30 -07001262 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file, nullptr)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001263 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001264 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001265 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1266 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001267 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001268
1269 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001270 }
1271
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001272 // The StackMapsHelper provides the stack maps in the native PC order.
1273 // For identical native PCs, the order from the CodeInfo is preserved.
1274 class StackMapsHelper {
1275 public:
1276 explicit StackMapsHelper(const uint8_t* raw_code_info)
1277 : code_info_(raw_code_info),
1278 encoding_(code_info_.ExtractEncoding()),
1279 number_of_stack_maps_(code_info_.GetNumberOfStackMaps(encoding_)),
1280 indexes_(),
1281 offset_(static_cast<size_t>(-1)),
1282 stack_map_index_(0u) {
1283 if (number_of_stack_maps_ != 0u) {
1284 // Check if native PCs are ordered.
1285 bool ordered = true;
1286 StackMap last = code_info_.GetStackMapAt(0u, encoding_);
1287 for (size_t i = 1; i != number_of_stack_maps_; ++i) {
1288 StackMap current = code_info_.GetStackMapAt(i, encoding_);
1289 if (last.GetNativePcOffset(encoding_.stack_map_encoding) >
1290 current.GetNativePcOffset(encoding_.stack_map_encoding)) {
1291 ordered = false;
1292 break;
1293 }
1294 last = current;
1295 }
1296 if (!ordered) {
1297 // Create indirection indexes for access in native PC order. We do not optimize
1298 // for the fact that there can currently be only two separately ordered ranges,
1299 // namely normal stack maps and catch-point stack maps.
1300 indexes_.resize(number_of_stack_maps_);
1301 std::iota(indexes_.begin(), indexes_.end(), 0u);
1302 std::sort(indexes_.begin(),
1303 indexes_.end(),
1304 [this](size_t lhs, size_t rhs) {
1305 StackMap left = code_info_.GetStackMapAt(lhs, encoding_);
1306 uint32_t left_pc = left.GetNativePcOffset(encoding_.stack_map_encoding);
1307 StackMap right = code_info_.GetStackMapAt(rhs, encoding_);
1308 uint32_t right_pc = right.GetNativePcOffset(encoding_.stack_map_encoding);
1309 // If the PCs are the same, compare indexes to preserve the original order.
1310 return (left_pc < right_pc) || (left_pc == right_pc && lhs < rhs);
1311 });
1312 }
1313 offset_ = GetStackMapAt(0).GetNativePcOffset(encoding_.stack_map_encoding);
1314 }
1315 }
1316
1317 const CodeInfo& GetCodeInfo() const {
1318 return code_info_;
1319 }
1320
1321 const CodeInfoEncoding& GetEncoding() const {
1322 return encoding_;
1323 }
1324
1325 size_t GetOffset() const {
1326 return offset_;
1327 }
1328
1329 StackMap GetStackMap() const {
1330 return GetStackMapAt(stack_map_index_);
1331 }
1332
1333 void Next() {
1334 ++stack_map_index_;
1335 offset_ = (stack_map_index_ == number_of_stack_maps_)
1336 ? static_cast<size_t>(-1)
1337 : GetStackMapAt(stack_map_index_).GetNativePcOffset(encoding_.stack_map_encoding);
1338 }
1339
1340 private:
1341 StackMap GetStackMapAt(size_t i) const {
1342 if (!indexes_.empty()) {
1343 i = indexes_[i];
1344 }
1345 DCHECK_LT(i, number_of_stack_maps_);
1346 return code_info_.GetStackMapAt(i, encoding_);
1347 }
1348
1349 const CodeInfo code_info_;
1350 const CodeInfoEncoding encoding_;
1351 const size_t number_of_stack_maps_;
1352 dchecked_vector<size_t> indexes_; // Used if stack map native PCs are not ordered.
1353 size_t offset_;
1354 size_t stack_map_index_;
1355 };
1356
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001357 void DumpCode(VariableIndentationOutputStream* vios,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001358 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1359 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001360 const void* quick_code = oat_method.GetQuickCode();
1361
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001362 if (code_size == 0) {
1363 code_size = oat_method.GetQuickCodeSize();
1364 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001365 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001366 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001367 return;
Vladimir Markoadbf93e2016-04-08 11:18:25 +01001368 } else if (!bad_input && IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1369 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1370 StackMapsHelper helper(oat_method.GetVmapTable());
1371 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1372 size_t offset = 0;
1373 while (offset < code_size) {
1374 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
1375 if (offset == helper.GetOffset()) {
1376 ScopedIndentation indent1(vios);
1377 StackMap stack_map = helper.GetStackMap();
1378 DCHECK(stack_map.IsValid());
1379 stack_map.Dump(vios,
1380 helper.GetCodeInfo(),
1381 helper.GetEncoding(),
1382 oat_method.GetCodeOffset(),
1383 code_item->registers_size_);
1384 do {
1385 helper.Next();
1386 // There may be multiple stack maps at a given PC. We display only the first one.
1387 } while (offset == helper.GetOffset());
1388 }
1389 DCHECK_LT(offset, helper.GetOffset());
1390 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001391 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001392 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1393 size_t offset = 0;
1394 while (offset < code_size) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001395 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001396 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001397 }
1398 }
1399
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001400 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001401 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001402 const OatDumperOptions& options_;
1403 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001404 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001405 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001406 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001407};
1408
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001409class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001410 public:
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001411 ImageDumper(std::ostream* os,
1412 gc::space::ImageSpace& image_space,
1413 const ImageHeader& image_header,
1414 OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001415 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001416 vios_(os),
1417 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001418 image_space_(image_space),
1419 image_header_(image_header),
1420 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001421
Mathieu Chartier90443472015-07-16 20:32:27 -07001422 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001423 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001424 std::ostream& indent_os = vios_.Stream();
1425
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001426 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001427
Jeff Haodcdc85b2015-12-04 14:06:18 -08001428 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1429
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001431
Mathieu Chartiere401d142015-04-22 13:56:20 -07001432 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1433
1434 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1435 auto section = static_cast<ImageHeader::ImageSections>(i);
1436 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1437 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001438
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001439 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001440
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001441 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001442
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001443 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1444
1445 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1446
1447 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001448
Alex Lighta59dd802014-07-02 16:28:08 -07001449 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1450
Igor Murashkin46774762014-10-22 11:37:02 -07001451 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1452
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001453 {
1454 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001455 static_assert(arraysize(image_roots_descriptions_) ==
1456 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001457 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1458 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1459 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001460 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001461 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001462 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001463 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001464 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001465 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001466 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1467 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001468 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001469 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1470 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001471 run++;
1472 } else {
1473 break;
1474 }
1475 }
1476 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001477 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001478 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001479 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001480 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001481 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001482 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001483 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001484 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001485 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001487 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001488 }
1489 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001490 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001491
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001492 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001493 os << "METHOD ROOTS\n";
1494 static_assert(arraysize(image_methods_descriptions_) ==
1495 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1496 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1497 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1498 const char* description = image_methods_descriptions_[i];
1499 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001500 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001501 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001502 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001503 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001504
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001505 Runtime* const runtime = Runtime::Current();
1506 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001507 std::string image_filename = image_space_.GetImageFilename();
1508 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001509 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001510 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001511 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001512 const OatFile* oat_file = image_space_.GetOatFile();
Alex Lighta59dd802014-07-02 16:28:08 -07001513 if (oat_file == nullptr) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001514 oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(oat_location);
1515 }
1516 if (oat_file == nullptr) {
1517 oat_file = OatFile::Open(oat_location,
1518 oat_location,
1519 nullptr,
1520 nullptr,
1521 false,
1522 /*low_4gb*/false,
1523 nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001524 &error_msg);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001525 }
1526 if (oat_file == nullptr) {
1527 os << "OAT FILE NOT FOUND: " << error_msg << "\n";
1528 return EXIT_FAILURE;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001529 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001531
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001532 stats_.oat_file_bytes = oat_file->Size();
1533
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001534 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001535
Mathieu Chartier02e25112013-08-14 16:14:24 -07001536 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001537 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001538 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1539 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001540 }
1541
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001542 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001543
Jeff Haodcdc85b2015-12-04 14:06:18 -08001544 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001545 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001546 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001547 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001548 {
1549 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1550 heap->FlushAllocStack();
1551 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001552 // Since FlushAllocStack() above resets the (active) allocation
1553 // stack. Need to revoke the thread-local allocation stacks that
1554 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001555 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001556 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001557 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001558 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001559 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001560 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001561 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001562 {
1563 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001564 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001565 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001566 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001567 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001568 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001569 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001570 }
1571 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001573 // Dump the normal objects before ArtMethods.
1574 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1575 indent_os << "\n";
1576 // TODO: Dump fields.
1577 // Dump methods after.
Jeff Haodcdc85b2015-12-04 14:06:18 -08001578 DumpArtMethodVisitor visitor(this);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001579 image_header_.VisitPackedArtMethods(&visitor,
1580 image_space_.Begin(),
1581 image_header_.GetPointerSize());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001582 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001583 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001584 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001585 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001586 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001587 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001588 size_t data_size = image_header_.GetDataSize(); // stored size in file.
1589 if (file == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001590 LOG(WARNING) << "Failed to find image in " << image_filename;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001591 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001592 stats_.file_bytes = file->GetLength();
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001593 // If the image is compressed, adjust to decompressed size.
1594 size_t uncompressed_size = image_header_.GetImageSize() - sizeof(ImageHeader);
1595 if (image_header_.GetStorageMode() == ImageHeader::kStorageModeUncompressed) {
1596 DCHECK_EQ(uncompressed_size, data_size) << "Sizes should match for uncompressed image";
1597 }
1598 stats_.file_bytes += uncompressed_size - data_size;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001599 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001600 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001601 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001602 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1603 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001604 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1605 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001606 const auto& intern_section = image_header_.GetImageSection(
1607 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001608 const auto& class_table_section = image_header_.GetImageSection(
1609 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001610 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1611
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001612 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001613
1614 // Objects are kObjectAlignment-aligned.
1615 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1616 if (object_section.Offset() > header_bytes) {
1617 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1618 }
1619
1620 // Field section is 4-byte aligned.
1621 constexpr size_t kFieldSectionAlignment = 4U;
1622 uint32_t end_objects = object_section.Offset() + object_section.Size();
1623 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1624 stats_.alignment_bytes += field_section.Offset() - end_objects;
1625
1626 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1627 uint32_t end_fields = field_section.Offset() + field_section.Size();
1628 CHECK_ALIGNED(method_section.Offset(), 4);
1629 stats_.alignment_bytes += method_section.Offset() - end_fields;
1630
1631 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1632 uint32_t end_methods = method_section.Offset() + method_section.Size();
1633 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1634 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1635
1636 // Intern table is 8-byte aligned.
1637 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1638 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1639 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1640
1641 // Add space between intern table and class table.
1642 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1643 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1644
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001645 // Add space between end of image data and bitmap. Expect the bitmap to be page-aligned.
1646 const size_t bitmap_offset = sizeof(ImageHeader) + data_size;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001647 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001648 stats_.alignment_bytes += RoundUp(bitmap_offset, kPageSize) - bitmap_offset;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001649
Mathieu Chartiere401d142015-04-22 13:56:20 -07001650 stats_.bitmap_bytes += bitmap_section.Size();
1651 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001652 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001653 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001654 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001655 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001656 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001657 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001658
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001659 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001660
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001661 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001662 }
1663
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001664 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001665 class DumpArtMethodVisitor : public ArtMethodVisitor {
1666 public:
1667 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1668
1669 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1670 std::ostream& indent_os = image_dumper_->vios_.Stream();
1671 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001672 image_dumper_->DumpMethod(method, indent_os);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001673 indent_os << "\n";
1674 }
1675
1676 private:
1677 ImageDumper* const image_dumper_;
1678 };
1679
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001680 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001681 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001682 CHECK(type != nullptr);
1683 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001684 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001685 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001686 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001688 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001689 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001690 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001691 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001692 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001693 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001694 }
1695 }
1696
Mathieu Chartierc7853442015-03-27 14:35:38 -07001697 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001698 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001699 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001700 switch (field->GetTypeAsPrimitiveType()) {
1701 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001702 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001703 break;
1704 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001705 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001706 break;
1707 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001708 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001709 break;
1710 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001711 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001712 break;
1713 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001714 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001715 break;
1716 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001717 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001718 break;
1719 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001720 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1721 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001722 break;
1723 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001724 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001725 break;
1726 case Primitive::kPrimNot: {
1727 // Get the value, don't compute the type unless it is non-null as we don't want
1728 // to cause class loading.
1729 mirror::Object* value = field->GetObj(obj);
1730 if (value == nullptr) {
1731 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001732 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001733 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001734 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001735 if (field_type != nullptr) {
1736 PrettyObjectValue(os, field_type, value);
1737 } else {
1738 os << StringPrintf("%p %s\n", value,
1739 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1740 }
Ian Rogers50239c72013-06-17 14:53:22 -07001741 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001742 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001743 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001744 default:
1745 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1746 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001747 }
1748 }
1749
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001750 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001751 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001752 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001753 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001754 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001755 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001756 for (ArtField& field : klass->GetIFields()) {
1757 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001758 }
1759 }
1760
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001761 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001762 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001763 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001764
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001765 const void* GetQuickOatCodeBegin(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001766 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001767 image_header_.GetPointerSize());
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001768 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001769 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001770 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001771 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001772 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001773 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001774 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001775 }
1776
Mathieu Chartiere401d142015-04-22 13:56:20 -07001777 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001778 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001779 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1780 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001781 return 0;
1782 }
1783 return oat_code_begin[-1];
1784 }
1785
Mathieu Chartiere401d142015-04-22 13:56:20 -07001786 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001787 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001788 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001789 if (oat_code_begin == nullptr) {
1790 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001791 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001792 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001793 }
1794
Mathieu Chartier90443472015-07-16 20:32:27 -07001795 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001796 DCHECK(obj != nullptr);
1797 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001798 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001799 if (!state->InDumpSpace(obj)) {
1800 return;
1801 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001802
1803 size_t object_bytes = obj->SizeOf();
1804 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1805 state->stats_.object_bytes += object_bytes;
1806 state->stats_.alignment_bytes += alignment_bytes;
1807
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001808 std::ostream& os = state->vios_.Stream();
1809
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001810 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001811 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001812 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1813 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001814 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001815 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001816 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1817 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001818 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001819 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001820 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001821 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001823 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001824 ScopedIndentation indent1(&state->vios_);
1825 DumpFields(os, obj, obj_class);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001826 const size_t image_pointer_size = state->image_header_.GetPointerSize();
Ian Rogersd5b32602012-02-26 16:40:04 -08001827 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001828 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1829 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001830 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001831 size_t run = 0;
1832 for (int32_t j = i + 1; j < length; j++) {
1833 if (value == obj_array->Get(j)) {
1834 run++;
1835 } else {
1836 break;
1837 }
1838 }
1839 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001840 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001841 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001842 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001843 i = i + run;
1844 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001845 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001846 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001847 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001848 }
1849 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001850 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001851 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001852 os << "STATICS:\n";
1853 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001854 for (ArtField& field : klass->GetSFields()) {
1855 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001856 }
1857 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001858 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001859 auto it = state->dex_caches_.find(obj);
1860 if (it != state->dex_caches_.end()) {
1861 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001862 const auto& field_section = state->image_header_.GetImageSection(
1863 ImageHeader::kSectionArtFields);
1864 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001865 size_t num_methods = dex_cache->NumResolvedMethods();
1866 if (num_methods != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001867 os << "Methods (size=" << num_methods << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001868 ScopedIndentation indent2(&state->vios_);
1869 auto* resolved_methods = dex_cache->GetResolvedMethods();
1870 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001871 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods,
1872 i,
1873 image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001874 size_t run = 0;
1875 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001876 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1877 j,
1878 image_pointer_size);
1879 ++j) {
1880 ++run;
1881 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001882 if (run == 0) {
1883 os << StringPrintf("%zd: ", i);
1884 } else {
1885 os << StringPrintf("%zd to %zd: ", i, i + run);
1886 i = i + run;
1887 }
1888 std::string msg;
1889 if (elem == nullptr) {
1890 msg = "null";
1891 } else if (method_section.Contains(
1892 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1893 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1894 } else {
1895 msg = "<not in method section>";
1896 }
1897 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001898 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001899 }
1900 size_t num_fields = dex_cache->NumResolvedFields();
1901 if (num_fields != 0u) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001902 os << "Fields (size=" << num_fields << "):\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01001903 ScopedIndentation indent2(&state->vios_);
1904 auto* resolved_fields = dex_cache->GetResolvedFields();
1905 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001906 auto* elem = mirror::DexCache::GetElementPtrSize(
1907 resolved_fields, i, image_pointer_size);
Vladimir Marko05792b92015-08-03 11:56:49 +01001908 size_t run = 0;
1909 for (size_t j = i + 1;
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001910 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1911 j,
1912 image_pointer_size);
1913 ++j) {
1914 ++run;
1915 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001916 if (run == 0) {
1917 os << StringPrintf("%zd: ", i);
1918 } else {
1919 os << StringPrintf("%zd to %zd: ", i, i + run);
1920 i = i + run;
1921 }
1922 std::string msg;
1923 if (elem == nullptr) {
1924 msg = "null";
1925 } else if (field_section.Contains(
1926 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1927 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1928 } else {
1929 msg = "<not in field section>";
1930 }
1931 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001932 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001933 }
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +01001934 size_t num_types = dex_cache->NumResolvedTypes();
1935 if (num_types != 0u) {
1936 os << "Types (size=" << num_types << "):\n";
1937 ScopedIndentation indent2(&state->vios_);
1938 auto* resolved_types = dex_cache->GetResolvedTypes();
1939 for (size_t i = 0; i < num_types; ++i) {
1940 auto* elem = resolved_types[i].Read();
1941 size_t run = 0;
1942 for (size_t j = i + 1; j != num_types && elem == resolved_types[j].Read(); ++j) {
1943 ++run;
1944 }
1945 if (run == 0) {
1946 os << StringPrintf("%zd: ", i);
1947 } else {
1948 os << StringPrintf("%zd to %zd: ", i, i + run);
1949 i = i + run;
1950 }
1951 std::string msg;
1952 if (elem == nullptr) {
1953 msg = "null";
1954 } else {
1955 msg = PrettyClass(elem);
1956 }
1957 os << StringPrintf("%p %s\n", elem, msg.c_str());
1958 }
1959 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001960 }
1961 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001962 std::string temp;
1963 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001964 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001965
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001966 void DumpMethod(ArtMethod* method, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001967 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001968 DCHECK(method != nullptr);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001969 const void* quick_oat_code_begin = GetQuickOatCodeBegin(method);
1970 const void* quick_oat_code_end = GetQuickOatCodeEnd(method);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001971 const size_t pointer_size = image_header_.GetPointerSize();
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001972 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1973 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001974 if (method->IsNative()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001975 bool first_occurrence;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001976 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
1977 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001978 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001979 stats_.native_to_managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001980 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001981 if (quick_oat_code_begin != method->GetEntryPointFromQuickCompiledCodePtrSize(
1982 image_header_.GetPointerSize())) {
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001983 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001984 }
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001985 } else if (method->IsAbstract() || method->IsClassInitializer()) {
Nicolas Geoffray796d6302016-03-13 22:22:31 +00001986 // Don't print information for these.
Mathieu Chartiere42888f2016-04-14 10:49:19 -07001987 } else if (method->IsRuntimeMethod()) {
1988 ImtConflictTable* table = method->GetImtConflictTable(image_header_.GetPointerSize());
1989 if (table != nullptr) {
1990 indent_os << "IMT conflict table " << table << " method: ";
1991 for (size_t i = 0, count = table->NumEntries(pointer_size); i < count; ++i) {
1992 indent_os << PrettyMethod(table->GetImplementationMethod(i, pointer_size)) << " ";
1993 }
1994 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001995 } else {
1996 const DexFile::CodeItem* code_item = method->GetCodeItem();
1997 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08001998 stats_.dex_instruction_bytes += dex_instruction_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001999
2000 bool first_occurrence;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002001 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002002 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002003 // Method compiled with the optimizing compiler have no vmap table.
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002004 vmap_table_bytes = ComputeOatSize(method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002005 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002006 stats_.vmap_table_bytes += vmap_table_bytes;
Roland Levillain6d7f1792015-07-02 10:59:15 +01002007 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008 }
2009
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002010 uint32_t quick_oat_code_size = GetQuickOatCodeSize(method);
2011 ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002012 if (first_occurrence) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002013 stats_.managed_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002014 if (method->IsConstructor()) {
2015 if (method->IsStatic()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002016 stats_.class_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002017 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002018 stats_.large_initializer_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002019 }
2020 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002021 stats_.large_method_code_bytes += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002022 }
2023 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002024 stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002025
Igor Murashkin7617abd2015-07-10 18:27:47 -07002026 uint32_t method_access_flags = method->GetAccessFlags();
2027
Mathieu Chartiere401d142015-04-22 13:56:20 -07002028 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002029 indent_os << StringPrintf("SIZE: Dex Instructions=%zd StackMaps=%zd AccessFlags=0x%x\n",
2030 dex_instruction_bytes,
2031 vmap_table_bytes,
Igor Murashkin7617abd2015-07-10 18:27:47 -07002032 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002033
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002034 size_t total_size = dex_instruction_bytes +
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002035 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_header_.GetPointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002036
2037 double expansion =
2038 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002039 stats_.ComputeOutliers(total_size, expansion, method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002040 }
2041 }
2042
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002043 std::set<const void*> already_seen_;
2044 // Compute the size of the given data within the oat file and whether this is the first time
2045 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002046 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002047 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002048 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002049 already_seen_.insert(oat_data);
2050 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002051 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002052 }
2053 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002054 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002055
2056 public:
2057 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002058 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002059 size_t file_bytes;
2060
2061 size_t header_bytes;
2062 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002063 size_t art_field_bytes;
2064 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002065 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002066 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002067 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002068 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002069 size_t alignment_bytes;
2070
2071 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002072 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002073 size_t managed_to_native_code_bytes;
2074 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002075 size_t class_initializer_code_bytes;
2076 size_t large_initializer_code_bytes;
2077 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002078
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002079 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002080
2081 size_t dex_instruction_bytes;
2082
Mathieu Chartiere401d142015-04-22 13:56:20 -07002083 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002084 std::vector<size_t> method_outlier_size;
2085 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002086 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002087
Roland Levillain3887c462015-08-12 18:15:42 +01002088 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002089 : oat_file_bytes(0),
2090 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002091 header_bytes(0),
2092 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002093 art_field_bytes(0),
2094 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002095 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002096 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002097 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002098 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002099 alignment_bytes(0),
2100 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002101 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002102 managed_to_native_code_bytes(0),
2103 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002104 class_initializer_code_bytes(0),
2105 large_initializer_code_bytes(0),
2106 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002107 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002108 dex_instruction_bytes(0) {}
2109
Elliott Hughesa0e18062012-04-13 15:59:59 -07002110 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002111 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002112 size_t bytes;
2113 size_t count;
2114 };
2115 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2116 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002117
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002118 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002119 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2120 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002121 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002122 it->second.count += 1;
2123 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002124 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002125 }
2126 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002127
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002128 double PercentOfOatBytes(size_t size) {
2129 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2130 }
2131
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002132 double PercentOfFileBytes(size_t size) {
2133 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2134 }
2135
2136 double PercentOfObjectBytes(size_t size) {
2137 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2138 }
2139
Mathieu Chartiere401d142015-04-22 13:56:20 -07002140 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002141 method_outlier_size.push_back(total_size);
2142 method_outlier_expansion.push_back(expansion);
2143 method_outlier.push_back(method);
2144 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002145
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002146 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002147 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002148 size_t sum_of_sizes = 0;
2149 size_t sum_of_sizes_squared = 0;
2150 size_t sum_of_expansion = 0;
2151 size_t sum_of_expansion_squared = 0;
2152 size_t n = method_outlier_size.size();
Mathieu Chartier1ebf8d32016-06-09 11:51:27 -07002153 if (n <= 1) {
Jeff Haodcdc85b2015-12-04 14:06:18 -08002154 return;
2155 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002156 for (size_t i = 0; i < n; i++) {
2157 size_t cur_size = method_outlier_size[i];
2158 sum_of_sizes += cur_size;
2159 sum_of_sizes_squared += cur_size * cur_size;
2160 double cur_expansion = method_outlier_expansion[i];
2161 sum_of_expansion += cur_expansion;
2162 sum_of_expansion_squared += cur_expansion * cur_expansion;
2163 }
2164 size_t size_mean = sum_of_sizes / n;
2165 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2166 double expansion_mean = sum_of_expansion / n;
2167 double expansion_variance =
2168 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2169
2170 // Dump methods whose size is a certain number of standard deviations from the mean
2171 size_t dumped_values = 0;
2172 size_t skipped_values = 0;
2173 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2174 size_t cur_size_variance = i * i * size_variance;
2175 bool first = true;
2176 for (size_t j = 0; j < n; j++) {
2177 size_t cur_size = method_outlier_size[j];
2178 if (cur_size > size_mean) {
2179 size_t cur_var = cur_size - size_mean;
2180 cur_var = cur_var * cur_var;
2181 if (cur_var > cur_size_variance) {
2182 if (dumped_values > 20) {
2183 if (i == 1) {
2184 skipped_values++;
2185 } else {
2186 i = 2; // jump to counting for 1 standard deviation
2187 break;
2188 }
2189 } else {
2190 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002191 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002192 first = false;
2193 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002194 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002195 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002196 method_outlier_size[j] = 0; // don't consider this method again
2197 dumped_values++;
2198 }
2199 }
2200 }
2201 }
2202 }
2203 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002204 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002205 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002206 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002207 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002208
2209 // Dump methods whose expansion is a certain number of standard deviations from the mean
2210 dumped_values = 0;
2211 skipped_values = 0;
2212 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2213 double cur_expansion_variance = i * i * expansion_variance;
2214 bool first = true;
2215 for (size_t j = 0; j < n; j++) {
2216 double cur_expansion = method_outlier_expansion[j];
2217 if (cur_expansion > expansion_mean) {
2218 size_t cur_var = cur_expansion - expansion_mean;
2219 cur_var = cur_var * cur_var;
2220 if (cur_var > cur_expansion_variance) {
2221 if (dumped_values > 20) {
2222 if (i == 1) {
2223 skipped_values++;
2224 } else {
2225 i = 2; // jump to counting for 1 standard deviation
2226 break;
2227 }
2228 } else {
2229 if (first) {
2230 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002231 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002232 first = false;
2233 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002234 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002235 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002236 method_outlier_expansion[j] = 0.0; // don't consider this method again
2237 dumped_values++;
2238 }
2239 }
2240 }
2241 }
2242 }
2243 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002244 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002245 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002246 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002247 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002248 }
2249
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002250 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002251 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002252 {
2253 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2254 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002255 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2256 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2257 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2258 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2259 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2260 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002261 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002262 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2263 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002264 header_bytes, PercentOfFileBytes(header_bytes),
2265 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002266 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2267 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002268 dex_cache_arrays_bytes,
2269 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002270 interned_strings_bytes,
2271 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002272 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002273 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002274 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2275 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002276 CHECK_EQ(file_bytes,
2277 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2278 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2279 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002280 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002281
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002282 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002283 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002284 for (const auto& sizes_and_count : sizes_and_counts) {
2285 const std::string& descriptor(sizes_and_count.first);
2286 double average = static_cast<double>(sizes_and_count.second.bytes) /
2287 static_cast<double>(sizes_and_count.second.count);
2288 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002289 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002290 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002291 descriptor.c_str(), sizes_and_count.second.bytes,
2292 sizes_and_count.second.count, average, percent);
2293 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002294 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002295 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002296 CHECK_EQ(object_bytes, object_bytes_total);
2297
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 os << StringPrintf("oat_file_bytes = %8zd\n"
2299 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2300 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2301 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2302 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2303 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2304 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002305 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002306 managed_code_bytes,
2307 PercentOfOatBytes(managed_code_bytes),
2308 managed_to_native_code_bytes,
2309 PercentOfOatBytes(managed_to_native_code_bytes),
2310 native_to_managed_code_bytes,
2311 PercentOfOatBytes(native_to_managed_code_bytes),
2312 class_initializer_code_bytes,
2313 PercentOfOatBytes(class_initializer_code_bytes),
2314 large_initializer_code_bytes,
2315 PercentOfOatBytes(large_initializer_code_bytes),
2316 large_method_code_bytes,
2317 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002319 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002320 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002321 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2322 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002323 }
2324
Vladimir Marko9d07e3d2016-03-31 12:02:28 +01002325 os << "\n" << StringPrintf("vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002326 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002327 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002328
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2330 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002331 static_cast<double>(managed_code_bytes) /
2332 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002333 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002334 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002335 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002336
2337 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002338 }
2339 } stats_;
2340
2341 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002342 enum {
2343 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2344 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2345 kLargeConstructorDexBytes = 4000,
2346 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2347 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2348 kLargeMethodDexBytes = 16000
2349 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002350
2351 // For performance, use the *os_ directly for anything that doesn't need indentation
2352 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002353 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002354 VariableIndentationOutputStream vios_;
2355 ScopedIndentation indent1_;
2356
Ian Rogers1d54e732013-05-02 21:10:01 -07002357 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002358 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002359 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002360 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002361 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002362
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002363 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002364};
2365
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002366static int DumpImage(gc::space::ImageSpace* image_space,
2367 OatDumperOptions* options,
2368 std::ostream* os) SHARED_REQUIRES(Locks::mutator_lock_) {
2369 const ImageHeader& image_header = image_space->GetImageHeader();
2370 if (!image_header.IsValid()) {
2371 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2372 return EXIT_FAILURE;
2373 }
2374 ImageDumper image_dumper(os, *image_space, image_header, options);
2375 if (!image_dumper.Dump()) {
2376 return EXIT_FAILURE;
2377 }
2378 return EXIT_SUCCESS;
2379}
2380
2381static int DumpImages(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002382 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002383 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002384 options->class_loader_ = &null_class_loader;
2385
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002386 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002387 if (options->app_image_ != nullptr) {
2388 if (options->app_oat_ == nullptr) {
2389 LOG(ERROR) << "Can not dump app image without app oat file";
Jeff Haodcdc85b2015-12-04 14:06:18 -08002390 return EXIT_FAILURE;
2391 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002392 // We can't know if the app image is 32 bits yet, but it contains pointers into the oat file.
2393 // We need to map the oat file in the low 4gb or else the fixup wont be able to fit oat file
2394 // pointers into 32 bit pointer sized ArtMethods.
2395 std::string error_msg;
2396 std::unique_ptr<OatFile> oat_file(OatFile::Open(options->app_oat_,
2397 options->app_oat_,
2398 nullptr,
2399 nullptr,
2400 false,
2401 /*low_4gb*/true,
2402 nullptr,
2403 &error_msg));
2404 if (oat_file == nullptr) {
2405 LOG(ERROR) << "Failed to open oat file " << options->app_oat_ << " with error " << error_msg;
Jeff Haodcdc85b2015-12-04 14:06:18 -08002406 return EXIT_FAILURE;
2407 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002408 std::unique_ptr<gc::space::ImageSpace> space(
2409 gc::space::ImageSpace::CreateFromAppImage(options->app_image_, oat_file.get(), &error_msg));
2410 if (space == nullptr) {
2411 LOG(ERROR) << "Failed to open app image " << options->app_image_ << " with error "
2412 << error_msg;
2413 }
2414 // Open dex files for the image.
2415 std::vector<std::unique_ptr<const DexFile>> dex_files;
2416 if (!runtime->GetClassLinker()->OpenImageDexFiles(space.get(), &dex_files, &error_msg)) {
2417 LOG(ERROR) << "Failed to open app image dex files " << options->app_image_ << " with error "
2418 << error_msg;
2419 }
2420 // Dump the actual image.
2421 int result = DumpImage(space.get(), options, os);
2422 if (result != EXIT_SUCCESS) {
2423 return result;
2424 }
2425 // Fall through to dump the boot images.
2426 }
2427
2428 gc::Heap* heap = runtime->GetHeap();
2429 CHECK(heap->HasBootImageSpace()) << "No image spaces";
2430 for (gc::space::ImageSpace* image_space : heap->GetBootImageSpaces()) {
2431 int result = DumpImage(image_space, options, os);
2432 if (result != EXIT_SUCCESS) {
2433 return result;
2434 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002435 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002436 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002437}
2438
Andreas Gampe00b25f32014-09-17 21:49:05 -07002439static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2440 std::ostream* os) {
2441 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2442
2443 Thread* self = Thread::Current();
2444 CHECK(self != nullptr);
2445 // Need well-known-classes.
2446 WellKnownClasses::Init(self->GetJniEnv());
2447
2448 // Need to register dex files to get a working dex cache.
2449 ScopedObjectAccess soa(self);
2450 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002451 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002452 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002453 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2454 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002455 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002456 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierf284d442016-06-02 11:48:30 -07002457 class_linker->RegisterDexFile(*dex_file, nullptr);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002458 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002459 }
2460
2461 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002462 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002463 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002464
2465 // Use the class loader while dumping.
2466 StackHandleScope<1> scope(self);
2467 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2468 soa.Decode<mirror::ClassLoader*>(class_loader));
2469 options->class_loader_ = &loader_handle;
2470
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002471 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002472 bool success = oat_dumper.Dump(*os);
2473 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2474}
2475
2476static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002477 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002478 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002479 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002480 options->class_loader_ = &null_class_loader;
2481
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002482 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002483 bool success = oat_dumper.Dump(*os);
2484 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2485}
2486
2487static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2488 std::ostream* os) {
2489 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002490 OatFile* oat_file = OatFile::Open(oat_filename,
2491 oat_filename,
2492 nullptr,
2493 nullptr,
2494 false,
2495 /*low_4gb*/false,
2496 nullptr,
2497 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002498 if (oat_file == nullptr) {
2499 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2500 return EXIT_FAILURE;
2501 }
2502
2503 if (runtime != nullptr) {
2504 return DumpOatWithRuntime(runtime, oat_file, options, os);
2505 } else {
2506 return DumpOatWithoutRuntime(oat_file, options, os);
2507 }
2508}
2509
David Srbecky2fdd03c2016-03-10 15:32:37 +00002510static int SymbolizeOat(const char* oat_filename, std::string& output_name, bool no_bits) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002511 std::string error_msg;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002512 OatFile* oat_file = OatFile::Open(oat_filename,
2513 oat_filename,
2514 nullptr,
2515 nullptr,
2516 false,
2517 /*low_4gb*/false,
2518 nullptr,
2519 &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002520 if (oat_file == nullptr) {
2521 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2522 return EXIT_FAILURE;
2523 }
2524
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002525 bool result;
2526 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2527 // files for 64-bit code in the past.
2528 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002529 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002530 result = oat_symbolizer.Symbolize();
2531 } else {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002532 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name, no_bits);
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002533 result = oat_symbolizer.Symbolize();
2534 }
2535 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002536 fprintf(stderr, "Failed to symbolize\n");
2537 return EXIT_FAILURE;
2538 }
2539
2540 return EXIT_SUCCESS;
2541}
2542
Igor Murashkin37743352014-11-13 14:38:00 -08002543struct OatdumpArgs : public CmdlineArgs {
2544 protected:
2545 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002546
Igor Murashkin37743352014-11-13 14:38:00 -08002547 virtual ParseStatus ParseCustom(const StringPiece& option,
2548 std::string* error_msg) OVERRIDE {
2549 {
2550 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2551 if (base_parse != kParseUnknownArgument) {
2552 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002553 }
2554 }
2555
Igor Murashkin37743352014-11-13 14:38:00 -08002556 if (option.starts_with("--oat-file=")) {
2557 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2558 } else if (option.starts_with("--image=")) {
2559 image_location_ = option.substr(strlen("--image=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002560 } else if (option == "--no-dump:vmap") {
2561 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002562 } else if (option =="--dump:code_info_stack_maps") {
2563 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002564 } else if (option == "--no-disassemble") {
2565 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002566 } else if (option =="--header-only") {
2567 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002568 } else if (option.starts_with("--symbolize=")) {
2569 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2570 symbolize_ = true;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002571 } else if (option.starts_with("--only-keep-debug")) {
2572 only_keep_debug_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002573 } else if (option.starts_with("--class-filter=")) {
2574 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002575 } else if (option.starts_with("--method-filter=")) {
2576 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002577 } else if (option.starts_with("--list-classes")) {
2578 list_classes_ = true;
2579 } else if (option.starts_with("--list-methods")) {
2580 list_methods_ = true;
2581 } else if (option.starts_with("--export-dex-to=")) {
2582 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2583 } else if (option.starts_with("--addr2instr=")) {
2584 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2585 *error_msg = "Address conversion failed";
2586 return kParseError;
2587 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002588 } else if (option.starts_with("--app-image=")) {
2589 app_image_ = option.substr(strlen("--app-image=")).data();
2590 } else if (option.starts_with("--app-oat=")) {
2591 app_oat_ = option.substr(strlen("--app-oat=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002592 } else {
2593 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002594 }
2595
Igor Murashkin37743352014-11-13 14:38:00 -08002596 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002597 }
2598
Igor Murashkin37743352014-11-13 14:38:00 -08002599 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2600 // Infer boot image location from the image location if possible.
2601 if (boot_image_location_ == nullptr) {
2602 boot_image_location_ = image_location_;
2603 }
2604
2605 // Perform the parent checks.
2606 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2607 if (parent_checks != kParseOk) {
2608 return parent_checks;
2609 }
2610
2611 // Perform our own checks.
2612 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2613 *error_msg = "Either --image or --oat-file must be specified";
2614 return kParseError;
2615 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2616 *error_msg = "Either --image or --oat-file must be specified but not both";
2617 return kParseError;
2618 }
2619
2620 return kParseOk;
2621 }
2622
2623 virtual std::string GetUsage() const {
2624 std::string usage;
2625
2626 usage +=
2627 "Usage: oatdump [options] ...\n"
2628 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2629 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2630 "\n"
2631 // Either oat-file or image is required.
2632 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2633 " Example: --oat-file=/system/framework/boot.oat\n"
2634 "\n"
2635 " --image=<file.art>: specifies an input image location.\n"
2636 " Example: --image=/system/framework/boot.art\n"
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002637 "\n"
2638 " --app-image=<file.art>: specifies an input app image. Must also have a specified\n"
2639 " boot image and app oat file.\n"
2640 " Example: --app-image=app.art\n"
2641 "\n"
2642 " --app-oat=<file.odex>: specifies an input app oat.\n"
2643 " Example: --app-oat=app.odex\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002644 "\n";
2645
2646 usage += Base::GetUsage();
2647
2648 usage += // Optional.
Igor Murashkin37743352014-11-13 14:38:00 -08002649 " --no-dump:vmap may be used to disable vmap dumping.\n"
2650 " Example: --no-dump:vmap\n"
2651 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002652 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2653 " Example: --dump:code_info_stack_maps\n"
2654 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002655 " --no-disassemble may be used to disable disassembly.\n"
2656 " Example: --no-disassemble\n"
2657 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002658 " --header-only may be used to print only the oat header.\n"
2659 " Example: --header-only\n"
2660 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002661 " --list-classes may be used to list target file classes (can be used with filters).\n"
2662 " Example: --list-classes\n"
2663 " Example: --list-classes --class-filter=com.example.foo\n"
2664 "\n"
2665 " --list-methods may be used to list target file methods (can be used with filters).\n"
2666 " Example: --list-methods\n"
2667 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2668 "\n"
2669 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2670 " Example: --symbolize=/system/framework/boot.oat\n"
2671 "\n"
David Srbecky2fdd03c2016-03-10 15:32:37 +00002672 " --only-keep-debug<file.oat>: Modifies the behaviour of --symbolize so that\n"
2673 " .rodata and .text sections are omitted in the output file to save space.\n"
2674 " Example: --symbolize=/system/framework/boot.oat --only-keep-debug\n"
2675 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002676 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2677 " Example: --class-filter=com.example.foo\n"
2678 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002679 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2680 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002681 "\n"
2682 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2683 " Example: --export-dex-to=/data/local/tmp\n"
2684 "\n"
2685 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2686 " address (e.g. PC from crash dump)\n"
2687 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002688 "\n";
2689
2690 return usage;
2691 }
2692
2693 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002694 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002695 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002696 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002697 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002698 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002699 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002700 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002701 bool disassemble_code_ = true;
2702 bool symbolize_ = false;
David Srbecky2fdd03c2016-03-10 15:32:37 +00002703 bool only_keep_debug_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002704 bool list_classes_ = false;
2705 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002706 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002707 uint32_t addr2instr_ = 0;
2708 const char* export_dex_location_ = nullptr;
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002709 const char* app_image_ = nullptr;
2710 const char* app_oat_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002711};
2712
Igor Murashkin37743352014-11-13 14:38:00 -08002713struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2714 virtual bool NeedsRuntime() OVERRIDE {
2715 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002716
Igor Murashkin37743352014-11-13 14:38:00 -08002717 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2718 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2719
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002720 oat_dumper_options_.reset(new OatDumperOptions(
Igor Murashkin37743352014-11-13 14:38:00 -08002721 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002722 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002723 args_->disassemble_code_,
2724 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002725 args_->class_filter_,
2726 args_->method_filter_,
2727 args_->list_classes_,
2728 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002729 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002730 args_->export_dex_location_,
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002731 args_->app_image_,
2732 args_->app_oat_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002733 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002734
2735 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2736 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002737 }
2738
Igor Murashkin37743352014-11-13 14:38:00 -08002739 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2740 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002741 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002742
Mathieu Chartierd424d082014-10-15 10:31:46 -07002743 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002744
Andreas Gampec24f3992014-12-17 20:40:11 -08002745 if (args_->symbolize_) {
David Srbecky2fdd03c2016-03-10 15:32:37 +00002746 // ELF has special kind of section called SHT_NOBITS which allows us to create
2747 // sections which exist but their data is omitted from the ELF file to save space.
2748 // This is what "strip --only-keep-debug" does when it creates separate ELF file
2749 // with only debug data. We use it in similar way to exclude .rodata and .text.
2750 bool no_bits = args_->only_keep_debug_;
2751 return SymbolizeOat(args_->oat_filename_, args_->output_name_, no_bits) == EXIT_SUCCESS;
Andreas Gampec24f3992014-12-17 20:40:11 -08002752 } else {
2753 return DumpOat(nullptr,
2754 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002755 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002756 args_->os_) == EXIT_SUCCESS;
2757 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002758 }
2759
Igor Murashkin37743352014-11-13 14:38:00 -08002760 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2761 CHECK(args_ != nullptr);
2762
2763 if (args_->oat_filename_ != nullptr) {
2764 return DumpOat(runtime,
2765 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002766 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002767 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002768 }
Igor Murashkin37743352014-11-13 14:38:00 -08002769
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -08002770 return DumpImages(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002771 }
2772
Igor Murashkin37743352014-11-13 14:38:00 -08002773 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2774};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002775
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002776} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002777
2778int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002779 art::OatdumpMain main;
2780 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002781}