blob: 6c6c807588c0472c274620bd99dcb7b4e604e12f [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080036#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080037#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070038#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070040#include "gc/space/image_space.h"
41#include "gc/space/large_object_space.h"
42#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080043#include "image-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000045#include "linker/buffered_output_stream.h"
46#include "linker/file_output_stream.h"
Ian Rogers1809a722013-08-09 22:05:32 -070047#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/array-inl.h"
49#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010050#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080056#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070057#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058#include "scoped_thread_state_change.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010059#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080061#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080062#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080063#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070064#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070065#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Igor Murashkin37743352014-11-13 14:38:00 -080067#include <sys/stat.h>
68#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070069
Igor Murashkin37743352014-11-13 14:38:00 -080070namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Mathieu Chartiere401d142015-04-22 13:56:20 -070072const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080073 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070074 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070075 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070077 "kRefsOnlySaveMethod",
78 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070079};
80
81const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Mathieu Chartierac8f4392015-08-27 13:54:20 -070086// Map is so that we don't allocate multiple dex files for the same OatDexFile.
87static std::map<const OatFile::OatDexFile*,
88 std::unique_ptr<const DexFile>> opened_dex_files;
89
90const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
91 DCHECK(oat_dex_file != nullptr);
92 auto it = opened_dex_files.find(oat_dex_file);
93 if (it != opened_dex_files.end()) {
94 return it->second.get();
95 }
96 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
97 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
98 return ret;
99}
100
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800101template <typename ElfTypes>
David Srbeckybc90fd02015-04-22 19:40:27 +0100102class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700103 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100104 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100105 oat_file_(oat_file), builder_(nullptr),
106 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700107 }
108
109 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
110 uint32_t,
111 const OatFile::OatMethod&,
112 const DexFile&,
113 uint32_t,
114 const DexFile::CodeItem*,
115 uint32_t);
116
117 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000118 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
David Srbecky5d811202016-03-08 13:21:22 +0000119 const InstructionSetFeatures* features = InstructionSetFeatures::FromBitmap(
120 isa, oat_file_->GetOatHeader().GetInstructionSetFeaturesBitmap());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000121
122 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
123 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000124 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800125 builder_.reset(new ElfBuilder<ElfTypes>(isa, features, output_stream.get()));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000126
127 builder_->Start();
128
129 auto* rodata = builder_->GetRoData();
130 auto* text = builder_->GetText();
131 auto* bss = builder_->GetBss();
132 auto* strtab = builder_->GetStrTab();
133 auto* symtab = builder_->GetSymTab();
134
135 rodata->Start();
136 const uint8_t* rodata_begin = oat_file_->Begin();
137 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
138 rodata->WriteFully(rodata_begin, rodata_size);
139 rodata->End();
140
141 text->Start();
142 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
143 const size_t text_size = oat_file_->End() - text_begin;
144 text->WriteFully(text_begin, text_size);
145 text->End();
146
147 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000148 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000149 }
150
Vladimir Marko944da602016-02-19 12:27:55 +0000151 builder_->PrepareDynamicSection(
152 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
153 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700154
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800155 Walk(&art::OatSymbolizer<ElfTypes>::RegisterForDedup);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700156
157 NormalizeState();
158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 strtab->Start();
160 strtab->Write(""); // strtab should start with empty string.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000161 AddTrampolineSymbols();
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800162 Walk(&art::OatSymbolizer<ElfTypes>::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000163 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700164
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165 symtab->Start();
166 symtab->Write();
167 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700168
David Srbecky6d8c8f02015-10-26 10:57:09 +0000169 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700170
Vladimir Marko10c13562015-11-25 14:33:36 +0000171 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172 }
173
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000174 void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
175 if (code_offset != 0) {
176 uint32_t name_offset = builder_->GetStrTab()->Write(name);
177 uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
Tamas Berghammer9e49ab12016-01-28 16:30:14 +0000178 // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
179 // the end of the section in case of the last symbol.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000180 builder_->GetSymTab()->Add(name_offset, builder_->GetText(), symbol_value,
181 /* is_relative */ true, /* size */ 0, STB_GLOBAL, STT_FUNC);
182 }
183 }
184
185 void AddTrampolineSymbols() {
186 const OatHeader& oat_header = oat_file_->GetOatHeader();
187 AddTrampolineSymbol("interpreterToInterpreterBridge",
188 oat_header.GetInterpreterToInterpreterBridgeOffset());
189 AddTrampolineSymbol("interpreterToCompiledCodeBridge",
190 oat_header.GetInterpreterToCompiledCodeBridgeOffset());
191 AddTrampolineSymbol("jniDlsymLookup",
192 oat_header.GetJniDlsymLookupOffset());
193 AddTrampolineSymbol("quickGenericJniTrampoline",
194 oat_header.GetQuickGenericJniTrampolineOffset());
195 AddTrampolineSymbol("quickImtConflictTrampoline",
196 oat_header.GetQuickImtConflictTrampolineOffset());
197 AddTrampolineSymbol("quickResolutionTrampoline",
198 oat_header.GetQuickResolutionTrampolineOffset());
199 AddTrampolineSymbol("quickToInterpreterBridge",
200 oat_header.GetQuickToInterpreterBridgeOffset());
201 }
202
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700203 void Walk(Callback callback) {
204 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
205 for (size_t i = 0; i < oat_dex_files.size(); i++) {
206 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700207 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700208 WalkOatDexFile(oat_dex_file, callback);
209 }
210 }
211
212 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
213 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700214 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
215 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216 return;
217 }
218 for (size_t class_def_index = 0;
219 class_def_index < dex_file->NumClassDefs();
220 class_def_index++) {
221 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
222 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
223 OatClassType type = oat_class.GetType();
224 switch (type) {
225 case kOatClassAllCompiled:
226 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700227 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700228 break;
229
230 case kOatClassNoneCompiled:
231 case kOatClassMax:
232 // Ignore.
233 break;
234 }
235 }
236 }
237
238 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
239 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700240 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700241 if (class_data == nullptr) { // empty class such as a marker interface?
242 return;
243 }
244 // Note: even if this is an interface or a native class, we still have to walk it, as there
245 // might be a static initializer.
246 ClassDataItemIterator it(dex_file, class_data);
247 SkipAllFields(&it);
248 uint32_t class_method_idx = 0;
249 while (it.HasNextDirectMethod()) {
250 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
251 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700252 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700253 class_method_idx++;
254 it.Next();
255 }
256 while (it.HasNextVirtualMethod()) {
257 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
258 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700259 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700260 class_method_idx++;
261 it.Next();
262 }
263 DCHECK(!it.HasNext());
264 }
265
266 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
267 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
268 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
269 uint32_t method_access_flags, Callback callback) {
270 if ((method_access_flags & kAccAbstract) != 0) {
271 // Abstract method, no code.
272 return;
273 }
274 if (oat_method.GetCodeOffset() == 0) {
275 // No code.
276 return;
277 }
278
279 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
280 method_access_flags);
281 }
282
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700283 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
284 uint32_t class_method_index ATTRIBUTE_UNUSED,
285 const OatFile::OatMethod& oat_method,
286 const DexFile& dex_file ATTRIBUTE_UNUSED,
287 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
288 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
289 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700290 state_[oat_method.GetCodeOffset()]++;
291 }
292
293 void NormalizeState() {
294 for (auto& x : state_) {
295 if (x.second == 1) {
296 state_[x.first] = 0;
297 }
298 }
299 }
300
301 enum class DedupState { // private
302 kNotDeduplicated,
303 kDeduplicatedFirst,
304 kDeduplicatedOther
305 };
306 DedupState IsDuplicated(uint32_t offset) {
307 if (state_[offset] == 0) {
308 return DedupState::kNotDeduplicated;
309 }
310 if (state_[offset] == 1) {
311 return DedupState::kDeduplicatedOther;
312 }
313 state_[offset] = 1;
314 return DedupState::kDeduplicatedFirst;
315 }
316
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700317 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
318 uint32_t class_method_index ATTRIBUTE_UNUSED,
319 const OatFile::OatMethod& oat_method,
320 const DexFile& dex_file,
321 uint32_t dex_method_idx,
322 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
323 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700324 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
325 if (dedup != DedupState::kDeduplicatedOther) {
326 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
327
328 if (dedup == DedupState::kDeduplicatedFirst) {
329 pretty_name = "[Dedup]" + pretty_name;
330 }
331
David Srbecky6d8c8f02015-10-26 10:57:09 +0000332 int name_offset = builder_->GetStrTab()->Write(pretty_name);
333 builder_->GetSymTab()->Add(name_offset, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700334 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
335 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700336 }
337 }
338
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700339 private:
340 static void SkipAllFields(ClassDataItemIterator* it) {
341 while (it->HasNextStaticField()) {
342 it->Next();
343 }
344 while (it->HasNextInstanceField()) {
345 it->Next();
346 }
347 }
348
349 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800350 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700351 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700352 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700353};
354
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700355class OatDumperOptions {
356 public:
357 OatDumperOptions(bool dump_raw_mapping_table,
358 bool dump_raw_gc_map,
359 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100360 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700361 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700362 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800363 const char* class_filter,
364 const char* method_filter,
365 bool list_classes,
366 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000367 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800368 const char* export_dex_location,
369 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700370 : dump_raw_mapping_table_(dump_raw_mapping_table),
371 dump_raw_gc_map_(dump_raw_gc_map),
372 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100373 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700374 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700375 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800376 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000377 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800378 list_classes_(list_classes),
379 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000380 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800381 export_dex_location_(export_dex_location),
382 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800383 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700384
385 const bool dump_raw_mapping_table_;
386 const bool dump_raw_gc_map_;
387 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100388 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700389 const bool disassemble_code_;
390 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800391 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000392 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800393 const bool list_classes_;
394 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000395 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800396 const char* const export_dex_location_;
397 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700398 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700399};
400
Elliott Hughese3c845c2012-02-28 17:23:01 -0800401class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700402 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100403 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000404 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800405 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700406 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800407 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800408 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
409 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800410 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000411 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100412 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800413 CHECK(options_.class_loader_ != nullptr);
414 CHECK(options_.class_filter_ != nullptr);
415 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800416 AddAllOffsets();
417 }
418
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700419 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700420 delete disassembler_;
421 }
422
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800423 InstructionSet GetInstructionSet() {
424 return instruction_set_;
425 }
426
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700427 bool Dump(std::ostream& os) {
428 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800429 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700430
431 os << "MAGIC:\n";
432 os << oat_header.GetMagic() << "\n\n";
433
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800434 os << "LOCATION:\n";
435 os << oat_file_.GetLocation() << "\n\n";
436
Brian Carlstromaded5f72011-10-07 17:15:04 -0700437 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800438 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700439
Elliott Hughesa72ec822012-03-05 17:12:22 -0800440 os << "INSTRUCTION SET:\n";
441 os << oat_header.GetInstructionSet() << "\n\n";
442
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700443 {
444 std::unique_ptr<const InstructionSetFeatures> features(
445 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
446 oat_header.GetInstructionSetFeaturesBitmap()));
447 os << "INSTRUCTION SET FEATURES:\n";
448 os << features->GetFeatureString() << "\n\n";
449 }
Dave Allison70202782013-10-22 17:52:19 -0700450
Brian Carlstromaded5f72011-10-07 17:15:04 -0700451 os << "DEX FILE COUNT:\n";
452 os << oat_header.GetDexFileCount() << "\n\n";
453
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800454#define DUMP_OAT_HEADER_OFFSET(label, offset) \
455 os << label " OFFSET:\n"; \
456 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800457 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800458 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
459 } \
460 os << StringPrintf("\n\n");
461
462 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
463 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
464 GetInterpreterToInterpreterBridgeOffset);
465 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
466 GetInterpreterToCompiledCodeBridgeOffset);
467 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
468 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800469 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
470 GetQuickGenericJniTrampolineOffset);
471 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
472 GetQuickImtConflictTrampolineOffset);
473 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
474 GetQuickResolutionTrampolineOffset);
475 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
476 GetQuickToInterpreterBridgeOffset);
477#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700478
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700479 os << "IMAGE PATCH DELTA:\n";
480 os << StringPrintf("%d (0x%08x)\n\n",
481 oat_header.GetImagePatchDelta(),
482 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700483
Brian Carlstrom28db0122012-10-18 16:20:41 -0700484 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
485 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
486
487 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800488 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700489
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700490 // Print the key-value store.
491 {
492 os << "KEY VALUE STORE:\n";
493 size_t index = 0;
494 const char* key;
495 const char* value;
496 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
497 os << key << " = " << value << "\n";
498 index++;
499 }
500 os << "\n";
501 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700502
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800503 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700504 os << "BEGIN:\n";
505 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700506
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700507 os << "END:\n";
508 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
509 }
510
511 os << "SIZE:\n";
512 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700513
514 os << std::flush;
515
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800516 // If set, adjust relative address to be searched
517 if (options_.addr2instr_ != 0) {
518 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
519 os << "SEARCH ADDRESS (executable offset + input):\n";
520 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
521 }
522
David Brazdilc03d7b62016-03-02 12:18:03 +0000523 if (!options_.dump_header_only_) {
524 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
525 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
526 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800527
David Brazdilc03d7b62016-03-02 12:18:03 +0000528 // If file export selected skip file analysis
529 if (options_.export_dex_location_) {
530 if (!ExportDexFile(os, *oat_dex_file)) {
531 success = false;
532 }
533 } else {
534 if (!DumpOatDexFile(os, *oat_dex_file)) {
535 success = false;
536 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800537 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700538 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700539 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000540
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700541 os << std::flush;
542 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700543 }
544
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700546 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
547 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800548 return 0; // Address not in oat file
549 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800550 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
551 reinterpret_cast<uintptr_t>(oat_file_.Begin());
552 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800553 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800554 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800555 return end_offset - begin_offset;
556 }
557
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800558 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700559 return oat_file_.GetOatHeader().GetInstructionSet();
560 }
561
Mathieu Chartier90443472015-07-16 20:32:27 -0700562 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800563 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
564 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700565 CHECK(oat_dex_file != nullptr);
566 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700567 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
568 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700569 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
570 << "': " << error_msg;
571 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800572 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700573 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800574 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700575 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700576 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100577 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800578 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100579 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800580 }
581 }
582 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700583 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800584 }
585
Brian Carlstromaded5f72011-10-07 17:15:04 -0700586 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800587 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800588 // We don't know the length of the code for each method, but we need to know where to stop
589 // when disassembling. What we do know is that a region of code will be followed by some other
590 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
591 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800592 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
593 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700594 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700595 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700596 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
597 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700598 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
599 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800600 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800601 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800602 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800603 for (size_t class_def_index = 0;
604 class_def_index < dex_file->NumClassDefs();
605 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800606 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100607 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700608 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700609 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800610 ClassDataItemIterator it(*dex_file, class_data);
611 SkipAllFields(it);
612 uint32_t class_method_index = 0;
613 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100614 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800615 it.Next();
616 }
617 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100618 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800619 it.Next();
620 }
621 }
622 }
623 }
624
625 // If the last thing in the file is code for a method, there won't be an offset for the "next"
626 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
627 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800628 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800629 }
630
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700631 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
632 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
633 }
634
Elliott Hughese3c845c2012-02-28 17:23:01 -0800635 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800636 uint32_t code_offset = oat_method.GetCodeOffset();
637 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
638 code_offset &= ~0x1;
639 }
640 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800641 offsets_.insert(oat_method.GetMappingTableOffset());
642 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800643 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800644 }
645
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700646 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
647 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800648 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700649 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800650 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800651 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700652
653 // Create the verifier early.
654
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700655 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700656 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
657 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700658 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700659 os << std::flush;
660 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700661 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100662
663 VariableIndentationOutputStream vios(&os);
664 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800665 for (size_t class_def_index = 0;
666 class_def_index < dex_file->NumClassDefs();
667 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700668 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
669 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800670
671 // TODO: Support regex
672 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
673 continue;
674 }
675
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700676 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100677 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700678 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
679 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100680 << " (" << oat_class.GetStatus() << ")"
681 << " (" << oat_class.GetType() << ")\n";
682 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800683 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700684 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700685 success = false;
686 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800687 if (stop_analysis) {
688 os << std::flush;
689 return success;
690 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700691 }
692
693 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700694 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700695 }
696
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800697 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
698 std::string error_msg;
699 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
700
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700701 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800702 if (dex_file == nullptr) {
703 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
704 return false;
705 }
706 size_t fsize = oat_dex_file.FileSize();
707
708 // Some quick checks just in case
709 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
710 os << "Invalid dex file\n";
711 return false;
712 }
713
714 // Verify output directory exists
715 if (!OS::DirectoryExists(options_.export_dex_location_)) {
716 // TODO: Extend OS::DirectoryExists if symlink support is required
717 os << options_.export_dex_location_ << " output directory not found or symlink\n";
718 return false;
719 }
720
721 // Beautify path names
722 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
723 return false;
724 }
725
726 std::string dex_orig_name;
727 size_t dex_orig_pos = dex_file_location.rfind('/');
728 if (dex_orig_pos == std::string::npos)
729 dex_orig_name = dex_file_location;
730 else
731 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
732
733 // A more elegant approach to efficiently name user installed apps is welcome
734 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
735 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
736 size_t apk_orig_pos = dex_file_location.rfind('/');
737 if (apk_orig_pos != std::string::npos) {
738 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
739 }
740 }
741
742 std::string out_dex_path(options_.export_dex_location_);
743 if (out_dex_path.back() != '/') {
744 out_dex_path.append("/");
745 }
746 out_dex_path.append(dex_orig_name);
747 out_dex_path.append("_export.dex");
748 if (out_dex_path.length() > PATH_MAX) {
749 return false;
750 }
751
752 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
753 if (file.get() == nullptr) {
754 os << "Failed to open output dex file " << out_dex_path;
755 return false;
756 }
757
758 if (!file->WriteFully(dex_file->Begin(), fsize)) {
759 os << "Failed to write dex file";
760 file->Erase();
761 return false;
762 }
763
764 if (file->FlushCloseOrErase() != 0) {
765 os << "Flush and close failed";
766 return false;
767 }
768
769 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
770 os << std::flush;
771
772 return true;
773 }
774
Elliott Hughese3c845c2012-02-28 17:23:01 -0800775 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700776 while (it.HasNextStaticField()) {
777 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700778 }
Ian Rogers0571d352011-11-03 19:51:38 -0700779 while (it.HasNextInstanceField()) {
780 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700781 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800782 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700783
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100784 bool DumpOatClass(VariableIndentationOutputStream* vios,
785 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800786 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700787 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800788 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700789 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700790 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100791 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700792 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800793 }
794 ClassDataItemIterator it(dex_file, class_data);
795 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700796 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700797 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100798 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700799 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800800 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700801 success = false;
802 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800803 if (addr_found) {
804 *stop_analysis = true;
805 return success;
806 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700807 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700808 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700809 }
Ian Rogers0571d352011-11-03 19:51:38 -0700810 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100811 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700812 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800813 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700814 success = false;
815 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800816 if (addr_found) {
817 *stop_analysis = true;
818 return success;
819 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700820 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700821 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700822 }
Ian Rogers0571d352011-11-03 19:51:38 -0700823 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100824 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700825 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700826 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800827
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700828 static constexpr uint32_t kPrologueBytes = 16;
829
830 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
831 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
832
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100833 bool DumpOatMethod(VariableIndentationOutputStream* vios,
834 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700835 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700836 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800837 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800838 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700839 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800840
841 // TODO: Support regex
842 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
843 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000844 return success;
845 }
846
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800847 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100848 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
849 class_method_index, pretty_method.c_str(),
850 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800851 if (options_.list_methods_) return success;
852
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800853 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
854 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
855 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
856 uint32_t code_offset = oat_method.GetCodeOffset();
857 uint32_t code_size = oat_method.GetQuickCodeSize();
858 if (resolved_addr2instr_ != 0) {
859 if (resolved_addr2instr_ > code_offset + code_size) {
860 return success;
861 } else {
862 *addr_found = true; // stop analyzing file at next iteration
863 }
864 }
865
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100866 // Everything below is indented at least once.
867 ScopedIndentation indent1(vios);
868
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800869 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100870 vios->Stream() << "DEX CODE:\n";
871 ScopedIndentation indent2(vios);
872 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700873 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700874
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700875 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700876 std::unique_ptr<verifier::MethodVerifier> verifier;
877 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700878 // We need to have the handle scope stay live until after the verifier since the verifier has
879 // a handle to the dex cache from hs.
880 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
882 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700883 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100884 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700885 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700886 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800887 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100888 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800889 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100890 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100891 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100892 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100894 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
896 oat_method_offsets_offset, oat_file_.Size());
897 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100898 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700899 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800900 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700901
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 ScopedIndentation indent2(vios);
903 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700904 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
905 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100906 vios->Stream() << StringPrintf("WARNING: "
907 "code offset 0x%08x is past end of file 0x%08zx.\n",
908 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909 success = false;
910 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100911 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100913 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800914 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100915 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800917 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100918 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100920 vios->Stream() << StringPrintf("WARNING: "
921 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
922 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700923 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800924 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100925 ScopedIndentation indent3(vios);
926 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800927 }
928 }
929 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100930 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
932 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700933
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800934 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100935 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700936 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100937 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700938 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100939 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
941 method_header_offset, oat_file_.Size());
942 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100943 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700944 return false;
945 }
946
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100947 ScopedIndentation indent2(vios);
948 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800949 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100950 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700951 }
952 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100953 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700954 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100955 vios->Stream() << StringPrintf("WARNING: "
956 "mapping table offset 0x%08x is past end of file 0x%08zx. "
957 "mapping table offset was loaded from offset 0x%08x.\n",
958 mapping_table_offset, oat_file_.Size(),
959 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700960 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800961 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 ScopedIndentation indent3(vios);
963 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700964 }
965
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100966 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800967 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100968 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700969 }
970 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100971 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700972 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100973 vios->Stream() << StringPrintf("WARNING: "
974 "vmap table offset 0x%08x is past end of file 0x%08zx. "
975 "vmap table offset was loaded from offset 0x%08x.\n",
976 vmap_table_offset, oat_file_.Size(),
977 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700978 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800979 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100980 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700981 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800982 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700983 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100984 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700985
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100986 ScopedIndentation indent2(vios);
987 vios->Stream()
988 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
989 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
990 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
991 vios->Stream() << "\n";
992 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
993 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
994 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700995 }
996 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100997 // Based on spill masks from QuickMethodFrameInfo so placed
998 // after it is dumped, but useful for understanding quick
999 // code, so dumped here.
1000 ScopedIndentation indent2(vios);
1001 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001002 }
1003 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001004 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001005 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1006 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001007 ScopedIndentation indent2(vios);
1008 vios->Stream() << StringPrintf("WARNING: "
1009 "code size offset 0x%08x is past end of file 0x%08zx.",
1010 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001011 success = false;
1012 } else {
1013 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001014 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1015 uint64_t aligned_code_end = aligned_code_begin + code_size;
1016
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001017 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001019 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001020 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1021 code_offset,
1022 code_size_offset,
1023 code_size,
1024 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001025
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001026 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001027 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001028 vios->Stream() << StringPrintf("WARNING: "
1029 "start of code at 0x%08x is past end of file 0x%08zx.",
1030 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001031 success = false;
1032 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001033 vios->Stream() << StringPrintf(
1034 "WARNING: "
1035 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1036 "code size is 0x%08x loaded from offset 0x%08x.\n",
1037 aligned_code_end, oat_file_.Size(),
1038 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001039 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001040 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001041 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001042 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001043 }
1044 }
1045 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001046 vios->Stream() << StringPrintf(
1047 "WARNING: "
1048 "code size %d is bigger than max expected threshold of %d. "
1049 "code size is 0x%08x loaded from offset 0x%08x.\n",
1050 code_size, kMaxCodeSize,
1051 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001052 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001053 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001054 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001055 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001056 }
1057 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001058 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001059 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001060 }
1061 }
1062 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001063 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001064 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001065 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001066
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001067 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1068 if (spill_mask == 0) {
1069 return;
1070 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001071 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001072 for (size_t i = 0; i < 32; i++) {
1073 if ((spill_mask & (1 << i)) != 0) {
1074 if (is_float) {
1075 os << "fr" << i;
1076 } else {
1077 os << "r" << i;
1078 }
1079 spill_mask ^= 1 << i; // clear bit
1080 if (spill_mask != 0) {
1081 os << ", ";
1082 } else {
1083 break;
1084 }
1085 }
1086 }
1087 os << ")";
1088 }
1089
Roland Levillain442b46a2015-02-18 16:54:21 +00001090 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001091 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001092 const OatFile::OatMethod& oat_method,
1093 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001094 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1095 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001096 const void* raw_code_info = oat_method.GetVmapTable();
1097 if (raw_code_info != nullptr) {
1098 CodeInfo code_info(raw_code_info);
1099 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001100 ScopedIndentation indent1(vios);
1101 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001102 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001103 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1104 // We don't encode the size in the table, so just emit that we have quickened
1105 // information.
1106 ScopedIndentation indent(vios);
1107 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001108 } else {
1109 // Otherwise, display the vmap table.
1110 const uint8_t* raw_table = oat_method.GetVmapTable();
1111 if (raw_table != nullptr) {
1112 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001113 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001114 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001115 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001116 }
1117
1118 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001119 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001120 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001121 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001122 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001123 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001124 oat_method.GetCodeOffset(),
1125 code_item.registers_size_,
1126 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001127 }
1128
Roland Levillain442b46a2015-02-18 16:54:21 +00001129 // Display a vmap table.
1130 void DumpVmapTable(std::ostream& os,
1131 const OatFile::OatMethod& oat_method,
1132 const VmapTable& vmap_table) {
1133 bool first = true;
1134 bool processing_fp = false;
1135 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1136 for (size_t i = 0; i < vmap_table.Size(); i++) {
1137 uint16_t dex_reg = vmap_table[i];
1138 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1139 processing_fp ? kFloatVReg : kIntVReg);
1140 os << (first ? "v" : ", v") << dex_reg;
1141 if (!processing_fp) {
1142 os << "/r" << cpu_reg;
1143 } else {
1144 os << "/fr" << cpu_reg;
1145 }
1146 first = false;
1147 if (!processing_fp && dex_reg == 0xFFFF) {
1148 processing_fp = true;
1149 spill_mask = oat_method.GetFpSpillMask();
1150 }
1151 }
1152 os << "\n";
1153 }
1154
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001155 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1156 const DexFile::CodeItem* code_item) {
1157 if (code_item != nullptr) {
1158 size_t num_locals_ins = code_item->registers_size_;
1159 size_t num_ins = code_item->ins_size_;
1160 size_t num_locals = num_locals_ins - num_ins;
1161 size_t num_outs = code_item->outs_size_;
1162
1163 os << "vr_stack_locations:";
1164 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1165 // For readability, delimit the different kinds of VRs.
1166 if (reg == num_locals_ins) {
1167 os << "\n\tmethod*:";
1168 } else if (reg == num_locals && num_ins > 0) {
1169 os << "\n\tins:";
1170 } else if (reg == 0 && num_locals > 0) {
1171 os << "\n\tlocals:";
1172 }
1173
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001174 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1175 code_item,
1176 oat_method.GetCoreSpillMask(),
1177 oat_method.GetFpSpillMask(),
1178 oat_method.GetFrameSizeInBytes(),
1179 reg,
1180 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001181 os << " v" << reg << "[sp + #" << offset << "]";
1182 }
1183
1184 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1185 if (out_reg == 0) {
1186 os << "\n\touts:";
1187 }
1188
1189 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1190 os << " v" << out_reg << "[sp + #" << offset << "]";
1191 }
1192
1193 os << "\n";
1194 }
1195 }
1196
Ian Rogersb23a7722012-10-09 16:54:26 -07001197 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001198 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001199 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001200 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001201 const VmapTable vmap_table(raw_table);
1202 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001203 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001204 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1205 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1206 : oat_method.GetCoreSpillMask();
1207 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001208 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001209 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1210 code_item,
1211 oat_method.GetCoreSpillMask(),
1212 oat_method.GetFpSpillMask(),
1213 oat_method.GetFrameSizeInBytes(),
1214 reg,
1215 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001216 os << "[sp + #" << offset << "]";
1217 }
1218 }
1219 }
1220
Ian Rogersef7d42f2014-01-06 12:55:46 -08001221 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1222 const DexFile::CodeItem* code_item,
1223 size_t num_regs, const uint8_t* reg_bitmap) {
1224 bool first = true;
1225 for (size_t reg = 0; reg < num_regs; reg++) {
1226 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1227 if (first) {
1228 os << " v" << reg << " (";
1229 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1230 os << ")";
1231 first = false;
1232 } else {
1233 os << ", v" << reg << " (";
1234 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1235 os << ")";
1236 }
1237 }
1238 }
1239 if (first) {
1240 os << "No registers in GC map\n";
1241 } else {
1242 os << "\n";
1243 }
1244 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001245 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1246 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001247 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001248 if (gc_map_raw == nullptr) {
1249 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001250 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001251 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001252 NativePcOffsetToReferenceMap map(gc_map_raw);
1253 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1254 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1255 map.GetNativePcOffset(entry);
1256 os << StringPrintf("%p", native_pc);
1257 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001258 }
1259 }
1260
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001261 void DumpMappingTable(VariableIndentationOutputStream* vios,
1262 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001263 const void* quick_code = oat_method.GetQuickCode();
1264 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001265 return;
1266 }
Ian Rogers1809a722013-08-09 22:05:32 -07001267 MappingTable table(oat_method.GetMappingTable());
1268 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001269 if (table.PcToDexSize() != 0) {
1270 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001271 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001272 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001273 ScopedIndentation indent1(vios);
1274 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001275 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001276 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001277 }
1278 if (table.DexToPcSize() != 0) {
1279 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001280 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001281 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001282 ScopedIndentation indent1(vios);
1283 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001284 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001286 }
1287 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001288 }
1289
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001290 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001291 const OatFile::OatMethod& oat_method,
1292 const DexFile::CodeItem* code_item,
1293 size_t offset,
1294 bool suspend_point_mapping) {
1295 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1296 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001297 ScopedIndentation indent1(vios);
1298 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001299 }
1300 // The return value is not used in the case of a method compiled
1301 // with the optimizing compiler.
1302 return DexFile::kDexNoIndex;
1303 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001304 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001305 }
1306 }
1307
Ian Rogers1809a722013-08-09 22:05:32 -07001308 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1309 size_t offset, bool suspend_point_mapping) {
1310 MappingTable table(oat_method.GetMappingTable());
1311 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1312 typedef MappingTable::PcToDexIterator It;
1313 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1314 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001315 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001316 return cur.DexPc();
1317 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001318 }
Ian Rogers1809a722013-08-09 22:05:32 -07001319 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1320 typedef MappingTable::DexToPcIterator It;
1321 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1322 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001323 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001324 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001325 }
1326 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001327 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001329 }
1330
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001331 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1332 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001333 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001334 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001335 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001336 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001337 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001338 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001339 bool first = true;
1340 for (size_t reg = 0; reg < num_regs; reg++) {
1341 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1342 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001343 os << "GC map objects: v" << reg << " (";
1344 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001345 os << ")";
1346 first = false;
1347 } else {
1348 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001349 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001350 os << ")";
1351 }
1352 }
1353 }
1354 if (!first) {
1355 os << "\n";
1356 }
1357 }
1358 }
1359 }
1360
Mathieu Chartier590fee92013-09-13 13:46:47 -07001361 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1362 const OatFile::OatMethod& oat_method,
1363 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1364 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001365 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001366 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001367 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1368 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1369 if (kind != kUndefined) {
1370 if (first) {
1371 os << "VRegs: v";
1372 first = false;
1373 } else {
1374 os << ", v";
1375 }
1376 os << reg << " (";
1377 switch (kind) {
1378 case kImpreciseConstant:
1379 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1380 DescribeVReg(os, oat_method, code_item, reg, kind);
1381 break;
1382 case kConstant:
1383 os << "Constant: " << kinds.at((reg * 2) + 1);
1384 break;
1385 default:
1386 DescribeVReg(os, oat_method, code_item, reg, kind);
1387 break;
1388 }
1389 os << ")";
1390 }
1391 }
1392 if (!first) {
1393 os << "\n";
1394 }
1395 }
1396
1397
Ian Rogersb23a7722012-10-09 16:54:26 -07001398 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001399 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001400 size_t i = 0;
1401 while (i < code_item->insns_size_in_code_units_) {
1402 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001403 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1404 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001405 i += instruction->SizeInCodeUnits();
1406 }
1407 }
1408 }
1409
Roland Levillainf2650d12015-05-28 14:53:28 +01001410 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1411 // the optimizing compiler?
1412 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1413 const DexFile::CodeItem* code_item) {
1414 // If the native GC map is null and the Dex `code_item` is not
1415 // null, then this method has been compiled with the optimizing
1416 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001417 return oat_method.GetQuickCode() != nullptr &&
1418 oat_method.GetGcMap() == nullptr &&
1419 code_item != nullptr;
1420 }
1421
1422 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1423 // the dextodex compiler?
1424 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1425 const DexFile::CodeItem* code_item) {
1426 // If the quick code is null, the Dex `code_item` is not
1427 // null, and the vmap table is not null, then this method has been compiled
1428 // with the dextodex compiler.
1429 return oat_method.GetQuickCode() == nullptr &&
1430 oat_method.GetVmapTable() != nullptr &&
1431 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001432 }
1433
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001434 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001435 const OatFile::OatMethod& oat_method,
1436 const DexFile::CodeItem* code_item,
1437 size_t offset) {
1438 // This method is only relevant for oat methods compiled with the
1439 // optimizing compiler.
1440 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1441
1442 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1443 const void* raw_code_info = oat_method.GetVmapTable();
1444 if (raw_code_info != nullptr) {
1445 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001446 StackMapEncoding encoding = code_info.ExtractEncoding();
1447 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001448 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001449 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1450 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001451 }
1452 }
1453 }
1454
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001455 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001456 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001457 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001458 const DexFile* dex_file,
1459 const DexFile::ClassDef& class_def,
1460 const DexFile::CodeItem* code_item,
1461 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001462 if ((method_access_flags & kAccNative) == 0) {
1463 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001464 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001465 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001466 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1467 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001468 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001469 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001470 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1471 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001472 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001473
1474 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001475 }
1476
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001477 void DumpCode(VariableIndentationOutputStream* vios,
1478 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001479 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1480 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001481 const void* quick_code = oat_method.GetQuickCode();
1482
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001483 if (code_size == 0) {
1484 code_size = oat_method.GetQuickCodeSize();
1485 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001486 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001487 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001488 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001489 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001490 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1491 size_t offset = 0;
1492 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001493 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001494 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001495 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001496 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001497 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001498 uint32_t dex_pc =
1499 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001500 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001501 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001502 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001503 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001504 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001505 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 }
1507 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001508 }
1509 }
1510
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001511 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001512 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001513 const OatDumperOptions& options_;
1514 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001515 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001516 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001517 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001518};
1519
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001520class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001521 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001522 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1523 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001524 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001525 vios_(os),
1526 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001527 image_space_(image_space),
1528 image_header_(image_header),
1529 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001530
Mathieu Chartier90443472015-07-16 20:32:27 -07001531 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001532 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001533 std::ostream& indent_os = vios_.Stream();
1534
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001535 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001536
Jeff Haodcdc85b2015-12-04 14:06:18 -08001537 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1538
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001539 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001540
Mathieu Chartiere401d142015-04-22 13:56:20 -07001541 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1542
1543 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1544 auto section = static_cast<ImageHeader::ImageSections>(i);
1545 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1546 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001547
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001549
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001550 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001551
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001552 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1553
1554 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1555
1556 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001557
Alex Lighta59dd802014-07-02 16:28:08 -07001558 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1559
Igor Murashkin46774762014-10-22 11:37:02 -07001560 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1561
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 {
1563 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001564 static_assert(arraysize(image_roots_descriptions_) ==
1565 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001566 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1567 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1568 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001569 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001570 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001572 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001573 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001574 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001575 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1576 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001577 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001578 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1579 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001580 run++;
1581 } else {
1582 break;
1583 }
1584 }
1585 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001586 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001587 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001588 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001589 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001590 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001591 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001592 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001593 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001594 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001596 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001597 }
1598 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001599 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001600
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001601 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001602 os << "METHOD ROOTS\n";
1603 static_assert(arraysize(image_methods_descriptions_) ==
1604 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1605 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1606 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1607 const char* description = image_methods_descriptions_[i];
1608 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001609 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001610 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001611 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001612 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001613
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001614 Runtime* const runtime = Runtime::Current();
1615 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001616 std::string image_filename = image_space_.GetImageFilename();
1617 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001619 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001620 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001621 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1622 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001623 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001624 oat_file = OatFile::Open(oat_location, oat_location,
1625 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001626 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001627 if (oat_file == nullptr) {
1628 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001629 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001630 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001631 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001633
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001634 stats_.oat_file_bytes = oat_file->Size();
1635
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001636 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001637
Mathieu Chartier02e25112013-08-14 16:14:24 -07001638 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001639 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001640 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1641 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001642 }
1643
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001645
Jeff Haodcdc85b2015-12-04 14:06:18 -08001646 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001647 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001648 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001649 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001650 {
1651 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1652 heap->FlushAllocStack();
1653 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001654 // Since FlushAllocStack() above resets the (active) allocation
1655 // stack. Need to revoke the thread-local allocation stacks that
1656 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001657 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001658 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001659 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001660 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001661 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001662 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001663 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001664 {
1665 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001666 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001667 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001668 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001669 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001670 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001671 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001672 }
1673 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001674 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001675 // Dump the normal objects before ArtMethods.
1676 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1677 indent_os << "\n";
1678 // TODO: Dump fields.
1679 // Dump methods after.
1680 const auto& methods_section = image_header_.GetMethodsSection();
1681 const size_t pointer_size =
1682 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1683 DumpArtMethodVisitor visitor(this);
1684 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001685 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001686 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001688 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001689 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001690 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001691 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001692 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001693 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001694 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001695 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001696 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001697 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001698 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001699 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1700 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001701 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1702 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001703 const auto& intern_section = image_header_.GetImageSection(
1704 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001705 const auto& class_table_section = image_header_.GetImageSection(
1706 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001707 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1708
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001709 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001710
1711 // Objects are kObjectAlignment-aligned.
1712 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1713 if (object_section.Offset() > header_bytes) {
1714 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1715 }
1716
1717 // Field section is 4-byte aligned.
1718 constexpr size_t kFieldSectionAlignment = 4U;
1719 uint32_t end_objects = object_section.Offset() + object_section.Size();
1720 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1721 stats_.alignment_bytes += field_section.Offset() - end_objects;
1722
1723 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1724 uint32_t end_fields = field_section.Offset() + field_section.Size();
1725 CHECK_ALIGNED(method_section.Offset(), 4);
1726 stats_.alignment_bytes += method_section.Offset() - end_fields;
1727
1728 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1729 uint32_t end_methods = method_section.Offset() + method_section.Size();
1730 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1731 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1732
1733 // Intern table is 8-byte aligned.
1734 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1735 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1736 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1737
1738 // Add space between intern table and class table.
1739 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1740 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1741
1742 // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1743 uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1744 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1745 stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1746
Mathieu Chartiere401d142015-04-22 13:56:20 -07001747 stats_.bitmap_bytes += bitmap_section.Size();
1748 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001749 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001750 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001751 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001752 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001753 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001754 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001755
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001756 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001757
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001758 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001759 }
1760
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001761 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001762 class DumpArtMethodVisitor : public ArtMethodVisitor {
1763 public:
1764 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1765
1766 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1767 std::ostream& indent_os = image_dumper_->vios_.Stream();
1768 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1769 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1770 indent_os << "\n";
1771 }
1772
1773 private:
1774 ImageDumper* const image_dumper_;
1775 };
1776
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001777 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001778 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001779 CHECK(type != nullptr);
1780 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001782 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001783 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001784 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001785 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001786 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001787 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001788 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001789 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001790 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001791 }
1792 }
1793
Mathieu Chartierc7853442015-03-27 14:35:38 -07001794 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001795 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001796 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001797 switch (field->GetTypeAsPrimitiveType()) {
1798 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001799 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001800 break;
1801 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001803 break;
1804 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001805 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001806 break;
1807 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001808 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001809 break;
1810 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001811 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001812 break;
1813 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001814 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001815 break;
1816 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001817 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1818 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001819 break;
1820 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001821 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001822 break;
1823 case Primitive::kPrimNot: {
1824 // Get the value, don't compute the type unless it is non-null as we don't want
1825 // to cause class loading.
1826 mirror::Object* value = field->GetObj(obj);
1827 if (value == nullptr) {
1828 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001829 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001830 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001831 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001832 if (field_type != nullptr) {
1833 PrettyObjectValue(os, field_type, value);
1834 } else {
1835 os << StringPrintf("%p %s\n", value,
1836 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1837 }
Ian Rogers50239c72013-06-17 14:53:22 -07001838 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001839 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001840 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001841 default:
1842 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1843 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001844 }
1845 }
1846
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001847 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001848 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001849 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001850 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001851 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001852 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001853 for (ArtField& field : klass->GetIFields()) {
1854 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001855 }
1856 }
1857
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001858 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001859 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001860 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001861
Mathieu Chartiere401d142015-04-22 13:56:20 -07001862 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001863 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001864 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1865 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001866 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001867 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001868 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001869 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001870 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001871 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001872 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001873 }
1874
Mathieu Chartiere401d142015-04-22 13:56:20 -07001875 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001876 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001877 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1878 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001879 return 0;
1880 }
1881 return oat_code_begin[-1];
1882 }
1883
Mathieu Chartiere401d142015-04-22 13:56:20 -07001884 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001885 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001886 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001887 if (oat_code_begin == nullptr) {
1888 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001889 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001890 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001891 }
1892
Mathieu Chartier90443472015-07-16 20:32:27 -07001893 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001894 DCHECK(obj != nullptr);
1895 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001896 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001897 if (!state->InDumpSpace(obj)) {
1898 return;
1899 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001900
1901 size_t object_bytes = obj->SizeOf();
1902 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1903 state->stats_.object_bytes += object_bytes;
1904 state->stats_.alignment_bytes += alignment_bytes;
1905
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001906 std::ostream& os = state->vios_.Stream();
1907
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001908 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001909 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001910 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1911 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001912 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001913 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001914 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1915 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001916 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001917 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001918 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001919 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001920 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001921 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001922 ScopedIndentation indent1(&state->vios_);
1923 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001924 const auto image_pointer_size =
1925 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001926 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001927 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1928 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001929 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001930 size_t run = 0;
1931 for (int32_t j = i + 1; j < length; j++) {
1932 if (value == obj_array->Get(j)) {
1933 run++;
1934 } else {
1935 break;
1936 }
1937 }
1938 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001939 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001940 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001941 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001942 i = i + run;
1943 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001944 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001945 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001946 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001947 }
1948 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001949 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001950 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001951 os << "STATICS:\n";
1952 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001953 for (ArtField& field : klass->GetSFields()) {
1954 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001955 }
1956 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001957 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001958 auto it = state->dex_caches_.find(obj);
1959 if (it != state->dex_caches_.end()) {
1960 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001961 const auto& field_section = state->image_header_.GetImageSection(
1962 ImageHeader::kSectionArtFields);
1963 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001964 size_t num_methods = dex_cache->NumResolvedMethods();
1965 if (num_methods != 0u) {
1966 os << "Methods (size=" << num_methods << "):";
1967 ScopedIndentation indent2(&state->vios_);
1968 auto* resolved_methods = dex_cache->GetResolvedMethods();
1969 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1970 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1971 size_t run = 0;
1972 for (size_t j = i + 1;
1973 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1974 j,
1975 image_pointer_size);
1976 ++j, ++run) {}
1977 if (run == 0) {
1978 os << StringPrintf("%zd: ", i);
1979 } else {
1980 os << StringPrintf("%zd to %zd: ", i, i + run);
1981 i = i + run;
1982 }
1983 std::string msg;
1984 if (elem == nullptr) {
1985 msg = "null";
1986 } else if (method_section.Contains(
1987 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1988 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1989 } else {
1990 msg = "<not in method section>";
1991 }
1992 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001993 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001994 }
1995 size_t num_fields = dex_cache->NumResolvedFields();
1996 if (num_fields != 0u) {
1997 os << "Fields (size=" << num_fields << "):";
1998 ScopedIndentation indent2(&state->vios_);
1999 auto* resolved_fields = dex_cache->GetResolvedFields();
2000 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
2001 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
2002 size_t run = 0;
2003 for (size_t j = i + 1;
2004 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
2005 j,
2006 image_pointer_size);
2007 ++j, ++run) {}
2008 if (run == 0) {
2009 os << StringPrintf("%zd: ", i);
2010 } else {
2011 os << StringPrintf("%zd to %zd: ", i, i + run);
2012 i = i + run;
2013 }
2014 std::string msg;
2015 if (elem == nullptr) {
2016 msg = "null";
2017 } else if (field_section.Contains(
2018 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2019 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
2020 } else {
2021 msg = "<not in field section>";
2022 }
2023 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002024 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002025 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002026 }
2027 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002028 std::string temp;
2029 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002030 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002031
Mathieu Chartiere401d142015-04-22 13:56:20 -07002032 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002033 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034 DCHECK(method != nullptr);
2035 const auto image_pointer_size =
2036 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002037 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2038 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002039 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2040 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002041 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002042 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2043 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2044 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2045 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002046 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002047 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002048 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002049 if (first_occurrence) {
2050 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2051 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002052 if (quick_oat_code_begin !=
2053 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
2054 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002055 }
2056 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2057 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2058 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002059 } else {
2060 const DexFile::CodeItem* code_item = method->GetCodeItem();
2061 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2062 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2063
2064 bool first_occurrence;
2065 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002066 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002067 if (first_occurrence) {
2068 state->stats_.gc_map_bytes += gc_map_bytes;
2069 }
2070
2071 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002072 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002073 if (first_occurrence) {
2074 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2075 }
2076
Roland Levillain6d7f1792015-07-02 10:59:15 +01002077 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002078 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002079 // Method compiled with the optimizing compiler have no vmap table.
2080 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002081 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002082 if (first_occurrence) {
2083 state->stats_.vmap_table_bytes += vmap_table_bytes;
2084 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002085 }
2086
Mathieu Chartiere401d142015-04-22 13:56:20 -07002087 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2088 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2089 if (first_occurrence) {
2090 state->stats_.managed_code_bytes += quick_oat_code_size;
2091 if (method->IsConstructor()) {
2092 if (method->IsStatic()) {
2093 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2094 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2095 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2096 }
2097 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2098 state->stats_.large_method_code_bytes += quick_oat_code_size;
2099 }
2100 }
2101 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2102
Igor Murashkin7617abd2015-07-10 18:27:47 -07002103 uint32_t method_access_flags = method->GetAccessFlags();
2104
Mathieu Chartiere401d142015-04-22 13:56:20 -07002105 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002106 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2107 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2108 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002109
2110 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002111 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002112
2113 double expansion =
2114 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2115 state->stats_.ComputeOutliers(total_size, expansion, method);
2116 }
2117 }
2118
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002119 std::set<const void*> already_seen_;
2120 // Compute the size of the given data within the oat file and whether this is the first time
2121 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002122 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002123 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002124 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002125 already_seen_.insert(oat_data);
2126 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002127 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002128 }
2129 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002130 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002131
2132 public:
2133 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002134 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002135 size_t file_bytes;
2136
2137 size_t header_bytes;
2138 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002139 size_t art_field_bytes;
2140 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002141 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002142 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002143 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002144 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002145 size_t alignment_bytes;
2146
2147 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002148 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002149 size_t managed_to_native_code_bytes;
2150 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002151 size_t class_initializer_code_bytes;
2152 size_t large_initializer_code_bytes;
2153 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002154
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002155 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002156 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002157 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002158
2159 size_t dex_instruction_bytes;
2160
Mathieu Chartiere401d142015-04-22 13:56:20 -07002161 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002162 std::vector<size_t> method_outlier_size;
2163 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002164 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002165
Roland Levillain3887c462015-08-12 18:15:42 +01002166 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002167 : oat_file_bytes(0),
2168 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002169 header_bytes(0),
2170 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002171 art_field_bytes(0),
2172 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002173 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002174 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002175 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002176 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002177 alignment_bytes(0),
2178 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002179 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002180 managed_to_native_code_bytes(0),
2181 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002182 class_initializer_code_bytes(0),
2183 large_initializer_code_bytes(0),
2184 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002185 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002186 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002187 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002188 dex_instruction_bytes(0) {}
2189
Elliott Hughesa0e18062012-04-13 15:59:59 -07002190 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002191 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002192 size_t bytes;
2193 size_t count;
2194 };
2195 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2196 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002197
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002198 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002199 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2200 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002201 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002202 it->second.count += 1;
2203 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002204 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002205 }
2206 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002207
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002208 double PercentOfOatBytes(size_t size) {
2209 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2210 }
2211
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002212 double PercentOfFileBytes(size_t size) {
2213 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2214 }
2215
2216 double PercentOfObjectBytes(size_t size) {
2217 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2218 }
2219
Mathieu Chartiere401d142015-04-22 13:56:20 -07002220 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002221 method_outlier_size.push_back(total_size);
2222 method_outlier_expansion.push_back(expansion);
2223 method_outlier.push_back(method);
2224 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002225
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002226 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002227 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002228 size_t sum_of_sizes = 0;
2229 size_t sum_of_sizes_squared = 0;
2230 size_t sum_of_expansion = 0;
2231 size_t sum_of_expansion_squared = 0;
2232 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002233 if (n == 0) {
2234 return;
2235 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002236 for (size_t i = 0; i < n; i++) {
2237 size_t cur_size = method_outlier_size[i];
2238 sum_of_sizes += cur_size;
2239 sum_of_sizes_squared += cur_size * cur_size;
2240 double cur_expansion = method_outlier_expansion[i];
2241 sum_of_expansion += cur_expansion;
2242 sum_of_expansion_squared += cur_expansion * cur_expansion;
2243 }
2244 size_t size_mean = sum_of_sizes / n;
2245 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2246 double expansion_mean = sum_of_expansion / n;
2247 double expansion_variance =
2248 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2249
2250 // Dump methods whose size is a certain number of standard deviations from the mean
2251 size_t dumped_values = 0;
2252 size_t skipped_values = 0;
2253 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2254 size_t cur_size_variance = i * i * size_variance;
2255 bool first = true;
2256 for (size_t j = 0; j < n; j++) {
2257 size_t cur_size = method_outlier_size[j];
2258 if (cur_size > size_mean) {
2259 size_t cur_var = cur_size - size_mean;
2260 cur_var = cur_var * cur_var;
2261 if (cur_var > cur_size_variance) {
2262 if (dumped_values > 20) {
2263 if (i == 1) {
2264 skipped_values++;
2265 } else {
2266 i = 2; // jump to counting for 1 standard deviation
2267 break;
2268 }
2269 } else {
2270 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002271 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002272 first = false;
2273 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002274 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002275 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002276 method_outlier_size[j] = 0; // don't consider this method again
2277 dumped_values++;
2278 }
2279 }
2280 }
2281 }
2282 }
2283 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002284 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002285 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002286 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002287 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002288
2289 // Dump methods whose expansion is a certain number of standard deviations from the mean
2290 dumped_values = 0;
2291 skipped_values = 0;
2292 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2293 double cur_expansion_variance = i * i * expansion_variance;
2294 bool first = true;
2295 for (size_t j = 0; j < n; j++) {
2296 double cur_expansion = method_outlier_expansion[j];
2297 if (cur_expansion > expansion_mean) {
2298 size_t cur_var = cur_expansion - expansion_mean;
2299 cur_var = cur_var * cur_var;
2300 if (cur_var > cur_expansion_variance) {
2301 if (dumped_values > 20) {
2302 if (i == 1) {
2303 skipped_values++;
2304 } else {
2305 i = 2; // jump to counting for 1 standard deviation
2306 break;
2307 }
2308 } else {
2309 if (first) {
2310 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002311 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002312 first = false;
2313 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002315 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002316 method_outlier_expansion[j] = 0.0; // don't consider this method again
2317 dumped_values++;
2318 }
2319 }
2320 }
2321 }
2322 }
2323 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002324 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002325 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002326 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002327 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002328 }
2329
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002330 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002331 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002332 {
2333 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2334 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002335 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2336 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2337 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2338 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2339 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2340 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002341 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002342 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2343 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 header_bytes, PercentOfFileBytes(header_bytes),
2345 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002346 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2347 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002348 dex_cache_arrays_bytes,
2349 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002350 interned_strings_bytes,
2351 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002352 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002353 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002354 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2355 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002356 CHECK_EQ(file_bytes,
2357 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2358 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2359 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002361
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002362 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002363 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002364 for (const auto& sizes_and_count : sizes_and_counts) {
2365 const std::string& descriptor(sizes_and_count.first);
2366 double average = static_cast<double>(sizes_and_count.second.bytes) /
2367 static_cast<double>(sizes_and_count.second.count);
2368 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002369 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002370 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002371 descriptor.c_str(), sizes_and_count.second.bytes,
2372 sizes_and_count.second.count, average, percent);
2373 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002374 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002375 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002376 CHECK_EQ(object_bytes, object_bytes_total);
2377
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 os << StringPrintf("oat_file_bytes = %8zd\n"
2379 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2380 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2381 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2382 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2383 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2384 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002385 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002386 managed_code_bytes,
2387 PercentOfOatBytes(managed_code_bytes),
2388 managed_to_native_code_bytes,
2389 PercentOfOatBytes(managed_to_native_code_bytes),
2390 native_to_managed_code_bytes,
2391 PercentOfOatBytes(native_to_managed_code_bytes),
2392 class_initializer_code_bytes,
2393 PercentOfOatBytes(class_initializer_code_bytes),
2394 large_initializer_code_bytes,
2395 PercentOfOatBytes(large_initializer_code_bytes),
2396 large_method_code_bytes,
2397 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002398 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002399 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002400 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002401 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2402 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002403 }
2404
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002405 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2406 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2407 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002408 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2409 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2410 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002411 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002412
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002413 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2414 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002415 static_cast<double>(managed_code_bytes) /
2416 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002417 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002418 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002419 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002420
2421 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002422 }
2423 } stats_;
2424
2425 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002426 enum {
2427 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2428 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2429 kLargeConstructorDexBytes = 4000,
2430 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2431 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2432 kLargeMethodDexBytes = 16000
2433 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002434
2435 // For performance, use the *os_ directly for anything that doesn't need indentation
2436 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002437 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002438 VariableIndentationOutputStream vios_;
2439 ScopedIndentation indent1_;
2440
Ian Rogers1d54e732013-05-02 21:10:01 -07002441 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002442 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002443 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002444 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002445 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002446
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002447 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002448};
2449
Jeff Haodcdc85b2015-12-04 14:06:18 -08002450static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002451 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002452 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002453 options->class_loader_ = &null_class_loader;
2454
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002455 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002456 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002457 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2458 CHECK(!image_spaces.empty());
2459 for (gc::space::ImageSpace* image_space : image_spaces) {
2460 const ImageHeader& image_header = image_space->GetImageHeader();
2461 if (!image_header.IsValid()) {
2462 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2463 return EXIT_FAILURE;
2464 }
2465
2466 ImageDumper image_dumper(os, *image_space, image_header, options);
2467 if (!image_dumper.Dump()) {
2468 return EXIT_FAILURE;
2469 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002470 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002471 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002472}
2473
Andreas Gampe00b25f32014-09-17 21:49:05 -07002474static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2475 std::ostream* os) {
2476 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2477
2478 Thread* self = Thread::Current();
2479 CHECK(self != nullptr);
2480 // Need well-known-classes.
2481 WellKnownClasses::Init(self->GetJniEnv());
2482
2483 // Need to register dex files to get a working dex cache.
2484 ScopedObjectAccess soa(self);
2485 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002486 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002487 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002488 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2489 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002490 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002491 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002492 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002493 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002494 }
2495
2496 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002497 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002498 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002499
2500 // Use the class loader while dumping.
2501 StackHandleScope<1> scope(self);
2502 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2503 soa.Decode<mirror::ClassLoader*>(class_loader));
2504 options->class_loader_ = &loader_handle;
2505
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002506 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002507 bool success = oat_dumper.Dump(*os);
2508 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2509}
2510
2511static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002512 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002513 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002514 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002515 options->class_loader_ = &null_class_loader;
2516
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002517 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002518 bool success = oat_dumper.Dump(*os);
2519 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2520}
2521
2522static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2523 std::ostream* os) {
2524 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002525 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002526 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002527 if (oat_file == nullptr) {
2528 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2529 return EXIT_FAILURE;
2530 }
2531
2532 if (runtime != nullptr) {
2533 return DumpOatWithRuntime(runtime, oat_file, options, os);
2534 } else {
2535 return DumpOatWithoutRuntime(oat_file, options, os);
2536 }
2537}
2538
2539static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2540 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002541 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002542 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002543 if (oat_file == nullptr) {
2544 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2545 return EXIT_FAILURE;
2546 }
2547
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002548 bool result;
2549 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2550 // files for 64-bit code in the past.
2551 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
2552 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name);
2553 result = oat_symbolizer.Symbolize();
2554 } else {
2555 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name);
2556 result = oat_symbolizer.Symbolize();
2557 }
2558 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002559 fprintf(stderr, "Failed to symbolize\n");
2560 return EXIT_FAILURE;
2561 }
2562
2563 return EXIT_SUCCESS;
2564}
2565
Igor Murashkin37743352014-11-13 14:38:00 -08002566struct OatdumpArgs : public CmdlineArgs {
2567 protected:
2568 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002569
Igor Murashkin37743352014-11-13 14:38:00 -08002570 virtual ParseStatus ParseCustom(const StringPiece& option,
2571 std::string* error_msg) OVERRIDE {
2572 {
2573 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2574 if (base_parse != kParseUnknownArgument) {
2575 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002576 }
2577 }
2578
Igor Murashkin37743352014-11-13 14:38:00 -08002579 if (option.starts_with("--oat-file=")) {
2580 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2581 } else if (option.starts_with("--image=")) {
2582 image_location_ = option.substr(strlen("--image=")).data();
2583 } else if (option =="--dump:raw_mapping_table") {
2584 dump_raw_mapping_table_ = true;
2585 } else if (option == "--dump:raw_gc_map") {
2586 dump_raw_gc_map_ = true;
2587 } else if (option == "--no-dump:vmap") {
2588 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002589 } else if (option =="--dump:code_info_stack_maps") {
2590 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002591 } else if (option == "--no-disassemble") {
2592 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002593 } else if (option =="--header-only") {
2594 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002595 } else if (option.starts_with("--symbolize=")) {
2596 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2597 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002598 } else if (option.starts_with("--class-filter=")) {
2599 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002600 } else if (option.starts_with("--method-filter=")) {
2601 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002602 } else if (option.starts_with("--list-classes")) {
2603 list_classes_ = true;
2604 } else if (option.starts_with("--list-methods")) {
2605 list_methods_ = true;
2606 } else if (option.starts_with("--export-dex-to=")) {
2607 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2608 } else if (option.starts_with("--addr2instr=")) {
2609 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2610 *error_msg = "Address conversion failed";
2611 return kParseError;
2612 }
Igor Murashkin37743352014-11-13 14:38:00 -08002613 } else {
2614 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002615 }
2616
Igor Murashkin37743352014-11-13 14:38:00 -08002617 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002618 }
2619
Igor Murashkin37743352014-11-13 14:38:00 -08002620 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2621 // Infer boot image location from the image location if possible.
2622 if (boot_image_location_ == nullptr) {
2623 boot_image_location_ = image_location_;
2624 }
2625
2626 // Perform the parent checks.
2627 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2628 if (parent_checks != kParseOk) {
2629 return parent_checks;
2630 }
2631
2632 // Perform our own checks.
2633 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2634 *error_msg = "Either --image or --oat-file must be specified";
2635 return kParseError;
2636 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2637 *error_msg = "Either --image or --oat-file must be specified but not both";
2638 return kParseError;
2639 }
2640
2641 return kParseOk;
2642 }
2643
2644 virtual std::string GetUsage() const {
2645 std::string usage;
2646
2647 usage +=
2648 "Usage: oatdump [options] ...\n"
2649 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2650 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2651 "\n"
2652 // Either oat-file or image is required.
2653 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2654 " Example: --oat-file=/system/framework/boot.oat\n"
2655 "\n"
2656 " --image=<file.art>: specifies an input image location.\n"
2657 " Example: --image=/system/framework/boot.art\n"
2658 "\n";
2659
2660 usage += Base::GetUsage();
2661
2662 usage += // Optional.
2663 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2664 " Example: --dump:raw_mapping_table\n"
2665 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002666 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002667 " Example: --dump:raw_gc_map\n"
2668 "\n"
2669 " --no-dump:vmap may be used to disable vmap dumping.\n"
2670 " Example: --no-dump:vmap\n"
2671 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002672 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2673 " Example: --dump:code_info_stack_maps\n"
2674 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002675 " --no-disassemble may be used to disable disassembly.\n"
2676 " Example: --no-disassemble\n"
2677 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002678 " --header-only may be used to print only the oat header.\n"
2679 " Example: --header-only\n"
2680 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002681 " --list-classes may be used to list target file classes (can be used with filters).\n"
2682 " Example: --list-classes\n"
2683 " Example: --list-classes --class-filter=com.example.foo\n"
2684 "\n"
2685 " --list-methods may be used to list target file methods (can be used with filters).\n"
2686 " Example: --list-methods\n"
2687 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2688 "\n"
2689 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2690 " Example: --symbolize=/system/framework/boot.oat\n"
2691 "\n"
2692 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2693 " Example: --class-filter=com.example.foo\n"
2694 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002695 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2696 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002697 "\n"
2698 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2699 " Example: --export-dex-to=/data/local/tmp\n"
2700 "\n"
2701 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2702 " address (e.g. PC from crash dump)\n"
2703 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002704 "\n";
2705
2706 return usage;
2707 }
2708
2709 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002710 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002711 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002712 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002713 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002714 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002715 bool dump_raw_mapping_table_ = false;
2716 bool dump_raw_gc_map_ = false;
2717 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002718 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002719 bool disassemble_code_ = true;
2720 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002721 bool list_classes_ = false;
2722 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002723 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002724 uint32_t addr2instr_ = 0;
2725 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002726};
2727
Igor Murashkin37743352014-11-13 14:38:00 -08002728struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2729 virtual bool NeedsRuntime() OVERRIDE {
2730 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002731
Igor Murashkin37743352014-11-13 14:38:00 -08002732 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2733 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2734
2735 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2736 args_->dump_raw_mapping_table_,
2737 args_->dump_raw_gc_map_,
2738 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002739 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002740 args_->disassemble_code_,
2741 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002742 args_->class_filter_,
2743 args_->method_filter_,
2744 args_->list_classes_,
2745 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002746 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002747 args_->export_dex_location_,
2748 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002749
2750 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2751 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002752 }
2753
Igor Murashkin37743352014-11-13 14:38:00 -08002754 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2755 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002756 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002757
Mathieu Chartierd424d082014-10-15 10:31:46 -07002758 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002759
Andreas Gampec24f3992014-12-17 20:40:11 -08002760 if (args_->symbolize_) {
2761 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2762 } else {
2763 return DumpOat(nullptr,
2764 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002765 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002766 args_->os_) == EXIT_SUCCESS;
2767 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002768 }
2769
Igor Murashkin37743352014-11-13 14:38:00 -08002770 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2771 CHECK(args_ != nullptr);
2772
2773 if (args_->oat_filename_ != nullptr) {
2774 return DumpOat(runtime,
2775 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002776 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002777 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002778 }
Igor Murashkin37743352014-11-13 14:38:00 -08002779
Jeff Haodcdc85b2015-12-04 14:06:18 -08002780 return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002781 }
2782
Igor Murashkin37743352014-11-13 14:38:00 -08002783 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2784};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002785
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002786} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002787
2788int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002789 art::OatdumpMain main;
2790 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002791}