blob: 285803ceb4cebda7f1aeb15585d85d8b04a497b9 [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
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800101template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100102class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700103 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100104 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100105 oat_file_(oat_file), builder_(nullptr),
106 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700107 }
108
109 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
110 uint32_t,
111 const OatFile::OatMethod&,
112 const DexFile&,
113 uint32_t,
114 const DexFile::CodeItem*,
115 uint32_t);
116
117 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000118 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000119 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
120 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000121
122 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
123 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000124 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800125 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126
127 builder_->Start();
128
129 auto* rodata = builder_->GetRoData();
130 auto* text = builder_->GetText();
131 auto* bss = builder_->GetBss();
132 auto* strtab = builder_->GetStrTab();
133 auto* symtab = builder_->GetSymTab();
134
135 rodata->Start();
136 const uint8_t* rodata_begin = oat_file_->Begin();
137 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
138 rodata->WriteFully(rodata_begin, rodata_size);
139 rodata->End();
140
141 text->Start();
142 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
143 const size_t text_size = oat_file_->End() - text_begin;
144 text->WriteFully(text_begin, text_size);
145 text->End();
146
147 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000148 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149 }
150
Vladimir Marko944da602016-02-19 12:27:55 +0000151 builder_->PrepareDynamicSection(
152 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
153 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700154
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800155 Walk(&art::OatSymbolizer<ElfTypes>::RegisterForDedup);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700156
157 NormalizeState();
158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 strtab->Start();
160 strtab->Write(""); // strtab should start with empty string.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000161 AddTrampolineSymbols();
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800162 Walk(&art::OatSymbolizer<ElfTypes>::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000163 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700164
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165 symtab->Start();
166 symtab->Write();
167 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700168
David Srbecky6d8c8f02015-10-26 10:57:09 +0000169 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700170
Vladimir Marko10c13562015-11-25 14:33:36 +0000171 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172 }
173
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000174 void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
175 if (code_offset != 0) {
176 uint32_t name_offset = builder_->GetStrTab()->Write(name);
177 uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
Tamas Berghammer9e49ab12016-01-28 16:30:14 +0000178 // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
179 // the end of the section in case of the last symbol.
David Srbecky197160d2016-03-07 17:33:57 +0000180 builder_->GetSymTab()->Add(
181 name_offset,
182 builder_->GetText(),
183 builder_->GetText()->GetAddress() + symbol_value,
184 /* size */ 0,
185 STB_GLOBAL,
186 STT_FUNC);
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000187 }
188 }
189
190 void AddTrampolineSymbols() {
191 const OatHeader& oat_header = oat_file_->GetOatHeader();
192 AddTrampolineSymbol("interpreterToInterpreterBridge",
193 oat_header.GetInterpreterToInterpreterBridgeOffset());
194 AddTrampolineSymbol("interpreterToCompiledCodeBridge",
195 oat_header.GetInterpreterToCompiledCodeBridgeOffset());
196 AddTrampolineSymbol("jniDlsymLookup",
197 oat_header.GetJniDlsymLookupOffset());
198 AddTrampolineSymbol("quickGenericJniTrampoline",
199 oat_header.GetQuickGenericJniTrampolineOffset());
200 AddTrampolineSymbol("quickImtConflictTrampoline",
201 oat_header.GetQuickImtConflictTrampolineOffset());
202 AddTrampolineSymbol("quickResolutionTrampoline",
203 oat_header.GetQuickResolutionTrampolineOffset());
204 AddTrampolineSymbol("quickToInterpreterBridge",
205 oat_header.GetQuickToInterpreterBridgeOffset());
206 }
207
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700208 void Walk(Callback callback) {
209 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
210 for (size_t i = 0; i < oat_dex_files.size(); i++) {
211 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700212 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700213 WalkOatDexFile(oat_dex_file, callback);
214 }
215 }
216
217 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
218 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700219 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
220 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700221 return;
222 }
223 for (size_t class_def_index = 0;
224 class_def_index < dex_file->NumClassDefs();
225 class_def_index++) {
226 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
227 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
228 OatClassType type = oat_class.GetType();
229 switch (type) {
230 case kOatClassAllCompiled:
231 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700232 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700233 break;
234
235 case kOatClassNoneCompiled:
236 case kOatClassMax:
237 // Ignore.
238 break;
239 }
240 }
241 }
242
243 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
244 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700245 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700246 if (class_data == nullptr) { // empty class such as a marker interface?
247 return;
248 }
249 // Note: even if this is an interface or a native class, we still have to walk it, as there
250 // might be a static initializer.
251 ClassDataItemIterator it(dex_file, class_data);
252 SkipAllFields(&it);
253 uint32_t class_method_idx = 0;
254 while (it.HasNextDirectMethod()) {
255 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
256 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700257 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700258 class_method_idx++;
259 it.Next();
260 }
261 while (it.HasNextVirtualMethod()) {
262 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
263 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700264 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700265 class_method_idx++;
266 it.Next();
267 }
268 DCHECK(!it.HasNext());
269 }
270
271 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
272 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
273 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
274 uint32_t method_access_flags, Callback callback) {
275 if ((method_access_flags & kAccAbstract) != 0) {
276 // Abstract method, no code.
277 return;
278 }
279 if (oat_method.GetCodeOffset() == 0) {
280 // No code.
281 return;
282 }
283
284 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
285 method_access_flags);
286 }
287
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700288 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
289 uint32_t class_method_index ATTRIBUTE_UNUSED,
290 const OatFile::OatMethod& oat_method,
291 const DexFile& dex_file ATTRIBUTE_UNUSED,
292 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
293 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
294 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700295 state_[oat_method.GetCodeOffset()]++;
296 }
297
298 void NormalizeState() {
299 for (auto& x : state_) {
300 if (x.second == 1) {
301 state_[x.first] = 0;
302 }
303 }
304 }
305
306 enum class DedupState { // private
307 kNotDeduplicated,
308 kDeduplicatedFirst,
309 kDeduplicatedOther
310 };
311 DedupState IsDuplicated(uint32_t offset) {
312 if (state_[offset] == 0) {
313 return DedupState::kNotDeduplicated;
314 }
315 if (state_[offset] == 1) {
316 return DedupState::kDeduplicatedOther;
317 }
318 state_[offset] = 1;
319 return DedupState::kDeduplicatedFirst;
320 }
321
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700322 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
323 uint32_t class_method_index ATTRIBUTE_UNUSED,
324 const OatFile::OatMethod& oat_method,
325 const DexFile& dex_file,
326 uint32_t dex_method_idx,
327 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
328 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700329 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
330 if (dedup != DedupState::kDeduplicatedOther) {
331 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
332
333 if (dedup == DedupState::kDeduplicatedFirst) {
334 pretty_name = "[Dedup]" + pretty_name;
335 }
336
David Srbecky6d8c8f02015-10-26 10:57:09 +0000337 int name_offset = builder_->GetStrTab()->Write(pretty_name);
David Srbecky197160d2016-03-07 17:33:57 +0000338 uint64_t address = oat_method.GetCodeOffset() -
339 oat_file_->GetOatHeader().GetExecutableOffset() +
340 builder_->GetText()->GetAddress();
341 builder_->GetSymTab()->Add(name_offset,
342 builder_->GetText(),
343 address,
344 oat_method.GetQuickCodeSize(),
345 STB_GLOBAL,
346 STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700347 }
348 }
349
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700350 private:
351 static void SkipAllFields(ClassDataItemIterator* it) {
352 while (it->HasNextStaticField()) {
353 it->Next();
354 }
355 while (it->HasNextInstanceField()) {
356 it->Next();
357 }
358 }
359
360 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800361 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700362 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700363 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700364};
365
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366class OatDumperOptions {
367 public:
368 OatDumperOptions(bool dump_raw_mapping_table,
369 bool dump_raw_gc_map,
370 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100371 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700372 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700373 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800374 const char* class_filter,
375 const char* method_filter,
376 bool list_classes,
377 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000378 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800379 const char* export_dex_location,
380 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381 : dump_raw_mapping_table_(dump_raw_mapping_table),
382 dump_raw_gc_map_(dump_raw_gc_map),
383 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100384 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700385 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700386 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800387 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000388 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800389 list_classes_(list_classes),
390 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000391 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800392 export_dex_location_(export_dex_location),
393 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800394 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700395
396 const bool dump_raw_mapping_table_;
397 const bool dump_raw_gc_map_;
398 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100399 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700400 const bool disassemble_code_;
401 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800402 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000403 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800404 const bool list_classes_;
405 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000406 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800407 const char* const export_dex_location_;
408 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700409 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700410};
411
Elliott Hughese3c845c2012-02-28 17:23:01 -0800412class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700413 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100414 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000415 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800416 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700417 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800418 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800419 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
420 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800421 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000422 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100423 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800424 CHECK(options_.class_loader_ != nullptr);
425 CHECK(options_.class_filter_ != nullptr);
426 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800427 AddAllOffsets();
428 }
429
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700430 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700431 delete disassembler_;
432 }
433
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800434 InstructionSet GetInstructionSet() {
435 return instruction_set_;
436 }
437
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700438 bool Dump(std::ostream& os) {
439 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800440 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700441
442 os << "MAGIC:\n";
443 os << oat_header.GetMagic() << "\n\n";
444
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800445 os << "LOCATION:\n";
446 os << oat_file_.GetLocation() << "\n\n";
447
Brian Carlstromaded5f72011-10-07 17:15:04 -0700448 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800449 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700450
Elliott Hughesa72ec822012-03-05 17:12:22 -0800451 os << "INSTRUCTION SET:\n";
452 os << oat_header.GetInstructionSet() << "\n\n";
453
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700454 {
455 std::unique_ptr<const InstructionSetFeatures> features(
456 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
457 oat_header.GetInstructionSetFeaturesBitmap()));
458 os << "INSTRUCTION SET FEATURES:\n";
459 os << features->GetFeatureString() << "\n\n";
460 }
Dave Allison70202782013-10-22 17:52:19 -0700461
Brian Carlstromaded5f72011-10-07 17:15:04 -0700462 os << "DEX FILE COUNT:\n";
463 os << oat_header.GetDexFileCount() << "\n\n";
464
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800465#define DUMP_OAT_HEADER_OFFSET(label, offset) \
466 os << label " OFFSET:\n"; \
467 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800468 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800469 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
470 } \
471 os << StringPrintf("\n\n");
472
473 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
474 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
475 GetInterpreterToInterpreterBridgeOffset);
476 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
477 GetInterpreterToCompiledCodeBridgeOffset);
478 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
479 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800480 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
481 GetQuickGenericJniTrampolineOffset);
482 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
483 GetQuickImtConflictTrampolineOffset);
484 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
485 GetQuickResolutionTrampolineOffset);
486 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
487 GetQuickToInterpreterBridgeOffset);
488#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700489
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700490 os << "IMAGE PATCH DELTA:\n";
491 os << StringPrintf("%d (0x%08x)\n\n",
492 oat_header.GetImagePatchDelta(),
493 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700494
Brian Carlstrom28db0122012-10-18 16:20:41 -0700495 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
496 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
497
498 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800499 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700500
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700501 // Print the key-value store.
502 {
503 os << "KEY VALUE STORE:\n";
504 size_t index = 0;
505 const char* key;
506 const char* value;
507 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
508 os << key << " = " << value << "\n";
509 index++;
510 }
511 os << "\n";
512 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700513
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800514 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700515 os << "BEGIN:\n";
516 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700517
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700518 os << "END:\n";
519 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
520 }
521
522 os << "SIZE:\n";
523 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700524
525 os << std::flush;
526
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800527 // If set, adjust relative address to be searched
528 if (options_.addr2instr_ != 0) {
529 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
530 os << "SEARCH ADDRESS (executable offset + input):\n";
531 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
532 }
533
David Brazdilc03d7b62016-03-02 12:18:03 +0000534 if (!options_.dump_header_only_) {
535 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
536 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
537 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800538
David Brazdilc03d7b62016-03-02 12:18:03 +0000539 // If file export selected skip file analysis
540 if (options_.export_dex_location_) {
541 if (!ExportDexFile(os, *oat_dex_file)) {
542 success = false;
543 }
544 } else {
545 if (!DumpOatDexFile(os, *oat_dex_file)) {
546 success = false;
547 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800548 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700549 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700550 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000551
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700552 os << std::flush;
553 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700554 }
555
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800556 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700557 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
558 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800559 return 0; // Address not in oat file
560 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800561 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
562 reinterpret_cast<uintptr_t>(oat_file_.Begin());
563 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800564 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800565 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800566 return end_offset - begin_offset;
567 }
568
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800569 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700570 return oat_file_.GetOatHeader().GetInstructionSet();
571 }
572
Mathieu Chartier90443472015-07-16 20:32:27 -0700573 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800574 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
575 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700576 CHECK(oat_dex_file != nullptr);
577 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700578 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
579 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700580 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
581 << "': " << error_msg;
582 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800583 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700584 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800585 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700586 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700587 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100588 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800589 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100590 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800591 }
592 }
593 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700594 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800595 }
596
Brian Carlstromaded5f72011-10-07 17:15:04 -0700597 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800598 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 // We don't know the length of the code for each method, but we need to know where to stop
600 // when disassembling. What we do know is that a region of code will be followed by some other
601 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
602 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800603 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
604 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700605 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700606 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700607 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
608 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700609 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
610 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800611 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800612 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800613 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800614 for (size_t class_def_index = 0;
615 class_def_index < dex_file->NumClassDefs();
616 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800617 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100618 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700619 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700620 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800621 ClassDataItemIterator it(*dex_file, class_data);
622 SkipAllFields(it);
623 uint32_t class_method_index = 0;
624 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100625 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800626 it.Next();
627 }
628 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100629 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800630 it.Next();
631 }
632 }
633 }
634 }
635
636 // If the last thing in the file is code for a method, there won't be an offset for the "next"
637 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
638 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800639 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800640 }
641
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700642 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
643 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
644 }
645
Elliott Hughese3c845c2012-02-28 17:23:01 -0800646 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800647 uint32_t code_offset = oat_method.GetCodeOffset();
648 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
649 code_offset &= ~0x1;
650 }
651 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800652 offsets_.insert(oat_method.GetMappingTableOffset());
653 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800654 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800655 }
656
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700657 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
658 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800659 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700660 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800661 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800662 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700663
664 // Create the verifier early.
665
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700666 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700667 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
668 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700669 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700670 os << std::flush;
671 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700672 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100673
674 VariableIndentationOutputStream vios(&os);
675 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800676 for (size_t class_def_index = 0;
677 class_def_index < dex_file->NumClassDefs();
678 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700679 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
680 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800681
682 // TODO: Support regex
683 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
684 continue;
685 }
686
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700687 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100688 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700689 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
690 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100691 << " (" << oat_class.GetStatus() << ")"
692 << " (" << oat_class.GetType() << ")\n";
693 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800694 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700695 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700696 success = false;
697 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800698 if (stop_analysis) {
699 os << std::flush;
700 return success;
701 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700702 }
703
704 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700705 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700706 }
707
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800708 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
709 std::string error_msg;
710 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
711
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700712 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800713 if (dex_file == nullptr) {
714 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
715 return false;
716 }
717 size_t fsize = oat_dex_file.FileSize();
718
719 // Some quick checks just in case
720 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
721 os << "Invalid dex file\n";
722 return false;
723 }
724
725 // Verify output directory exists
726 if (!OS::DirectoryExists(options_.export_dex_location_)) {
727 // TODO: Extend OS::DirectoryExists if symlink support is required
728 os << options_.export_dex_location_ << " output directory not found or symlink\n";
729 return false;
730 }
731
732 // Beautify path names
733 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
734 return false;
735 }
736
737 std::string dex_orig_name;
738 size_t dex_orig_pos = dex_file_location.rfind('/');
739 if (dex_orig_pos == std::string::npos)
740 dex_orig_name = dex_file_location;
741 else
742 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
743
744 // A more elegant approach to efficiently name user installed apps is welcome
745 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
746 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
747 size_t apk_orig_pos = dex_file_location.rfind('/');
748 if (apk_orig_pos != std::string::npos) {
749 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
750 }
751 }
752
753 std::string out_dex_path(options_.export_dex_location_);
754 if (out_dex_path.back() != '/') {
755 out_dex_path.append("/");
756 }
757 out_dex_path.append(dex_orig_name);
758 out_dex_path.append("_export.dex");
759 if (out_dex_path.length() > PATH_MAX) {
760 return false;
761 }
762
763 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
764 if (file.get() == nullptr) {
765 os << "Failed to open output dex file " << out_dex_path;
766 return false;
767 }
768
769 if (!file->WriteFully(dex_file->Begin(), fsize)) {
770 os << "Failed to write dex file";
771 file->Erase();
772 return false;
773 }
774
775 if (file->FlushCloseOrErase() != 0) {
776 os << "Flush and close failed";
777 return false;
778 }
779
780 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
781 os << std::flush;
782
783 return true;
784 }
785
Elliott Hughese3c845c2012-02-28 17:23:01 -0800786 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700787 while (it.HasNextStaticField()) {
788 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700789 }
Ian Rogers0571d352011-11-03 19:51:38 -0700790 while (it.HasNextInstanceField()) {
791 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700792 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800793 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700794
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100795 bool DumpOatClass(VariableIndentationOutputStream* vios,
796 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800797 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700798 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800799 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700800 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700801 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100802 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700803 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800804 }
805 ClassDataItemIterator it(dex_file, class_data);
806 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700807 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700808 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100809 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700810 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800811 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700812 success = false;
813 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800814 if (addr_found) {
815 *stop_analysis = true;
816 return success;
817 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700818 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700819 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700820 }
Ian Rogers0571d352011-11-03 19:51:38 -0700821 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100822 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700823 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800824 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700825 success = false;
826 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800827 if (addr_found) {
828 *stop_analysis = true;
829 return success;
830 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700831 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700832 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700833 }
Ian Rogers0571d352011-11-03 19:51:38 -0700834 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100835 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700836 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700837 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800838
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700839 static constexpr uint32_t kPrologueBytes = 16;
840
841 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
842 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
843
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100844 bool DumpOatMethod(VariableIndentationOutputStream* vios,
845 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700846 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700847 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800848 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800849 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700850 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800851
852 // TODO: Support regex
853 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
854 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000855 return success;
856 }
857
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800858 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100859 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
860 class_method_index, pretty_method.c_str(),
861 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800862 if (options_.list_methods_) return success;
863
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800864 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
865 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
866 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
867 uint32_t code_offset = oat_method.GetCodeOffset();
868 uint32_t code_size = oat_method.GetQuickCodeSize();
869 if (resolved_addr2instr_ != 0) {
870 if (resolved_addr2instr_ > code_offset + code_size) {
871 return success;
872 } else {
873 *addr_found = true; // stop analyzing file at next iteration
874 }
875 }
876
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100877 // Everything below is indented at least once.
878 ScopedIndentation indent1(vios);
879
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800880 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 vios->Stream() << "DEX CODE:\n";
882 ScopedIndentation indent2(vios);
883 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700884 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700885
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700886 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700887 std::unique_ptr<verifier::MethodVerifier> verifier;
888 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700889 // We need to have the handle scope stay live until after the verifier since the verifier has
890 // a handle to the dex cache from hs.
891 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100892 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
893 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700894 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100895 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700896 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700897 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800898 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100899 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800900 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100901 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100902 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100903 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700904 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100905 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700906 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
907 oat_method_offsets_offset, oat_file_.Size());
908 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800911 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100913 ScopedIndentation indent2(vios);
914 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
916 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100917 vios->Stream() << StringPrintf("WARNING: "
918 "code offset 0x%08x is past end of file 0x%08zx.\n",
919 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 success = false;
921 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100922 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700923
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100924 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800925 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100926 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800928 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100929 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100931 vios->Stream() << StringPrintf("WARNING: "
932 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
933 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700934 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800935 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100936 ScopedIndentation indent3(vios);
937 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800938 }
939 }
940 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100941 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700942 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
943 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700944
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800945 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100946 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700947 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100948 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100950 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700951 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
952 method_header_offset, oat_file_.Size());
953 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100954 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700955 return false;
956 }
957
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100958 ScopedIndentation indent2(vios);
959 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800960 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100961 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700962 }
963 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100964 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700965 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100966 vios->Stream() << StringPrintf("WARNING: "
967 "mapping table offset 0x%08x is past end of file 0x%08zx. "
968 "mapping table offset was loaded from offset 0x%08x.\n",
969 mapping_table_offset, oat_file_.Size(),
970 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700971 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800972 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100973 ScopedIndentation indent3(vios);
974 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700975 }
976
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100977 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800978 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100979 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700980 }
981 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100982 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700983 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100984 vios->Stream() << StringPrintf("WARNING: "
985 "vmap table offset 0x%08x is past end of file 0x%08zx. "
986 "vmap table offset was loaded from offset 0x%08x.\n",
987 vmap_table_offset, oat_file_.Size(),
988 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700989 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800990 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100991 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700992 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800993 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700994 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100995 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100997 ScopedIndentation indent2(vios);
998 vios->Stream()
999 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1000 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1001 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1002 vios->Stream() << "\n";
1003 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1004 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1005 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001006 }
1007 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001008 // Based on spill masks from QuickMethodFrameInfo so placed
1009 // after it is dumped, but useful for understanding quick
1010 // code, so dumped here.
1011 ScopedIndentation indent2(vios);
1012 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001013 }
1014 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001015 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001016 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1017 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 ScopedIndentation indent2(vios);
1019 vios->Stream() << StringPrintf("WARNING: "
1020 "code size offset 0x%08x is past end of file 0x%08zx.",
1021 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001022 success = false;
1023 } else {
1024 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001025 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1026 uint64_t aligned_code_end = aligned_code_begin + code_size;
1027
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001028 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001029 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001030 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001031 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1032 code_offset,
1033 code_size_offset,
1034 code_size,
1035 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001036
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001037 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001038 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001039 vios->Stream() << StringPrintf("WARNING: "
1040 "start of code at 0x%08x is past end of file 0x%08zx.",
1041 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001042 success = false;
1043 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001044 vios->Stream() << StringPrintf(
1045 "WARNING: "
1046 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1047 "code size is 0x%08x loaded from offset 0x%08x.\n",
1048 aligned_code_end, oat_file_.Size(),
1049 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001050 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001051 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001052 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001053 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001054 }
1055 }
1056 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001057 vios->Stream() << StringPrintf(
1058 "WARNING: "
1059 "code size %d is bigger than max expected threshold of %d. "
1060 "code size is 0x%08x loaded from offset 0x%08x.\n",
1061 code_size, kMaxCodeSize,
1062 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001063 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001064 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001065 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001066 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001067 }
1068 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001069 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001070 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001071 }
1072 }
1073 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001074 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001075 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001076 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001077
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001078 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1079 if (spill_mask == 0) {
1080 return;
1081 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001082 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001083 for (size_t i = 0; i < 32; i++) {
1084 if ((spill_mask & (1 << i)) != 0) {
1085 if (is_float) {
1086 os << "fr" << i;
1087 } else {
1088 os << "r" << i;
1089 }
1090 spill_mask ^= 1 << i; // clear bit
1091 if (spill_mask != 0) {
1092 os << ", ";
1093 } else {
1094 break;
1095 }
1096 }
1097 }
1098 os << ")";
1099 }
1100
Roland Levillain442b46a2015-02-18 16:54:21 +00001101 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001102 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001103 const OatFile::OatMethod& oat_method,
1104 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001105 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1106 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001107 const void* raw_code_info = oat_method.GetVmapTable();
1108 if (raw_code_info != nullptr) {
1109 CodeInfo code_info(raw_code_info);
1110 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001111 ScopedIndentation indent1(vios);
1112 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001113 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001114 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1115 // We don't encode the size in the table, so just emit that we have quickened
1116 // information.
1117 ScopedIndentation indent(vios);
1118 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001119 } else {
1120 // Otherwise, display the vmap table.
1121 const uint8_t* raw_table = oat_method.GetVmapTable();
1122 if (raw_table != nullptr) {
1123 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001124 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001125 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001126 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001127 }
1128
1129 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001130 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001131 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001132 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001133 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001134 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001135 oat_method.GetCodeOffset(),
1136 code_item.registers_size_,
1137 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001138 }
1139
Roland Levillain442b46a2015-02-18 16:54:21 +00001140 // Display a vmap table.
1141 void DumpVmapTable(std::ostream& os,
1142 const OatFile::OatMethod& oat_method,
1143 const VmapTable& vmap_table) {
1144 bool first = true;
1145 bool processing_fp = false;
1146 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1147 for (size_t i = 0; i < vmap_table.Size(); i++) {
1148 uint16_t dex_reg = vmap_table[i];
1149 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1150 processing_fp ? kFloatVReg : kIntVReg);
1151 os << (first ? "v" : ", v") << dex_reg;
1152 if (!processing_fp) {
1153 os << "/r" << cpu_reg;
1154 } else {
1155 os << "/fr" << cpu_reg;
1156 }
1157 first = false;
1158 if (!processing_fp && dex_reg == 0xFFFF) {
1159 processing_fp = true;
1160 spill_mask = oat_method.GetFpSpillMask();
1161 }
1162 }
1163 os << "\n";
1164 }
1165
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001166 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1167 const DexFile::CodeItem* code_item) {
1168 if (code_item != nullptr) {
1169 size_t num_locals_ins = code_item->registers_size_;
1170 size_t num_ins = code_item->ins_size_;
1171 size_t num_locals = num_locals_ins - num_ins;
1172 size_t num_outs = code_item->outs_size_;
1173
1174 os << "vr_stack_locations:";
1175 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1176 // For readability, delimit the different kinds of VRs.
1177 if (reg == num_locals_ins) {
1178 os << "\n\tmethod*:";
1179 } else if (reg == num_locals && num_ins > 0) {
1180 os << "\n\tins:";
1181 } else if (reg == 0 && num_locals > 0) {
1182 os << "\n\tlocals:";
1183 }
1184
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001185 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1186 code_item,
1187 oat_method.GetCoreSpillMask(),
1188 oat_method.GetFpSpillMask(),
1189 oat_method.GetFrameSizeInBytes(),
1190 reg,
1191 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001192 os << " v" << reg << "[sp + #" << offset << "]";
1193 }
1194
1195 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1196 if (out_reg == 0) {
1197 os << "\n\touts:";
1198 }
1199
1200 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1201 os << " v" << out_reg << "[sp + #" << offset << "]";
1202 }
1203
1204 os << "\n";
1205 }
1206 }
1207
Ian Rogersb23a7722012-10-09 16:54:26 -07001208 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001209 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001210 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001211 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001212 const VmapTable vmap_table(raw_table);
1213 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001214 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001215 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1216 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1217 : oat_method.GetCoreSpillMask();
1218 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001219 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001220 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1221 code_item,
1222 oat_method.GetCoreSpillMask(),
1223 oat_method.GetFpSpillMask(),
1224 oat_method.GetFrameSizeInBytes(),
1225 reg,
1226 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001227 os << "[sp + #" << offset << "]";
1228 }
1229 }
1230 }
1231
Ian Rogersef7d42f2014-01-06 12:55:46 -08001232 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1233 const DexFile::CodeItem* code_item,
1234 size_t num_regs, const uint8_t* reg_bitmap) {
1235 bool first = true;
1236 for (size_t reg = 0; reg < num_regs; reg++) {
1237 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1238 if (first) {
1239 os << " v" << reg << " (";
1240 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1241 os << ")";
1242 first = false;
1243 } else {
1244 os << ", v" << reg << " (";
1245 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1246 os << ")";
1247 }
1248 }
1249 }
1250 if (first) {
1251 os << "No registers in GC map\n";
1252 } else {
1253 os << "\n";
1254 }
1255 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001256 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1257 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001258 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001259 if (gc_map_raw == nullptr) {
1260 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001261 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001262 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001263 NativePcOffsetToReferenceMap map(gc_map_raw);
1264 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1265 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1266 map.GetNativePcOffset(entry);
1267 os << StringPrintf("%p", native_pc);
1268 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001269 }
1270 }
1271
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001272 void DumpMappingTable(VariableIndentationOutputStream* vios,
1273 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001274 const void* quick_code = oat_method.GetQuickCode();
1275 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001276 return;
1277 }
Ian Rogers1809a722013-08-09 22:05:32 -07001278 MappingTable table(oat_method.GetMappingTable());
1279 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001280 if (table.PcToDexSize() != 0) {
1281 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001282 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001283 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001284 ScopedIndentation indent1(vios);
1285 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001286 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001287 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001288 }
1289 if (table.DexToPcSize() != 0) {
1290 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001291 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001292 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001293 ScopedIndentation indent1(vios);
1294 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001295 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001296 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001297 }
1298 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001299 }
1300
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001301 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001302 const OatFile::OatMethod& oat_method,
1303 const DexFile::CodeItem* code_item,
1304 size_t offset,
1305 bool suspend_point_mapping) {
1306 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1307 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001308 ScopedIndentation indent1(vios);
1309 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001310 }
1311 // The return value is not used in the case of a method compiled
1312 // with the optimizing compiler.
1313 return DexFile::kDexNoIndex;
1314 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001315 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001316 }
1317 }
1318
Ian Rogers1809a722013-08-09 22:05:32 -07001319 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1320 size_t offset, bool suspend_point_mapping) {
1321 MappingTable table(oat_method.GetMappingTable());
1322 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1323 typedef MappingTable::PcToDexIterator It;
1324 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1325 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001326 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001327 return cur.DexPc();
1328 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001329 }
Ian Rogers1809a722013-08-09 22:05:32 -07001330 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1331 typedef MappingTable::DexToPcIterator It;
1332 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1333 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001334 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001335 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001336 }
1337 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001338 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001339 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001340 }
1341
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001342 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1343 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001344 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001345 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001346 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001347 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001348 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001349 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001350 bool first = true;
1351 for (size_t reg = 0; reg < num_regs; reg++) {
1352 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1353 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001354 os << "GC map objects: v" << reg << " (";
1355 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001356 os << ")";
1357 first = false;
1358 } else {
1359 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001360 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001361 os << ")";
1362 }
1363 }
1364 }
1365 if (!first) {
1366 os << "\n";
1367 }
1368 }
1369 }
1370 }
1371
Mathieu Chartier590fee92013-09-13 13:46:47 -07001372 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1373 const OatFile::OatMethod& oat_method,
1374 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1375 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001376 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001377 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001378 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1379 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1380 if (kind != kUndefined) {
1381 if (first) {
1382 os << "VRegs: v";
1383 first = false;
1384 } else {
1385 os << ", v";
1386 }
1387 os << reg << " (";
1388 switch (kind) {
1389 case kImpreciseConstant:
1390 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1391 DescribeVReg(os, oat_method, code_item, reg, kind);
1392 break;
1393 case kConstant:
1394 os << "Constant: " << kinds.at((reg * 2) + 1);
1395 break;
1396 default:
1397 DescribeVReg(os, oat_method, code_item, reg, kind);
1398 break;
1399 }
1400 os << ")";
1401 }
1402 }
1403 if (!first) {
1404 os << "\n";
1405 }
1406 }
1407
1408
Ian Rogersb23a7722012-10-09 16:54:26 -07001409 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001410 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001411 size_t i = 0;
1412 while (i < code_item->insns_size_in_code_units_) {
1413 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001414 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1415 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001416 i += instruction->SizeInCodeUnits();
1417 }
1418 }
1419 }
1420
Roland Levillainf2650d12015-05-28 14:53:28 +01001421 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1422 // the optimizing compiler?
1423 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1424 const DexFile::CodeItem* code_item) {
1425 // If the native GC map is null and the Dex `code_item` is not
1426 // null, then this method has been compiled with the optimizing
1427 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001428 return oat_method.GetQuickCode() != nullptr &&
1429 oat_method.GetGcMap() == nullptr &&
1430 code_item != nullptr;
1431 }
1432
1433 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1434 // the dextodex compiler?
1435 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1436 const DexFile::CodeItem* code_item) {
1437 // If the quick code is null, the Dex `code_item` is not
1438 // null, and the vmap table is not null, then this method has been compiled
1439 // with the dextodex compiler.
1440 return oat_method.GetQuickCode() == nullptr &&
1441 oat_method.GetVmapTable() != nullptr &&
1442 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001443 }
1444
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001445 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001446 const OatFile::OatMethod& oat_method,
1447 const DexFile::CodeItem* code_item,
1448 size_t offset) {
1449 // This method is only relevant for oat methods compiled with the
1450 // optimizing compiler.
1451 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1452
1453 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1454 const void* raw_code_info = oat_method.GetVmapTable();
1455 if (raw_code_info != nullptr) {
1456 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001457 StackMapEncoding encoding = code_info.ExtractEncoding();
1458 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001459 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001460 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1461 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001462 }
1463 }
1464 }
1465
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001466 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001467 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001468 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001469 const DexFile* dex_file,
1470 const DexFile::ClassDef& class_def,
1471 const DexFile::CodeItem* code_item,
1472 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 if ((method_access_flags & kAccNative) == 0) {
1474 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001475 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001476 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001477 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1478 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001479 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001480 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001481 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1482 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001483 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001484
1485 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 }
1487
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001488 void DumpCode(VariableIndentationOutputStream* vios,
1489 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001490 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1491 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001492 const void* quick_code = oat_method.GetQuickCode();
1493
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001494 if (code_size == 0) {
1495 code_size = oat_method.GetQuickCodeSize();
1496 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001497 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001498 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001499 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001500 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001501 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1502 size_t offset = 0;
1503 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001504 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001505 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001506 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001507 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001508 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001509 uint32_t dex_pc =
1510 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001511 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001512 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001513 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001514 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001515 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001516 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001517 }
1518 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001519 }
1520 }
1521
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001522 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001523 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001524 const OatDumperOptions& options_;
1525 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001526 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001527 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001528 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001529};
1530
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001531class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001532 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001533 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1534 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001535 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001536 vios_(os),
1537 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001538 image_space_(image_space),
1539 image_header_(image_header),
1540 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001541
Mathieu Chartier90443472015-07-16 20:32:27 -07001542 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001544 std::ostream& indent_os = vios_.Stream();
1545
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001547
Jeff Haodcdc85b2015-12-04 14:06:18 -08001548 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1549
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001550 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001551
Mathieu Chartiere401d142015-04-22 13:56:20 -07001552 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1553
1554 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1555 auto section = static_cast<ImageHeader::ImageSections>(i);
1556 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1557 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001558
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001559 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001560
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001561 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001562
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001563 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1564
1565 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1566
1567 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001568
Alex Lighta59dd802014-07-02 16:28:08 -07001569 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1570
Igor Murashkin46774762014-10-22 11:37:02 -07001571 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1572
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001573 {
1574 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001575 static_assert(arraysize(image_roots_descriptions_) ==
1576 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001577 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1578 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1579 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001580 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001581 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001582 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001583 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001584 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001585 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001586 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1587 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001588 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001589 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1590 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001591 run++;
1592 } else {
1593 break;
1594 }
1595 }
1596 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001597 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001598 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001599 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001600 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001601 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001602 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001603 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001604 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001605 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001606 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001607 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001608 }
1609 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001610 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001611
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001612 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001613 os << "METHOD ROOTS\n";
1614 static_assert(arraysize(image_methods_descriptions_) ==
1615 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1616 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1617 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1618 const char* description = image_methods_descriptions_[i];
1619 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001620 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001621 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001622 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001624
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001625 Runtime* const runtime = Runtime::Current();
1626 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001627 std::string image_filename = image_space_.GetImageFilename();
1628 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001631 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001632 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1633 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001634 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001635 oat_file = OatFile::Open(oat_location, oat_location,
1636 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001637 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001638 if (oat_file == nullptr) {
1639 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001640 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001641 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001642 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001643 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001644
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001645 stats_.oat_file_bytes = oat_file->Size();
1646
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001647 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648
Mathieu Chartier02e25112013-08-14 16:14:24 -07001649 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001650 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001651 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1652 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001653 }
1654
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001655 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001656
Jeff Haodcdc85b2015-12-04 14:06:18 -08001657 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001658 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001659 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001660 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001661 {
1662 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1663 heap->FlushAllocStack();
1664 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001665 // Since FlushAllocStack() above resets the (active) allocation
1666 // stack. Need to revoke the thread-local allocation stacks that
1667 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001668 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001669 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001670 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001671 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001672 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001673 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001674 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001675 {
1676 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001677 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001678 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001679 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001680 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001681 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001682 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001683 }
1684 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001685 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001686 // Dump the normal objects before ArtMethods.
1687 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1688 indent_os << "\n";
1689 // TODO: Dump fields.
1690 // Dump methods after.
1691 const auto& methods_section = image_header_.GetMethodsSection();
1692 const size_t pointer_size =
1693 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1694 DumpArtMethodVisitor visitor(this);
1695 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001696 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001697 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001698 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001699 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001700 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001701 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001702 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001703 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001704 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001705 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001706 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001707 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001708 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001709 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001710 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1711 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001712 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1713 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001714 const auto& intern_section = image_header_.GetImageSection(
1715 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001716 const auto& class_table_section = image_header_.GetImageSection(
1717 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001718 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1719
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001720 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001721
1722 // Objects are kObjectAlignment-aligned.
1723 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1724 if (object_section.Offset() > header_bytes) {
1725 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1726 }
1727
1728 // Field section is 4-byte aligned.
1729 constexpr size_t kFieldSectionAlignment = 4U;
1730 uint32_t end_objects = object_section.Offset() + object_section.Size();
1731 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1732 stats_.alignment_bytes += field_section.Offset() - end_objects;
1733
1734 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1735 uint32_t end_fields = field_section.Offset() + field_section.Size();
1736 CHECK_ALIGNED(method_section.Offset(), 4);
1737 stats_.alignment_bytes += method_section.Offset() - end_fields;
1738
1739 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1740 uint32_t end_methods = method_section.Offset() + method_section.Size();
1741 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1742 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1743
1744 // Intern table is 8-byte aligned.
1745 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1746 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1747 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1748
1749 // Add space between intern table and class table.
1750 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1751 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1752
1753 // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1754 uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1755 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1756 stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1757
Mathieu Chartiere401d142015-04-22 13:56:20 -07001758 stats_.bitmap_bytes += bitmap_section.Size();
1759 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001760 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001761 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001762 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001763 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001764 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001765 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001766
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001767 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001768
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001769 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001770 }
1771
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001772 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001773 class DumpArtMethodVisitor : public ArtMethodVisitor {
1774 public:
1775 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1776
1777 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1778 std::ostream& indent_os = image_dumper_->vios_.Stream();
1779 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1780 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1781 indent_os << "\n";
1782 }
1783
1784 private:
1785 ImageDumper* const image_dumper_;
1786 };
1787
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001788 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001789 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001790 CHECK(type != nullptr);
1791 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001792 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001793 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001794 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001795 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001796 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001797 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001798 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001799 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001800 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001802 }
1803 }
1804
Mathieu Chartierc7853442015-03-27 14:35:38 -07001805 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001806 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001807 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001808 switch (field->GetTypeAsPrimitiveType()) {
1809 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001810 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001811 break;
1812 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001813 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001814 break;
1815 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001816 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001817 break;
1818 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001819 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001820 break;
1821 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001822 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001823 break;
1824 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001825 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001826 break;
1827 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001828 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1829 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001830 break;
1831 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001832 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001833 break;
1834 case Primitive::kPrimNot: {
1835 // Get the value, don't compute the type unless it is non-null as we don't want
1836 // to cause class loading.
1837 mirror::Object* value = field->GetObj(obj);
1838 if (value == nullptr) {
1839 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001840 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001841 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001842 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001843 if (field_type != nullptr) {
1844 PrettyObjectValue(os, field_type, value);
1845 } else {
1846 os << StringPrintf("%p %s\n", value,
1847 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1848 }
Ian Rogers50239c72013-06-17 14:53:22 -07001849 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001850 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001851 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001852 default:
1853 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1854 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001855 }
1856 }
1857
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001858 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001859 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001860 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001861 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001862 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001863 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001864 for (ArtField& field : klass->GetIFields()) {
1865 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001866 }
1867 }
1868
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001869 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001870 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001871 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001872
Mathieu Chartiere401d142015-04-22 13:56:20 -07001873 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001874 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001875 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1876 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001877 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001878 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001879 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001880 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001881 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001882 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001883 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001884 }
1885
Mathieu Chartiere401d142015-04-22 13:56:20 -07001886 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001887 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001888 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1889 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001890 return 0;
1891 }
1892 return oat_code_begin[-1];
1893 }
1894
Mathieu Chartiere401d142015-04-22 13:56:20 -07001895 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001896 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001897 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001898 if (oat_code_begin == nullptr) {
1899 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001900 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001901 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001902 }
1903
Mathieu Chartier90443472015-07-16 20:32:27 -07001904 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001905 DCHECK(obj != nullptr);
1906 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001907 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001908 if (!state->InDumpSpace(obj)) {
1909 return;
1910 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001911
1912 size_t object_bytes = obj->SizeOf();
1913 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1914 state->stats_.object_bytes += object_bytes;
1915 state->stats_.alignment_bytes += alignment_bytes;
1916
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001917 std::ostream& os = state->vios_.Stream();
1918
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001919 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001920 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001921 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1922 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001923 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001924 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001925 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1926 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001927 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001928 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001929 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001930 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001931 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001932 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001933 ScopedIndentation indent1(&state->vios_);
1934 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001935 const auto image_pointer_size =
1936 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001937 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001938 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1939 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001940 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001941 size_t run = 0;
1942 for (int32_t j = i + 1; j < length; j++) {
1943 if (value == obj_array->Get(j)) {
1944 run++;
1945 } else {
1946 break;
1947 }
1948 }
1949 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001950 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001951 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001952 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001953 i = i + run;
1954 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001955 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001956 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001957 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001958 }
1959 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001960 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001961 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001962 os << "STATICS:\n";
1963 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001964 for (ArtField& field : klass->GetSFields()) {
1965 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001966 }
1967 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001968 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001969 auto it = state->dex_caches_.find(obj);
1970 if (it != state->dex_caches_.end()) {
1971 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001972 const auto& field_section = state->image_header_.GetImageSection(
1973 ImageHeader::kSectionArtFields);
1974 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001975 size_t num_methods = dex_cache->NumResolvedMethods();
1976 if (num_methods != 0u) {
1977 os << "Methods (size=" << num_methods << "):";
1978 ScopedIndentation indent2(&state->vios_);
1979 auto* resolved_methods = dex_cache->GetResolvedMethods();
1980 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1981 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1982 size_t run = 0;
1983 for (size_t j = i + 1;
1984 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1985 j,
1986 image_pointer_size);
1987 ++j, ++run) {}
1988 if (run == 0) {
1989 os << StringPrintf("%zd: ", i);
1990 } else {
1991 os << StringPrintf("%zd to %zd: ", i, i + run);
1992 i = i + run;
1993 }
1994 std::string msg;
1995 if (elem == nullptr) {
1996 msg = "null";
1997 } else if (method_section.Contains(
1998 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1999 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
2000 } else {
2001 msg = "<not in method section>";
2002 }
2003 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002004 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002005 }
2006 size_t num_fields = dex_cache->NumResolvedFields();
2007 if (num_fields != 0u) {
2008 os << "Fields (size=" << num_fields << "):";
2009 ScopedIndentation indent2(&state->vios_);
2010 auto* resolved_fields = dex_cache->GetResolvedFields();
2011 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
2012 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
2013 size_t run = 0;
2014 for (size_t j = i + 1;
2015 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
2016 j,
2017 image_pointer_size);
2018 ++j, ++run) {}
2019 if (run == 0) {
2020 os << StringPrintf("%zd: ", i);
2021 } else {
2022 os << StringPrintf("%zd to %zd: ", i, i + run);
2023 i = i + run;
2024 }
2025 std::string msg;
2026 if (elem == nullptr) {
2027 msg = "null";
2028 } else if (field_section.Contains(
2029 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2030 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
2031 } else {
2032 msg = "<not in field section>";
2033 }
2034 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002036 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002037 }
2038 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002039 std::string temp;
2040 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002041 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002042
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002044 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002045 DCHECK(method != nullptr);
2046 const auto image_pointer_size =
2047 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002048 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2049 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002050 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2051 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002052 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002053 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2054 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2055 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2056 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002057 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002058 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002059 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002060 if (first_occurrence) {
2061 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2062 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002063 if (quick_oat_code_begin !=
2064 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
2065 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002066 }
2067 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2068 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2069 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002070 } else {
2071 const DexFile::CodeItem* code_item = method->GetCodeItem();
2072 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2073 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2074
2075 bool first_occurrence;
2076 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002077 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002078 if (first_occurrence) {
2079 state->stats_.gc_map_bytes += gc_map_bytes;
2080 }
2081
2082 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002083 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002084 if (first_occurrence) {
2085 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2086 }
2087
Roland Levillain6d7f1792015-07-02 10:59:15 +01002088 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002089 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002090 // Method compiled with the optimizing compiler have no vmap table.
2091 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002092 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002093 if (first_occurrence) {
2094 state->stats_.vmap_table_bytes += vmap_table_bytes;
2095 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002096 }
2097
Mathieu Chartiere401d142015-04-22 13:56:20 -07002098 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2099 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2100 if (first_occurrence) {
2101 state->stats_.managed_code_bytes += quick_oat_code_size;
2102 if (method->IsConstructor()) {
2103 if (method->IsStatic()) {
2104 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2105 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2106 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2107 }
2108 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2109 state->stats_.large_method_code_bytes += quick_oat_code_size;
2110 }
2111 }
2112 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2113
Igor Murashkin7617abd2015-07-10 18:27:47 -07002114 uint32_t method_access_flags = method->GetAccessFlags();
2115
Mathieu Chartiere401d142015-04-22 13:56:20 -07002116 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002117 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2118 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2119 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002120
2121 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002122 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002123
2124 double expansion =
2125 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2126 state->stats_.ComputeOutliers(total_size, expansion, method);
2127 }
2128 }
2129
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002130 std::set<const void*> already_seen_;
2131 // Compute the size of the given data within the oat file and whether this is the first time
2132 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002133 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002134 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002135 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002136 already_seen_.insert(oat_data);
2137 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002138 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002139 }
2140 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002141 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002142
2143 public:
2144 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002145 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002146 size_t file_bytes;
2147
2148 size_t header_bytes;
2149 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002150 size_t art_field_bytes;
2151 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002152 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002153 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002154 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002155 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002156 size_t alignment_bytes;
2157
2158 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002159 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002160 size_t managed_to_native_code_bytes;
2161 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002162 size_t class_initializer_code_bytes;
2163 size_t large_initializer_code_bytes;
2164 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002165
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002166 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002167 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002168 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002169
2170 size_t dex_instruction_bytes;
2171
Mathieu Chartiere401d142015-04-22 13:56:20 -07002172 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002173 std::vector<size_t> method_outlier_size;
2174 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002175 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002176
Roland Levillain3887c462015-08-12 18:15:42 +01002177 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002178 : oat_file_bytes(0),
2179 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002180 header_bytes(0),
2181 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002182 art_field_bytes(0),
2183 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002184 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002185 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002186 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002187 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002188 alignment_bytes(0),
2189 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002190 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002191 managed_to_native_code_bytes(0),
2192 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002193 class_initializer_code_bytes(0),
2194 large_initializer_code_bytes(0),
2195 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002196 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002197 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002198 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002199 dex_instruction_bytes(0) {}
2200
Elliott Hughesa0e18062012-04-13 15:59:59 -07002201 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002202 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002203 size_t bytes;
2204 size_t count;
2205 };
2206 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2207 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002208
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002209 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002210 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2211 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002212 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002213 it->second.count += 1;
2214 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002215 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002216 }
2217 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002218
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002219 double PercentOfOatBytes(size_t size) {
2220 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2221 }
2222
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002223 double PercentOfFileBytes(size_t size) {
2224 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2225 }
2226
2227 double PercentOfObjectBytes(size_t size) {
2228 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2229 }
2230
Mathieu Chartiere401d142015-04-22 13:56:20 -07002231 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002232 method_outlier_size.push_back(total_size);
2233 method_outlier_expansion.push_back(expansion);
2234 method_outlier.push_back(method);
2235 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002236
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002237 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002238 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002239 size_t sum_of_sizes = 0;
2240 size_t sum_of_sizes_squared = 0;
2241 size_t sum_of_expansion = 0;
2242 size_t sum_of_expansion_squared = 0;
2243 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002244 if (n == 0) {
2245 return;
2246 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002247 for (size_t i = 0; i < n; i++) {
2248 size_t cur_size = method_outlier_size[i];
2249 sum_of_sizes += cur_size;
2250 sum_of_sizes_squared += cur_size * cur_size;
2251 double cur_expansion = method_outlier_expansion[i];
2252 sum_of_expansion += cur_expansion;
2253 sum_of_expansion_squared += cur_expansion * cur_expansion;
2254 }
2255 size_t size_mean = sum_of_sizes / n;
2256 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2257 double expansion_mean = sum_of_expansion / n;
2258 double expansion_variance =
2259 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2260
2261 // Dump methods whose size is a certain number of standard deviations from the mean
2262 size_t dumped_values = 0;
2263 size_t skipped_values = 0;
2264 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2265 size_t cur_size_variance = i * i * size_variance;
2266 bool first = true;
2267 for (size_t j = 0; j < n; j++) {
2268 size_t cur_size = method_outlier_size[j];
2269 if (cur_size > size_mean) {
2270 size_t cur_var = cur_size - size_mean;
2271 cur_var = cur_var * cur_var;
2272 if (cur_var > cur_size_variance) {
2273 if (dumped_values > 20) {
2274 if (i == 1) {
2275 skipped_values++;
2276 } else {
2277 i = 2; // jump to counting for 1 standard deviation
2278 break;
2279 }
2280 } else {
2281 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002282 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002283 first = false;
2284 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002285 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002286 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002287 method_outlier_size[j] = 0; // don't consider this method again
2288 dumped_values++;
2289 }
2290 }
2291 }
2292 }
2293 }
2294 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002295 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002296 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002297 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002298 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002299
2300 // Dump methods whose expansion is a certain number of standard deviations from the mean
2301 dumped_values = 0;
2302 skipped_values = 0;
2303 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2304 double cur_expansion_variance = i * i * expansion_variance;
2305 bool first = true;
2306 for (size_t j = 0; j < n; j++) {
2307 double cur_expansion = method_outlier_expansion[j];
2308 if (cur_expansion > expansion_mean) {
2309 size_t cur_var = cur_expansion - expansion_mean;
2310 cur_var = cur_var * cur_var;
2311 if (cur_var > cur_expansion_variance) {
2312 if (dumped_values > 20) {
2313 if (i == 1) {
2314 skipped_values++;
2315 } else {
2316 i = 2; // jump to counting for 1 standard deviation
2317 break;
2318 }
2319 } else {
2320 if (first) {
2321 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002322 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002323 first = false;
2324 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002325 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002326 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002327 method_outlier_expansion[j] = 0.0; // don't consider this method again
2328 dumped_values++;
2329 }
2330 }
2331 }
2332 }
2333 }
2334 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002335 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002336 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002337 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002338 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002339 }
2340
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002341 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002342 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 {
2344 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2345 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002346 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2347 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2348 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2349 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2350 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2351 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002352 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002353 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2354 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 header_bytes, PercentOfFileBytes(header_bytes),
2356 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002357 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2358 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002359 dex_cache_arrays_bytes,
2360 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002361 interned_strings_bytes,
2362 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002363 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002364 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002365 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2366 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002367 CHECK_EQ(file_bytes,
2368 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2369 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2370 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002371 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002372
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002373 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002374 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002375 for (const auto& sizes_and_count : sizes_and_counts) {
2376 const std::string& descriptor(sizes_and_count.first);
2377 double average = static_cast<double>(sizes_and_count.second.bytes) /
2378 static_cast<double>(sizes_and_count.second.count);
2379 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002381 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002382 descriptor.c_str(), sizes_and_count.second.bytes,
2383 sizes_and_count.second.count, average, percent);
2384 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002385 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002386 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002387 CHECK_EQ(object_bytes, object_bytes_total);
2388
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002389 os << StringPrintf("oat_file_bytes = %8zd\n"
2390 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2391 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2392 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2393 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2394 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2395 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002396 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002397 managed_code_bytes,
2398 PercentOfOatBytes(managed_code_bytes),
2399 managed_to_native_code_bytes,
2400 PercentOfOatBytes(managed_to_native_code_bytes),
2401 native_to_managed_code_bytes,
2402 PercentOfOatBytes(native_to_managed_code_bytes),
2403 class_initializer_code_bytes,
2404 PercentOfOatBytes(class_initializer_code_bytes),
2405 large_initializer_code_bytes,
2406 PercentOfOatBytes(large_initializer_code_bytes),
2407 large_method_code_bytes,
2408 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002409 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002410 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002411 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002412 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2413 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002414 }
2415
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002416 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2417 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2418 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002419 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2420 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2421 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002422 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002423
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002424 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2425 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002426 static_cast<double>(managed_code_bytes) /
2427 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002428 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002429 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002430 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002431
2432 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002433 }
2434 } stats_;
2435
2436 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002437 enum {
2438 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2439 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2440 kLargeConstructorDexBytes = 4000,
2441 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2442 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2443 kLargeMethodDexBytes = 16000
2444 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002445
2446 // For performance, use the *os_ directly for anything that doesn't need indentation
2447 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002448 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002449 VariableIndentationOutputStream vios_;
2450 ScopedIndentation indent1_;
2451
Ian Rogers1d54e732013-05-02 21:10:01 -07002452 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002453 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002454 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002455 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002456 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002457
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002458 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002459};
2460
Jeff Haodcdc85b2015-12-04 14:06:18 -08002461static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002462 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002463 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002464 options->class_loader_ = &null_class_loader;
2465
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002466 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002467 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002468 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2469 CHECK(!image_spaces.empty());
2470 for (gc::space::ImageSpace* image_space : image_spaces) {
2471 const ImageHeader& image_header = image_space->GetImageHeader();
2472 if (!image_header.IsValid()) {
2473 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2474 return EXIT_FAILURE;
2475 }
2476
2477 ImageDumper image_dumper(os, *image_space, image_header, options);
2478 if (!image_dumper.Dump()) {
2479 return EXIT_FAILURE;
2480 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002481 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002482 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002483}
2484
Andreas Gampe00b25f32014-09-17 21:49:05 -07002485static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2486 std::ostream* os) {
2487 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2488
2489 Thread* self = Thread::Current();
2490 CHECK(self != nullptr);
2491 // Need well-known-classes.
2492 WellKnownClasses::Init(self->GetJniEnv());
2493
2494 // Need to register dex files to get a working dex cache.
2495 ScopedObjectAccess soa(self);
2496 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002497 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002498 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002499 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2500 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002501 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002502 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002503 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002504 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002505 }
2506
2507 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002508 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002509 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002510
2511 // Use the class loader while dumping.
2512 StackHandleScope<1> scope(self);
2513 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2514 soa.Decode<mirror::ClassLoader*>(class_loader));
2515 options->class_loader_ = &loader_handle;
2516
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002517 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002518 bool success = oat_dumper.Dump(*os);
2519 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2520}
2521
2522static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002523 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002524 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002525 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002526 options->class_loader_ = &null_class_loader;
2527
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002528 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002529 bool success = oat_dumper.Dump(*os);
2530 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2531}
2532
2533static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2534 std::ostream* os) {
2535 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002536 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002537 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002538 if (oat_file == nullptr) {
2539 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2540 return EXIT_FAILURE;
2541 }
2542
2543 if (runtime != nullptr) {
2544 return DumpOatWithRuntime(runtime, oat_file, options, os);
2545 } else {
2546 return DumpOatWithoutRuntime(oat_file, options, os);
2547 }
2548}
2549
2550static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2551 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002552 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002553 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002554 if (oat_file == nullptr) {
2555 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2556 return EXIT_FAILURE;
2557 }
2558
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002559 bool result;
2560 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2561 // files for 64-bit code in the past.
2562 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
2563 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name);
2564 result = oat_symbolizer.Symbolize();
2565 } else {
2566 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name);
2567 result = oat_symbolizer.Symbolize();
2568 }
2569 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002570 fprintf(stderr, "Failed to symbolize\n");
2571 return EXIT_FAILURE;
2572 }
2573
2574 return EXIT_SUCCESS;
2575}
2576
Igor Murashkin37743352014-11-13 14:38:00 -08002577struct OatdumpArgs : public CmdlineArgs {
2578 protected:
2579 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002580
Igor Murashkin37743352014-11-13 14:38:00 -08002581 virtual ParseStatus ParseCustom(const StringPiece& option,
2582 std::string* error_msg) OVERRIDE {
2583 {
2584 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2585 if (base_parse != kParseUnknownArgument) {
2586 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002587 }
2588 }
2589
Igor Murashkin37743352014-11-13 14:38:00 -08002590 if (option.starts_with("--oat-file=")) {
2591 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2592 } else if (option.starts_with("--image=")) {
2593 image_location_ = option.substr(strlen("--image=")).data();
2594 } else if (option =="--dump:raw_mapping_table") {
2595 dump_raw_mapping_table_ = true;
2596 } else if (option == "--dump:raw_gc_map") {
2597 dump_raw_gc_map_ = true;
2598 } else if (option == "--no-dump:vmap") {
2599 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002600 } else if (option =="--dump:code_info_stack_maps") {
2601 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002602 } else if (option == "--no-disassemble") {
2603 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002604 } else if (option =="--header-only") {
2605 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002606 } else if (option.starts_with("--symbolize=")) {
2607 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2608 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002609 } else if (option.starts_with("--class-filter=")) {
2610 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002611 } else if (option.starts_with("--method-filter=")) {
2612 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002613 } else if (option.starts_with("--list-classes")) {
2614 list_classes_ = true;
2615 } else if (option.starts_with("--list-methods")) {
2616 list_methods_ = true;
2617 } else if (option.starts_with("--export-dex-to=")) {
2618 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2619 } else if (option.starts_with("--addr2instr=")) {
2620 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2621 *error_msg = "Address conversion failed";
2622 return kParseError;
2623 }
Igor Murashkin37743352014-11-13 14:38:00 -08002624 } else {
2625 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002626 }
2627
Igor Murashkin37743352014-11-13 14:38:00 -08002628 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002629 }
2630
Igor Murashkin37743352014-11-13 14:38:00 -08002631 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2632 // Infer boot image location from the image location if possible.
2633 if (boot_image_location_ == nullptr) {
2634 boot_image_location_ = image_location_;
2635 }
2636
2637 // Perform the parent checks.
2638 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2639 if (parent_checks != kParseOk) {
2640 return parent_checks;
2641 }
2642
2643 // Perform our own checks.
2644 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2645 *error_msg = "Either --image or --oat-file must be specified";
2646 return kParseError;
2647 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2648 *error_msg = "Either --image or --oat-file must be specified but not both";
2649 return kParseError;
2650 }
2651
2652 return kParseOk;
2653 }
2654
2655 virtual std::string GetUsage() const {
2656 std::string usage;
2657
2658 usage +=
2659 "Usage: oatdump [options] ...\n"
2660 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2661 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2662 "\n"
2663 // Either oat-file or image is required.
2664 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2665 " Example: --oat-file=/system/framework/boot.oat\n"
2666 "\n"
2667 " --image=<file.art>: specifies an input image location.\n"
2668 " Example: --image=/system/framework/boot.art\n"
2669 "\n";
2670
2671 usage += Base::GetUsage();
2672
2673 usage += // Optional.
2674 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2675 " Example: --dump:raw_mapping_table\n"
2676 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002677 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002678 " Example: --dump:raw_gc_map\n"
2679 "\n"
2680 " --no-dump:vmap may be used to disable vmap dumping.\n"
2681 " Example: --no-dump:vmap\n"
2682 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002683 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2684 " Example: --dump:code_info_stack_maps\n"
2685 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002686 " --no-disassemble may be used to disable disassembly.\n"
2687 " Example: --no-disassemble\n"
2688 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002689 " --header-only may be used to print only the oat header.\n"
2690 " Example: --header-only\n"
2691 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002692 " --list-classes may be used to list target file classes (can be used with filters).\n"
2693 " Example: --list-classes\n"
2694 " Example: --list-classes --class-filter=com.example.foo\n"
2695 "\n"
2696 " --list-methods may be used to list target file methods (can be used with filters).\n"
2697 " Example: --list-methods\n"
2698 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2699 "\n"
2700 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2701 " Example: --symbolize=/system/framework/boot.oat\n"
2702 "\n"
2703 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2704 " Example: --class-filter=com.example.foo\n"
2705 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002706 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2707 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002708 "\n"
2709 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2710 " Example: --export-dex-to=/data/local/tmp\n"
2711 "\n"
2712 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2713 " address (e.g. PC from crash dump)\n"
2714 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002715 "\n";
2716
2717 return usage;
2718 }
2719
2720 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002721 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002722 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002723 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002724 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002725 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002726 bool dump_raw_mapping_table_ = false;
2727 bool dump_raw_gc_map_ = false;
2728 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002729 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002730 bool disassemble_code_ = true;
2731 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002732 bool list_classes_ = false;
2733 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002734 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002735 uint32_t addr2instr_ = 0;
2736 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002737};
2738
Igor Murashkin37743352014-11-13 14:38:00 -08002739struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2740 virtual bool NeedsRuntime() OVERRIDE {
2741 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002742
Igor Murashkin37743352014-11-13 14:38:00 -08002743 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2744 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2745
2746 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2747 args_->dump_raw_mapping_table_,
2748 args_->dump_raw_gc_map_,
2749 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002750 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002751 args_->disassemble_code_,
2752 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002753 args_->class_filter_,
2754 args_->method_filter_,
2755 args_->list_classes_,
2756 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002757 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002758 args_->export_dex_location_,
2759 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002760
2761 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2762 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002763 }
2764
Igor Murashkin37743352014-11-13 14:38:00 -08002765 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2766 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002767 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002768
Mathieu Chartierd424d082014-10-15 10:31:46 -07002769 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002770
Andreas Gampec24f3992014-12-17 20:40:11 -08002771 if (args_->symbolize_) {
2772 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2773 } else {
2774 return DumpOat(nullptr,
2775 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002776 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002777 args_->os_) == EXIT_SUCCESS;
2778 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002779 }
2780
Igor Murashkin37743352014-11-13 14:38:00 -08002781 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2782 CHECK(args_ != nullptr);
2783
2784 if (args_->oat_filename_ != nullptr) {
2785 return DumpOat(runtime,
2786 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002787 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002788 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002789 }
Igor Murashkin37743352014-11-13 14:38:00 -08002790
Jeff Haodcdc85b2015-12-04 14:06:18 -08002791 return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002792 }
2793
Igor Murashkin37743352014-11-13 14:38:00 -08002794 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2795};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002796
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002797} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002798
2799int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002800 art::OatdumpMain main;
2801 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002802}