blob: 4ca0ee58e3e72bdd52986bc6a8aa0bb5dd7682c8 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080036#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080037#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070038#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070040#include "gc/space/image_space.h"
41#include "gc/space/large_object_space.h"
42#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080043#include "image-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000045#include "linker/buffered_output_stream.h"
46#include "linker/file_output_stream.h"
Ian Rogers1809a722013-08-09 22:05:32 -070047#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/array-inl.h"
49#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010050#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080056#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070057#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058#include "scoped_thread_state_change.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010059#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080061#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080062#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080063#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070064#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070065#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Igor Murashkin37743352014-11-13 14:38:00 -080067#include <sys/stat.h>
68#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070069
Igor Murashkin37743352014-11-13 14:38:00 -080070namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Mathieu Chartiere401d142015-04-22 13:56:20 -070072const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080073 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070074 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070075 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070077 "kRefsOnlySaveMethod",
78 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070079};
80
81const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Mathieu Chartierac8f4392015-08-27 13:54:20 -070086// Map is so that we don't allocate multiple dex files for the same OatDexFile.
87static std::map<const OatFile::OatDexFile*,
88 std::unique_ptr<const DexFile>> opened_dex_files;
89
90const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
91 DCHECK(oat_dex_file != nullptr);
92 auto it = opened_dex_files.find(oat_dex_file);
93 if (it != opened_dex_files.end()) {
94 return it->second.get();
95 }
96 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
97 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
98 return ret;
99}
100
David Srbeckybc90fd02015-04-22 19:40:27 +0100101class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700102 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100103 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100104 oat_file_(oat_file), builder_(nullptr),
105 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700106 }
107
108 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
109 uint32_t,
110 const OatFile::OatMethod&,
111 const DexFile&,
112 uint32_t,
113 const DexFile::CodeItem*,
114 uint32_t);
115
116 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000117 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000118 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
119 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000120
121 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
122 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000123 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
David Srbecky5d811202016-03-08 13:21:22 +0000124 builder_.reset(new ElfBuilder<ElfTypes32>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000125
126 builder_->Start();
127
128 auto* rodata = builder_->GetRoData();
129 auto* text = builder_->GetText();
130 auto* bss = builder_->GetBss();
131 auto* strtab = builder_->GetStrTab();
132 auto* symtab = builder_->GetSymTab();
133
134 rodata->Start();
135 const uint8_t* rodata_begin = oat_file_->Begin();
136 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
137 rodata->WriteFully(rodata_begin, rodata_size);
138 rodata->End();
139
140 text->Start();
141 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
142 const size_t text_size = oat_file_->End() - text_begin;
143 text->WriteFully(text_begin, text_size);
144 text->End();
145
146 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000147 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000148 }
149
Douglas Leung316a2182015-09-17 15:26:25 -0700150 if (isa == kMips || isa == kMips64) {
151 builder_->WriteMIPSabiflagsSection();
152 }
Vladimir Marko944da602016-02-19 12:27:55 +0000153 builder_->PrepareDynamicSection(
154 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
155 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700156
157 Walk(&art::OatSymbolizer::RegisterForDedup);
158
159 NormalizeState();
160
David Srbecky6d8c8f02015-10-26 10:57:09 +0000161 strtab->Start();
162 strtab->Write(""); // strtab should start with empty string.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000163 AddTrampolineSymbols();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700164 Walk(&art::OatSymbolizer::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700166
David Srbecky6d8c8f02015-10-26 10:57:09 +0000167 symtab->Start();
168 symtab->Write();
169 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700170
David Srbecky6d8c8f02015-10-26 10:57:09 +0000171 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172
Vladimir Marko10c13562015-11-25 14:33:36 +0000173 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700174 }
175
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000176 void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
177 if (code_offset != 0) {
178 uint32_t name_offset = builder_->GetStrTab()->Write(name);
179 uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
Tamas Berghammer9e49ab12016-01-28 16:30:14 +0000180 // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
181 // the end of the section in case of the last symbol.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000182 builder_->GetSymTab()->Add(name_offset, builder_->GetText(), symbol_value,
183 /* is_relative */ true, /* size */ 0, STB_GLOBAL, STT_FUNC);
184 }
185 }
186
187 void AddTrampolineSymbols() {
188 const OatHeader& oat_header = oat_file_->GetOatHeader();
189 AddTrampolineSymbol("interpreterToInterpreterBridge",
190 oat_header.GetInterpreterToInterpreterBridgeOffset());
191 AddTrampolineSymbol("interpreterToCompiledCodeBridge",
192 oat_header.GetInterpreterToCompiledCodeBridgeOffset());
193 AddTrampolineSymbol("jniDlsymLookup",
194 oat_header.GetJniDlsymLookupOffset());
195 AddTrampolineSymbol("quickGenericJniTrampoline",
196 oat_header.GetQuickGenericJniTrampolineOffset());
197 AddTrampolineSymbol("quickImtConflictTrampoline",
198 oat_header.GetQuickImtConflictTrampolineOffset());
199 AddTrampolineSymbol("quickResolutionTrampoline",
200 oat_header.GetQuickResolutionTrampolineOffset());
201 AddTrampolineSymbol("quickToInterpreterBridge",
202 oat_header.GetQuickToInterpreterBridgeOffset());
203 }
204
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700205 void Walk(Callback callback) {
206 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
207 for (size_t i = 0; i < oat_dex_files.size(); i++) {
208 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700209 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700210 WalkOatDexFile(oat_dex_file, callback);
211 }
212 }
213
214 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
215 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700216 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
217 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700218 return;
219 }
220 for (size_t class_def_index = 0;
221 class_def_index < dex_file->NumClassDefs();
222 class_def_index++) {
223 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
224 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
225 OatClassType type = oat_class.GetType();
226 switch (type) {
227 case kOatClassAllCompiled:
228 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700229 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700230 break;
231
232 case kOatClassNoneCompiled:
233 case kOatClassMax:
234 // Ignore.
235 break;
236 }
237 }
238 }
239
240 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
241 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700242 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700243 if (class_data == nullptr) { // empty class such as a marker interface?
244 return;
245 }
246 // Note: even if this is an interface or a native class, we still have to walk it, as there
247 // might be a static initializer.
248 ClassDataItemIterator it(dex_file, class_data);
249 SkipAllFields(&it);
250 uint32_t class_method_idx = 0;
251 while (it.HasNextDirectMethod()) {
252 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
253 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700254 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700255 class_method_idx++;
256 it.Next();
257 }
258 while (it.HasNextVirtualMethod()) {
259 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
260 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700261 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700262 class_method_idx++;
263 it.Next();
264 }
265 DCHECK(!it.HasNext());
266 }
267
268 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
269 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
270 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
271 uint32_t method_access_flags, Callback callback) {
272 if ((method_access_flags & kAccAbstract) != 0) {
273 // Abstract method, no code.
274 return;
275 }
276 if (oat_method.GetCodeOffset() == 0) {
277 // No code.
278 return;
279 }
280
281 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
282 method_access_flags);
283 }
284
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700285 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
286 uint32_t class_method_index ATTRIBUTE_UNUSED,
287 const OatFile::OatMethod& oat_method,
288 const DexFile& dex_file ATTRIBUTE_UNUSED,
289 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
290 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
291 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700292 state_[oat_method.GetCodeOffset()]++;
293 }
294
295 void NormalizeState() {
296 for (auto& x : state_) {
297 if (x.second == 1) {
298 state_[x.first] = 0;
299 }
300 }
301 }
302
303 enum class DedupState { // private
304 kNotDeduplicated,
305 kDeduplicatedFirst,
306 kDeduplicatedOther
307 };
308 DedupState IsDuplicated(uint32_t offset) {
309 if (state_[offset] == 0) {
310 return DedupState::kNotDeduplicated;
311 }
312 if (state_[offset] == 1) {
313 return DedupState::kDeduplicatedOther;
314 }
315 state_[offset] = 1;
316 return DedupState::kDeduplicatedFirst;
317 }
318
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700319 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
320 uint32_t class_method_index ATTRIBUTE_UNUSED,
321 const OatFile::OatMethod& oat_method,
322 const DexFile& dex_file,
323 uint32_t dex_method_idx,
324 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
325 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700326 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
327 if (dedup != DedupState::kDeduplicatedOther) {
328 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
329
330 if (dedup == DedupState::kDeduplicatedFirst) {
331 pretty_name = "[Dedup]" + pretty_name;
332 }
333
David Srbecky6d8c8f02015-10-26 10:57:09 +0000334 int name_offset = builder_->GetStrTab()->Write(pretty_name);
335 builder_->GetSymTab()->Add(name_offset, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700336 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
337 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700338 }
339 }
340
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700341 private:
342 static void SkipAllFields(ClassDataItemIterator* it) {
343 while (it->HasNextStaticField()) {
344 it->Next();
345 }
346 while (it->HasNextInstanceField()) {
347 it->Next();
348 }
349 }
350
351 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100352 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700353 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700354 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700355};
356
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700357class OatDumperOptions {
358 public:
359 OatDumperOptions(bool dump_raw_mapping_table,
360 bool dump_raw_gc_map,
361 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100362 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700364 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800365 const char* class_filter,
366 const char* method_filter,
367 bool list_classes,
368 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000369 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800370 const char* export_dex_location,
371 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700372 : dump_raw_mapping_table_(dump_raw_mapping_table),
373 dump_raw_gc_map_(dump_raw_gc_map),
374 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100375 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700376 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700377 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800378 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000379 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800380 list_classes_(list_classes),
381 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000382 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800383 export_dex_location_(export_dex_location),
384 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800385 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700386
387 const bool dump_raw_mapping_table_;
388 const bool dump_raw_gc_map_;
389 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100390 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700391 const bool disassemble_code_;
392 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800393 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000394 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800395 const bool list_classes_;
396 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000397 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800398 const char* const export_dex_location_;
399 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700400 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700401};
402
Elliott Hughese3c845c2012-02-28 17:23:01 -0800403class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700404 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100405 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000406 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800407 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700408 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800409 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800410 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
411 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800412 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000413 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100414 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800415 CHECK(options_.class_loader_ != nullptr);
416 CHECK(options_.class_filter_ != nullptr);
417 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800418 AddAllOffsets();
419 }
420
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700421 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700422 delete disassembler_;
423 }
424
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800425 InstructionSet GetInstructionSet() {
426 return instruction_set_;
427 }
428
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700429 bool Dump(std::ostream& os) {
430 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800431 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700432
433 os << "MAGIC:\n";
434 os << oat_header.GetMagic() << "\n\n";
435
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800436 os << "LOCATION:\n";
437 os << oat_file_.GetLocation() << "\n\n";
438
Brian Carlstromaded5f72011-10-07 17:15:04 -0700439 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800440 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700441
Elliott Hughesa72ec822012-03-05 17:12:22 -0800442 os << "INSTRUCTION SET:\n";
443 os << oat_header.GetInstructionSet() << "\n\n";
444
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700445 {
446 std::unique_ptr<const InstructionSetFeatures> features(
447 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
448 oat_header.GetInstructionSetFeaturesBitmap()));
449 os << "INSTRUCTION SET FEATURES:\n";
450 os << features->GetFeatureString() << "\n\n";
451 }
Dave Allison70202782013-10-22 17:52:19 -0700452
Brian Carlstromaded5f72011-10-07 17:15:04 -0700453 os << "DEX FILE COUNT:\n";
454 os << oat_header.GetDexFileCount() << "\n\n";
455
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800456#define DUMP_OAT_HEADER_OFFSET(label, offset) \
457 os << label " OFFSET:\n"; \
458 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800459 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800460 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
461 } \
462 os << StringPrintf("\n\n");
463
464 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
465 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
466 GetInterpreterToInterpreterBridgeOffset);
467 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
468 GetInterpreterToCompiledCodeBridgeOffset);
469 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
470 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800471 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
472 GetQuickGenericJniTrampolineOffset);
473 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
474 GetQuickImtConflictTrampolineOffset);
475 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
476 GetQuickResolutionTrampolineOffset);
477 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
478 GetQuickToInterpreterBridgeOffset);
479#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700481 os << "IMAGE PATCH DELTA:\n";
482 os << StringPrintf("%d (0x%08x)\n\n",
483 oat_header.GetImagePatchDelta(),
484 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700485
Brian Carlstrom28db0122012-10-18 16:20:41 -0700486 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
487 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
488
489 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800490 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700491
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700492 // Print the key-value store.
493 {
494 os << "KEY VALUE STORE:\n";
495 size_t index = 0;
496 const char* key;
497 const char* value;
498 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
499 os << key << " = " << value << "\n";
500 index++;
501 }
502 os << "\n";
503 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700504
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800505 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700506 os << "BEGIN:\n";
507 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700508
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700509 os << "END:\n";
510 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
511 }
512
513 os << "SIZE:\n";
514 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700515
516 os << std::flush;
517
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800518 // If set, adjust relative address to be searched
519 if (options_.addr2instr_ != 0) {
520 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
521 os << "SEARCH ADDRESS (executable offset + input):\n";
522 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
523 }
524
David Brazdilc03d7b62016-03-02 12:18:03 +0000525 if (!options_.dump_header_only_) {
526 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
527 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
528 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800529
David Brazdilc03d7b62016-03-02 12:18:03 +0000530 // If file export selected skip file analysis
531 if (options_.export_dex_location_) {
532 if (!ExportDexFile(os, *oat_dex_file)) {
533 success = false;
534 }
535 } else {
536 if (!DumpOatDexFile(os, *oat_dex_file)) {
537 success = false;
538 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800539 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700540 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700541 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000542
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700543 os << std::flush;
544 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545 }
546
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800547 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700548 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
549 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 return 0; // Address not in oat file
551 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800552 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
553 reinterpret_cast<uintptr_t>(oat_file_.Begin());
554 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800555 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800556 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800557 return end_offset - begin_offset;
558 }
559
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800560 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700561 return oat_file_.GetOatHeader().GetInstructionSet();
562 }
563
Mathieu Chartier90443472015-07-16 20:32:27 -0700564 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800565 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
566 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700567 CHECK(oat_dex_file != nullptr);
568 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700569 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
570 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700571 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
572 << "': " << error_msg;
573 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800574 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700575 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800576 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700577 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700578 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100579 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800580 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100581 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800582 }
583 }
584 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700585 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800586 }
587
Brian Carlstromaded5f72011-10-07 17:15:04 -0700588 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800589 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800590 // We don't know the length of the code for each method, but we need to know where to stop
591 // when disassembling. What we do know is that a region of code will be followed by some other
592 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
593 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800594 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
595 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700596 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700597 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700598 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
599 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700600 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
601 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800602 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800603 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800604 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800605 for (size_t class_def_index = 0;
606 class_def_index < dex_file->NumClassDefs();
607 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800608 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100609 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700610 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700611 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800612 ClassDataItemIterator it(*dex_file, class_data);
613 SkipAllFields(it);
614 uint32_t class_method_index = 0;
615 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100616 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800617 it.Next();
618 }
619 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100620 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800621 it.Next();
622 }
623 }
624 }
625 }
626
627 // If the last thing in the file is code for a method, there won't be an offset for the "next"
628 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
629 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800630 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800631 }
632
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700633 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
634 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
635 }
636
Elliott Hughese3c845c2012-02-28 17:23:01 -0800637 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800638 uint32_t code_offset = oat_method.GetCodeOffset();
639 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
640 code_offset &= ~0x1;
641 }
642 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800643 offsets_.insert(oat_method.GetMappingTableOffset());
644 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800645 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800646 }
647
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700648 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
649 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800650 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700651 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800652 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800653 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700654
655 // Create the verifier early.
656
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700657 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700658 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
659 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700660 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700661 os << std::flush;
662 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700663 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100664
665 VariableIndentationOutputStream vios(&os);
666 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800667 for (size_t class_def_index = 0;
668 class_def_index < dex_file->NumClassDefs();
669 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700670 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
671 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800672
673 // TODO: Support regex
674 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
675 continue;
676 }
677
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700678 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100679 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700680 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
681 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100682 << " (" << oat_class.GetStatus() << ")"
683 << " (" << oat_class.GetType() << ")\n";
684 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800685 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700686 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700687 success = false;
688 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800689 if (stop_analysis) {
690 os << std::flush;
691 return success;
692 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700693 }
694
695 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700696 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700697 }
698
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800699 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
700 std::string error_msg;
701 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
702
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700703 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800704 if (dex_file == nullptr) {
705 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
706 return false;
707 }
708 size_t fsize = oat_dex_file.FileSize();
709
710 // Some quick checks just in case
711 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
712 os << "Invalid dex file\n";
713 return false;
714 }
715
716 // Verify output directory exists
717 if (!OS::DirectoryExists(options_.export_dex_location_)) {
718 // TODO: Extend OS::DirectoryExists if symlink support is required
719 os << options_.export_dex_location_ << " output directory not found or symlink\n";
720 return false;
721 }
722
723 // Beautify path names
724 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
725 return false;
726 }
727
728 std::string dex_orig_name;
729 size_t dex_orig_pos = dex_file_location.rfind('/');
730 if (dex_orig_pos == std::string::npos)
731 dex_orig_name = dex_file_location;
732 else
733 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
734
735 // A more elegant approach to efficiently name user installed apps is welcome
736 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
737 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
738 size_t apk_orig_pos = dex_file_location.rfind('/');
739 if (apk_orig_pos != std::string::npos) {
740 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
741 }
742 }
743
744 std::string out_dex_path(options_.export_dex_location_);
745 if (out_dex_path.back() != '/') {
746 out_dex_path.append("/");
747 }
748 out_dex_path.append(dex_orig_name);
749 out_dex_path.append("_export.dex");
750 if (out_dex_path.length() > PATH_MAX) {
751 return false;
752 }
753
754 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
755 if (file.get() == nullptr) {
756 os << "Failed to open output dex file " << out_dex_path;
757 return false;
758 }
759
760 if (!file->WriteFully(dex_file->Begin(), fsize)) {
761 os << "Failed to write dex file";
762 file->Erase();
763 return false;
764 }
765
766 if (file->FlushCloseOrErase() != 0) {
767 os << "Flush and close failed";
768 return false;
769 }
770
771 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
772 os << std::flush;
773
774 return true;
775 }
776
Elliott Hughese3c845c2012-02-28 17:23:01 -0800777 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700778 while (it.HasNextStaticField()) {
779 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700780 }
Ian Rogers0571d352011-11-03 19:51:38 -0700781 while (it.HasNextInstanceField()) {
782 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700783 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800784 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700785
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100786 bool DumpOatClass(VariableIndentationOutputStream* vios,
787 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800788 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700789 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800790 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700791 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700792 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100793 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700794 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800795 }
796 ClassDataItemIterator it(dex_file, class_data);
797 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700798 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700799 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100800 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700801 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800802 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700803 success = false;
804 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800805 if (addr_found) {
806 *stop_analysis = true;
807 return success;
808 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700809 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700810 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700811 }
Ian Rogers0571d352011-11-03 19:51:38 -0700812 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100813 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700814 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800815 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700816 success = false;
817 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800818 if (addr_found) {
819 *stop_analysis = true;
820 return success;
821 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700822 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700823 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700824 }
Ian Rogers0571d352011-11-03 19:51:38 -0700825 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100826 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700827 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700828 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800829
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700830 static constexpr uint32_t kPrologueBytes = 16;
831
832 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
833 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
834
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100835 bool DumpOatMethod(VariableIndentationOutputStream* vios,
836 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700837 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700838 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800839 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800840 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700841 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800842
843 // TODO: Support regex
844 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
845 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000846 return success;
847 }
848
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800849 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100850 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
851 class_method_index, pretty_method.c_str(),
852 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800853 if (options_.list_methods_) return success;
854
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800855 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
856 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
857 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
858 uint32_t code_offset = oat_method.GetCodeOffset();
859 uint32_t code_size = oat_method.GetQuickCodeSize();
860 if (resolved_addr2instr_ != 0) {
861 if (resolved_addr2instr_ > code_offset + code_size) {
862 return success;
863 } else {
864 *addr_found = true; // stop analyzing file at next iteration
865 }
866 }
867
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100868 // Everything below is indented at least once.
869 ScopedIndentation indent1(vios);
870
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800871 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100872 vios->Stream() << "DEX CODE:\n";
873 ScopedIndentation indent2(vios);
874 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700875 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700876
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700877 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700878 std::unique_ptr<verifier::MethodVerifier> verifier;
879 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700880 // We need to have the handle scope stay live until after the verifier since the verifier has
881 // a handle to the dex cache from hs.
882 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100883 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
884 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700885 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100886 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700887 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700888 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800889 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100890 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800891 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100892 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100893 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100894 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100896 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700897 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
898 oat_method_offsets_offset, oat_file_.Size());
899 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100900 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700901 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800902 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100904 ScopedIndentation indent2(vios);
905 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700906 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
907 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100908 vios->Stream() << StringPrintf("WARNING: "
909 "code offset 0x%08x is past end of file 0x%08zx.\n",
910 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700911 success = false;
912 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100913 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700914
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100915 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800916 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100917 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700918 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800919 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100920 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700921 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100922 vios->Stream() << StringPrintf("WARNING: "
923 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
924 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700925 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800926 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100927 ScopedIndentation indent3(vios);
928 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800929 }
930 }
931 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100932 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
934 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700935
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800936 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100937 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700938 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100939 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100941 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700942 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
943 method_header_offset, oat_file_.Size());
944 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100945 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700946 return false;
947 }
948
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100949 ScopedIndentation indent2(vios);
950 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800951 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100952 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700953 }
954 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100955 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700956 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100957 vios->Stream() << StringPrintf("WARNING: "
958 "mapping table offset 0x%08x is past end of file 0x%08zx. "
959 "mapping table offset was loaded from offset 0x%08x.\n",
960 mapping_table_offset, oat_file_.Size(),
961 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700962 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800963 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100964 ScopedIndentation indent3(vios);
965 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700966 }
967
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100968 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800969 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100970 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700971 }
972 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100973 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700974 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100975 vios->Stream() << StringPrintf("WARNING: "
976 "vmap table offset 0x%08x is past end of file 0x%08zx. "
977 "vmap table offset was loaded from offset 0x%08x.\n",
978 vmap_table_offset, oat_file_.Size(),
979 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700980 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800981 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100982 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700983 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800984 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700985 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100986 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100988 ScopedIndentation indent2(vios);
989 vios->Stream()
990 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
991 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
992 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
993 vios->Stream() << "\n";
994 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
995 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
996 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700997 }
998 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100999 // Based on spill masks from QuickMethodFrameInfo so placed
1000 // after it is dumped, but useful for understanding quick
1001 // code, so dumped here.
1002 ScopedIndentation indent2(vios);
1003 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001004 }
1005 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001006 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001007 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1008 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001009 ScopedIndentation indent2(vios);
1010 vios->Stream() << StringPrintf("WARNING: "
1011 "code size offset 0x%08x is past end of file 0x%08zx.",
1012 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001013 success = false;
1014 } else {
1015 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001016 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1017 uint64_t aligned_code_end = aligned_code_begin + code_size;
1018
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001019 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001020 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001021 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1023 code_offset,
1024 code_size_offset,
1025 code_size,
1026 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001027
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001028 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001029 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001030 vios->Stream() << StringPrintf("WARNING: "
1031 "start of code at 0x%08x is past end of file 0x%08zx.",
1032 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001033 success = false;
1034 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001035 vios->Stream() << StringPrintf(
1036 "WARNING: "
1037 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1038 "code size is 0x%08x loaded from offset 0x%08x.\n",
1039 aligned_code_end, oat_file_.Size(),
1040 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001041 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001042 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001043 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001044 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001045 }
1046 }
1047 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001048 vios->Stream() << StringPrintf(
1049 "WARNING: "
1050 "code size %d is bigger than max expected threshold of %d. "
1051 "code size is 0x%08x loaded from offset 0x%08x.\n",
1052 code_size, kMaxCodeSize,
1053 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001054 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001055 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001056 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001057 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001058 }
1059 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001060 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001061 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001062 }
1063 }
1064 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001065 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001066 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001067 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001068
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001069 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1070 if (spill_mask == 0) {
1071 return;
1072 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001073 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001074 for (size_t i = 0; i < 32; i++) {
1075 if ((spill_mask & (1 << i)) != 0) {
1076 if (is_float) {
1077 os << "fr" << i;
1078 } else {
1079 os << "r" << i;
1080 }
1081 spill_mask ^= 1 << i; // clear bit
1082 if (spill_mask != 0) {
1083 os << ", ";
1084 } else {
1085 break;
1086 }
1087 }
1088 }
1089 os << ")";
1090 }
1091
Roland Levillain442b46a2015-02-18 16:54:21 +00001092 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001093 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001094 const OatFile::OatMethod& oat_method,
1095 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001096 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1097 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001098 const void* raw_code_info = oat_method.GetVmapTable();
1099 if (raw_code_info != nullptr) {
1100 CodeInfo code_info(raw_code_info);
1101 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001102 ScopedIndentation indent1(vios);
1103 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001104 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001105 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1106 // We don't encode the size in the table, so just emit that we have quickened
1107 // information.
1108 ScopedIndentation indent(vios);
1109 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001110 } else {
1111 // Otherwise, display the vmap table.
1112 const uint8_t* raw_table = oat_method.GetVmapTable();
1113 if (raw_table != nullptr) {
1114 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001115 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001116 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001117 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001118 }
1119
1120 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001121 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001122 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001123 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001124 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001125 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001126 oat_method.GetCodeOffset(),
1127 code_item.registers_size_,
1128 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001129 }
1130
Roland Levillain442b46a2015-02-18 16:54:21 +00001131 // Display a vmap table.
1132 void DumpVmapTable(std::ostream& os,
1133 const OatFile::OatMethod& oat_method,
1134 const VmapTable& vmap_table) {
1135 bool first = true;
1136 bool processing_fp = false;
1137 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1138 for (size_t i = 0; i < vmap_table.Size(); i++) {
1139 uint16_t dex_reg = vmap_table[i];
1140 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1141 processing_fp ? kFloatVReg : kIntVReg);
1142 os << (first ? "v" : ", v") << dex_reg;
1143 if (!processing_fp) {
1144 os << "/r" << cpu_reg;
1145 } else {
1146 os << "/fr" << cpu_reg;
1147 }
1148 first = false;
1149 if (!processing_fp && dex_reg == 0xFFFF) {
1150 processing_fp = true;
1151 spill_mask = oat_method.GetFpSpillMask();
1152 }
1153 }
1154 os << "\n";
1155 }
1156
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001157 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1158 const DexFile::CodeItem* code_item) {
1159 if (code_item != nullptr) {
1160 size_t num_locals_ins = code_item->registers_size_;
1161 size_t num_ins = code_item->ins_size_;
1162 size_t num_locals = num_locals_ins - num_ins;
1163 size_t num_outs = code_item->outs_size_;
1164
1165 os << "vr_stack_locations:";
1166 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1167 // For readability, delimit the different kinds of VRs.
1168 if (reg == num_locals_ins) {
1169 os << "\n\tmethod*:";
1170 } else if (reg == num_locals && num_ins > 0) {
1171 os << "\n\tins:";
1172 } else if (reg == 0 && num_locals > 0) {
1173 os << "\n\tlocals:";
1174 }
1175
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001176 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1177 code_item,
1178 oat_method.GetCoreSpillMask(),
1179 oat_method.GetFpSpillMask(),
1180 oat_method.GetFrameSizeInBytes(),
1181 reg,
1182 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001183 os << " v" << reg << "[sp + #" << offset << "]";
1184 }
1185
1186 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1187 if (out_reg == 0) {
1188 os << "\n\touts:";
1189 }
1190
1191 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1192 os << " v" << out_reg << "[sp + #" << offset << "]";
1193 }
1194
1195 os << "\n";
1196 }
1197 }
1198
Ian Rogersb23a7722012-10-09 16:54:26 -07001199 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001200 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001201 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001202 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001203 const VmapTable vmap_table(raw_table);
1204 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001205 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001206 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1207 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1208 : oat_method.GetCoreSpillMask();
1209 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001210 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001211 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1212 code_item,
1213 oat_method.GetCoreSpillMask(),
1214 oat_method.GetFpSpillMask(),
1215 oat_method.GetFrameSizeInBytes(),
1216 reg,
1217 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001218 os << "[sp + #" << offset << "]";
1219 }
1220 }
1221 }
1222
Ian Rogersef7d42f2014-01-06 12:55:46 -08001223 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1224 const DexFile::CodeItem* code_item,
1225 size_t num_regs, const uint8_t* reg_bitmap) {
1226 bool first = true;
1227 for (size_t reg = 0; reg < num_regs; reg++) {
1228 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1229 if (first) {
1230 os << " v" << reg << " (";
1231 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1232 os << ")";
1233 first = false;
1234 } else {
1235 os << ", v" << reg << " (";
1236 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1237 os << ")";
1238 }
1239 }
1240 }
1241 if (first) {
1242 os << "No registers in GC map\n";
1243 } else {
1244 os << "\n";
1245 }
1246 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001247 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1248 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001249 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001250 if (gc_map_raw == nullptr) {
1251 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001252 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001253 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001254 NativePcOffsetToReferenceMap map(gc_map_raw);
1255 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1256 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1257 map.GetNativePcOffset(entry);
1258 os << StringPrintf("%p", native_pc);
1259 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001260 }
1261 }
1262
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001263 void DumpMappingTable(VariableIndentationOutputStream* vios,
1264 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001265 const void* quick_code = oat_method.GetQuickCode();
1266 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001267 return;
1268 }
Ian Rogers1809a722013-08-09 22:05:32 -07001269 MappingTable table(oat_method.GetMappingTable());
1270 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001271 if (table.PcToDexSize() != 0) {
1272 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001273 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001274 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001275 ScopedIndentation indent1(vios);
1276 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001277 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001278 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001279 }
1280 if (table.DexToPcSize() != 0) {
1281 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001282 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001283 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); 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 Rogers3a5c1ce2012-02-29 10:06:46 -08001288 }
1289 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001290 }
1291
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001292 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001293 const OatFile::OatMethod& oat_method,
1294 const DexFile::CodeItem* code_item,
1295 size_t offset,
1296 bool suspend_point_mapping) {
1297 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1298 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001299 ScopedIndentation indent1(vios);
1300 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001301 }
1302 // The return value is not used in the case of a method compiled
1303 // with the optimizing compiler.
1304 return DexFile::kDexNoIndex;
1305 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001306 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001307 }
1308 }
1309
Ian Rogers1809a722013-08-09 22:05:32 -07001310 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1311 size_t offset, bool suspend_point_mapping) {
1312 MappingTable table(oat_method.GetMappingTable());
1313 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1314 typedef MappingTable::PcToDexIterator It;
1315 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1316 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001317 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001318 return cur.DexPc();
1319 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001320 }
Ian Rogers1809a722013-08-09 22:05:32 -07001321 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1322 typedef MappingTable::DexToPcIterator It;
1323 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1324 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001325 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001326 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001327 }
1328 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001329 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001330 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001331 }
1332
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001333 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1334 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001335 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001336 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001337 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001338 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001339 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001340 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001341 bool first = true;
1342 for (size_t reg = 0; reg < num_regs; reg++) {
1343 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1344 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001345 os << "GC map objects: v" << reg << " (";
1346 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001347 os << ")";
1348 first = false;
1349 } else {
1350 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001352 os << ")";
1353 }
1354 }
1355 }
1356 if (!first) {
1357 os << "\n";
1358 }
1359 }
1360 }
1361 }
1362
Mathieu Chartier590fee92013-09-13 13:46:47 -07001363 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1364 const OatFile::OatMethod& oat_method,
1365 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1366 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001367 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001368 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001369 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1370 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1371 if (kind != kUndefined) {
1372 if (first) {
1373 os << "VRegs: v";
1374 first = false;
1375 } else {
1376 os << ", v";
1377 }
1378 os << reg << " (";
1379 switch (kind) {
1380 case kImpreciseConstant:
1381 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1382 DescribeVReg(os, oat_method, code_item, reg, kind);
1383 break;
1384 case kConstant:
1385 os << "Constant: " << kinds.at((reg * 2) + 1);
1386 break;
1387 default:
1388 DescribeVReg(os, oat_method, code_item, reg, kind);
1389 break;
1390 }
1391 os << ")";
1392 }
1393 }
1394 if (!first) {
1395 os << "\n";
1396 }
1397 }
1398
1399
Ian Rogersb23a7722012-10-09 16:54:26 -07001400 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001401 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001402 size_t i = 0;
1403 while (i < code_item->insns_size_in_code_units_) {
1404 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001405 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1406 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001407 i += instruction->SizeInCodeUnits();
1408 }
1409 }
1410 }
1411
Roland Levillainf2650d12015-05-28 14:53:28 +01001412 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1413 // the optimizing compiler?
1414 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1415 const DexFile::CodeItem* code_item) {
1416 // If the native GC map is null and the Dex `code_item` is not
1417 // null, then this method has been compiled with the optimizing
1418 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001419 return oat_method.GetQuickCode() != nullptr &&
1420 oat_method.GetGcMap() == nullptr &&
1421 code_item != nullptr;
1422 }
1423
1424 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1425 // the dextodex compiler?
1426 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1427 const DexFile::CodeItem* code_item) {
1428 // If the quick code is null, the Dex `code_item` is not
1429 // null, and the vmap table is not null, then this method has been compiled
1430 // with the dextodex compiler.
1431 return oat_method.GetQuickCode() == nullptr &&
1432 oat_method.GetVmapTable() != nullptr &&
1433 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001434 }
1435
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001436 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001437 const OatFile::OatMethod& oat_method,
1438 const DexFile::CodeItem* code_item,
1439 size_t offset) {
1440 // This method is only relevant for oat methods compiled with the
1441 // optimizing compiler.
1442 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1443
1444 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1445 const void* raw_code_info = oat_method.GetVmapTable();
1446 if (raw_code_info != nullptr) {
1447 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001448 StackMapEncoding encoding = code_info.ExtractEncoding();
1449 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001450 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001451 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1452 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001453 }
1454 }
1455 }
1456
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001457 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001458 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001459 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001460 const DexFile* dex_file,
1461 const DexFile::ClassDef& class_def,
1462 const DexFile::CodeItem* code_item,
1463 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 if ((method_access_flags & kAccNative) == 0) {
1465 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001466 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001467 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001468 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1469 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001470 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001471 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001472 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1473 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001474 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001475
1476 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001477 }
1478
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001479 void DumpCode(VariableIndentationOutputStream* vios,
1480 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001481 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1482 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001483 const void* quick_code = oat_method.GetQuickCode();
1484
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001485 if (code_size == 0) {
1486 code_size = oat_method.GetQuickCodeSize();
1487 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001488 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001489 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001490 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001491 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001492 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1493 size_t offset = 0;
1494 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001495 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001496 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001497 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001498 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001499 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001500 uint32_t dex_pc =
1501 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001502 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001503 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001504 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001505 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001506 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001507 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001508 }
1509 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001510 }
1511 }
1512
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001513 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001514 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001515 const OatDumperOptions& options_;
1516 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001517 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001518 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001519 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001520};
1521
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001522class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001523 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001524 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1525 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001526 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001527 vios_(os),
1528 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001529 image_space_(image_space),
1530 image_header_(image_header),
1531 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001532
Mathieu Chartier90443472015-07-16 20:32:27 -07001533 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001534 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001535 std::ostream& indent_os = vios_.Stream();
1536
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001537 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001538
Jeff Haodcdc85b2015-12-04 14:06:18 -08001539 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1540
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001541 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001542
Mathieu Chartiere401d142015-04-22 13:56:20 -07001543 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1544
1545 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1546 auto section = static_cast<ImageHeader::ImageSections>(i);
1547 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1548 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001549
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001550 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001551
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001552 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001553
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001554 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1555
1556 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1557
1558 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001559
Alex Lighta59dd802014-07-02 16:28:08 -07001560 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1561
Igor Murashkin46774762014-10-22 11:37:02 -07001562 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1563
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 {
1565 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001566 static_assert(arraysize(image_roots_descriptions_) ==
1567 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1569 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1570 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001571 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001572 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001573 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001574 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001575 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001576 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001577 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1578 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001579 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001580 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1581 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001582 run++;
1583 } else {
1584 break;
1585 }
1586 }
1587 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001588 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001589 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001590 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001591 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001592 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001593 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001594 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001596 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001597 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001598 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001599 }
1600 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001601 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001602
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001603 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001604 os << "METHOD ROOTS\n";
1605 static_assert(arraysize(image_methods_descriptions_) ==
1606 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1607 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1608 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1609 const char* description = image_methods_descriptions_[i];
1610 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001611 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001612 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001613 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001615
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001616 Runtime* const runtime = Runtime::Current();
1617 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001618 std::string image_filename = image_space_.GetImageFilename();
1619 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001620 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001622 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001623 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1624 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001625 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001626 oat_file = OatFile::Open(oat_location, oat_location,
1627 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001628 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001629 if (oat_file == nullptr) {
1630 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001631 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001632 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001633 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001634 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001635
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001636 stats_.oat_file_bytes = oat_file->Size();
1637
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001638 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001639
Mathieu Chartier02e25112013-08-14 16:14:24 -07001640 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001641 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001642 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1643 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001644 }
1645
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001646 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001647
Jeff Haodcdc85b2015-12-04 14:06:18 -08001648 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001649 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001650 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001651 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001652 {
1653 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1654 heap->FlushAllocStack();
1655 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001656 // Since FlushAllocStack() above resets the (active) allocation
1657 // stack. Need to revoke the thread-local allocation stacks that
1658 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001659 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001660 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001661 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001662 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001663 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001664 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001665 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001666 {
1667 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001668 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001669 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001670 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001671 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001672 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001673 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001674 }
1675 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001676 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001677 // Dump the normal objects before ArtMethods.
1678 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1679 indent_os << "\n";
1680 // TODO: Dump fields.
1681 // Dump methods after.
1682 const auto& methods_section = image_header_.GetMethodsSection();
1683 const size_t pointer_size =
1684 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1685 DumpArtMethodVisitor visitor(this);
1686 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001688 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001689 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001690 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001691 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001692 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001693 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001694 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001695 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001696 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001697 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001698 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001699 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001700 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001701 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1702 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001703 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1704 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001705 const auto& intern_section = image_header_.GetImageSection(
1706 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001707 const auto& class_table_section = image_header_.GetImageSection(
1708 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001709 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1710
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001711 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001712
1713 // Objects are kObjectAlignment-aligned.
1714 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1715 if (object_section.Offset() > header_bytes) {
1716 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1717 }
1718
1719 // Field section is 4-byte aligned.
1720 constexpr size_t kFieldSectionAlignment = 4U;
1721 uint32_t end_objects = object_section.Offset() + object_section.Size();
1722 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1723 stats_.alignment_bytes += field_section.Offset() - end_objects;
1724
1725 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1726 uint32_t end_fields = field_section.Offset() + field_section.Size();
1727 CHECK_ALIGNED(method_section.Offset(), 4);
1728 stats_.alignment_bytes += method_section.Offset() - end_fields;
1729
1730 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1731 uint32_t end_methods = method_section.Offset() + method_section.Size();
1732 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1733 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1734
1735 // Intern table is 8-byte aligned.
1736 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1737 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1738 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1739
1740 // Add space between intern table and class table.
1741 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1742 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1743
1744 // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1745 uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1746 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1747 stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1748
Mathieu Chartiere401d142015-04-22 13:56:20 -07001749 stats_.bitmap_bytes += bitmap_section.Size();
1750 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001751 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001752 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001753 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001754 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001755 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001756 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001757
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001758 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001759
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001760 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001761 }
1762
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001763 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001764 class DumpArtMethodVisitor : public ArtMethodVisitor {
1765 public:
1766 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1767
1768 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1769 std::ostream& indent_os = image_dumper_->vios_.Stream();
1770 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1771 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1772 indent_os << "\n";
1773 }
1774
1775 private:
1776 ImageDumper* const image_dumper_;
1777 };
1778
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001779 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001780 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001781 CHECK(type != nullptr);
1782 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001783 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001784 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001785 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001786 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001787 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001788 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001789 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001790 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001791 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001792 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001793 }
1794 }
1795
Mathieu Chartierc7853442015-03-27 14:35:38 -07001796 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001797 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001798 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001799 switch (field->GetTypeAsPrimitiveType()) {
1800 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001801 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001802 break;
1803 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001804 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001805 break;
1806 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001807 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001808 break;
1809 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001810 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001811 break;
1812 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001813 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001814 break;
1815 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001816 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001817 break;
1818 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001819 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1820 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001821 break;
1822 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001823 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001824 break;
1825 case Primitive::kPrimNot: {
1826 // Get the value, don't compute the type unless it is non-null as we don't want
1827 // to cause class loading.
1828 mirror::Object* value = field->GetObj(obj);
1829 if (value == nullptr) {
1830 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001831 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001832 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001833 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001834 if (field_type != nullptr) {
1835 PrettyObjectValue(os, field_type, value);
1836 } else {
1837 os << StringPrintf("%p %s\n", value,
1838 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1839 }
Ian Rogers50239c72013-06-17 14:53:22 -07001840 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001841 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001842 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001843 default:
1844 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1845 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001846 }
1847 }
1848
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001849 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001850 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001851 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001852 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001853 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001854 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001855 for (ArtField& field : klass->GetIFields()) {
1856 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001857 }
1858 }
1859
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001860 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001861 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001862 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001863
Mathieu Chartiere401d142015-04-22 13:56:20 -07001864 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001865 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001866 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1867 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001868 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001869 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001870 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001871 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001872 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001873 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001874 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001875 }
1876
Mathieu Chartiere401d142015-04-22 13:56:20 -07001877 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001878 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001879 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1880 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001881 return 0;
1882 }
1883 return oat_code_begin[-1];
1884 }
1885
Mathieu Chartiere401d142015-04-22 13:56:20 -07001886 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001887 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001888 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001889 if (oat_code_begin == nullptr) {
1890 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001891 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001892 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001893 }
1894
Mathieu Chartier90443472015-07-16 20:32:27 -07001895 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001896 DCHECK(obj != nullptr);
1897 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001898 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001899 if (!state->InDumpSpace(obj)) {
1900 return;
1901 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001902
1903 size_t object_bytes = obj->SizeOf();
1904 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1905 state->stats_.object_bytes += object_bytes;
1906 state->stats_.alignment_bytes += alignment_bytes;
1907
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001908 std::ostream& os = state->vios_.Stream();
1909
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001910 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001911 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001912 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1913 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001914 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001915 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001916 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1917 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001918 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001919 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001920 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001921 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001922 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001923 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001924 ScopedIndentation indent1(&state->vios_);
1925 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001926 const auto image_pointer_size =
1927 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001928 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001929 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1930 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001931 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001932 size_t run = 0;
1933 for (int32_t j = i + 1; j < length; j++) {
1934 if (value == obj_array->Get(j)) {
1935 run++;
1936 } else {
1937 break;
1938 }
1939 }
1940 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001941 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001942 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001943 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001944 i = i + run;
1945 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001946 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001947 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001948 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001949 }
1950 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001951 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001952 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001953 os << "STATICS:\n";
1954 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001955 for (ArtField& field : klass->GetSFields()) {
1956 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001957 }
1958 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001959 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001960 auto it = state->dex_caches_.find(obj);
1961 if (it != state->dex_caches_.end()) {
1962 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001963 const auto& field_section = state->image_header_.GetImageSection(
1964 ImageHeader::kSectionArtFields);
1965 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001966 size_t num_methods = dex_cache->NumResolvedMethods();
1967 if (num_methods != 0u) {
1968 os << "Methods (size=" << num_methods << "):";
1969 ScopedIndentation indent2(&state->vios_);
1970 auto* resolved_methods = dex_cache->GetResolvedMethods();
1971 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1972 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1973 size_t run = 0;
1974 for (size_t j = i + 1;
1975 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1976 j,
1977 image_pointer_size);
1978 ++j, ++run) {}
1979 if (run == 0) {
1980 os << StringPrintf("%zd: ", i);
1981 } else {
1982 os << StringPrintf("%zd to %zd: ", i, i + run);
1983 i = i + run;
1984 }
1985 std::string msg;
1986 if (elem == nullptr) {
1987 msg = "null";
1988 } else if (method_section.Contains(
1989 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1990 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1991 } else {
1992 msg = "<not in method section>";
1993 }
1994 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001995 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001996 }
1997 size_t num_fields = dex_cache->NumResolvedFields();
1998 if (num_fields != 0u) {
1999 os << "Fields (size=" << num_fields << "):";
2000 ScopedIndentation indent2(&state->vios_);
2001 auto* resolved_fields = dex_cache->GetResolvedFields();
2002 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
2003 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
2004 size_t run = 0;
2005 for (size_t j = i + 1;
2006 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
2007 j,
2008 image_pointer_size);
2009 ++j, ++run) {}
2010 if (run == 0) {
2011 os << StringPrintf("%zd: ", i);
2012 } else {
2013 os << StringPrintf("%zd to %zd: ", i, i + run);
2014 i = i + run;
2015 }
2016 std::string msg;
2017 if (elem == nullptr) {
2018 msg = "null";
2019 } else if (field_section.Contains(
2020 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2021 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
2022 } else {
2023 msg = "<not in field section>";
2024 }
2025 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002026 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002027 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002028 }
2029 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002030 std::string temp;
2031 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002032 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002033
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002035 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002036 DCHECK(method != nullptr);
2037 const auto image_pointer_size =
2038 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002039 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2040 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002041 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2042 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002044 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2045 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2046 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2047 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002049 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002050 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002051 if (first_occurrence) {
2052 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2053 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002054 if (quick_oat_code_begin !=
2055 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
2056 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002057 }
2058 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2059 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2060 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002061 } else {
2062 const DexFile::CodeItem* code_item = method->GetCodeItem();
2063 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2064 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2065
2066 bool first_occurrence;
2067 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002068 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002069 if (first_occurrence) {
2070 state->stats_.gc_map_bytes += gc_map_bytes;
2071 }
2072
2073 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002074 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002075 if (first_occurrence) {
2076 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2077 }
2078
Roland Levillain6d7f1792015-07-02 10:59:15 +01002079 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002080 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002081 // Method compiled with the optimizing compiler have no vmap table.
2082 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002083 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002084 if (first_occurrence) {
2085 state->stats_.vmap_table_bytes += vmap_table_bytes;
2086 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002087 }
2088
Mathieu Chartiere401d142015-04-22 13:56:20 -07002089 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2090 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2091 if (first_occurrence) {
2092 state->stats_.managed_code_bytes += quick_oat_code_size;
2093 if (method->IsConstructor()) {
2094 if (method->IsStatic()) {
2095 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2096 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2097 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2098 }
2099 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2100 state->stats_.large_method_code_bytes += quick_oat_code_size;
2101 }
2102 }
2103 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2104
Igor Murashkin7617abd2015-07-10 18:27:47 -07002105 uint32_t method_access_flags = method->GetAccessFlags();
2106
Mathieu Chartiere401d142015-04-22 13:56:20 -07002107 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002108 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2109 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2110 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002111
2112 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002113 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002114
2115 double expansion =
2116 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2117 state->stats_.ComputeOutliers(total_size, expansion, method);
2118 }
2119 }
2120
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002121 std::set<const void*> already_seen_;
2122 // Compute the size of the given data within the oat file and whether this is the first time
2123 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002124 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002125 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002126 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002127 already_seen_.insert(oat_data);
2128 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002129 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002130 }
2131 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002132 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002133
2134 public:
2135 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002136 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002137 size_t file_bytes;
2138
2139 size_t header_bytes;
2140 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002141 size_t art_field_bytes;
2142 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002143 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002144 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002145 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002146 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002147 size_t alignment_bytes;
2148
2149 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002150 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002151 size_t managed_to_native_code_bytes;
2152 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002153 size_t class_initializer_code_bytes;
2154 size_t large_initializer_code_bytes;
2155 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002156
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002157 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002158 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002159 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002160
2161 size_t dex_instruction_bytes;
2162
Mathieu Chartiere401d142015-04-22 13:56:20 -07002163 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002164 std::vector<size_t> method_outlier_size;
2165 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002166 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002167
Roland Levillain3887c462015-08-12 18:15:42 +01002168 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002169 : oat_file_bytes(0),
2170 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002171 header_bytes(0),
2172 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002173 art_field_bytes(0),
2174 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002175 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002176 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002177 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002178 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002179 alignment_bytes(0),
2180 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002181 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002182 managed_to_native_code_bytes(0),
2183 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002184 class_initializer_code_bytes(0),
2185 large_initializer_code_bytes(0),
2186 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002187 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002188 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002189 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002190 dex_instruction_bytes(0) {}
2191
Elliott Hughesa0e18062012-04-13 15:59:59 -07002192 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002193 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002194 size_t bytes;
2195 size_t count;
2196 };
2197 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2198 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002199
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002200 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002201 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2202 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002203 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002204 it->second.count += 1;
2205 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002206 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002207 }
2208 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002209
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002210 double PercentOfOatBytes(size_t size) {
2211 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2212 }
2213
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002214 double PercentOfFileBytes(size_t size) {
2215 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2216 }
2217
2218 double PercentOfObjectBytes(size_t size) {
2219 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2220 }
2221
Mathieu Chartiere401d142015-04-22 13:56:20 -07002222 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002223 method_outlier_size.push_back(total_size);
2224 method_outlier_expansion.push_back(expansion);
2225 method_outlier.push_back(method);
2226 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002227
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002228 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002229 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002230 size_t sum_of_sizes = 0;
2231 size_t sum_of_sizes_squared = 0;
2232 size_t sum_of_expansion = 0;
2233 size_t sum_of_expansion_squared = 0;
2234 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002235 if (n == 0) {
2236 return;
2237 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002238 for (size_t i = 0; i < n; i++) {
2239 size_t cur_size = method_outlier_size[i];
2240 sum_of_sizes += cur_size;
2241 sum_of_sizes_squared += cur_size * cur_size;
2242 double cur_expansion = method_outlier_expansion[i];
2243 sum_of_expansion += cur_expansion;
2244 sum_of_expansion_squared += cur_expansion * cur_expansion;
2245 }
2246 size_t size_mean = sum_of_sizes / n;
2247 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2248 double expansion_mean = sum_of_expansion / n;
2249 double expansion_variance =
2250 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2251
2252 // Dump methods whose size is a certain number of standard deviations from the mean
2253 size_t dumped_values = 0;
2254 size_t skipped_values = 0;
2255 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2256 size_t cur_size_variance = i * i * size_variance;
2257 bool first = true;
2258 for (size_t j = 0; j < n; j++) {
2259 size_t cur_size = method_outlier_size[j];
2260 if (cur_size > size_mean) {
2261 size_t cur_var = cur_size - size_mean;
2262 cur_var = cur_var * cur_var;
2263 if (cur_var > cur_size_variance) {
2264 if (dumped_values > 20) {
2265 if (i == 1) {
2266 skipped_values++;
2267 } else {
2268 i = 2; // jump to counting for 1 standard deviation
2269 break;
2270 }
2271 } else {
2272 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002273 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002274 first = false;
2275 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002276 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002277 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002278 method_outlier_size[j] = 0; // don't consider this method again
2279 dumped_values++;
2280 }
2281 }
2282 }
2283 }
2284 }
2285 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002286 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002287 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002288 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002289 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002290
2291 // Dump methods whose expansion is a certain number of standard deviations from the mean
2292 dumped_values = 0;
2293 skipped_values = 0;
2294 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2295 double cur_expansion_variance = i * i * expansion_variance;
2296 bool first = true;
2297 for (size_t j = 0; j < n; j++) {
2298 double cur_expansion = method_outlier_expansion[j];
2299 if (cur_expansion > expansion_mean) {
2300 size_t cur_var = cur_expansion - expansion_mean;
2301 cur_var = cur_var * cur_var;
2302 if (cur_var > cur_expansion_variance) {
2303 if (dumped_values > 20) {
2304 if (i == 1) {
2305 skipped_values++;
2306 } else {
2307 i = 2; // jump to counting for 1 standard deviation
2308 break;
2309 }
2310 } else {
2311 if (first) {
2312 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002313 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002314 first = false;
2315 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002316 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002317 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002318 method_outlier_expansion[j] = 0.0; // don't consider this method again
2319 dumped_values++;
2320 }
2321 }
2322 }
2323 }
2324 }
2325 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002327 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002328 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002329 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002330 }
2331
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002332 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002333 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002334 {
2335 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2336 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002337 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2338 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2339 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2340 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2341 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2342 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002343 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002344 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2345 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002346 header_bytes, PercentOfFileBytes(header_bytes),
2347 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002348 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2349 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002350 dex_cache_arrays_bytes,
2351 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002352 interned_strings_bytes,
2353 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002354 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002355 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002356 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2357 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002358 CHECK_EQ(file_bytes,
2359 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2360 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2361 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002362 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002363
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002364 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002365 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002366 for (const auto& sizes_and_count : sizes_and_counts) {
2367 const std::string& descriptor(sizes_and_count.first);
2368 double average = static_cast<double>(sizes_and_count.second.bytes) /
2369 static_cast<double>(sizes_and_count.second.count);
2370 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002371 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002372 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002373 descriptor.c_str(), sizes_and_count.second.bytes,
2374 sizes_and_count.second.count, average, percent);
2375 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002376 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002377 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002378 CHECK_EQ(object_bytes, object_bytes_total);
2379
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 os << StringPrintf("oat_file_bytes = %8zd\n"
2381 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2382 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2383 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2384 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2385 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2386 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002387 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002388 managed_code_bytes,
2389 PercentOfOatBytes(managed_code_bytes),
2390 managed_to_native_code_bytes,
2391 PercentOfOatBytes(managed_to_native_code_bytes),
2392 native_to_managed_code_bytes,
2393 PercentOfOatBytes(native_to_managed_code_bytes),
2394 class_initializer_code_bytes,
2395 PercentOfOatBytes(class_initializer_code_bytes),
2396 large_initializer_code_bytes,
2397 PercentOfOatBytes(large_initializer_code_bytes),
2398 large_method_code_bytes,
2399 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002400 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002401 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002402 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002403 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2404 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002405 }
2406
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002407 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2408 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2409 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002410 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2411 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2412 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002413 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002414
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002415 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2416 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002417 static_cast<double>(managed_code_bytes) /
2418 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002419 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002420 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002421 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002422
2423 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002424 }
2425 } stats_;
2426
2427 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002428 enum {
2429 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2430 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2431 kLargeConstructorDexBytes = 4000,
2432 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2433 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2434 kLargeMethodDexBytes = 16000
2435 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002436
2437 // For performance, use the *os_ directly for anything that doesn't need indentation
2438 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002439 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002440 VariableIndentationOutputStream vios_;
2441 ScopedIndentation indent1_;
2442
Ian Rogers1d54e732013-05-02 21:10:01 -07002443 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002444 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002445 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002446 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002447 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002448
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002449 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002450};
2451
Jeff Haodcdc85b2015-12-04 14:06:18 -08002452static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002453 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002454 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002455 options->class_loader_ = &null_class_loader;
2456
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002457 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002458 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002459 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2460 CHECK(!image_spaces.empty());
2461 for (gc::space::ImageSpace* image_space : image_spaces) {
2462 const ImageHeader& image_header = image_space->GetImageHeader();
2463 if (!image_header.IsValid()) {
2464 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2465 return EXIT_FAILURE;
2466 }
2467
2468 ImageDumper image_dumper(os, *image_space, image_header, options);
2469 if (!image_dumper.Dump()) {
2470 return EXIT_FAILURE;
2471 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002472 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002473 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002474}
2475
Andreas Gampe00b25f32014-09-17 21:49:05 -07002476static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2477 std::ostream* os) {
2478 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2479
2480 Thread* self = Thread::Current();
2481 CHECK(self != nullptr);
2482 // Need well-known-classes.
2483 WellKnownClasses::Init(self->GetJniEnv());
2484
2485 // Need to register dex files to get a working dex cache.
2486 ScopedObjectAccess soa(self);
2487 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002488 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002489 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002490 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2491 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002492 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002493 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002494 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002495 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002496 }
2497
2498 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002499 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002500 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002501
2502 // Use the class loader while dumping.
2503 StackHandleScope<1> scope(self);
2504 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2505 soa.Decode<mirror::ClassLoader*>(class_loader));
2506 options->class_loader_ = &loader_handle;
2507
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002508 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002509 bool success = oat_dumper.Dump(*os);
2510 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2511}
2512
2513static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002514 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002515 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002516 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002517 options->class_loader_ = &null_class_loader;
2518
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002519 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002520 bool success = oat_dumper.Dump(*os);
2521 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2522}
2523
2524static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2525 std::ostream* os) {
2526 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002527 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002528 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002529 if (oat_file == nullptr) {
2530 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2531 return EXIT_FAILURE;
2532 }
2533
2534 if (runtime != nullptr) {
2535 return DumpOatWithRuntime(runtime, oat_file, options, os);
2536 } else {
2537 return DumpOatWithoutRuntime(oat_file, options, os);
2538 }
2539}
2540
2541static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2542 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002543 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002544 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545 if (oat_file == nullptr) {
2546 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2547 return EXIT_FAILURE;
2548 }
2549
2550 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002551 if (!oat_symbolizer.Symbolize()) {
2552 fprintf(stderr, "Failed to symbolize\n");
2553 return EXIT_FAILURE;
2554 }
2555
2556 return EXIT_SUCCESS;
2557}
2558
Igor Murashkin37743352014-11-13 14:38:00 -08002559struct OatdumpArgs : public CmdlineArgs {
2560 protected:
2561 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002562
Igor Murashkin37743352014-11-13 14:38:00 -08002563 virtual ParseStatus ParseCustom(const StringPiece& option,
2564 std::string* error_msg) OVERRIDE {
2565 {
2566 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2567 if (base_parse != kParseUnknownArgument) {
2568 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002569 }
2570 }
2571
Igor Murashkin37743352014-11-13 14:38:00 -08002572 if (option.starts_with("--oat-file=")) {
2573 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2574 } else if (option.starts_with("--image=")) {
2575 image_location_ = option.substr(strlen("--image=")).data();
2576 } else if (option =="--dump:raw_mapping_table") {
2577 dump_raw_mapping_table_ = true;
2578 } else if (option == "--dump:raw_gc_map") {
2579 dump_raw_gc_map_ = true;
2580 } else if (option == "--no-dump:vmap") {
2581 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002582 } else if (option =="--dump:code_info_stack_maps") {
2583 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002584 } else if (option == "--no-disassemble") {
2585 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002586 } else if (option =="--header-only") {
2587 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002588 } else if (option.starts_with("--symbolize=")) {
2589 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2590 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002591 } else if (option.starts_with("--class-filter=")) {
2592 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002593 } else if (option.starts_with("--method-filter=")) {
2594 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002595 } else if (option.starts_with("--list-classes")) {
2596 list_classes_ = true;
2597 } else if (option.starts_with("--list-methods")) {
2598 list_methods_ = true;
2599 } else if (option.starts_with("--export-dex-to=")) {
2600 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2601 } else if (option.starts_with("--addr2instr=")) {
2602 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2603 *error_msg = "Address conversion failed";
2604 return kParseError;
2605 }
Igor Murashkin37743352014-11-13 14:38:00 -08002606 } else {
2607 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002608 }
2609
Igor Murashkin37743352014-11-13 14:38:00 -08002610 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002611 }
2612
Igor Murashkin37743352014-11-13 14:38:00 -08002613 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2614 // Infer boot image location from the image location if possible.
2615 if (boot_image_location_ == nullptr) {
2616 boot_image_location_ = image_location_;
2617 }
2618
2619 // Perform the parent checks.
2620 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2621 if (parent_checks != kParseOk) {
2622 return parent_checks;
2623 }
2624
2625 // Perform our own checks.
2626 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2627 *error_msg = "Either --image or --oat-file must be specified";
2628 return kParseError;
2629 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2630 *error_msg = "Either --image or --oat-file must be specified but not both";
2631 return kParseError;
2632 }
2633
2634 return kParseOk;
2635 }
2636
2637 virtual std::string GetUsage() const {
2638 std::string usage;
2639
2640 usage +=
2641 "Usage: oatdump [options] ...\n"
2642 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2643 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2644 "\n"
2645 // Either oat-file or image is required.
2646 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2647 " Example: --oat-file=/system/framework/boot.oat\n"
2648 "\n"
2649 " --image=<file.art>: specifies an input image location.\n"
2650 " Example: --image=/system/framework/boot.art\n"
2651 "\n";
2652
2653 usage += Base::GetUsage();
2654
2655 usage += // Optional.
2656 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2657 " Example: --dump:raw_mapping_table\n"
2658 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002659 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002660 " Example: --dump:raw_gc_map\n"
2661 "\n"
2662 " --no-dump:vmap may be used to disable vmap dumping.\n"
2663 " Example: --no-dump:vmap\n"
2664 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002665 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2666 " Example: --dump:code_info_stack_maps\n"
2667 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002668 " --no-disassemble may be used to disable disassembly.\n"
2669 " Example: --no-disassemble\n"
2670 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002671 " --header-only may be used to print only the oat header.\n"
2672 " Example: --header-only\n"
2673 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002674 " --list-classes may be used to list target file classes (can be used with filters).\n"
2675 " Example: --list-classes\n"
2676 " Example: --list-classes --class-filter=com.example.foo\n"
2677 "\n"
2678 " --list-methods may be used to list target file methods (can be used with filters).\n"
2679 " Example: --list-methods\n"
2680 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2681 "\n"
2682 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2683 " Example: --symbolize=/system/framework/boot.oat\n"
2684 "\n"
2685 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2686 " Example: --class-filter=com.example.foo\n"
2687 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002688 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2689 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002690 "\n"
2691 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2692 " Example: --export-dex-to=/data/local/tmp\n"
2693 "\n"
2694 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2695 " address (e.g. PC from crash dump)\n"
2696 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002697 "\n";
2698
2699 return usage;
2700 }
2701
2702 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002703 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002704 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002705 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002706 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002707 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002708 bool dump_raw_mapping_table_ = false;
2709 bool dump_raw_gc_map_ = false;
2710 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002711 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002712 bool disassemble_code_ = true;
2713 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002714 bool list_classes_ = false;
2715 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002716 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002717 uint32_t addr2instr_ = 0;
2718 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002719};
2720
Igor Murashkin37743352014-11-13 14:38:00 -08002721struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2722 virtual bool NeedsRuntime() OVERRIDE {
2723 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002724
Igor Murashkin37743352014-11-13 14:38:00 -08002725 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2726 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2727
2728 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2729 args_->dump_raw_mapping_table_,
2730 args_->dump_raw_gc_map_,
2731 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002732 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002733 args_->disassemble_code_,
2734 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002735 args_->class_filter_,
2736 args_->method_filter_,
2737 args_->list_classes_,
2738 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002739 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002740 args_->export_dex_location_,
2741 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002742
2743 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2744 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002745 }
2746
Igor Murashkin37743352014-11-13 14:38:00 -08002747 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2748 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002749 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002750
Mathieu Chartierd424d082014-10-15 10:31:46 -07002751 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002752
Andreas Gampec24f3992014-12-17 20:40:11 -08002753 if (args_->symbolize_) {
2754 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2755 } else {
2756 return DumpOat(nullptr,
2757 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002758 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002759 args_->os_) == EXIT_SUCCESS;
2760 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002761 }
2762
Igor Murashkin37743352014-11-13 14:38:00 -08002763 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2764 CHECK(args_ != nullptr);
2765
2766 if (args_->oat_filename_ != nullptr) {
2767 return DumpOat(runtime,
2768 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002769 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002770 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002771 }
Igor Murashkin37743352014-11-13 14:38:00 -08002772
Jeff Haodcdc85b2015-12-04 14:06:18 -08002773 return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002774 }
2775
Igor Murashkin37743352014-11-13 14:38:00 -08002776 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2777};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002778
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002779} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002780
2781int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002782 art::OatdumpMain main;
2783 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002784}