blob: b52f30ec33dfc8806399069d2edf68063f9def16 [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"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080036#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080037#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070038#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070040#include "gc/space/image_space.h"
41#include "gc/space/large_object_space.h"
42#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080043#include "image-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000045#include "linker/buffered_output_stream.h"
46#include "linker/file_output_stream.h"
Ian Rogers1809a722013-08-09 22:05:32 -070047#include "mapping_table.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 Geoffray6bc43742015-10-12 18:11:10 +010059#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080061#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080062#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080063#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070064#include "vmap_table.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
David Srbeckybc90fd02015-04-22 19:40:27 +0100101class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700102 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100103 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100104 oat_file_(oat_file), builder_(nullptr),
105 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700106 }
107
108 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
109 uint32_t,
110 const OatFile::OatMethod&,
111 const DexFile&,
112 uint32_t,
113 const DexFile::CodeItem*,
114 uint32_t);
115
116 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000117 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000118 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
119 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000120
121 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
122 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000123 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
David Srbecky5d811202016-03-08 13:21:22 +0000124 builder_.reset(new ElfBuilder<ElfTypes32>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000125
126 builder_->Start();
127
128 auto* rodata = builder_->GetRoData();
129 auto* text = builder_->GetText();
130 auto* bss = builder_->GetBss();
131 auto* strtab = builder_->GetStrTab();
132 auto* symtab = builder_->GetSymTab();
133
134 rodata->Start();
135 const uint8_t* rodata_begin = oat_file_->Begin();
136 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
137 rodata->WriteFully(rodata_begin, rodata_size);
138 rodata->End();
139
140 text->Start();
141 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
142 const size_t text_size = oat_file_->End() - text_begin;
143 text->WriteFully(text_begin, text_size);
144 text->End();
145
146 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000147 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148 }
149
Vladimir Marko944da602016-02-19 12:27:55 +0000150 builder_->PrepareDynamicSection(
151 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
152 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700153
154 Walk(&art::OatSymbolizer::RegisterForDedup);
155
156 NormalizeState();
157
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158 strtab->Start();
159 strtab->Write(""); // strtab should start with empty string.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000160 AddTrampolineSymbols();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700161 Walk(&art::OatSymbolizer::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000162 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700163
David Srbecky6d8c8f02015-10-26 10:57:09 +0000164 symtab->Start();
165 symtab->Write();
166 symtab->End();
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
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000173 void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
174 if (code_offset != 0) {
175 uint32_t name_offset = builder_->GetStrTab()->Write(name);
176 uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
Tamas Berghammer9e49ab12016-01-28 16:30:14 +0000177 // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
178 // the end of the section in case of the last symbol.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000179 builder_->GetSymTab()->Add(name_offset, builder_->GetText(), symbol_value,
180 /* is_relative */ true, /* size */ 0, STB_GLOBAL, STT_FUNC);
181 }
182 }
183
184 void AddTrampolineSymbols() {
185 const OatHeader& oat_header = oat_file_->GetOatHeader();
186 AddTrampolineSymbol("interpreterToInterpreterBridge",
187 oat_header.GetInterpreterToInterpreterBridgeOffset());
188 AddTrampolineSymbol("interpreterToCompiledCodeBridge",
189 oat_header.GetInterpreterToCompiledCodeBridgeOffset());
190 AddTrampolineSymbol("jniDlsymLookup",
191 oat_header.GetJniDlsymLookupOffset());
192 AddTrampolineSymbol("quickGenericJniTrampoline",
193 oat_header.GetQuickGenericJniTrampolineOffset());
194 AddTrampolineSymbol("quickImtConflictTrampoline",
195 oat_header.GetQuickImtConflictTrampolineOffset());
196 AddTrampolineSymbol("quickResolutionTrampoline",
197 oat_header.GetQuickResolutionTrampolineOffset());
198 AddTrampolineSymbol("quickToInterpreterBridge",
199 oat_header.GetQuickToInterpreterBridgeOffset());
200 }
201
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700202 void Walk(Callback callback) {
203 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
204 for (size_t i = 0; i < oat_dex_files.size(); i++) {
205 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700206 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700207 WalkOatDexFile(oat_dex_file, callback);
208 }
209 }
210
211 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
212 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700213 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
214 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700215 return;
216 }
217 for (size_t class_def_index = 0;
218 class_def_index < dex_file->NumClassDefs();
219 class_def_index++) {
220 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
221 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
222 OatClassType type = oat_class.GetType();
223 switch (type) {
224 case kOatClassAllCompiled:
225 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700226 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700227 break;
228
229 case kOatClassNoneCompiled:
230 case kOatClassMax:
231 // Ignore.
232 break;
233 }
234 }
235 }
236
237 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
238 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700239 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700240 if (class_data == nullptr) { // empty class such as a marker interface?
241 return;
242 }
243 // Note: even if this is an interface or a native class, we still have to walk it, as there
244 // might be a static initializer.
245 ClassDataItemIterator it(dex_file, class_data);
246 SkipAllFields(&it);
247 uint32_t class_method_idx = 0;
248 while (it.HasNextDirectMethod()) {
249 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
250 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700251 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700252 class_method_idx++;
253 it.Next();
254 }
255 while (it.HasNextVirtualMethod()) {
256 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
257 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700258 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700259 class_method_idx++;
260 it.Next();
261 }
262 DCHECK(!it.HasNext());
263 }
264
265 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
266 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
267 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
268 uint32_t method_access_flags, Callback callback) {
269 if ((method_access_flags & kAccAbstract) != 0) {
270 // Abstract method, no code.
271 return;
272 }
273 if (oat_method.GetCodeOffset() == 0) {
274 // No code.
275 return;
276 }
277
278 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
279 method_access_flags);
280 }
281
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700282 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
283 uint32_t class_method_index ATTRIBUTE_UNUSED,
284 const OatFile::OatMethod& oat_method,
285 const DexFile& dex_file ATTRIBUTE_UNUSED,
286 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
287 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
288 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700289 state_[oat_method.GetCodeOffset()]++;
290 }
291
292 void NormalizeState() {
293 for (auto& x : state_) {
294 if (x.second == 1) {
295 state_[x.first] = 0;
296 }
297 }
298 }
299
300 enum class DedupState { // private
301 kNotDeduplicated,
302 kDeduplicatedFirst,
303 kDeduplicatedOther
304 };
305 DedupState IsDuplicated(uint32_t offset) {
306 if (state_[offset] == 0) {
307 return DedupState::kNotDeduplicated;
308 }
309 if (state_[offset] == 1) {
310 return DedupState::kDeduplicatedOther;
311 }
312 state_[offset] = 1;
313 return DedupState::kDeduplicatedFirst;
314 }
315
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700316 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
317 uint32_t class_method_index ATTRIBUTE_UNUSED,
318 const OatFile::OatMethod& oat_method,
319 const DexFile& dex_file,
320 uint32_t dex_method_idx,
321 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
322 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700323 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
324 if (dedup != DedupState::kDeduplicatedOther) {
325 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
326
327 if (dedup == DedupState::kDeduplicatedFirst) {
328 pretty_name = "[Dedup]" + pretty_name;
329 }
330
David Srbecky6d8c8f02015-10-26 10:57:09 +0000331 int name_offset = builder_->GetStrTab()->Write(pretty_name);
332 builder_->GetSymTab()->Add(name_offset, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700333 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
334 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700335 }
336 }
337
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700338 private:
339 static void SkipAllFields(ClassDataItemIterator* it) {
340 while (it->HasNextStaticField()) {
341 it->Next();
342 }
343 while (it->HasNextInstanceField()) {
344 it->Next();
345 }
346 }
347
348 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100349 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700350 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700351 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700352};
353
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700354class OatDumperOptions {
355 public:
356 OatDumperOptions(bool dump_raw_mapping_table,
357 bool dump_raw_gc_map,
358 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100359 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700360 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700361 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800362 const char* class_filter,
363 const char* method_filter,
364 bool list_classes,
365 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000366 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800367 const char* export_dex_location,
368 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700369 : dump_raw_mapping_table_(dump_raw_mapping_table),
370 dump_raw_gc_map_(dump_raw_gc_map),
371 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100372 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700373 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700374 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800375 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000376 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800377 list_classes_(list_classes),
378 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000379 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800380 export_dex_location_(export_dex_location),
381 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800382 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700383
384 const bool dump_raw_mapping_table_;
385 const bool dump_raw_gc_map_;
386 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100387 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700388 const bool disassemble_code_;
389 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800390 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000391 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800392 const bool list_classes_;
393 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000394 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800395 const char* const export_dex_location_;
396 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700397 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700398};
399
Elliott Hughese3c845c2012-02-28 17:23:01 -0800400class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700401 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100402 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000403 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800404 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700405 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800406 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800407 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
408 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800409 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000410 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100411 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800412 CHECK(options_.class_loader_ != nullptr);
413 CHECK(options_.class_filter_ != nullptr);
414 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800415 AddAllOffsets();
416 }
417
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700418 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700419 delete disassembler_;
420 }
421
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800422 InstructionSet GetInstructionSet() {
423 return instruction_set_;
424 }
425
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700426 bool Dump(std::ostream& os) {
427 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800428 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700429
430 os << "MAGIC:\n";
431 os << oat_header.GetMagic() << "\n\n";
432
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800433 os << "LOCATION:\n";
434 os << oat_file_.GetLocation() << "\n\n";
435
Brian Carlstromaded5f72011-10-07 17:15:04 -0700436 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800437 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700438
Elliott Hughesa72ec822012-03-05 17:12:22 -0800439 os << "INSTRUCTION SET:\n";
440 os << oat_header.GetInstructionSet() << "\n\n";
441
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700442 {
443 std::unique_ptr<const InstructionSetFeatures> features(
444 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
445 oat_header.GetInstructionSetFeaturesBitmap()));
446 os << "INSTRUCTION SET FEATURES:\n";
447 os << features->GetFeatureString() << "\n\n";
448 }
Dave Allison70202782013-10-22 17:52:19 -0700449
Brian Carlstromaded5f72011-10-07 17:15:04 -0700450 os << "DEX FILE COUNT:\n";
451 os << oat_header.GetDexFileCount() << "\n\n";
452
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800453#define DUMP_OAT_HEADER_OFFSET(label, offset) \
454 os << label " OFFSET:\n"; \
455 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800456 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800457 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
458 } \
459 os << StringPrintf("\n\n");
460
461 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
462 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
463 GetInterpreterToInterpreterBridgeOffset);
464 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
465 GetInterpreterToCompiledCodeBridgeOffset);
466 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
467 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800468 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
469 GetQuickGenericJniTrampolineOffset);
470 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
471 GetQuickImtConflictTrampolineOffset);
472 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
473 GetQuickResolutionTrampolineOffset);
474 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
475 GetQuickToInterpreterBridgeOffset);
476#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700477
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700478 os << "IMAGE PATCH DELTA:\n";
479 os << StringPrintf("%d (0x%08x)\n\n",
480 oat_header.GetImagePatchDelta(),
481 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700482
Brian Carlstrom28db0122012-10-18 16:20:41 -0700483 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
484 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
485
486 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800487 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700488
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700489 // Print the key-value store.
490 {
491 os << "KEY VALUE STORE:\n";
492 size_t index = 0;
493 const char* key;
494 const char* value;
495 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
496 os << key << " = " << value << "\n";
497 index++;
498 }
499 os << "\n";
500 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700501
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800502 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700503 os << "BEGIN:\n";
504 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700505
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700506 os << "END:\n";
507 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
508 }
509
510 os << "SIZE:\n";
511 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700512
513 os << std::flush;
514
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800515 // If set, adjust relative address to be searched
516 if (options_.addr2instr_ != 0) {
517 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
518 os << "SEARCH ADDRESS (executable offset + input):\n";
519 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
520 }
521
David Brazdilc03d7b62016-03-02 12:18:03 +0000522 if (!options_.dump_header_only_) {
523 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
524 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
525 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800526
David Brazdilc03d7b62016-03-02 12:18:03 +0000527 // If file export selected skip file analysis
528 if (options_.export_dex_location_) {
529 if (!ExportDexFile(os, *oat_dex_file)) {
530 success = false;
531 }
532 } else {
533 if (!DumpOatDexFile(os, *oat_dex_file)) {
534 success = false;
535 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800536 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700537 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700538 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000539
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700540 os << std::flush;
541 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700542 }
543
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800544 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700545 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
546 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800547 return 0; // Address not in oat file
548 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800549 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
550 reinterpret_cast<uintptr_t>(oat_file_.Begin());
551 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800552 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800553 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800554 return end_offset - begin_offset;
555 }
556
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800557 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700558 return oat_file_.GetOatHeader().GetInstructionSet();
559 }
560
Mathieu Chartier90443472015-07-16 20:32:27 -0700561 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800562 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
563 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700564 CHECK(oat_dex_file != nullptr);
565 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700566 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
567 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700568 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
569 << "': " << error_msg;
570 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800571 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700572 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800573 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700574 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700575 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100576 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800577 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100578 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800579 }
580 }
581 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700582 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800583 }
584
Brian Carlstromaded5f72011-10-07 17:15:04 -0700585 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800586 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800587 // We don't know the length of the code for each method, but we need to know where to stop
588 // when disassembling. What we do know is that a region of code will be followed by some other
589 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
590 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800591 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
592 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700593 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700594 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700595 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
596 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700597 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
598 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800599 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800600 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800601 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800602 for (size_t class_def_index = 0;
603 class_def_index < dex_file->NumClassDefs();
604 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800605 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100606 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700607 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700608 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800609 ClassDataItemIterator it(*dex_file, class_data);
610 SkipAllFields(it);
611 uint32_t class_method_index = 0;
612 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100613 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800614 it.Next();
615 }
616 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100617 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800618 it.Next();
619 }
620 }
621 }
622 }
623
624 // If the last thing in the file is code for a method, there won't be an offset for the "next"
625 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
626 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800627 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800628 }
629
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700630 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
631 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
632 }
633
Elliott Hughese3c845c2012-02-28 17:23:01 -0800634 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800635 uint32_t code_offset = oat_method.GetCodeOffset();
636 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
637 code_offset &= ~0x1;
638 }
639 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800640 offsets_.insert(oat_method.GetMappingTableOffset());
641 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800642 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800643 }
644
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700645 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
646 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800647 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700648 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800649 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800650 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700651
652 // Create the verifier early.
653
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700654 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700655 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
656 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700657 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700658 os << std::flush;
659 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700660 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100661
662 VariableIndentationOutputStream vios(&os);
663 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800664 for (size_t class_def_index = 0;
665 class_def_index < dex_file->NumClassDefs();
666 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700667 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
668 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800669
670 // TODO: Support regex
671 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
672 continue;
673 }
674
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700675 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100676 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700677 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
678 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100679 << " (" << oat_class.GetStatus() << ")"
680 << " (" << oat_class.GetType() << ")\n";
681 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800682 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700683 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700684 success = false;
685 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800686 if (stop_analysis) {
687 os << std::flush;
688 return success;
689 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700690 }
691
692 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700693 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700694 }
695
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800696 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
697 std::string error_msg;
698 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
699
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700700 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800701 if (dex_file == nullptr) {
702 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
703 return false;
704 }
705 size_t fsize = oat_dex_file.FileSize();
706
707 // Some quick checks just in case
708 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
709 os << "Invalid dex file\n";
710 return false;
711 }
712
713 // Verify output directory exists
714 if (!OS::DirectoryExists(options_.export_dex_location_)) {
715 // TODO: Extend OS::DirectoryExists if symlink support is required
716 os << options_.export_dex_location_ << " output directory not found or symlink\n";
717 return false;
718 }
719
720 // Beautify path names
721 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
722 return false;
723 }
724
725 std::string dex_orig_name;
726 size_t dex_orig_pos = dex_file_location.rfind('/');
727 if (dex_orig_pos == std::string::npos)
728 dex_orig_name = dex_file_location;
729 else
730 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
731
732 // A more elegant approach to efficiently name user installed apps is welcome
733 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
734 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
735 size_t apk_orig_pos = dex_file_location.rfind('/');
736 if (apk_orig_pos != std::string::npos) {
737 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
738 }
739 }
740
741 std::string out_dex_path(options_.export_dex_location_);
742 if (out_dex_path.back() != '/') {
743 out_dex_path.append("/");
744 }
745 out_dex_path.append(dex_orig_name);
746 out_dex_path.append("_export.dex");
747 if (out_dex_path.length() > PATH_MAX) {
748 return false;
749 }
750
751 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
752 if (file.get() == nullptr) {
753 os << "Failed to open output dex file " << out_dex_path;
754 return false;
755 }
756
757 if (!file->WriteFully(dex_file->Begin(), fsize)) {
758 os << "Failed to write dex file";
759 file->Erase();
760 return false;
761 }
762
763 if (file->FlushCloseOrErase() != 0) {
764 os << "Flush and close failed";
765 return false;
766 }
767
768 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
769 os << std::flush;
770
771 return true;
772 }
773
Elliott Hughese3c845c2012-02-28 17:23:01 -0800774 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700775 while (it.HasNextStaticField()) {
776 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700777 }
Ian Rogers0571d352011-11-03 19:51:38 -0700778 while (it.HasNextInstanceField()) {
779 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700780 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800781 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700782
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100783 bool DumpOatClass(VariableIndentationOutputStream* vios,
784 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800785 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700786 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800787 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700788 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700789 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100790 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700791 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800792 }
793 ClassDataItemIterator it(dex_file, class_data);
794 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700795 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700796 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100797 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700798 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800799 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700800 success = false;
801 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800802 if (addr_found) {
803 *stop_analysis = true;
804 return success;
805 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700806 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700807 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700808 }
Ian Rogers0571d352011-11-03 19:51:38 -0700809 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100810 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700811 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800812 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700813 success = false;
814 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800815 if (addr_found) {
816 *stop_analysis = true;
817 return success;
818 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700819 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700820 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700821 }
Ian Rogers0571d352011-11-03 19:51:38 -0700822 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100823 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700824 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700825 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800826
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700827 static constexpr uint32_t kPrologueBytes = 16;
828
829 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
830 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
831
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100832 bool DumpOatMethod(VariableIndentationOutputStream* vios,
833 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700834 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700835 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800836 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800837 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700838 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800839
840 // TODO: Support regex
841 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
842 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000843 return success;
844 }
845
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800846 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100847 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
848 class_method_index, pretty_method.c_str(),
849 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800850 if (options_.list_methods_) return success;
851
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800852 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
853 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
854 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
855 uint32_t code_offset = oat_method.GetCodeOffset();
856 uint32_t code_size = oat_method.GetQuickCodeSize();
857 if (resolved_addr2instr_ != 0) {
858 if (resolved_addr2instr_ > code_offset + code_size) {
859 return success;
860 } else {
861 *addr_found = true; // stop analyzing file at next iteration
862 }
863 }
864
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100865 // Everything below is indented at least once.
866 ScopedIndentation indent1(vios);
867
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800868 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100869 vios->Stream() << "DEX CODE:\n";
870 ScopedIndentation indent2(vios);
871 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700872 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700873
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700874 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700875 std::unique_ptr<verifier::MethodVerifier> verifier;
876 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700877 // We need to have the handle scope stay live until after the verifier since the verifier has
878 // a handle to the dex cache from hs.
879 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100880 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
881 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700882 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100883 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700884 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700885 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800886 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100887 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800888 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100889 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100890 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100891 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700892 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100893 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700894 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
895 oat_method_offsets_offset, oat_file_.Size());
896 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100897 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700898 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800899 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700900
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100901 ScopedIndentation indent2(vios);
902 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
904 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100905 vios->Stream() << StringPrintf("WARNING: "
906 "code offset 0x%08x is past end of file 0x%08zx.\n",
907 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 success = false;
909 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100910 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700911
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100912 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800913 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100914 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800916 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100917 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700918 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100919 vios->Stream() << StringPrintf("WARNING: "
920 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
921 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700922 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800923 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100924 ScopedIndentation indent3(vios);
925 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800926 }
927 }
928 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100929 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
931 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700932
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800933 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100934 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700935 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100936 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700937 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100938 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700939 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
940 method_header_offset, oat_file_.Size());
941 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100942 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700943 return false;
944 }
945
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100946 ScopedIndentation indent2(vios);
947 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800948 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100949 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700950 }
951 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100952 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700953 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100954 vios->Stream() << StringPrintf("WARNING: "
955 "mapping table offset 0x%08x is past end of file 0x%08zx. "
956 "mapping table offset was loaded from offset 0x%08x.\n",
957 mapping_table_offset, oat_file_.Size(),
958 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700959 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800960 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100961 ScopedIndentation indent3(vios);
962 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700963 }
964
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100965 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800966 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100967 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700968 }
969 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100970 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700971 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100972 vios->Stream() << StringPrintf("WARNING: "
973 "vmap table offset 0x%08x is past end of file 0x%08zx. "
974 "vmap table offset was loaded from offset 0x%08x.\n",
975 vmap_table_offset, oat_file_.Size(),
976 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700977 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800978 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100979 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700980 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800981 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700982 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100983 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700984
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100985 ScopedIndentation indent2(vios);
986 vios->Stream()
987 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
988 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
989 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
990 vios->Stream() << "\n";
991 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
992 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
993 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700994 }
995 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100996 // Based on spill masks from QuickMethodFrameInfo so placed
997 // after it is dumped, but useful for understanding quick
998 // code, so dumped here.
999 ScopedIndentation indent2(vios);
1000 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001001 }
1002 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001003 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001004 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1005 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001006 ScopedIndentation indent2(vios);
1007 vios->Stream() << StringPrintf("WARNING: "
1008 "code size offset 0x%08x is past end of file 0x%08zx.",
1009 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001010 success = false;
1011 } else {
1012 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001013 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1014 uint64_t aligned_code_end = aligned_code_begin + code_size;
1015
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001016 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001017 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001018 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001019 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1020 code_offset,
1021 code_size_offset,
1022 code_size,
1023 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001024
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001025 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001026 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001027 vios->Stream() << StringPrintf("WARNING: "
1028 "start of code at 0x%08x is past end of file 0x%08zx.",
1029 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001030 success = false;
1031 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001032 vios->Stream() << StringPrintf(
1033 "WARNING: "
1034 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1035 "code size is 0x%08x loaded from offset 0x%08x.\n",
1036 aligned_code_end, oat_file_.Size(),
1037 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001038 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001039 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001040 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001041 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001042 }
1043 }
1044 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001045 vios->Stream() << StringPrintf(
1046 "WARNING: "
1047 "code size %d is bigger than max expected threshold of %d. "
1048 "code size is 0x%08x loaded from offset 0x%08x.\n",
1049 code_size, kMaxCodeSize,
1050 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001051 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001052 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001053 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001054 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001055 }
1056 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001057 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001058 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001059 }
1060 }
1061 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001062 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001063 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001064 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001065
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001066 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1067 if (spill_mask == 0) {
1068 return;
1069 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001070 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001071 for (size_t i = 0; i < 32; i++) {
1072 if ((spill_mask & (1 << i)) != 0) {
1073 if (is_float) {
1074 os << "fr" << i;
1075 } else {
1076 os << "r" << i;
1077 }
1078 spill_mask ^= 1 << i; // clear bit
1079 if (spill_mask != 0) {
1080 os << ", ";
1081 } else {
1082 break;
1083 }
1084 }
1085 }
1086 os << ")";
1087 }
1088
Roland Levillain442b46a2015-02-18 16:54:21 +00001089 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001090 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001091 const OatFile::OatMethod& oat_method,
1092 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001093 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1094 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001095 const void* raw_code_info = oat_method.GetVmapTable();
1096 if (raw_code_info != nullptr) {
1097 CodeInfo code_info(raw_code_info);
1098 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001099 ScopedIndentation indent1(vios);
1100 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001101 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001102 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1103 // We don't encode the size in the table, so just emit that we have quickened
1104 // information.
1105 ScopedIndentation indent(vios);
1106 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001107 } else {
1108 // Otherwise, display the vmap table.
1109 const uint8_t* raw_table = oat_method.GetVmapTable();
1110 if (raw_table != nullptr) {
1111 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001112 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001113 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001114 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001115 }
1116
1117 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001118 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001119 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001120 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001121 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001122 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001123 oat_method.GetCodeOffset(),
1124 code_item.registers_size_,
1125 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001126 }
1127
Roland Levillain442b46a2015-02-18 16:54:21 +00001128 // Display a vmap table.
1129 void DumpVmapTable(std::ostream& os,
1130 const OatFile::OatMethod& oat_method,
1131 const VmapTable& vmap_table) {
1132 bool first = true;
1133 bool processing_fp = false;
1134 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1135 for (size_t i = 0; i < vmap_table.Size(); i++) {
1136 uint16_t dex_reg = vmap_table[i];
1137 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1138 processing_fp ? kFloatVReg : kIntVReg);
1139 os << (first ? "v" : ", v") << dex_reg;
1140 if (!processing_fp) {
1141 os << "/r" << cpu_reg;
1142 } else {
1143 os << "/fr" << cpu_reg;
1144 }
1145 first = false;
1146 if (!processing_fp && dex_reg == 0xFFFF) {
1147 processing_fp = true;
1148 spill_mask = oat_method.GetFpSpillMask();
1149 }
1150 }
1151 os << "\n";
1152 }
1153
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001154 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1155 const DexFile::CodeItem* code_item) {
1156 if (code_item != nullptr) {
1157 size_t num_locals_ins = code_item->registers_size_;
1158 size_t num_ins = code_item->ins_size_;
1159 size_t num_locals = num_locals_ins - num_ins;
1160 size_t num_outs = code_item->outs_size_;
1161
1162 os << "vr_stack_locations:";
1163 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1164 // For readability, delimit the different kinds of VRs.
1165 if (reg == num_locals_ins) {
1166 os << "\n\tmethod*:";
1167 } else if (reg == num_locals && num_ins > 0) {
1168 os << "\n\tins:";
1169 } else if (reg == 0 && num_locals > 0) {
1170 os << "\n\tlocals:";
1171 }
1172
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001173 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1174 code_item,
1175 oat_method.GetCoreSpillMask(),
1176 oat_method.GetFpSpillMask(),
1177 oat_method.GetFrameSizeInBytes(),
1178 reg,
1179 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001180 os << " v" << reg << "[sp + #" << offset << "]";
1181 }
1182
1183 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1184 if (out_reg == 0) {
1185 os << "\n\touts:";
1186 }
1187
1188 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1189 os << " v" << out_reg << "[sp + #" << offset << "]";
1190 }
1191
1192 os << "\n";
1193 }
1194 }
1195
Ian Rogersb23a7722012-10-09 16:54:26 -07001196 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001197 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001198 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001199 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001200 const VmapTable vmap_table(raw_table);
1201 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001202 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001203 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1204 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1205 : oat_method.GetCoreSpillMask();
1206 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001207 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001208 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1209 code_item,
1210 oat_method.GetCoreSpillMask(),
1211 oat_method.GetFpSpillMask(),
1212 oat_method.GetFrameSizeInBytes(),
1213 reg,
1214 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001215 os << "[sp + #" << offset << "]";
1216 }
1217 }
1218 }
1219
Ian Rogersef7d42f2014-01-06 12:55:46 -08001220 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1221 const DexFile::CodeItem* code_item,
1222 size_t num_regs, const uint8_t* reg_bitmap) {
1223 bool first = true;
1224 for (size_t reg = 0; reg < num_regs; reg++) {
1225 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1226 if (first) {
1227 os << " v" << reg << " (";
1228 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1229 os << ")";
1230 first = false;
1231 } else {
1232 os << ", v" << reg << " (";
1233 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1234 os << ")";
1235 }
1236 }
1237 }
1238 if (first) {
1239 os << "No registers in GC map\n";
1240 } else {
1241 os << "\n";
1242 }
1243 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001244 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1245 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001246 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001247 if (gc_map_raw == nullptr) {
1248 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001249 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001250 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001251 NativePcOffsetToReferenceMap map(gc_map_raw);
1252 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1253 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1254 map.GetNativePcOffset(entry);
1255 os << StringPrintf("%p", native_pc);
1256 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001257 }
1258 }
1259
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001260 void DumpMappingTable(VariableIndentationOutputStream* vios,
1261 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001262 const void* quick_code = oat_method.GetQuickCode();
1263 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001264 return;
1265 }
Ian Rogers1809a722013-08-09 22:05:32 -07001266 MappingTable table(oat_method.GetMappingTable());
1267 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001268 if (table.PcToDexSize() != 0) {
1269 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001270 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001271 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001272 ScopedIndentation indent1(vios);
1273 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001274 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001275 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001276 }
1277 if (table.DexToPcSize() != 0) {
1278 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001279 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001280 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001281 ScopedIndentation indent1(vios);
1282 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001283 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001284 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001285 }
1286 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001287 }
1288
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001289 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001290 const OatFile::OatMethod& oat_method,
1291 const DexFile::CodeItem* code_item,
1292 size_t offset,
1293 bool suspend_point_mapping) {
1294 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1295 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001296 ScopedIndentation indent1(vios);
1297 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001298 }
1299 // The return value is not used in the case of a method compiled
1300 // with the optimizing compiler.
1301 return DexFile::kDexNoIndex;
1302 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001303 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001304 }
1305 }
1306
Ian Rogers1809a722013-08-09 22:05:32 -07001307 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1308 size_t offset, bool suspend_point_mapping) {
1309 MappingTable table(oat_method.GetMappingTable());
1310 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1311 typedef MappingTable::PcToDexIterator It;
1312 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1313 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001314 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001315 return cur.DexPc();
1316 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001317 }
Ian Rogers1809a722013-08-09 22:05:32 -07001318 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1319 typedef MappingTable::DexToPcIterator It;
1320 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1321 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001322 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001323 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001324 }
1325 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001326 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001327 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001328 }
1329
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001330 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1331 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001332 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001333 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001334 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001335 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001336 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001337 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001338 bool first = true;
1339 for (size_t reg = 0; reg < num_regs; reg++) {
1340 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1341 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001342 os << "GC map objects: v" << reg << " (";
1343 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001344 os << ")";
1345 first = false;
1346 } else {
1347 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001348 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001349 os << ")";
1350 }
1351 }
1352 }
1353 if (!first) {
1354 os << "\n";
1355 }
1356 }
1357 }
1358 }
1359
Mathieu Chartier590fee92013-09-13 13:46:47 -07001360 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1361 const OatFile::OatMethod& oat_method,
1362 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1363 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001364 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001365 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001366 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1367 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1368 if (kind != kUndefined) {
1369 if (first) {
1370 os << "VRegs: v";
1371 first = false;
1372 } else {
1373 os << ", v";
1374 }
1375 os << reg << " (";
1376 switch (kind) {
1377 case kImpreciseConstant:
1378 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1379 DescribeVReg(os, oat_method, code_item, reg, kind);
1380 break;
1381 case kConstant:
1382 os << "Constant: " << kinds.at((reg * 2) + 1);
1383 break;
1384 default:
1385 DescribeVReg(os, oat_method, code_item, reg, kind);
1386 break;
1387 }
1388 os << ")";
1389 }
1390 }
1391 if (!first) {
1392 os << "\n";
1393 }
1394 }
1395
1396
Ian Rogersb23a7722012-10-09 16:54:26 -07001397 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001398 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001399 size_t i = 0;
1400 while (i < code_item->insns_size_in_code_units_) {
1401 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001402 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1403 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001404 i += instruction->SizeInCodeUnits();
1405 }
1406 }
1407 }
1408
Roland Levillainf2650d12015-05-28 14:53:28 +01001409 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1410 // the optimizing compiler?
1411 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1412 const DexFile::CodeItem* code_item) {
1413 // If the native GC map is null and the Dex `code_item` is not
1414 // null, then this method has been compiled with the optimizing
1415 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001416 return oat_method.GetQuickCode() != nullptr &&
1417 oat_method.GetGcMap() == nullptr &&
1418 code_item != nullptr;
1419 }
1420
1421 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1422 // the dextodex compiler?
1423 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1424 const DexFile::CodeItem* code_item) {
1425 // If the quick code is null, the Dex `code_item` is not
1426 // null, and the vmap table is not null, then this method has been compiled
1427 // with the dextodex compiler.
1428 return oat_method.GetQuickCode() == nullptr &&
1429 oat_method.GetVmapTable() != nullptr &&
1430 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001431 }
1432
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001433 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001434 const OatFile::OatMethod& oat_method,
1435 const DexFile::CodeItem* code_item,
1436 size_t offset) {
1437 // This method is only relevant for oat methods compiled with the
1438 // optimizing compiler.
1439 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1440
1441 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1442 const void* raw_code_info = oat_method.GetVmapTable();
1443 if (raw_code_info != nullptr) {
1444 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001445 StackMapEncoding encoding = code_info.ExtractEncoding();
1446 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001447 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1449 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001450 }
1451 }
1452 }
1453
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001454 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001455 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001456 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001457 const DexFile* dex_file,
1458 const DexFile::ClassDef& class_def,
1459 const DexFile::CodeItem* code_item,
1460 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 if ((method_access_flags & kAccNative) == 0) {
1462 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001463 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001464 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001465 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1466 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001467 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001468 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001469 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1470 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001471 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001472
1473 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001474 }
1475
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001476 void DumpCode(VariableIndentationOutputStream* vios,
1477 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001478 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1479 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001480 const void* quick_code = oat_method.GetQuickCode();
1481
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001482 if (code_size == 0) {
1483 code_size = oat_method.GetQuickCodeSize();
1484 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001485 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001486 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001487 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001488 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001489 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1490 size_t offset = 0;
1491 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001492 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001493 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001494 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001495 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001496 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001497 uint32_t dex_pc =
1498 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001499 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001500 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001501 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001502 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001503 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001504 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001505 }
1506 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001507 }
1508 }
1509
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001510 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001511 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001512 const OatDumperOptions& options_;
1513 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001514 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001515 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001516 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001517};
1518
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001519class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001520 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001521 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1522 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001523 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001524 vios_(os),
1525 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001526 image_space_(image_space),
1527 image_header_(image_header),
1528 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001529
Mathieu Chartier90443472015-07-16 20:32:27 -07001530 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001532 std::ostream& indent_os = vios_.Stream();
1533
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001534 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001535
Jeff Haodcdc85b2015-12-04 14:06:18 -08001536 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1537
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001538 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001539
Mathieu Chartiere401d142015-04-22 13:56:20 -07001540 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1541
1542 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1543 auto section = static_cast<ImageHeader::ImageSections>(i);
1544 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1545 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001546
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001547 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001548
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001549 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001550
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001551 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1552
1553 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1554
1555 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001556
Alex Lighta59dd802014-07-02 16:28:08 -07001557 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1558
Igor Murashkin46774762014-10-22 11:37:02 -07001559 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1560
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001561 {
1562 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001563 static_assert(arraysize(image_roots_descriptions_) ==
1564 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1566 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1567 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001568 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001569 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001570 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001571 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001572 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001573 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001574 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1575 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001576 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001577 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1578 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001579 run++;
1580 } else {
1581 break;
1582 }
1583 }
1584 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001585 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001586 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001587 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001588 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001589 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001590 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001591 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001593 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001594 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001595 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001596 }
1597 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001598 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001599
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001600 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001601 os << "METHOD ROOTS\n";
1602 static_assert(arraysize(image_methods_descriptions_) ==
1603 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1604 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1605 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1606 const char* description = image_methods_descriptions_[i];
1607 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001608 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001609 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001610 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001612
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001613 Runtime* const runtime = Runtime::Current();
1614 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001615 std::string image_filename = image_space_.GetImageFilename();
1616 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001617 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001619 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001620 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1621 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001622 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001623 oat_file = OatFile::Open(oat_location, oat_location,
1624 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001625 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001626 if (oat_file == nullptr) {
1627 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001628 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001629 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001630 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001631 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001632
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001633 stats_.oat_file_bytes = oat_file->Size();
1634
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001635 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001636
Mathieu Chartier02e25112013-08-14 16:14:24 -07001637 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001638 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001639 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1640 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001641 }
1642
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001643 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001644
Jeff Haodcdc85b2015-12-04 14:06:18 -08001645 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001646 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001647 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001648 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001649 {
1650 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1651 heap->FlushAllocStack();
1652 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001653 // Since FlushAllocStack() above resets the (active) allocation
1654 // stack. Need to revoke the thread-local allocation stacks that
1655 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001656 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001657 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001658 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001659 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001660 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001661 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001662 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001663 {
1664 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001665 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001666 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001667 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001668 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001669 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001670 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001671 }
1672 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001673 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001674 // Dump the normal objects before ArtMethods.
1675 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1676 indent_os << "\n";
1677 // TODO: Dump fields.
1678 // Dump methods after.
1679 const auto& methods_section = image_header_.GetMethodsSection();
1680 const size_t pointer_size =
1681 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1682 DumpArtMethodVisitor visitor(this);
1683 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001684 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001685 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001687 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001688 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001689 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001690 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001691 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001692 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001693 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001694 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001695 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001696 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001697 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001698 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1699 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001700 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1701 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001702 const auto& intern_section = image_header_.GetImageSection(
1703 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001704 const auto& class_table_section = image_header_.GetImageSection(
1705 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001706 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1707
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001708 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001709
1710 // Objects are kObjectAlignment-aligned.
1711 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1712 if (object_section.Offset() > header_bytes) {
1713 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1714 }
1715
1716 // Field section is 4-byte aligned.
1717 constexpr size_t kFieldSectionAlignment = 4U;
1718 uint32_t end_objects = object_section.Offset() + object_section.Size();
1719 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1720 stats_.alignment_bytes += field_section.Offset() - end_objects;
1721
1722 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1723 uint32_t end_fields = field_section.Offset() + field_section.Size();
1724 CHECK_ALIGNED(method_section.Offset(), 4);
1725 stats_.alignment_bytes += method_section.Offset() - end_fields;
1726
1727 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1728 uint32_t end_methods = method_section.Offset() + method_section.Size();
1729 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1730 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1731
1732 // Intern table is 8-byte aligned.
1733 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1734 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1735 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1736
1737 // Add space between intern table and class table.
1738 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1739 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1740
1741 // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1742 uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1743 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1744 stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1745
Mathieu Chartiere401d142015-04-22 13:56:20 -07001746 stats_.bitmap_bytes += bitmap_section.Size();
1747 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001748 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001749 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001750 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001751 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001752 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001753 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001754
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001755 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001756
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001757 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001758 }
1759
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001760 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001761 class DumpArtMethodVisitor : public ArtMethodVisitor {
1762 public:
1763 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1764
1765 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1766 std::ostream& indent_os = image_dumper_->vios_.Stream();
1767 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1768 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1769 indent_os << "\n";
1770 }
1771
1772 private:
1773 ImageDumper* const image_dumper_;
1774 };
1775
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001776 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001777 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001778 CHECK(type != nullptr);
1779 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001780 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001781 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001782 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001783 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001784 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001785 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001786 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001787 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001788 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001789 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001790 }
1791 }
1792
Mathieu Chartierc7853442015-03-27 14:35:38 -07001793 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001794 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001795 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001796 switch (field->GetTypeAsPrimitiveType()) {
1797 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001798 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001799 break;
1800 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001802 break;
1803 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001804 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001805 break;
1806 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001807 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001808 break;
1809 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001810 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001811 break;
1812 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001813 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001814 break;
1815 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001816 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1817 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001818 break;
1819 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001820 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001821 break;
1822 case Primitive::kPrimNot: {
1823 // Get the value, don't compute the type unless it is non-null as we don't want
1824 // to cause class loading.
1825 mirror::Object* value = field->GetObj(obj);
1826 if (value == nullptr) {
1827 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001828 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001829 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001830 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001831 if (field_type != nullptr) {
1832 PrettyObjectValue(os, field_type, value);
1833 } else {
1834 os << StringPrintf("%p %s\n", value,
1835 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1836 }
Ian Rogers50239c72013-06-17 14:53:22 -07001837 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001838 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001839 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001840 default:
1841 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1842 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001843 }
1844 }
1845
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001846 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001847 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001848 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001849 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001850 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001851 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001852 for (ArtField& field : klass->GetIFields()) {
1853 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001854 }
1855 }
1856
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001857 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001858 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001859 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001860
Mathieu Chartiere401d142015-04-22 13:56:20 -07001861 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001862 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001863 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1864 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001865 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001866 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001867 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001868 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001869 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001870 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001871 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001872 }
1873
Mathieu Chartiere401d142015-04-22 13:56:20 -07001874 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001875 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001876 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1877 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001878 return 0;
1879 }
1880 return oat_code_begin[-1];
1881 }
1882
Mathieu Chartiere401d142015-04-22 13:56:20 -07001883 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001884 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001885 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001886 if (oat_code_begin == nullptr) {
1887 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001888 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001889 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001890 }
1891
Mathieu Chartier90443472015-07-16 20:32:27 -07001892 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001893 DCHECK(obj != nullptr);
1894 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001895 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001896 if (!state->InDumpSpace(obj)) {
1897 return;
1898 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001899
1900 size_t object_bytes = obj->SizeOf();
1901 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1902 state->stats_.object_bytes += object_bytes;
1903 state->stats_.alignment_bytes += alignment_bytes;
1904
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001905 std::ostream& os = state->vios_.Stream();
1906
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001907 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001908 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001909 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1910 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001911 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001912 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001913 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1914 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001915 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001916 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001917 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001918 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001920 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001921 ScopedIndentation indent1(&state->vios_);
1922 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001923 const auto image_pointer_size =
1924 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001925 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001926 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1927 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001928 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001929 size_t run = 0;
1930 for (int32_t j = i + 1; j < length; j++) {
1931 if (value == obj_array->Get(j)) {
1932 run++;
1933 } else {
1934 break;
1935 }
1936 }
1937 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001938 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001939 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001940 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001941 i = i + run;
1942 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001943 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001944 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001945 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001946 }
1947 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001948 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001949 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001950 os << "STATICS:\n";
1951 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001952 for (ArtField& field : klass->GetSFields()) {
1953 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001954 }
1955 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001956 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001957 auto it = state->dex_caches_.find(obj);
1958 if (it != state->dex_caches_.end()) {
1959 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001960 const auto& field_section = state->image_header_.GetImageSection(
1961 ImageHeader::kSectionArtFields);
1962 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001963 size_t num_methods = dex_cache->NumResolvedMethods();
1964 if (num_methods != 0u) {
1965 os << "Methods (size=" << num_methods << "):";
1966 ScopedIndentation indent2(&state->vios_);
1967 auto* resolved_methods = dex_cache->GetResolvedMethods();
1968 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1969 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1970 size_t run = 0;
1971 for (size_t j = i + 1;
1972 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1973 j,
1974 image_pointer_size);
1975 ++j, ++run) {}
1976 if (run == 0) {
1977 os << StringPrintf("%zd: ", i);
1978 } else {
1979 os << StringPrintf("%zd to %zd: ", i, i + run);
1980 i = i + run;
1981 }
1982 std::string msg;
1983 if (elem == nullptr) {
1984 msg = "null";
1985 } else if (method_section.Contains(
1986 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1987 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1988 } else {
1989 msg = "<not in method section>";
1990 }
1991 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001992 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001993 }
1994 size_t num_fields = dex_cache->NumResolvedFields();
1995 if (num_fields != 0u) {
1996 os << "Fields (size=" << num_fields << "):";
1997 ScopedIndentation indent2(&state->vios_);
1998 auto* resolved_fields = dex_cache->GetResolvedFields();
1999 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
2000 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
2001 size_t run = 0;
2002 for (size_t j = i + 1;
2003 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
2004 j,
2005 image_pointer_size);
2006 ++j, ++run) {}
2007 if (run == 0) {
2008 os << StringPrintf("%zd: ", i);
2009 } else {
2010 os << StringPrintf("%zd to %zd: ", i, i + run);
2011 i = i + run;
2012 }
2013 std::string msg;
2014 if (elem == nullptr) {
2015 msg = "null";
2016 } else if (field_section.Contains(
2017 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2018 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
2019 } else {
2020 msg = "<not in field section>";
2021 }
2022 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002023 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002024 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002025 }
2026 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002027 std::string temp;
2028 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002029 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002030
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002032 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002033 DCHECK(method != nullptr);
2034 const auto image_pointer_size =
2035 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002036 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2037 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002038 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2039 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002040 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002041 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2042 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2043 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2044 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002045 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002046 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002047 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048 if (first_occurrence) {
2049 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2050 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002051 if (quick_oat_code_begin !=
2052 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
2053 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002054 }
2055 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2056 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2057 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058 } else {
2059 const DexFile::CodeItem* code_item = method->GetCodeItem();
2060 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2061 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2062
2063 bool first_occurrence;
2064 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002065 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002066 if (first_occurrence) {
2067 state->stats_.gc_map_bytes += gc_map_bytes;
2068 }
2069
2070 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002071 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002072 if (first_occurrence) {
2073 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2074 }
2075
Roland Levillain6d7f1792015-07-02 10:59:15 +01002076 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002077 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002078 // Method compiled with the optimizing compiler have no vmap table.
2079 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002080 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002081 if (first_occurrence) {
2082 state->stats_.vmap_table_bytes += vmap_table_bytes;
2083 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002084 }
2085
Mathieu Chartiere401d142015-04-22 13:56:20 -07002086 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2087 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2088 if (first_occurrence) {
2089 state->stats_.managed_code_bytes += quick_oat_code_size;
2090 if (method->IsConstructor()) {
2091 if (method->IsStatic()) {
2092 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2093 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2094 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2095 }
2096 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2097 state->stats_.large_method_code_bytes += quick_oat_code_size;
2098 }
2099 }
2100 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2101
Igor Murashkin7617abd2015-07-10 18:27:47 -07002102 uint32_t method_access_flags = method->GetAccessFlags();
2103
Mathieu Chartiere401d142015-04-22 13:56:20 -07002104 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002105 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2106 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2107 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002108
2109 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002110 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002111
2112 double expansion =
2113 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2114 state->stats_.ComputeOutliers(total_size, expansion, method);
2115 }
2116 }
2117
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002118 std::set<const void*> already_seen_;
2119 // Compute the size of the given data within the oat file and whether this is the first time
2120 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002121 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002122 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002123 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002124 already_seen_.insert(oat_data);
2125 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002126 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002127 }
2128 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002129 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002130
2131 public:
2132 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002133 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002134 size_t file_bytes;
2135
2136 size_t header_bytes;
2137 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002138 size_t art_field_bytes;
2139 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002140 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002141 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002142 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002143 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002144 size_t alignment_bytes;
2145
2146 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002147 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002148 size_t managed_to_native_code_bytes;
2149 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002150 size_t class_initializer_code_bytes;
2151 size_t large_initializer_code_bytes;
2152 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002153
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002154 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002155 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002156 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002157
2158 size_t dex_instruction_bytes;
2159
Mathieu Chartiere401d142015-04-22 13:56:20 -07002160 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002161 std::vector<size_t> method_outlier_size;
2162 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002163 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002164
Roland Levillain3887c462015-08-12 18:15:42 +01002165 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002166 : oat_file_bytes(0),
2167 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002168 header_bytes(0),
2169 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002170 art_field_bytes(0),
2171 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002172 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002173 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002174 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002175 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002176 alignment_bytes(0),
2177 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002178 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002179 managed_to_native_code_bytes(0),
2180 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002181 class_initializer_code_bytes(0),
2182 large_initializer_code_bytes(0),
2183 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002184 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002185 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002186 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002187 dex_instruction_bytes(0) {}
2188
Elliott Hughesa0e18062012-04-13 15:59:59 -07002189 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002190 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002191 size_t bytes;
2192 size_t count;
2193 };
2194 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2195 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002196
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002197 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002198 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2199 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002200 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002201 it->second.count += 1;
2202 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002203 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002204 }
2205 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002206
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002207 double PercentOfOatBytes(size_t size) {
2208 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2209 }
2210
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002211 double PercentOfFileBytes(size_t size) {
2212 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2213 }
2214
2215 double PercentOfObjectBytes(size_t size) {
2216 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2217 }
2218
Mathieu Chartiere401d142015-04-22 13:56:20 -07002219 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002220 method_outlier_size.push_back(total_size);
2221 method_outlier_expansion.push_back(expansion);
2222 method_outlier.push_back(method);
2223 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002224
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002225 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002226 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002227 size_t sum_of_sizes = 0;
2228 size_t sum_of_sizes_squared = 0;
2229 size_t sum_of_expansion = 0;
2230 size_t sum_of_expansion_squared = 0;
2231 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002232 if (n == 0) {
2233 return;
2234 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002235 for (size_t i = 0; i < n; i++) {
2236 size_t cur_size = method_outlier_size[i];
2237 sum_of_sizes += cur_size;
2238 sum_of_sizes_squared += cur_size * cur_size;
2239 double cur_expansion = method_outlier_expansion[i];
2240 sum_of_expansion += cur_expansion;
2241 sum_of_expansion_squared += cur_expansion * cur_expansion;
2242 }
2243 size_t size_mean = sum_of_sizes / n;
2244 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2245 double expansion_mean = sum_of_expansion / n;
2246 double expansion_variance =
2247 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2248
2249 // Dump methods whose size is a certain number of standard deviations from the mean
2250 size_t dumped_values = 0;
2251 size_t skipped_values = 0;
2252 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2253 size_t cur_size_variance = i * i * size_variance;
2254 bool first = true;
2255 for (size_t j = 0; j < n; j++) {
2256 size_t cur_size = method_outlier_size[j];
2257 if (cur_size > size_mean) {
2258 size_t cur_var = cur_size - size_mean;
2259 cur_var = cur_var * cur_var;
2260 if (cur_var > cur_size_variance) {
2261 if (dumped_values > 20) {
2262 if (i == 1) {
2263 skipped_values++;
2264 } else {
2265 i = 2; // jump to counting for 1 standard deviation
2266 break;
2267 }
2268 } else {
2269 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002270 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002271 first = false;
2272 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002274 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002275 method_outlier_size[j] = 0; // don't consider this method again
2276 dumped_values++;
2277 }
2278 }
2279 }
2280 }
2281 }
2282 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002283 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002284 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002285 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002286 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002287
2288 // Dump methods whose expansion is a certain number of standard deviations from the mean
2289 dumped_values = 0;
2290 skipped_values = 0;
2291 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2292 double cur_expansion_variance = i * i * expansion_variance;
2293 bool first = true;
2294 for (size_t j = 0; j < n; j++) {
2295 double cur_expansion = method_outlier_expansion[j];
2296 if (cur_expansion > expansion_mean) {
2297 size_t cur_var = cur_expansion - expansion_mean;
2298 cur_var = cur_var * cur_var;
2299 if (cur_var > cur_expansion_variance) {
2300 if (dumped_values > 20) {
2301 if (i == 1) {
2302 skipped_values++;
2303 } else {
2304 i = 2; // jump to counting for 1 standard deviation
2305 break;
2306 }
2307 } else {
2308 if (first) {
2309 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002310 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002311 first = false;
2312 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002314 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002315 method_outlier_expansion[j] = 0.0; // don't consider this method again
2316 dumped_values++;
2317 }
2318 }
2319 }
2320 }
2321 }
2322 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002324 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002325 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002326 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002327 }
2328
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002329 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002330 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002331 {
2332 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2333 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002334 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2335 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2336 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2337 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2338 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2339 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002340 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002341 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2342 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 header_bytes, PercentOfFileBytes(header_bytes),
2344 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002345 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2346 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002347 dex_cache_arrays_bytes,
2348 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002349 interned_strings_bytes,
2350 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002351 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002352 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002353 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2354 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002355 CHECK_EQ(file_bytes,
2356 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2357 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2358 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002360
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002361 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002362 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002363 for (const auto& sizes_and_count : sizes_and_counts) {
2364 const std::string& descriptor(sizes_and_count.first);
2365 double average = static_cast<double>(sizes_and_count.second.bytes) /
2366 static_cast<double>(sizes_and_count.second.count);
2367 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002369 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002370 descriptor.c_str(), sizes_and_count.second.bytes,
2371 sizes_and_count.second.count, average, percent);
2372 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002373 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002374 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002375 CHECK_EQ(object_bytes, object_bytes_total);
2376
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002377 os << StringPrintf("oat_file_bytes = %8zd\n"
2378 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2379 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2380 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2381 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2382 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2383 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002384 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002385 managed_code_bytes,
2386 PercentOfOatBytes(managed_code_bytes),
2387 managed_to_native_code_bytes,
2388 PercentOfOatBytes(managed_to_native_code_bytes),
2389 native_to_managed_code_bytes,
2390 PercentOfOatBytes(native_to_managed_code_bytes),
2391 class_initializer_code_bytes,
2392 PercentOfOatBytes(class_initializer_code_bytes),
2393 large_initializer_code_bytes,
2394 PercentOfOatBytes(large_initializer_code_bytes),
2395 large_method_code_bytes,
2396 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002397 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002398 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002399 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002400 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2401 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002402 }
2403
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002404 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2405 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2406 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002407 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2408 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2409 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002410 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002411
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002412 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2413 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002414 static_cast<double>(managed_code_bytes) /
2415 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002416 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002417 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002418 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002419
2420 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002421 }
2422 } stats_;
2423
2424 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002425 enum {
2426 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2427 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2428 kLargeConstructorDexBytes = 4000,
2429 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2430 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2431 kLargeMethodDexBytes = 16000
2432 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002433
2434 // For performance, use the *os_ directly for anything that doesn't need indentation
2435 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002436 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002437 VariableIndentationOutputStream vios_;
2438 ScopedIndentation indent1_;
2439
Ian Rogers1d54e732013-05-02 21:10:01 -07002440 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002441 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002442 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002443 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002444 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002445
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002446 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002447};
2448
Jeff Haodcdc85b2015-12-04 14:06:18 -08002449static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002450 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002451 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002452 options->class_loader_ = &null_class_loader;
2453
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002454 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002455 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002456 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2457 CHECK(!image_spaces.empty());
2458 for (gc::space::ImageSpace* image_space : image_spaces) {
2459 const ImageHeader& image_header = image_space->GetImageHeader();
2460 if (!image_header.IsValid()) {
2461 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2462 return EXIT_FAILURE;
2463 }
2464
2465 ImageDumper image_dumper(os, *image_space, image_header, options);
2466 if (!image_dumper.Dump()) {
2467 return EXIT_FAILURE;
2468 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002469 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002470 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002471}
2472
Andreas Gampe00b25f32014-09-17 21:49:05 -07002473static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2474 std::ostream* os) {
2475 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2476
2477 Thread* self = Thread::Current();
2478 CHECK(self != nullptr);
2479 // Need well-known-classes.
2480 WellKnownClasses::Init(self->GetJniEnv());
2481
2482 // Need to register dex files to get a working dex cache.
2483 ScopedObjectAccess soa(self);
2484 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002485 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002486 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002487 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2488 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002489 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002490 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002491 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002492 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002493 }
2494
2495 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002496 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002497 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002498
2499 // Use the class loader while dumping.
2500 StackHandleScope<1> scope(self);
2501 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2502 soa.Decode<mirror::ClassLoader*>(class_loader));
2503 options->class_loader_ = &loader_handle;
2504
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002505 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002506 bool success = oat_dumper.Dump(*os);
2507 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2508}
2509
2510static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002511 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002512 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002513 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002514 options->class_loader_ = &null_class_loader;
2515
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002516 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002517 bool success = oat_dumper.Dump(*os);
2518 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2519}
2520
2521static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2522 std::ostream* os) {
2523 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002524 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002525 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002526 if (oat_file == nullptr) {
2527 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2528 return EXIT_FAILURE;
2529 }
2530
2531 if (runtime != nullptr) {
2532 return DumpOatWithRuntime(runtime, oat_file, options, os);
2533 } else {
2534 return DumpOatWithoutRuntime(oat_file, options, os);
2535 }
2536}
2537
2538static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2539 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002540 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002541 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002542 if (oat_file == nullptr) {
2543 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2544 return EXIT_FAILURE;
2545 }
2546
2547 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002548 if (!oat_symbolizer.Symbolize()) {
2549 fprintf(stderr, "Failed to symbolize\n");
2550 return EXIT_FAILURE;
2551 }
2552
2553 return EXIT_SUCCESS;
2554}
2555
Igor Murashkin37743352014-11-13 14:38:00 -08002556struct OatdumpArgs : public CmdlineArgs {
2557 protected:
2558 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002559
Igor Murashkin37743352014-11-13 14:38:00 -08002560 virtual ParseStatus ParseCustom(const StringPiece& option,
2561 std::string* error_msg) OVERRIDE {
2562 {
2563 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2564 if (base_parse != kParseUnknownArgument) {
2565 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002566 }
2567 }
2568
Igor Murashkin37743352014-11-13 14:38:00 -08002569 if (option.starts_with("--oat-file=")) {
2570 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2571 } else if (option.starts_with("--image=")) {
2572 image_location_ = option.substr(strlen("--image=")).data();
2573 } else if (option =="--dump:raw_mapping_table") {
2574 dump_raw_mapping_table_ = true;
2575 } else if (option == "--dump:raw_gc_map") {
2576 dump_raw_gc_map_ = true;
2577 } else if (option == "--no-dump:vmap") {
2578 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002579 } else if (option =="--dump:code_info_stack_maps") {
2580 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002581 } else if (option == "--no-disassemble") {
2582 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002583 } else if (option =="--header-only") {
2584 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002585 } else if (option.starts_with("--symbolize=")) {
2586 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2587 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002588 } else if (option.starts_with("--class-filter=")) {
2589 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002590 } else if (option.starts_with("--method-filter=")) {
2591 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002592 } else if (option.starts_with("--list-classes")) {
2593 list_classes_ = true;
2594 } else if (option.starts_with("--list-methods")) {
2595 list_methods_ = true;
2596 } else if (option.starts_with("--export-dex-to=")) {
2597 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2598 } else if (option.starts_with("--addr2instr=")) {
2599 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2600 *error_msg = "Address conversion failed";
2601 return kParseError;
2602 }
Igor Murashkin37743352014-11-13 14:38:00 -08002603 } else {
2604 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002605 }
2606
Igor Murashkin37743352014-11-13 14:38:00 -08002607 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002608 }
2609
Igor Murashkin37743352014-11-13 14:38:00 -08002610 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2611 // Infer boot image location from the image location if possible.
2612 if (boot_image_location_ == nullptr) {
2613 boot_image_location_ = image_location_;
2614 }
2615
2616 // Perform the parent checks.
2617 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2618 if (parent_checks != kParseOk) {
2619 return parent_checks;
2620 }
2621
2622 // Perform our own checks.
2623 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2624 *error_msg = "Either --image or --oat-file must be specified";
2625 return kParseError;
2626 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2627 *error_msg = "Either --image or --oat-file must be specified but not both";
2628 return kParseError;
2629 }
2630
2631 return kParseOk;
2632 }
2633
2634 virtual std::string GetUsage() const {
2635 std::string usage;
2636
2637 usage +=
2638 "Usage: oatdump [options] ...\n"
2639 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2640 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2641 "\n"
2642 // Either oat-file or image is required.
2643 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2644 " Example: --oat-file=/system/framework/boot.oat\n"
2645 "\n"
2646 " --image=<file.art>: specifies an input image location.\n"
2647 " Example: --image=/system/framework/boot.art\n"
2648 "\n";
2649
2650 usage += Base::GetUsage();
2651
2652 usage += // Optional.
2653 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2654 " Example: --dump:raw_mapping_table\n"
2655 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002656 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002657 " Example: --dump:raw_gc_map\n"
2658 "\n"
2659 " --no-dump:vmap may be used to disable vmap dumping.\n"
2660 " Example: --no-dump:vmap\n"
2661 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002662 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2663 " Example: --dump:code_info_stack_maps\n"
2664 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002665 " --no-disassemble may be used to disable disassembly.\n"
2666 " Example: --no-disassemble\n"
2667 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002668 " --header-only may be used to print only the oat header.\n"
2669 " Example: --header-only\n"
2670 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002671 " --list-classes may be used to list target file classes (can be used with filters).\n"
2672 " Example: --list-classes\n"
2673 " Example: --list-classes --class-filter=com.example.foo\n"
2674 "\n"
2675 " --list-methods may be used to list target file methods (can be used with filters).\n"
2676 " Example: --list-methods\n"
2677 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2678 "\n"
2679 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2680 " Example: --symbolize=/system/framework/boot.oat\n"
2681 "\n"
2682 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2683 " Example: --class-filter=com.example.foo\n"
2684 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002685 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2686 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002687 "\n"
2688 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2689 " Example: --export-dex-to=/data/local/tmp\n"
2690 "\n"
2691 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2692 " address (e.g. PC from crash dump)\n"
2693 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002694 "\n";
2695
2696 return usage;
2697 }
2698
2699 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002700 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002701 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002702 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002703 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002704 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002705 bool dump_raw_mapping_table_ = false;
2706 bool dump_raw_gc_map_ = false;
2707 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002708 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002709 bool disassemble_code_ = true;
2710 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002711 bool list_classes_ = false;
2712 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002713 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002714 uint32_t addr2instr_ = 0;
2715 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002716};
2717
Igor Murashkin37743352014-11-13 14:38:00 -08002718struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2719 virtual bool NeedsRuntime() OVERRIDE {
2720 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002721
Igor Murashkin37743352014-11-13 14:38:00 -08002722 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2723 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2724
2725 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2726 args_->dump_raw_mapping_table_,
2727 args_->dump_raw_gc_map_,
2728 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002729 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002730 args_->disassemble_code_,
2731 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002732 args_->class_filter_,
2733 args_->method_filter_,
2734 args_->list_classes_,
2735 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002736 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002737 args_->export_dex_location_,
2738 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002739
2740 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2741 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002742 }
2743
Igor Murashkin37743352014-11-13 14:38:00 -08002744 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2745 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002746 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002747
Mathieu Chartierd424d082014-10-15 10:31:46 -07002748 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002749
Andreas Gampec24f3992014-12-17 20:40:11 -08002750 if (args_->symbolize_) {
2751 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2752 } 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
Jeff Haodcdc85b2015-12-04 14:06:18 -08002770 return DumpImage(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}