blob: 0e17fc2efa7530e445bacf01a15c890b654eeb99 [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
Douglas Leung316a2182015-09-17 15:26:25 -0700151 if (isa == kMips || isa == kMips64) {
152 builder_->WriteMIPSabiflagsSection();
153 }
Vladimir Marko944da602016-02-19 12:27:55 +0000154 builder_->PrepareDynamicSection(
155 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
156 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700157
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800158 Walk(&art::OatSymbolizer<ElfTypes>::RegisterForDedup);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700159
160 NormalizeState();
161
David Srbecky6d8c8f02015-10-26 10:57:09 +0000162 strtab->Start();
163 strtab->Write(""); // strtab should start with empty string.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000164 AddTrampolineSymbols();
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800165 Walk(&art::OatSymbolizer<ElfTypes>::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700167
David Srbecky6d8c8f02015-10-26 10:57:09 +0000168 symtab->Start();
169 symtab->Write();
170 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700171
David Srbecky6d8c8f02015-10-26 10:57:09 +0000172 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700173
Vladimir Marko10c13562015-11-25 14:33:36 +0000174 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700175 }
176
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000177 void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
178 if (code_offset != 0) {
179 uint32_t name_offset = builder_->GetStrTab()->Write(name);
180 uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
Tamas Berghammer9e49ab12016-01-28 16:30:14 +0000181 // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
182 // the end of the section in case of the last symbol.
David Srbecky197160d2016-03-07 17:33:57 +0000183 builder_->GetSymTab()->Add(
184 name_offset,
185 builder_->GetText(),
186 builder_->GetText()->GetAddress() + symbol_value,
187 /* size */ 0,
188 STB_GLOBAL,
189 STT_FUNC);
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000190 }
191 }
192
193 void AddTrampolineSymbols() {
194 const OatHeader& oat_header = oat_file_->GetOatHeader();
195 AddTrampolineSymbol("interpreterToInterpreterBridge",
196 oat_header.GetInterpreterToInterpreterBridgeOffset());
197 AddTrampolineSymbol("interpreterToCompiledCodeBridge",
198 oat_header.GetInterpreterToCompiledCodeBridgeOffset());
199 AddTrampolineSymbol("jniDlsymLookup",
200 oat_header.GetJniDlsymLookupOffset());
201 AddTrampolineSymbol("quickGenericJniTrampoline",
202 oat_header.GetQuickGenericJniTrampolineOffset());
203 AddTrampolineSymbol("quickImtConflictTrampoline",
204 oat_header.GetQuickImtConflictTrampolineOffset());
205 AddTrampolineSymbol("quickResolutionTrampoline",
206 oat_header.GetQuickResolutionTrampolineOffset());
207 AddTrampolineSymbol("quickToInterpreterBridge",
208 oat_header.GetQuickToInterpreterBridgeOffset());
209 }
210
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700211 void Walk(Callback callback) {
212 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
213 for (size_t i = 0; i < oat_dex_files.size(); i++) {
214 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700215 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700216 WalkOatDexFile(oat_dex_file, callback);
217 }
218 }
219
220 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
221 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700222 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
223 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224 return;
225 }
226 for (size_t class_def_index = 0;
227 class_def_index < dex_file->NumClassDefs();
228 class_def_index++) {
229 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
230 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
231 OatClassType type = oat_class.GetType();
232 switch (type) {
233 case kOatClassAllCompiled:
234 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700235 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 break;
237
238 case kOatClassNoneCompiled:
239 case kOatClassMax:
240 // Ignore.
241 break;
242 }
243 }
244 }
245
246 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
247 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700248 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700249 if (class_data == nullptr) { // empty class such as a marker interface?
250 return;
251 }
252 // Note: even if this is an interface or a native class, we still have to walk it, as there
253 // might be a static initializer.
254 ClassDataItemIterator it(dex_file, class_data);
255 SkipAllFields(&it);
256 uint32_t class_method_idx = 0;
257 while (it.HasNextDirectMethod()) {
258 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
259 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700260 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700261 class_method_idx++;
262 it.Next();
263 }
264 while (it.HasNextVirtualMethod()) {
265 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
266 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700267 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700268 class_method_idx++;
269 it.Next();
270 }
271 DCHECK(!it.HasNext());
272 }
273
274 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
275 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
276 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
277 uint32_t method_access_flags, Callback callback) {
278 if ((method_access_flags & kAccAbstract) != 0) {
279 // Abstract method, no code.
280 return;
281 }
282 if (oat_method.GetCodeOffset() == 0) {
283 // No code.
284 return;
285 }
286
287 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
288 method_access_flags);
289 }
290
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700291 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
292 uint32_t class_method_index ATTRIBUTE_UNUSED,
293 const OatFile::OatMethod& oat_method,
294 const DexFile& dex_file ATTRIBUTE_UNUSED,
295 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
296 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
297 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700298 state_[oat_method.GetCodeOffset()]++;
299 }
300
301 void NormalizeState() {
302 for (auto& x : state_) {
303 if (x.second == 1) {
304 state_[x.first] = 0;
305 }
306 }
307 }
308
309 enum class DedupState { // private
310 kNotDeduplicated,
311 kDeduplicatedFirst,
312 kDeduplicatedOther
313 };
314 DedupState IsDuplicated(uint32_t offset) {
315 if (state_[offset] == 0) {
316 return DedupState::kNotDeduplicated;
317 }
318 if (state_[offset] == 1) {
319 return DedupState::kDeduplicatedOther;
320 }
321 state_[offset] = 1;
322 return DedupState::kDeduplicatedFirst;
323 }
324
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700325 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
326 uint32_t class_method_index ATTRIBUTE_UNUSED,
327 const OatFile::OatMethod& oat_method,
328 const DexFile& dex_file,
329 uint32_t dex_method_idx,
330 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
331 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700332 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
333 if (dedup != DedupState::kDeduplicatedOther) {
334 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
335
336 if (dedup == DedupState::kDeduplicatedFirst) {
337 pretty_name = "[Dedup]" + pretty_name;
338 }
339
David Srbecky6d8c8f02015-10-26 10:57:09 +0000340 int name_offset = builder_->GetStrTab()->Write(pretty_name);
David Srbecky197160d2016-03-07 17:33:57 +0000341 uint64_t address = oat_method.GetCodeOffset() -
342 oat_file_->GetOatHeader().GetExecutableOffset() +
343 builder_->GetText()->GetAddress();
344 builder_->GetSymTab()->Add(name_offset,
345 builder_->GetText(),
346 address,
347 oat_method.GetQuickCodeSize(),
348 STB_GLOBAL,
349 STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700350 }
351 }
352
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700353 private:
354 static void SkipAllFields(ClassDataItemIterator* it) {
355 while (it->HasNextStaticField()) {
356 it->Next();
357 }
358 while (it->HasNextInstanceField()) {
359 it->Next();
360 }
361 }
362
363 const OatFile* oat_file_;
Andreas Gampe2d8614b2016-03-07 16:31:34 -0800364 std::unique_ptr<ElfBuilder<ElfTypes> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700365 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700366 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700367};
368
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700369class OatDumperOptions {
370 public:
371 OatDumperOptions(bool dump_raw_mapping_table,
372 bool dump_raw_gc_map,
373 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100374 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700375 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700376 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800377 const char* class_filter,
378 const char* method_filter,
379 bool list_classes,
380 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000381 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800382 const char* export_dex_location,
383 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700384 : dump_raw_mapping_table_(dump_raw_mapping_table),
385 dump_raw_gc_map_(dump_raw_gc_map),
386 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100387 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700388 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700389 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800390 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000391 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800392 list_classes_(list_classes),
393 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000394 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800395 export_dex_location_(export_dex_location),
396 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800397 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700398
399 const bool dump_raw_mapping_table_;
400 const bool dump_raw_gc_map_;
401 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100402 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700403 const bool disassemble_code_;
404 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800405 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000406 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800407 const bool list_classes_;
408 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000409 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800410 const char* const export_dex_location_;
411 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700412 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700413};
414
Elliott Hughese3c845c2012-02-28 17:23:01 -0800415class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700416 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100417 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000418 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800419 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700420 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800421 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800422 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
423 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800424 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000425 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100426 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800427 CHECK(options_.class_loader_ != nullptr);
428 CHECK(options_.class_filter_ != nullptr);
429 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800430 AddAllOffsets();
431 }
432
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700433 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700434 delete disassembler_;
435 }
436
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800437 InstructionSet GetInstructionSet() {
438 return instruction_set_;
439 }
440
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700441 bool Dump(std::ostream& os) {
442 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800443 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700444
445 os << "MAGIC:\n";
446 os << oat_header.GetMagic() << "\n\n";
447
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800448 os << "LOCATION:\n";
449 os << oat_file_.GetLocation() << "\n\n";
450
Brian Carlstromaded5f72011-10-07 17:15:04 -0700451 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800452 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700453
Elliott Hughesa72ec822012-03-05 17:12:22 -0800454 os << "INSTRUCTION SET:\n";
455 os << oat_header.GetInstructionSet() << "\n\n";
456
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700457 {
458 std::unique_ptr<const InstructionSetFeatures> features(
459 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
460 oat_header.GetInstructionSetFeaturesBitmap()));
461 os << "INSTRUCTION SET FEATURES:\n";
462 os << features->GetFeatureString() << "\n\n";
463 }
Dave Allison70202782013-10-22 17:52:19 -0700464
Brian Carlstromaded5f72011-10-07 17:15:04 -0700465 os << "DEX FILE COUNT:\n";
466 os << oat_header.GetDexFileCount() << "\n\n";
467
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800468#define DUMP_OAT_HEADER_OFFSET(label, offset) \
469 os << label " OFFSET:\n"; \
470 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800471 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800472 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
473 } \
474 os << StringPrintf("\n\n");
475
476 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
477 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
478 GetInterpreterToInterpreterBridgeOffset);
479 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
480 GetInterpreterToCompiledCodeBridgeOffset);
481 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
482 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800483 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
484 GetQuickGenericJniTrampolineOffset);
485 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
486 GetQuickImtConflictTrampolineOffset);
487 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
488 GetQuickResolutionTrampolineOffset);
489 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
490 GetQuickToInterpreterBridgeOffset);
491#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700492
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700493 os << "IMAGE PATCH DELTA:\n";
494 os << StringPrintf("%d (0x%08x)\n\n",
495 oat_header.GetImagePatchDelta(),
496 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700497
Brian Carlstrom28db0122012-10-18 16:20:41 -0700498 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
499 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
500
501 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800502 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700503
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700504 // Print the key-value store.
505 {
506 os << "KEY VALUE STORE:\n";
507 size_t index = 0;
508 const char* key;
509 const char* value;
510 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
511 os << key << " = " << value << "\n";
512 index++;
513 }
514 os << "\n";
515 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700516
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800517 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700518 os << "BEGIN:\n";
519 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700520
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700521 os << "END:\n";
522 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
523 }
524
525 os << "SIZE:\n";
526 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700527
528 os << std::flush;
529
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800530 // If set, adjust relative address to be searched
531 if (options_.addr2instr_ != 0) {
532 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
533 os << "SEARCH ADDRESS (executable offset + input):\n";
534 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
535 }
536
David Brazdilc03d7b62016-03-02 12:18:03 +0000537 if (!options_.dump_header_only_) {
538 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
539 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
540 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800541
David Brazdilc03d7b62016-03-02 12:18:03 +0000542 // If file export selected skip file analysis
543 if (options_.export_dex_location_) {
544 if (!ExportDexFile(os, *oat_dex_file)) {
545 success = false;
546 }
547 } else {
548 if (!DumpOatDexFile(os, *oat_dex_file)) {
549 success = false;
550 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800551 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700552 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700553 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000554
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700555 os << std::flush;
556 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700557 }
558
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800559 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700560 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
561 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800562 return 0; // Address not in oat file
563 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800564 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
565 reinterpret_cast<uintptr_t>(oat_file_.Begin());
566 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800567 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800568 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800569 return end_offset - begin_offset;
570 }
571
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800572 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700573 return oat_file_.GetOatHeader().GetInstructionSet();
574 }
575
Mathieu Chartier90443472015-07-16 20:32:27 -0700576 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800577 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
578 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700579 CHECK(oat_dex_file != nullptr);
580 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700581 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
582 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700583 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
584 << "': " << error_msg;
585 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800586 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700587 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800588 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700589 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700590 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100591 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800592 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100593 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800594 }
595 }
596 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700597 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800598 }
599
Brian Carlstromaded5f72011-10-07 17:15:04 -0700600 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800601 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800602 // We don't know the length of the code for each method, but we need to know where to stop
603 // when disassembling. What we do know is that a region of code will be followed by some other
604 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
605 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800606 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
607 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700608 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700609 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700610 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
611 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700612 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
613 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800614 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800615 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800616 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800617 for (size_t class_def_index = 0;
618 class_def_index < dex_file->NumClassDefs();
619 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800620 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100621 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700622 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700623 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800624 ClassDataItemIterator it(*dex_file, class_data);
625 SkipAllFields(it);
626 uint32_t class_method_index = 0;
627 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100628 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800629 it.Next();
630 }
631 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100632 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800633 it.Next();
634 }
635 }
636 }
637 }
638
639 // If the last thing in the file is code for a method, there won't be an offset for the "next"
640 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
641 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800642 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800643 }
644
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700645 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
646 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
647 }
648
Elliott Hughese3c845c2012-02-28 17:23:01 -0800649 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800650 uint32_t code_offset = oat_method.GetCodeOffset();
651 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
652 code_offset &= ~0x1;
653 }
654 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800655 offsets_.insert(oat_method.GetMappingTableOffset());
656 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800657 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800658 }
659
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700660 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
661 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800662 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700663 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800664 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800665 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700666
667 // Create the verifier early.
668
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700669 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700670 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
671 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700672 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700673 os << std::flush;
674 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700675 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100676
677 VariableIndentationOutputStream vios(&os);
678 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800679 for (size_t class_def_index = 0;
680 class_def_index < dex_file->NumClassDefs();
681 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700682 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
683 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800684
685 // TODO: Support regex
686 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
687 continue;
688 }
689
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700690 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100691 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700692 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
693 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100694 << " (" << oat_class.GetStatus() << ")"
695 << " (" << oat_class.GetType() << ")\n";
696 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800697 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700698 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700699 success = false;
700 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800701 if (stop_analysis) {
702 os << std::flush;
703 return success;
704 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700705 }
706
707 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700708 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700709 }
710
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800711 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
712 std::string error_msg;
713 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
714
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700715 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800716 if (dex_file == nullptr) {
717 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
718 return false;
719 }
720 size_t fsize = oat_dex_file.FileSize();
721
722 // Some quick checks just in case
723 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
724 os << "Invalid dex file\n";
725 return false;
726 }
727
728 // Verify output directory exists
729 if (!OS::DirectoryExists(options_.export_dex_location_)) {
730 // TODO: Extend OS::DirectoryExists if symlink support is required
731 os << options_.export_dex_location_ << " output directory not found or symlink\n";
732 return false;
733 }
734
735 // Beautify path names
736 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
737 return false;
738 }
739
740 std::string dex_orig_name;
741 size_t dex_orig_pos = dex_file_location.rfind('/');
742 if (dex_orig_pos == std::string::npos)
743 dex_orig_name = dex_file_location;
744 else
745 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
746
747 // A more elegant approach to efficiently name user installed apps is welcome
748 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
749 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
750 size_t apk_orig_pos = dex_file_location.rfind('/');
751 if (apk_orig_pos != std::string::npos) {
752 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
753 }
754 }
755
756 std::string out_dex_path(options_.export_dex_location_);
757 if (out_dex_path.back() != '/') {
758 out_dex_path.append("/");
759 }
760 out_dex_path.append(dex_orig_name);
761 out_dex_path.append("_export.dex");
762 if (out_dex_path.length() > PATH_MAX) {
763 return false;
764 }
765
766 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
767 if (file.get() == nullptr) {
768 os << "Failed to open output dex file " << out_dex_path;
769 return false;
770 }
771
772 if (!file->WriteFully(dex_file->Begin(), fsize)) {
773 os << "Failed to write dex file";
774 file->Erase();
775 return false;
776 }
777
778 if (file->FlushCloseOrErase() != 0) {
779 os << "Flush and close failed";
780 return false;
781 }
782
783 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
784 os << std::flush;
785
786 return true;
787 }
788
Elliott Hughese3c845c2012-02-28 17:23:01 -0800789 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700790 while (it.HasNextStaticField()) {
791 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700792 }
Ian Rogers0571d352011-11-03 19:51:38 -0700793 while (it.HasNextInstanceField()) {
794 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700795 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800796 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700797
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100798 bool DumpOatClass(VariableIndentationOutputStream* vios,
799 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800800 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700801 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800802 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700803 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700804 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100805 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700806 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800807 }
808 ClassDataItemIterator it(dex_file, class_data);
809 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700810 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700811 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100812 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700813 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800814 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700815 success = false;
816 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800817 if (addr_found) {
818 *stop_analysis = true;
819 return success;
820 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700821 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700822 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700823 }
Ian Rogers0571d352011-11-03 19:51:38 -0700824 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100825 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700826 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800827 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700828 success = false;
829 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800830 if (addr_found) {
831 *stop_analysis = true;
832 return success;
833 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700834 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700835 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700836 }
Ian Rogers0571d352011-11-03 19:51:38 -0700837 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100838 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700839 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700840 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800841
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700842 static constexpr uint32_t kPrologueBytes = 16;
843
844 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
845 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
846
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100847 bool DumpOatMethod(VariableIndentationOutputStream* vios,
848 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700849 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700850 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800851 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800852 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700853 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800854
855 // TODO: Support regex
856 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
857 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000858 return success;
859 }
860
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800861 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100862 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
863 class_method_index, pretty_method.c_str(),
864 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800865 if (options_.list_methods_) return success;
866
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800867 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
868 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
869 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
870 uint32_t code_offset = oat_method.GetCodeOffset();
871 uint32_t code_size = oat_method.GetQuickCodeSize();
872 if (resolved_addr2instr_ != 0) {
873 if (resolved_addr2instr_ > code_offset + code_size) {
874 return success;
875 } else {
876 *addr_found = true; // stop analyzing file at next iteration
877 }
878 }
879
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100880 // Everything below is indented at least once.
881 ScopedIndentation indent1(vios);
882
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800883 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100884 vios->Stream() << "DEX CODE:\n";
885 ScopedIndentation indent2(vios);
886 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700887 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700888
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700889 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700890 std::unique_ptr<verifier::MethodVerifier> verifier;
891 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700892 // We need to have the handle scope stay live until after the verifier since the verifier has
893 // a handle to the dex cache from hs.
894 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100895 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
896 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700897 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100898 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700899 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700900 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800901 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800903 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100904 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100905 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100906 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700907 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100908 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
910 oat_method_offsets_offset, oat_file_.Size());
911 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100912 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700913 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800914 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100916 ScopedIndentation indent2(vios);
917 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700918 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
919 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100920 vios->Stream() << StringPrintf("WARNING: "
921 "code offset 0x%08x is past end of file 0x%08zx.\n",
922 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700923 success = false;
924 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100925 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700926
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100927 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800928 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100929 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800931 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100932 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100934 vios->Stream() << StringPrintf("WARNING: "
935 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
936 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700937 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800938 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100939 ScopedIndentation indent3(vios);
940 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800941 }
942 }
943 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100944 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700945 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
946 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700947
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800948 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100949 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700950 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100951 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700952 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100953 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700954 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
955 method_header_offset, oat_file_.Size());
956 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100957 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700958 return false;
959 }
960
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100961 ScopedIndentation indent2(vios);
962 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800963 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100964 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700965 }
966 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100967 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700968 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100969 vios->Stream() << StringPrintf("WARNING: "
970 "mapping table offset 0x%08x is past end of file 0x%08zx. "
971 "mapping table offset was loaded from offset 0x%08x.\n",
972 mapping_table_offset, oat_file_.Size(),
973 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700974 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800975 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100976 ScopedIndentation indent3(vios);
977 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700978 }
979
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100980 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800981 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100982 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700983 }
984 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100985 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700986 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100987 vios->Stream() << StringPrintf("WARNING: "
988 "vmap table offset 0x%08x is past end of file 0x%08zx. "
989 "vmap table offset was loaded from offset 0x%08x.\n",
990 vmap_table_offset, oat_file_.Size(),
991 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700992 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800993 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100994 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700995 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800996 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700997 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100998 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700999
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001000 ScopedIndentation indent2(vios);
1001 vios->Stream()
1002 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
1003 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
1004 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
1005 vios->Stream() << "\n";
1006 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
1007 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
1008 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001009 }
1010 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001011 // Based on spill masks from QuickMethodFrameInfo so placed
1012 // after it is dumped, but useful for understanding quick
1013 // code, so dumped here.
1014 ScopedIndentation indent2(vios);
1015 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001016 }
1017 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001019 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1020 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001021 ScopedIndentation indent2(vios);
1022 vios->Stream() << StringPrintf("WARNING: "
1023 "code size offset 0x%08x is past end of file 0x%08zx.",
1024 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001025 success = false;
1026 } else {
1027 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001028 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1029 uint64_t aligned_code_end = aligned_code_begin + code_size;
1030
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001031 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001032 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001033 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001034 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1035 code_offset,
1036 code_size_offset,
1037 code_size,
1038 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001039
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001040 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001041 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001042 vios->Stream() << StringPrintf("WARNING: "
1043 "start of code at 0x%08x is past end of file 0x%08zx.",
1044 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001045 success = false;
1046 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001047 vios->Stream() << StringPrintf(
1048 "WARNING: "
1049 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1050 "code size is 0x%08x loaded from offset 0x%08x.\n",
1051 aligned_code_end, oat_file_.Size(),
1052 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001053 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001054 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001055 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001056 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001057 }
1058 }
1059 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001060 vios->Stream() << StringPrintf(
1061 "WARNING: "
1062 "code size %d is bigger than max expected threshold of %d. "
1063 "code size is 0x%08x loaded from offset 0x%08x.\n",
1064 code_size, kMaxCodeSize,
1065 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001066 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001067 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001068 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001069 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001070 }
1071 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001072 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001073 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001074 }
1075 }
1076 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001077 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001078 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001079 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001080
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001081 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1082 if (spill_mask == 0) {
1083 return;
1084 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001085 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001086 for (size_t i = 0; i < 32; i++) {
1087 if ((spill_mask & (1 << i)) != 0) {
1088 if (is_float) {
1089 os << "fr" << i;
1090 } else {
1091 os << "r" << i;
1092 }
1093 spill_mask ^= 1 << i; // clear bit
1094 if (spill_mask != 0) {
1095 os << ", ";
1096 } else {
1097 break;
1098 }
1099 }
1100 }
1101 os << ")";
1102 }
1103
Roland Levillain442b46a2015-02-18 16:54:21 +00001104 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001105 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001106 const OatFile::OatMethod& oat_method,
1107 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001108 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1109 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001110 const void* raw_code_info = oat_method.GetVmapTable();
1111 if (raw_code_info != nullptr) {
1112 CodeInfo code_info(raw_code_info);
1113 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001114 ScopedIndentation indent1(vios);
1115 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001116 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001117 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1118 // We don't encode the size in the table, so just emit that we have quickened
1119 // information.
1120 ScopedIndentation indent(vios);
1121 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001122 } else {
1123 // Otherwise, display the vmap table.
1124 const uint8_t* raw_table = oat_method.GetVmapTable();
1125 if (raw_table != nullptr) {
1126 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001127 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001128 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001129 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001130 }
1131
1132 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001133 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001134 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001135 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001136 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001137 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001138 oat_method.GetCodeOffset(),
1139 code_item.registers_size_,
1140 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001141 }
1142
Roland Levillain442b46a2015-02-18 16:54:21 +00001143 // Display a vmap table.
1144 void DumpVmapTable(std::ostream& os,
1145 const OatFile::OatMethod& oat_method,
1146 const VmapTable& vmap_table) {
1147 bool first = true;
1148 bool processing_fp = false;
1149 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1150 for (size_t i = 0; i < vmap_table.Size(); i++) {
1151 uint16_t dex_reg = vmap_table[i];
1152 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1153 processing_fp ? kFloatVReg : kIntVReg);
1154 os << (first ? "v" : ", v") << dex_reg;
1155 if (!processing_fp) {
1156 os << "/r" << cpu_reg;
1157 } else {
1158 os << "/fr" << cpu_reg;
1159 }
1160 first = false;
1161 if (!processing_fp && dex_reg == 0xFFFF) {
1162 processing_fp = true;
1163 spill_mask = oat_method.GetFpSpillMask();
1164 }
1165 }
1166 os << "\n";
1167 }
1168
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001169 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1170 const DexFile::CodeItem* code_item) {
1171 if (code_item != nullptr) {
1172 size_t num_locals_ins = code_item->registers_size_;
1173 size_t num_ins = code_item->ins_size_;
1174 size_t num_locals = num_locals_ins - num_ins;
1175 size_t num_outs = code_item->outs_size_;
1176
1177 os << "vr_stack_locations:";
1178 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1179 // For readability, delimit the different kinds of VRs.
1180 if (reg == num_locals_ins) {
1181 os << "\n\tmethod*:";
1182 } else if (reg == num_locals && num_ins > 0) {
1183 os << "\n\tins:";
1184 } else if (reg == 0 && num_locals > 0) {
1185 os << "\n\tlocals:";
1186 }
1187
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001188 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1189 code_item,
1190 oat_method.GetCoreSpillMask(),
1191 oat_method.GetFpSpillMask(),
1192 oat_method.GetFrameSizeInBytes(),
1193 reg,
1194 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001195 os << " v" << reg << "[sp + #" << offset << "]";
1196 }
1197
1198 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1199 if (out_reg == 0) {
1200 os << "\n\touts:";
1201 }
1202
1203 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1204 os << " v" << out_reg << "[sp + #" << offset << "]";
1205 }
1206
1207 os << "\n";
1208 }
1209 }
1210
Ian Rogersb23a7722012-10-09 16:54:26 -07001211 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001212 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001213 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001214 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001215 const VmapTable vmap_table(raw_table);
1216 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001217 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001218 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1219 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1220 : oat_method.GetCoreSpillMask();
1221 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001222 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001223 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1224 code_item,
1225 oat_method.GetCoreSpillMask(),
1226 oat_method.GetFpSpillMask(),
1227 oat_method.GetFrameSizeInBytes(),
1228 reg,
1229 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001230 os << "[sp + #" << offset << "]";
1231 }
1232 }
1233 }
1234
Ian Rogersef7d42f2014-01-06 12:55:46 -08001235 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1236 const DexFile::CodeItem* code_item,
1237 size_t num_regs, const uint8_t* reg_bitmap) {
1238 bool first = true;
1239 for (size_t reg = 0; reg < num_regs; reg++) {
1240 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1241 if (first) {
1242 os << " v" << reg << " (";
1243 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1244 os << ")";
1245 first = false;
1246 } else {
1247 os << ", v" << reg << " (";
1248 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1249 os << ")";
1250 }
1251 }
1252 }
1253 if (first) {
1254 os << "No registers in GC map\n";
1255 } else {
1256 os << "\n";
1257 }
1258 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001259 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1260 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001261 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001262 if (gc_map_raw == nullptr) {
1263 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001264 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001265 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001266 NativePcOffsetToReferenceMap map(gc_map_raw);
1267 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1268 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1269 map.GetNativePcOffset(entry);
1270 os << StringPrintf("%p", native_pc);
1271 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001272 }
1273 }
1274
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001275 void DumpMappingTable(VariableIndentationOutputStream* vios,
1276 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001277 const void* quick_code = oat_method.GetQuickCode();
1278 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001279 return;
1280 }
Ian Rogers1809a722013-08-09 22:05:32 -07001281 MappingTable table(oat_method.GetMappingTable());
1282 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001283 if (table.PcToDexSize() != 0) {
1284 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001285 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001286 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001287 ScopedIndentation indent1(vios);
1288 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001289 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001290 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001291 }
1292 if (table.DexToPcSize() != 0) {
1293 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001294 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001295 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001296 ScopedIndentation indent1(vios);
1297 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001298 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001299 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001300 }
1301 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001302 }
1303
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001304 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001305 const OatFile::OatMethod& oat_method,
1306 const DexFile::CodeItem* code_item,
1307 size_t offset,
1308 bool suspend_point_mapping) {
1309 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1310 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001311 ScopedIndentation indent1(vios);
1312 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001313 }
1314 // The return value is not used in the case of a method compiled
1315 // with the optimizing compiler.
1316 return DexFile::kDexNoIndex;
1317 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001318 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001319 }
1320 }
1321
Ian Rogers1809a722013-08-09 22:05:32 -07001322 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1323 size_t offset, bool suspend_point_mapping) {
1324 MappingTable table(oat_method.GetMappingTable());
1325 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1326 typedef MappingTable::PcToDexIterator It;
1327 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1328 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001329 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001330 return cur.DexPc();
1331 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001332 }
Ian Rogers1809a722013-08-09 22:05:32 -07001333 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1334 typedef MappingTable::DexToPcIterator It;
1335 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1336 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001337 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001338 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001339 }
1340 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001341 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001342 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001343 }
1344
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001345 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1346 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001347 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001348 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001349 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001350 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001351 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001352 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001353 bool first = true;
1354 for (size_t reg = 0; reg < num_regs; reg++) {
1355 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1356 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001357 os << "GC map objects: v" << reg << " (";
1358 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001359 os << ")";
1360 first = false;
1361 } else {
1362 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001363 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001364 os << ")";
1365 }
1366 }
1367 }
1368 if (!first) {
1369 os << "\n";
1370 }
1371 }
1372 }
1373 }
1374
Mathieu Chartier590fee92013-09-13 13:46:47 -07001375 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1376 const OatFile::OatMethod& oat_method,
1377 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1378 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001379 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001380 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001381 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1382 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1383 if (kind != kUndefined) {
1384 if (first) {
1385 os << "VRegs: v";
1386 first = false;
1387 } else {
1388 os << ", v";
1389 }
1390 os << reg << " (";
1391 switch (kind) {
1392 case kImpreciseConstant:
1393 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1394 DescribeVReg(os, oat_method, code_item, reg, kind);
1395 break;
1396 case kConstant:
1397 os << "Constant: " << kinds.at((reg * 2) + 1);
1398 break;
1399 default:
1400 DescribeVReg(os, oat_method, code_item, reg, kind);
1401 break;
1402 }
1403 os << ")";
1404 }
1405 }
1406 if (!first) {
1407 os << "\n";
1408 }
1409 }
1410
1411
Ian Rogersb23a7722012-10-09 16:54:26 -07001412 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001413 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001414 size_t i = 0;
1415 while (i < code_item->insns_size_in_code_units_) {
1416 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001417 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1418 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001419 i += instruction->SizeInCodeUnits();
1420 }
1421 }
1422 }
1423
Roland Levillainf2650d12015-05-28 14:53:28 +01001424 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1425 // the optimizing compiler?
1426 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1427 const DexFile::CodeItem* code_item) {
1428 // If the native GC map is null and the Dex `code_item` is not
1429 // null, then this method has been compiled with the optimizing
1430 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001431 return oat_method.GetQuickCode() != nullptr &&
1432 oat_method.GetGcMap() == nullptr &&
1433 code_item != nullptr;
1434 }
1435
1436 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1437 // the dextodex compiler?
1438 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1439 const DexFile::CodeItem* code_item) {
1440 // If the quick code is null, the Dex `code_item` is not
1441 // null, and the vmap table is not null, then this method has been compiled
1442 // with the dextodex compiler.
1443 return oat_method.GetQuickCode() == nullptr &&
1444 oat_method.GetVmapTable() != nullptr &&
1445 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001446 }
1447
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001449 const OatFile::OatMethod& oat_method,
1450 const DexFile::CodeItem* code_item,
1451 size_t offset) {
1452 // This method is only relevant for oat methods compiled with the
1453 // optimizing compiler.
1454 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1455
1456 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1457 const void* raw_code_info = oat_method.GetVmapTable();
1458 if (raw_code_info != nullptr) {
1459 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001460 StackMapEncoding encoding = code_info.ExtractEncoding();
1461 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001462 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001463 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1464 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001465 }
1466 }
1467 }
1468
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001469 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001470 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001471 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001472 const DexFile* dex_file,
1473 const DexFile::ClassDef& class_def,
1474 const DexFile::CodeItem* code_item,
1475 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001476 if ((method_access_flags & kAccNative) == 0) {
1477 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001478 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001479 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001480 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1481 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001482 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001483 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001484 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1485 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001487
1488 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001489 }
1490
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001491 void DumpCode(VariableIndentationOutputStream* vios,
1492 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001493 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1494 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001495 const void* quick_code = oat_method.GetQuickCode();
1496
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001497 if (code_size == 0) {
1498 code_size = oat_method.GetQuickCodeSize();
1499 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001500 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001501 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001502 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001503 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001504 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1505 size_t offset = 0;
1506 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001507 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001508 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001509 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001510 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001511 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001512 uint32_t dex_pc =
1513 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001514 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001515 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001516 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001517 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001518 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001519 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001520 }
1521 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001522 }
1523 }
1524
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001525 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001526 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001527 const OatDumperOptions& options_;
1528 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001529 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001530 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001531 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001532};
1533
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001534class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001535 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001536 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1537 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001538 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001539 vios_(os),
1540 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001541 image_space_(image_space),
1542 image_header_(image_header),
1543 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001544
Mathieu Chartier90443472015-07-16 20:32:27 -07001545 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001547 std::ostream& indent_os = vios_.Stream();
1548
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001550
Jeff Haodcdc85b2015-12-04 14:06:18 -08001551 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1552
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001553 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001554
Mathieu Chartiere401d142015-04-22 13:56:20 -07001555 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1556
1557 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1558 auto section = static_cast<ImageHeader::ImageSections>(i);
1559 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1560 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001561
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001563
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001564 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001565
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001566 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1567
1568 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1569
1570 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001571
Alex Lighta59dd802014-07-02 16:28:08 -07001572 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1573
Igor Murashkin46774762014-10-22 11:37:02 -07001574 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1575
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001576 {
1577 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001578 static_assert(arraysize(image_roots_descriptions_) ==
1579 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001580 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1581 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1582 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001583 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001584 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001585 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001586 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001587 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001588 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001589 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1590 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001591 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001592 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1593 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001594 run++;
1595 } else {
1596 break;
1597 }
1598 }
1599 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001600 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001601 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001602 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001603 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001604 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001605 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001606 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001607 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001608 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001609 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001610 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001611 }
1612 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001613 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001615 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001616 os << "METHOD ROOTS\n";
1617 static_assert(arraysize(image_methods_descriptions_) ==
1618 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1619 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1620 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1621 const char* description = image_methods_descriptions_[i];
1622 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001623 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001624 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001625 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001627
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001628 Runtime* const runtime = Runtime::Current();
1629 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001630 std::string image_filename = image_space_.GetImageFilename();
1631 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001634 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001635 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1636 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001637 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001638 oat_file = OatFile::Open(oat_location, oat_location,
1639 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001640 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001641 if (oat_file == nullptr) {
1642 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001643 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001644 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001645 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001646 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001647
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001648 stats_.oat_file_bytes = oat_file->Size();
1649
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001650 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001651
Mathieu Chartier02e25112013-08-14 16:14:24 -07001652 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001653 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001654 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1655 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001656 }
1657
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001658 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001659
Jeff Haodcdc85b2015-12-04 14:06:18 -08001660 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001661 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001662 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001663 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001664 {
1665 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1666 heap->FlushAllocStack();
1667 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001668 // Since FlushAllocStack() above resets the (active) allocation
1669 // stack. Need to revoke the thread-local allocation stacks that
1670 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001671 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001672 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001673 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001674 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001675 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001676 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001677 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001678 {
1679 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001680 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001681 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001682 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001683 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001684 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001685 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001686 }
1687 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001688 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001689 // Dump the normal objects before ArtMethods.
1690 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1691 indent_os << "\n";
1692 // TODO: Dump fields.
1693 // Dump methods after.
1694 const auto& methods_section = image_header_.GetMethodsSection();
1695 const size_t pointer_size =
1696 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1697 DumpArtMethodVisitor visitor(this);
1698 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001699 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001700 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001702 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001703 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001704 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001705 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001706 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001707 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001708 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001709 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001710 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001711 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001712 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001713 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1714 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001715 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1716 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001717 const auto& intern_section = image_header_.GetImageSection(
1718 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001719 const auto& class_table_section = image_header_.GetImageSection(
1720 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001721 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1722
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001723 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001724
1725 // Objects are kObjectAlignment-aligned.
1726 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1727 if (object_section.Offset() > header_bytes) {
1728 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1729 }
1730
1731 // Field section is 4-byte aligned.
1732 constexpr size_t kFieldSectionAlignment = 4U;
1733 uint32_t end_objects = object_section.Offset() + object_section.Size();
1734 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1735 stats_.alignment_bytes += field_section.Offset() - end_objects;
1736
1737 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1738 uint32_t end_fields = field_section.Offset() + field_section.Size();
1739 CHECK_ALIGNED(method_section.Offset(), 4);
1740 stats_.alignment_bytes += method_section.Offset() - end_fields;
1741
1742 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1743 uint32_t end_methods = method_section.Offset() + method_section.Size();
1744 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1745 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1746
1747 // Intern table is 8-byte aligned.
1748 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1749 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1750 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1751
1752 // Add space between intern table and class table.
1753 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1754 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1755
1756 // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1757 uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1758 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1759 stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1760
Mathieu Chartiere401d142015-04-22 13:56:20 -07001761 stats_.bitmap_bytes += bitmap_section.Size();
1762 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001763 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001764 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001765 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001766 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001767 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001768 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001769
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001770 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001771
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001772 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001773 }
1774
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001775 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001776 class DumpArtMethodVisitor : public ArtMethodVisitor {
1777 public:
1778 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1779
1780 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1781 std::ostream& indent_os = image_dumper_->vios_.Stream();
1782 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1783 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1784 indent_os << "\n";
1785 }
1786
1787 private:
1788 ImageDumper* const image_dumper_;
1789 };
1790
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001791 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001792 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001793 CHECK(type != nullptr);
1794 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001795 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001796 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001797 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001798 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001799 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001800 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001801 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001803 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001804 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001805 }
1806 }
1807
Mathieu Chartierc7853442015-03-27 14:35:38 -07001808 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001809 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001810 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001811 switch (field->GetTypeAsPrimitiveType()) {
1812 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001813 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001814 break;
1815 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001816 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001817 break;
1818 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001819 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001820 break;
1821 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001823 break;
1824 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001825 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001826 break;
1827 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001828 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001829 break;
1830 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001831 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1832 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001833 break;
1834 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001835 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001836 break;
1837 case Primitive::kPrimNot: {
1838 // Get the value, don't compute the type unless it is non-null as we don't want
1839 // to cause class loading.
1840 mirror::Object* value = field->GetObj(obj);
1841 if (value == nullptr) {
1842 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001843 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001844 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001845 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001846 if (field_type != nullptr) {
1847 PrettyObjectValue(os, field_type, value);
1848 } else {
1849 os << StringPrintf("%p %s\n", value,
1850 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1851 }
Ian Rogers50239c72013-06-17 14:53:22 -07001852 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001853 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001854 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001855 default:
1856 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1857 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001858 }
1859 }
1860
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001861 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001862 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001863 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001864 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001865 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001866 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001867 for (ArtField& field : klass->GetIFields()) {
1868 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001869 }
1870 }
1871
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001872 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001873 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001874 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001875
Mathieu Chartiere401d142015-04-22 13:56:20 -07001876 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001877 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001878 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1879 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001880 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001881 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001882 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001883 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001884 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001885 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001886 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001887 }
1888
Mathieu Chartiere401d142015-04-22 13:56:20 -07001889 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001890 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001891 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1892 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001893 return 0;
1894 }
1895 return oat_code_begin[-1];
1896 }
1897
Mathieu Chartiere401d142015-04-22 13:56:20 -07001898 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001899 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001900 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001901 if (oat_code_begin == nullptr) {
1902 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001903 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001904 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001905 }
1906
Mathieu Chartier90443472015-07-16 20:32:27 -07001907 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001908 DCHECK(obj != nullptr);
1909 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001910 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001911 if (!state->InDumpSpace(obj)) {
1912 return;
1913 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001914
1915 size_t object_bytes = obj->SizeOf();
1916 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1917 state->stats_.object_bytes += object_bytes;
1918 state->stats_.alignment_bytes += alignment_bytes;
1919
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001920 std::ostream& os = state->vios_.Stream();
1921
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001922 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001923 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001924 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1925 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001926 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001927 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001928 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1929 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001930 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001931 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001932 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001933 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001934 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001935 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001936 ScopedIndentation indent1(&state->vios_);
1937 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001938 const auto image_pointer_size =
1939 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001940 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001941 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1942 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001943 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001944 size_t run = 0;
1945 for (int32_t j = i + 1; j < length; j++) {
1946 if (value == obj_array->Get(j)) {
1947 run++;
1948 } else {
1949 break;
1950 }
1951 }
1952 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001953 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001954 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001955 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001956 i = i + run;
1957 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001958 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001959 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001960 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001961 }
1962 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001963 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001964 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001965 os << "STATICS:\n";
1966 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001967 for (ArtField& field : klass->GetSFields()) {
1968 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001969 }
1970 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001971 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001972 auto it = state->dex_caches_.find(obj);
1973 if (it != state->dex_caches_.end()) {
1974 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001975 const auto& field_section = state->image_header_.GetImageSection(
1976 ImageHeader::kSectionArtFields);
1977 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001978 size_t num_methods = dex_cache->NumResolvedMethods();
1979 if (num_methods != 0u) {
1980 os << "Methods (size=" << num_methods << "):";
1981 ScopedIndentation indent2(&state->vios_);
1982 auto* resolved_methods = dex_cache->GetResolvedMethods();
1983 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1984 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1985 size_t run = 0;
1986 for (size_t j = i + 1;
1987 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1988 j,
1989 image_pointer_size);
1990 ++j, ++run) {}
1991 if (run == 0) {
1992 os << StringPrintf("%zd: ", i);
1993 } else {
1994 os << StringPrintf("%zd to %zd: ", i, i + run);
1995 i = i + run;
1996 }
1997 std::string msg;
1998 if (elem == nullptr) {
1999 msg = "null";
2000 } else if (method_section.Contains(
2001 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2002 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
2003 } else {
2004 msg = "<not in method section>";
2005 }
2006 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07002007 }
Vladimir Marko05792b92015-08-03 11:56:49 +01002008 }
2009 size_t num_fields = dex_cache->NumResolvedFields();
2010 if (num_fields != 0u) {
2011 os << "Fields (size=" << num_fields << "):";
2012 ScopedIndentation indent2(&state->vios_);
2013 auto* resolved_fields = dex_cache->GetResolvedFields();
2014 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
2015 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
2016 size_t run = 0;
2017 for (size_t j = i + 1;
2018 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
2019 j,
2020 image_pointer_size);
2021 ++j, ++run) {}
2022 if (run == 0) {
2023 os << StringPrintf("%zd: ", i);
2024 } else {
2025 os << StringPrintf("%zd to %zd: ", i, i + run);
2026 i = i + run;
2027 }
2028 std::string msg;
2029 if (elem == nullptr) {
2030 msg = "null";
2031 } else if (field_section.Contains(
2032 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2033 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
2034 } else {
2035 msg = "<not in field section>";
2036 }
2037 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002038 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002039 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002040 }
2041 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002042 std::string temp;
2043 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002044 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002045
Mathieu Chartiere401d142015-04-22 13:56:20 -07002046 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002047 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048 DCHECK(method != nullptr);
2049 const auto image_pointer_size =
2050 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002051 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2052 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002053 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2054 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002055 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002056 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2057 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2058 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2059 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002060 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002061 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002062 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002063 if (first_occurrence) {
2064 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2065 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002066 if (quick_oat_code_begin !=
2067 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
2068 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002069 }
2070 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2071 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2072 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002073 } else {
2074 const DexFile::CodeItem* code_item = method->GetCodeItem();
2075 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2076 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2077
2078 bool first_occurrence;
2079 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002080 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002081 if (first_occurrence) {
2082 state->stats_.gc_map_bytes += gc_map_bytes;
2083 }
2084
2085 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002086 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002087 if (first_occurrence) {
2088 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2089 }
2090
Roland Levillain6d7f1792015-07-02 10:59:15 +01002091 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002092 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002093 // Method compiled with the optimizing compiler have no vmap table.
2094 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002095 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002096 if (first_occurrence) {
2097 state->stats_.vmap_table_bytes += vmap_table_bytes;
2098 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002099 }
2100
Mathieu Chartiere401d142015-04-22 13:56:20 -07002101 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2102 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2103 if (first_occurrence) {
2104 state->stats_.managed_code_bytes += quick_oat_code_size;
2105 if (method->IsConstructor()) {
2106 if (method->IsStatic()) {
2107 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2108 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2109 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2110 }
2111 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2112 state->stats_.large_method_code_bytes += quick_oat_code_size;
2113 }
2114 }
2115 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2116
Igor Murashkin7617abd2015-07-10 18:27:47 -07002117 uint32_t method_access_flags = method->GetAccessFlags();
2118
Mathieu Chartiere401d142015-04-22 13:56:20 -07002119 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002120 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2121 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2122 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002123
2124 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002125 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002126
2127 double expansion =
2128 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2129 state->stats_.ComputeOutliers(total_size, expansion, method);
2130 }
2131 }
2132
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002133 std::set<const void*> already_seen_;
2134 // Compute the size of the given data within the oat file and whether this is the first time
2135 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002136 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002137 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002138 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002139 already_seen_.insert(oat_data);
2140 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002141 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002142 }
2143 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002144 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002145
2146 public:
2147 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002148 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002149 size_t file_bytes;
2150
2151 size_t header_bytes;
2152 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002153 size_t art_field_bytes;
2154 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002155 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002156 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002157 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002158 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002159 size_t alignment_bytes;
2160
2161 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002162 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002163 size_t managed_to_native_code_bytes;
2164 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002165 size_t class_initializer_code_bytes;
2166 size_t large_initializer_code_bytes;
2167 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002168
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002169 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002170 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002171 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002172
2173 size_t dex_instruction_bytes;
2174
Mathieu Chartiere401d142015-04-22 13:56:20 -07002175 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002176 std::vector<size_t> method_outlier_size;
2177 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002178 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002179
Roland Levillain3887c462015-08-12 18:15:42 +01002180 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002181 : oat_file_bytes(0),
2182 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002183 header_bytes(0),
2184 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002185 art_field_bytes(0),
2186 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002187 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002188 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002189 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002190 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002191 alignment_bytes(0),
2192 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002193 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002194 managed_to_native_code_bytes(0),
2195 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002196 class_initializer_code_bytes(0),
2197 large_initializer_code_bytes(0),
2198 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002199 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002200 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002201 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002202 dex_instruction_bytes(0) {}
2203
Elliott Hughesa0e18062012-04-13 15:59:59 -07002204 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002205 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002206 size_t bytes;
2207 size_t count;
2208 };
2209 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2210 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002211
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002212 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002213 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2214 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002215 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002216 it->second.count += 1;
2217 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002218 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002219 }
2220 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002221
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002222 double PercentOfOatBytes(size_t size) {
2223 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2224 }
2225
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002226 double PercentOfFileBytes(size_t size) {
2227 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2228 }
2229
2230 double PercentOfObjectBytes(size_t size) {
2231 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2232 }
2233
Mathieu Chartiere401d142015-04-22 13:56:20 -07002234 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002235 method_outlier_size.push_back(total_size);
2236 method_outlier_expansion.push_back(expansion);
2237 method_outlier.push_back(method);
2238 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002239
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002240 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002241 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002242 size_t sum_of_sizes = 0;
2243 size_t sum_of_sizes_squared = 0;
2244 size_t sum_of_expansion = 0;
2245 size_t sum_of_expansion_squared = 0;
2246 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002247 if (n == 0) {
2248 return;
2249 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002250 for (size_t i = 0; i < n; i++) {
2251 size_t cur_size = method_outlier_size[i];
2252 sum_of_sizes += cur_size;
2253 sum_of_sizes_squared += cur_size * cur_size;
2254 double cur_expansion = method_outlier_expansion[i];
2255 sum_of_expansion += cur_expansion;
2256 sum_of_expansion_squared += cur_expansion * cur_expansion;
2257 }
2258 size_t size_mean = sum_of_sizes / n;
2259 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2260 double expansion_mean = sum_of_expansion / n;
2261 double expansion_variance =
2262 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2263
2264 // Dump methods whose size is a certain number of standard deviations from the mean
2265 size_t dumped_values = 0;
2266 size_t skipped_values = 0;
2267 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2268 size_t cur_size_variance = i * i * size_variance;
2269 bool first = true;
2270 for (size_t j = 0; j < n; j++) {
2271 size_t cur_size = method_outlier_size[j];
2272 if (cur_size > size_mean) {
2273 size_t cur_var = cur_size - size_mean;
2274 cur_var = cur_var * cur_var;
2275 if (cur_var > cur_size_variance) {
2276 if (dumped_values > 20) {
2277 if (i == 1) {
2278 skipped_values++;
2279 } else {
2280 i = 2; // jump to counting for 1 standard deviation
2281 break;
2282 }
2283 } else {
2284 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002285 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002286 first = false;
2287 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002288 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002289 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002290 method_outlier_size[j] = 0; // don't consider this method again
2291 dumped_values++;
2292 }
2293 }
2294 }
2295 }
2296 }
2297 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002299 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002300 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002301 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002302
2303 // Dump methods whose expansion is a certain number of standard deviations from the mean
2304 dumped_values = 0;
2305 skipped_values = 0;
2306 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2307 double cur_expansion_variance = i * i * expansion_variance;
2308 bool first = true;
2309 for (size_t j = 0; j < n; j++) {
2310 double cur_expansion = method_outlier_expansion[j];
2311 if (cur_expansion > expansion_mean) {
2312 size_t cur_var = cur_expansion - expansion_mean;
2313 cur_var = cur_var * cur_var;
2314 if (cur_var > cur_expansion_variance) {
2315 if (dumped_values > 20) {
2316 if (i == 1) {
2317 skipped_values++;
2318 } else {
2319 i = 2; // jump to counting for 1 standard deviation
2320 break;
2321 }
2322 } else {
2323 if (first) {
2324 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002325 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002326 first = false;
2327 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002329 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002330 method_outlier_expansion[j] = 0.0; // don't consider this method again
2331 dumped_values++;
2332 }
2333 }
2334 }
2335 }
2336 }
2337 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002339 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002340 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002341 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002342 }
2343
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002344 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002345 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002346 {
2347 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2348 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002349 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2350 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2351 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2352 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2353 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2354 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002355 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002356 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2357 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002358 header_bytes, PercentOfFileBytes(header_bytes),
2359 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002360 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2361 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002362 dex_cache_arrays_bytes,
2363 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002364 interned_strings_bytes,
2365 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002366 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002367 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2369 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002370 CHECK_EQ(file_bytes,
2371 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2372 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2373 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002374 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002375
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002376 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002377 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002378 for (const auto& sizes_and_count : sizes_and_counts) {
2379 const std::string& descriptor(sizes_and_count.first);
2380 double average = static_cast<double>(sizes_and_count.second.bytes) /
2381 static_cast<double>(sizes_and_count.second.count);
2382 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002384 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002385 descriptor.c_str(), sizes_and_count.second.bytes,
2386 sizes_and_count.second.count, average, percent);
2387 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002388 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002389 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002390 CHECK_EQ(object_bytes, object_bytes_total);
2391
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002392 os << StringPrintf("oat_file_bytes = %8zd\n"
2393 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2394 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2395 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2396 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2397 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2398 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002399 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002400 managed_code_bytes,
2401 PercentOfOatBytes(managed_code_bytes),
2402 managed_to_native_code_bytes,
2403 PercentOfOatBytes(managed_to_native_code_bytes),
2404 native_to_managed_code_bytes,
2405 PercentOfOatBytes(native_to_managed_code_bytes),
2406 class_initializer_code_bytes,
2407 PercentOfOatBytes(class_initializer_code_bytes),
2408 large_initializer_code_bytes,
2409 PercentOfOatBytes(large_initializer_code_bytes),
2410 large_method_code_bytes,
2411 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002412 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002413 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002414 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002415 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2416 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002417 }
2418
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002419 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2420 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2421 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002422 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2423 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2424 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002425 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002426
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002427 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2428 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002429 static_cast<double>(managed_code_bytes) /
2430 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002431 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002432 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002433 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002434
2435 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002436 }
2437 } stats_;
2438
2439 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002440 enum {
2441 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2442 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2443 kLargeConstructorDexBytes = 4000,
2444 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2445 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2446 kLargeMethodDexBytes = 16000
2447 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002448
2449 // For performance, use the *os_ directly for anything that doesn't need indentation
2450 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002451 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002452 VariableIndentationOutputStream vios_;
2453 ScopedIndentation indent1_;
2454
Ian Rogers1d54e732013-05-02 21:10:01 -07002455 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002456 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002457 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002458 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002459 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002460
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002461 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002462};
2463
Jeff Haodcdc85b2015-12-04 14:06:18 -08002464static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002465 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002466 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002467 options->class_loader_ = &null_class_loader;
2468
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002469 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002470 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002471 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2472 CHECK(!image_spaces.empty());
2473 for (gc::space::ImageSpace* image_space : image_spaces) {
2474 const ImageHeader& image_header = image_space->GetImageHeader();
2475 if (!image_header.IsValid()) {
2476 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2477 return EXIT_FAILURE;
2478 }
2479
2480 ImageDumper image_dumper(os, *image_space, image_header, options);
2481 if (!image_dumper.Dump()) {
2482 return EXIT_FAILURE;
2483 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002484 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002485 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002486}
2487
Andreas Gampe00b25f32014-09-17 21:49:05 -07002488static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2489 std::ostream* os) {
2490 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2491
2492 Thread* self = Thread::Current();
2493 CHECK(self != nullptr);
2494 // Need well-known-classes.
2495 WellKnownClasses::Init(self->GetJniEnv());
2496
2497 // Need to register dex files to get a working dex cache.
2498 ScopedObjectAccess soa(self);
2499 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002500 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002501 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002502 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2503 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002504 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002505 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002506 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002507 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002508 }
2509
2510 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002511 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002512 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002513
2514 // Use the class loader while dumping.
2515 StackHandleScope<1> scope(self);
2516 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2517 soa.Decode<mirror::ClassLoader*>(class_loader));
2518 options->class_loader_ = &loader_handle;
2519
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002520 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002521 bool success = oat_dumper.Dump(*os);
2522 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2523}
2524
2525static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002526 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002527 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002528 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002529 options->class_loader_ = &null_class_loader;
2530
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002531 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002532 bool success = oat_dumper.Dump(*os);
2533 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2534}
2535
2536static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2537 std::ostream* os) {
2538 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002539 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002540 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002541 if (oat_file == nullptr) {
2542 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2543 return EXIT_FAILURE;
2544 }
2545
2546 if (runtime != nullptr) {
2547 return DumpOatWithRuntime(runtime, oat_file, options, os);
2548 } else {
2549 return DumpOatWithoutRuntime(oat_file, options, os);
2550 }
2551}
2552
2553static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2554 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002555 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002556 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002557 if (oat_file == nullptr) {
2558 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2559 return EXIT_FAILURE;
2560 }
2561
Andreas Gampe2d8614b2016-03-07 16:31:34 -08002562 bool result;
2563 // Try to produce an ELF file of the same type. This is finicky, as we have used 32-bit ELF
2564 // files for 64-bit code in the past.
2565 if (Is64BitInstructionSet(oat_file->GetOatHeader().GetInstructionSet())) {
2566 OatSymbolizer<ElfTypes64> oat_symbolizer(oat_file, output_name);
2567 result = oat_symbolizer.Symbolize();
2568 } else {
2569 OatSymbolizer<ElfTypes32> oat_symbolizer(oat_file, output_name);
2570 result = oat_symbolizer.Symbolize();
2571 }
2572 if (!result) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002573 fprintf(stderr, "Failed to symbolize\n");
2574 return EXIT_FAILURE;
2575 }
2576
2577 return EXIT_SUCCESS;
2578}
2579
Igor Murashkin37743352014-11-13 14:38:00 -08002580struct OatdumpArgs : public CmdlineArgs {
2581 protected:
2582 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002583
Igor Murashkin37743352014-11-13 14:38:00 -08002584 virtual ParseStatus ParseCustom(const StringPiece& option,
2585 std::string* error_msg) OVERRIDE {
2586 {
2587 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2588 if (base_parse != kParseUnknownArgument) {
2589 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002590 }
2591 }
2592
Igor Murashkin37743352014-11-13 14:38:00 -08002593 if (option.starts_with("--oat-file=")) {
2594 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2595 } else if (option.starts_with("--image=")) {
2596 image_location_ = option.substr(strlen("--image=")).data();
2597 } else if (option =="--dump:raw_mapping_table") {
2598 dump_raw_mapping_table_ = true;
2599 } else if (option == "--dump:raw_gc_map") {
2600 dump_raw_gc_map_ = true;
2601 } else if (option == "--no-dump:vmap") {
2602 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002603 } else if (option =="--dump:code_info_stack_maps") {
2604 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002605 } else if (option == "--no-disassemble") {
2606 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002607 } else if (option =="--header-only") {
2608 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002609 } else if (option.starts_with("--symbolize=")) {
2610 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2611 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002612 } else if (option.starts_with("--class-filter=")) {
2613 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002614 } else if (option.starts_with("--method-filter=")) {
2615 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002616 } else if (option.starts_with("--list-classes")) {
2617 list_classes_ = true;
2618 } else if (option.starts_with("--list-methods")) {
2619 list_methods_ = true;
2620 } else if (option.starts_with("--export-dex-to=")) {
2621 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2622 } else if (option.starts_with("--addr2instr=")) {
2623 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2624 *error_msg = "Address conversion failed";
2625 return kParseError;
2626 }
Igor Murashkin37743352014-11-13 14:38:00 -08002627 } else {
2628 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002629 }
2630
Igor Murashkin37743352014-11-13 14:38:00 -08002631 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002632 }
2633
Igor Murashkin37743352014-11-13 14:38:00 -08002634 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2635 // Infer boot image location from the image location if possible.
2636 if (boot_image_location_ == nullptr) {
2637 boot_image_location_ = image_location_;
2638 }
2639
2640 // Perform the parent checks.
2641 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2642 if (parent_checks != kParseOk) {
2643 return parent_checks;
2644 }
2645
2646 // Perform our own checks.
2647 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2648 *error_msg = "Either --image or --oat-file must be specified";
2649 return kParseError;
2650 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2651 *error_msg = "Either --image or --oat-file must be specified but not both";
2652 return kParseError;
2653 }
2654
2655 return kParseOk;
2656 }
2657
2658 virtual std::string GetUsage() const {
2659 std::string usage;
2660
2661 usage +=
2662 "Usage: oatdump [options] ...\n"
2663 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2664 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2665 "\n"
2666 // Either oat-file or image is required.
2667 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2668 " Example: --oat-file=/system/framework/boot.oat\n"
2669 "\n"
2670 " --image=<file.art>: specifies an input image location.\n"
2671 " Example: --image=/system/framework/boot.art\n"
2672 "\n";
2673
2674 usage += Base::GetUsage();
2675
2676 usage += // Optional.
2677 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2678 " Example: --dump:raw_mapping_table\n"
2679 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002680 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002681 " Example: --dump:raw_gc_map\n"
2682 "\n"
2683 " --no-dump:vmap may be used to disable vmap dumping.\n"
2684 " Example: --no-dump:vmap\n"
2685 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002686 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2687 " Example: --dump:code_info_stack_maps\n"
2688 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002689 " --no-disassemble may be used to disable disassembly.\n"
2690 " Example: --no-disassemble\n"
2691 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002692 " --header-only may be used to print only the oat header.\n"
2693 " Example: --header-only\n"
2694 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002695 " --list-classes may be used to list target file classes (can be used with filters).\n"
2696 " Example: --list-classes\n"
2697 " Example: --list-classes --class-filter=com.example.foo\n"
2698 "\n"
2699 " --list-methods may be used to list target file methods (can be used with filters).\n"
2700 " Example: --list-methods\n"
2701 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2702 "\n"
2703 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2704 " Example: --symbolize=/system/framework/boot.oat\n"
2705 "\n"
2706 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2707 " Example: --class-filter=com.example.foo\n"
2708 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002709 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2710 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002711 "\n"
2712 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2713 " Example: --export-dex-to=/data/local/tmp\n"
2714 "\n"
2715 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2716 " address (e.g. PC from crash dump)\n"
2717 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002718 "\n";
2719
2720 return usage;
2721 }
2722
2723 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002724 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002725 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002726 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002727 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002728 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002729 bool dump_raw_mapping_table_ = false;
2730 bool dump_raw_gc_map_ = false;
2731 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002732 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002733 bool disassemble_code_ = true;
2734 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002735 bool list_classes_ = false;
2736 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002737 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002738 uint32_t addr2instr_ = 0;
2739 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002740};
2741
Igor Murashkin37743352014-11-13 14:38:00 -08002742struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2743 virtual bool NeedsRuntime() OVERRIDE {
2744 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002745
Igor Murashkin37743352014-11-13 14:38:00 -08002746 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2747 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2748
2749 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2750 args_->dump_raw_mapping_table_,
2751 args_->dump_raw_gc_map_,
2752 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002753 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002754 args_->disassemble_code_,
2755 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002756 args_->class_filter_,
2757 args_->method_filter_,
2758 args_->list_classes_,
2759 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002760 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002761 args_->export_dex_location_,
2762 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002763
2764 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2765 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002766 }
2767
Igor Murashkin37743352014-11-13 14:38:00 -08002768 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2769 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002770 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002771
Mathieu Chartierd424d082014-10-15 10:31:46 -07002772 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002773
Andreas Gampec24f3992014-12-17 20:40:11 -08002774 if (args_->symbolize_) {
2775 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2776 } else {
2777 return DumpOat(nullptr,
2778 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002779 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002780 args_->os_) == EXIT_SUCCESS;
2781 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002782 }
2783
Igor Murashkin37743352014-11-13 14:38:00 -08002784 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2785 CHECK(args_ != nullptr);
2786
2787 if (args_->oat_filename_ != nullptr) {
2788 return DumpOat(runtime,
2789 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002790 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002791 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002792 }
Igor Murashkin37743352014-11-13 14:38:00 -08002793
Jeff Haodcdc85b2015-12-04 14:06:18 -08002794 return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002795 }
2796
Igor Murashkin37743352014-11-13 14:38:00 -08002797 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2798};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002799
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002800} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002801
2802int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002803 art::OatdumpMain main;
2804 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002805}