blob: af08fc4e936a8188dbce81f1f8d70710a2a47f5c [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080036#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080037#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070038#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070040#include "gc/space/image_space.h"
41#include "gc/space/large_object_space.h"
42#include "gc/space/space-inl.h"
Mathieu Chartier4a26f172016-01-26 14:26:18 -080043#include "image-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000045#include "linker/buffered_output_stream.h"
46#include "linker/file_output_stream.h"
Ian Rogers1809a722013-08-09 22:05:32 -070047#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/array-inl.h"
49#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010050#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080056#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070057#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058#include "scoped_thread_state_change.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010059#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080061#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080062#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080063#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070064#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070065#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Igor Murashkin37743352014-11-13 14:38:00 -080067#include <sys/stat.h>
68#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070069
Igor Murashkin37743352014-11-13 14:38:00 -080070namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Mathieu Chartiere401d142015-04-22 13:56:20 -070072const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080073 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070074 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070075 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070077 "kRefsOnlySaveMethod",
78 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070079};
80
81const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Mathieu Chartierac8f4392015-08-27 13:54:20 -070086// Map is so that we don't allocate multiple dex files for the same OatDexFile.
87static std::map<const OatFile::OatDexFile*,
88 std::unique_ptr<const DexFile>> opened_dex_files;
89
90const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
91 DCHECK(oat_dex_file != nullptr);
92 auto it = opened_dex_files.find(oat_dex_file);
93 if (it != opened_dex_files.end()) {
94 return it->second.get();
95 }
96 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
97 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
98 return ret;
99}
100
David Srbeckybc90fd02015-04-22 19:40:27 +0100101class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700102 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100103 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100104 oat_file_(oat_file), builder_(nullptr),
105 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700106 }
107
108 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
109 uint32_t,
110 const OatFile::OatMethod&,
111 const DexFile&,
112 uint32_t,
113 const DexFile::CodeItem*,
114 uint32_t);
115
116 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000117 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
118
119 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
120 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000121 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000122 builder_.reset(new ElfBuilder<ElfTypes32>(isa, output_stream.get()));
123
124 builder_->Start();
125
126 auto* rodata = builder_->GetRoData();
127 auto* text = builder_->GetText();
128 auto* bss = builder_->GetBss();
129 auto* strtab = builder_->GetStrTab();
130 auto* symtab = builder_->GetSymTab();
131
132 rodata->Start();
133 const uint8_t* rodata_begin = oat_file_->Begin();
134 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
135 rodata->WriteFully(rodata_begin, rodata_size);
136 rodata->End();
137
138 text->Start();
139 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
140 const size_t text_size = oat_file_->End() - text_begin;
141 text->WriteFully(text_begin, text_size);
142 text->End();
143
144 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000145 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000146 }
147
148 builder_->WriteDynamicSection(elf_file->GetPath());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700149
150 Walk(&art::OatSymbolizer::RegisterForDedup);
151
152 NormalizeState();
153
David Srbecky6d8c8f02015-10-26 10:57:09 +0000154 strtab->Start();
155 strtab->Write(""); // strtab should start with empty string.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000156 AddTrampolineSymbols();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700157 Walk(&art::OatSymbolizer::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000158 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700159
David Srbecky6d8c8f02015-10-26 10:57:09 +0000160 symtab->Start();
161 symtab->Write();
162 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700163
David Srbecky6d8c8f02015-10-26 10:57:09 +0000164 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700165
Vladimir Marko10c13562015-11-25 14:33:36 +0000166 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700167 }
168
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000169 void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
170 if (code_offset != 0) {
171 uint32_t name_offset = builder_->GetStrTab()->Write(name);
172 uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
Tamas Berghammer9e49ab12016-01-28 16:30:14 +0000173 // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
174 // the end of the section in case of the last symbol.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000175 builder_->GetSymTab()->Add(name_offset, builder_->GetText(), symbol_value,
176 /* is_relative */ true, /* size */ 0, STB_GLOBAL, STT_FUNC);
177 }
178 }
179
180 void AddTrampolineSymbols() {
181 const OatHeader& oat_header = oat_file_->GetOatHeader();
182 AddTrampolineSymbol("interpreterToInterpreterBridge",
183 oat_header.GetInterpreterToInterpreterBridgeOffset());
184 AddTrampolineSymbol("interpreterToCompiledCodeBridge",
185 oat_header.GetInterpreterToCompiledCodeBridgeOffset());
186 AddTrampolineSymbol("jniDlsymLookup",
187 oat_header.GetJniDlsymLookupOffset());
188 AddTrampolineSymbol("quickGenericJniTrampoline",
189 oat_header.GetQuickGenericJniTrampolineOffset());
190 AddTrampolineSymbol("quickImtConflictTrampoline",
191 oat_header.GetQuickImtConflictTrampolineOffset());
192 AddTrampolineSymbol("quickResolutionTrampoline",
193 oat_header.GetQuickResolutionTrampolineOffset());
194 AddTrampolineSymbol("quickToInterpreterBridge",
195 oat_header.GetQuickToInterpreterBridgeOffset());
196 }
197
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700198 void Walk(Callback callback) {
199 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
200 for (size_t i = 0; i < oat_dex_files.size(); i++) {
201 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700202 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700203 WalkOatDexFile(oat_dex_file, callback);
204 }
205 }
206
207 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
208 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700209 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
210 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700211 return;
212 }
213 for (size_t class_def_index = 0;
214 class_def_index < dex_file->NumClassDefs();
215 class_def_index++) {
216 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
217 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
218 OatClassType type = oat_class.GetType();
219 switch (type) {
220 case kOatClassAllCompiled:
221 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700222 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700223 break;
224
225 case kOatClassNoneCompiled:
226 case kOatClassMax:
227 // Ignore.
228 break;
229 }
230 }
231 }
232
233 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
234 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700235 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700236 if (class_data == nullptr) { // empty class such as a marker interface?
237 return;
238 }
239 // Note: even if this is an interface or a native class, we still have to walk it, as there
240 // might be a static initializer.
241 ClassDataItemIterator it(dex_file, class_data);
242 SkipAllFields(&it);
243 uint32_t class_method_idx = 0;
244 while (it.HasNextDirectMethod()) {
245 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
246 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700247 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700248 class_method_idx++;
249 it.Next();
250 }
251 while (it.HasNextVirtualMethod()) {
252 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
253 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700254 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700255 class_method_idx++;
256 it.Next();
257 }
258 DCHECK(!it.HasNext());
259 }
260
261 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
262 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
263 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
264 uint32_t method_access_flags, Callback callback) {
265 if ((method_access_flags & kAccAbstract) != 0) {
266 // Abstract method, no code.
267 return;
268 }
269 if (oat_method.GetCodeOffset() == 0) {
270 // No code.
271 return;
272 }
273
274 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
275 method_access_flags);
276 }
277
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700278 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
279 uint32_t class_method_index ATTRIBUTE_UNUSED,
280 const OatFile::OatMethod& oat_method,
281 const DexFile& dex_file ATTRIBUTE_UNUSED,
282 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
283 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
284 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700285 state_[oat_method.GetCodeOffset()]++;
286 }
287
288 void NormalizeState() {
289 for (auto& x : state_) {
290 if (x.second == 1) {
291 state_[x.first] = 0;
292 }
293 }
294 }
295
296 enum class DedupState { // private
297 kNotDeduplicated,
298 kDeduplicatedFirst,
299 kDeduplicatedOther
300 };
301 DedupState IsDuplicated(uint32_t offset) {
302 if (state_[offset] == 0) {
303 return DedupState::kNotDeduplicated;
304 }
305 if (state_[offset] == 1) {
306 return DedupState::kDeduplicatedOther;
307 }
308 state_[offset] = 1;
309 return DedupState::kDeduplicatedFirst;
310 }
311
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700312 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
313 uint32_t class_method_index ATTRIBUTE_UNUSED,
314 const OatFile::OatMethod& oat_method,
315 const DexFile& dex_file,
316 uint32_t dex_method_idx,
317 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
318 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700319 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
320 if (dedup != DedupState::kDeduplicatedOther) {
321 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
322
323 if (dedup == DedupState::kDeduplicatedFirst) {
324 pretty_name = "[Dedup]" + pretty_name;
325 }
326
David Srbecky6d8c8f02015-10-26 10:57:09 +0000327 int name_offset = builder_->GetStrTab()->Write(pretty_name);
328 builder_->GetSymTab()->Add(name_offset, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700329 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
330 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700331 }
332 }
333
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700334 private:
335 static void SkipAllFields(ClassDataItemIterator* it) {
336 while (it->HasNextStaticField()) {
337 it->Next();
338 }
339 while (it->HasNextInstanceField()) {
340 it->Next();
341 }
342 }
343
344 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100345 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700346 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700347 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700348};
349
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700350class OatDumperOptions {
351 public:
352 OatDumperOptions(bool dump_raw_mapping_table,
353 bool dump_raw_gc_map,
354 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100355 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700356 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700357 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800358 const char* class_filter,
359 const char* method_filter,
360 bool list_classes,
361 bool list_methods,
362 const char* export_dex_location,
363 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700364 : dump_raw_mapping_table_(dump_raw_mapping_table),
365 dump_raw_gc_map_(dump_raw_gc_map),
366 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100367 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700368 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700369 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800370 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000371 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800372 list_classes_(list_classes),
373 list_methods_(list_methods),
374 export_dex_location_(export_dex_location),
375 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800376 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700377
378 const bool dump_raw_mapping_table_;
379 const bool dump_raw_gc_map_;
380 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100381 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700382 const bool disassemble_code_;
383 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800384 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000385 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800386 const bool list_classes_;
387 const bool list_methods_;
388 const char* const export_dex_location_;
389 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700390 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700391};
392
Elliott Hughese3c845c2012-02-28 17:23:01 -0800393class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700394 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100395 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000396 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800397 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700398 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800399 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800400 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
401 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800402 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000403 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100404 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800405 CHECK(options_.class_loader_ != nullptr);
406 CHECK(options_.class_filter_ != nullptr);
407 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800408 AddAllOffsets();
409 }
410
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700411 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700412 delete disassembler_;
413 }
414
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800415 InstructionSet GetInstructionSet() {
416 return instruction_set_;
417 }
418
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700419 bool Dump(std::ostream& os) {
420 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800421 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700422
423 os << "MAGIC:\n";
424 os << oat_header.GetMagic() << "\n\n";
425
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800426 os << "LOCATION:\n";
427 os << oat_file_.GetLocation() << "\n\n";
428
Brian Carlstromaded5f72011-10-07 17:15:04 -0700429 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800430 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700431
Elliott Hughesa72ec822012-03-05 17:12:22 -0800432 os << "INSTRUCTION SET:\n";
433 os << oat_header.GetInstructionSet() << "\n\n";
434
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700435 {
436 std::unique_ptr<const InstructionSetFeatures> features(
437 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
438 oat_header.GetInstructionSetFeaturesBitmap()));
439 os << "INSTRUCTION SET FEATURES:\n";
440 os << features->GetFeatureString() << "\n\n";
441 }
Dave Allison70202782013-10-22 17:52:19 -0700442
Brian Carlstromaded5f72011-10-07 17:15:04 -0700443 os << "DEX FILE COUNT:\n";
444 os << oat_header.GetDexFileCount() << "\n\n";
445
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800446#define DUMP_OAT_HEADER_OFFSET(label, offset) \
447 os << label " OFFSET:\n"; \
448 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800449 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800450 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
451 } \
452 os << StringPrintf("\n\n");
453
454 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
455 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
456 GetInterpreterToInterpreterBridgeOffset);
457 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
458 GetInterpreterToCompiledCodeBridgeOffset);
459 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
460 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800461 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
462 GetQuickGenericJniTrampolineOffset);
463 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
464 GetQuickImtConflictTrampolineOffset);
465 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
466 GetQuickResolutionTrampolineOffset);
467 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
468 GetQuickToInterpreterBridgeOffset);
469#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700470
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700471 os << "IMAGE PATCH DELTA:\n";
472 os << StringPrintf("%d (0x%08x)\n\n",
473 oat_header.GetImagePatchDelta(),
474 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700475
Brian Carlstrom28db0122012-10-18 16:20:41 -0700476 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
477 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
478
479 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800480 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700481
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700482 // Print the key-value store.
483 {
484 os << "KEY VALUE STORE:\n";
485 size_t index = 0;
486 const char* key;
487 const char* value;
488 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
489 os << key << " = " << value << "\n";
490 index++;
491 }
492 os << "\n";
493 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700494
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800495 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700496 os << "BEGIN:\n";
497 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700498
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700499 os << "END:\n";
500 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
501 }
502
503 os << "SIZE:\n";
504 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700505
506 os << std::flush;
507
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800508 // If set, adjust relative address to be searched
509 if (options_.addr2instr_ != 0) {
510 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
511 os << "SEARCH ADDRESS (executable offset + input):\n";
512 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
513 }
514
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800515 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
516 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700517 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800518
519 // If file export selected skip file analysis
520 if (options_.export_dex_location_) {
521 if (!ExportDexFile(os, *oat_dex_file)) {
522 success = false;
523 }
524 } else {
525 if (!DumpOatDexFile(os, *oat_dex_file)) {
526 success = false;
527 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700528 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700529 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700530 os << std::flush;
531 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700532 }
533
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800534 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700535 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
536 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 return 0; // Address not in oat file
538 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800539 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
540 reinterpret_cast<uintptr_t>(oat_file_.Begin());
541 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800543 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800544 return end_offset - begin_offset;
545 }
546
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800547 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700548 return oat_file_.GetOatHeader().GetInstructionSet();
549 }
550
Mathieu Chartier90443472015-07-16 20:32:27 -0700551 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800552 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
553 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700554 CHECK(oat_dex_file != nullptr);
555 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700556 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
557 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700558 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
559 << "': " << error_msg;
560 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800561 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700562 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800563 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700564 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700565 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100566 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800567 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100568 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800569 }
570 }
571 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700572 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800573 }
574
Brian Carlstromaded5f72011-10-07 17:15:04 -0700575 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800576 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800577 // We don't know the length of the code for each method, but we need to know where to stop
578 // when disassembling. What we do know is that a region of code will be followed by some other
579 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
580 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800581 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
582 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700583 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700584 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700585 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
586 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700587 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
588 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800589 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800590 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800591 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800592 for (size_t class_def_index = 0;
593 class_def_index < dex_file->NumClassDefs();
594 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800595 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100596 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700597 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700598 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 ClassDataItemIterator it(*dex_file, class_data);
600 SkipAllFields(it);
601 uint32_t class_method_index = 0;
602 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100603 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800604 it.Next();
605 }
606 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100607 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800608 it.Next();
609 }
610 }
611 }
612 }
613
614 // If the last thing in the file is code for a method, there won't be an offset for the "next"
615 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
616 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800617 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800618 }
619
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700620 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
621 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
622 }
623
Elliott Hughese3c845c2012-02-28 17:23:01 -0800624 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800625 uint32_t code_offset = oat_method.GetCodeOffset();
626 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
627 code_offset &= ~0x1;
628 }
629 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800630 offsets_.insert(oat_method.GetMappingTableOffset());
631 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800632 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800633 }
634
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700635 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
636 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800637 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700638 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800639 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800640 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700641
642 // Create the verifier early.
643
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700644 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700645 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
646 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700647 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700648 os << std::flush;
649 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700650 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100651
652 VariableIndentationOutputStream vios(&os);
653 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800654 for (size_t class_def_index = 0;
655 class_def_index < dex_file->NumClassDefs();
656 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700657 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
658 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800659
660 // TODO: Support regex
661 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
662 continue;
663 }
664
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700665 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100666 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700667 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
668 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100669 << " (" << oat_class.GetStatus() << ")"
670 << " (" << oat_class.GetType() << ")\n";
671 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800672 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700673 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700674 success = false;
675 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800676 if (stop_analysis) {
677 os << std::flush;
678 return success;
679 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700680 }
681
682 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700683 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700684 }
685
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800686 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
687 std::string error_msg;
688 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
689
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700690 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800691 if (dex_file == nullptr) {
692 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
693 return false;
694 }
695 size_t fsize = oat_dex_file.FileSize();
696
697 // Some quick checks just in case
698 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
699 os << "Invalid dex file\n";
700 return false;
701 }
702
703 // Verify output directory exists
704 if (!OS::DirectoryExists(options_.export_dex_location_)) {
705 // TODO: Extend OS::DirectoryExists if symlink support is required
706 os << options_.export_dex_location_ << " output directory not found or symlink\n";
707 return false;
708 }
709
710 // Beautify path names
711 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
712 return false;
713 }
714
715 std::string dex_orig_name;
716 size_t dex_orig_pos = dex_file_location.rfind('/');
717 if (dex_orig_pos == std::string::npos)
718 dex_orig_name = dex_file_location;
719 else
720 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
721
722 // A more elegant approach to efficiently name user installed apps is welcome
723 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
724 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
725 size_t apk_orig_pos = dex_file_location.rfind('/');
726 if (apk_orig_pos != std::string::npos) {
727 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
728 }
729 }
730
731 std::string out_dex_path(options_.export_dex_location_);
732 if (out_dex_path.back() != '/') {
733 out_dex_path.append("/");
734 }
735 out_dex_path.append(dex_orig_name);
736 out_dex_path.append("_export.dex");
737 if (out_dex_path.length() > PATH_MAX) {
738 return false;
739 }
740
741 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
742 if (file.get() == nullptr) {
743 os << "Failed to open output dex file " << out_dex_path;
744 return false;
745 }
746
747 if (!file->WriteFully(dex_file->Begin(), fsize)) {
748 os << "Failed to write dex file";
749 file->Erase();
750 return false;
751 }
752
753 if (file->FlushCloseOrErase() != 0) {
754 os << "Flush and close failed";
755 return false;
756 }
757
758 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
759 os << std::flush;
760
761 return true;
762 }
763
Elliott Hughese3c845c2012-02-28 17:23:01 -0800764 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700765 while (it.HasNextStaticField()) {
766 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700767 }
Ian Rogers0571d352011-11-03 19:51:38 -0700768 while (it.HasNextInstanceField()) {
769 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700770 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800771 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700772
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100773 bool DumpOatClass(VariableIndentationOutputStream* vios,
774 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800775 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700776 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800777 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700778 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700779 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100780 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700781 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800782 }
783 ClassDataItemIterator it(dex_file, class_data);
784 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700785 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700786 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100787 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700788 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800789 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700790 success = false;
791 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800792 if (addr_found) {
793 *stop_analysis = true;
794 return success;
795 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700796 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700797 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700798 }
Ian Rogers0571d352011-11-03 19:51:38 -0700799 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100800 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700801 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800802 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700803 success = false;
804 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800805 if (addr_found) {
806 *stop_analysis = true;
807 return success;
808 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700809 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700810 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700811 }
Ian Rogers0571d352011-11-03 19:51:38 -0700812 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100813 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700814 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700815 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800816
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700817 static constexpr uint32_t kPrologueBytes = 16;
818
819 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
820 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
821
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100822 bool DumpOatMethod(VariableIndentationOutputStream* vios,
823 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700824 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700825 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800826 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800827 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700828 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800829
830 // TODO: Support regex
831 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
832 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000833 return success;
834 }
835
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800836 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100837 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
838 class_method_index, pretty_method.c_str(),
839 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800840 if (options_.list_methods_) return success;
841
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800842 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
843 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
844 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
845 uint32_t code_offset = oat_method.GetCodeOffset();
846 uint32_t code_size = oat_method.GetQuickCodeSize();
847 if (resolved_addr2instr_ != 0) {
848 if (resolved_addr2instr_ > code_offset + code_size) {
849 return success;
850 } else {
851 *addr_found = true; // stop analyzing file at next iteration
852 }
853 }
854
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100855 // Everything below is indented at least once.
856 ScopedIndentation indent1(vios);
857
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800858 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100859 vios->Stream() << "DEX CODE:\n";
860 ScopedIndentation indent2(vios);
861 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700862 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700863
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700864 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700865 std::unique_ptr<verifier::MethodVerifier> verifier;
866 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700867 // We need to have the handle scope stay live until after the verifier since the verifier has
868 // a handle to the dex cache from hs.
869 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100870 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
871 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700872 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100873 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700874 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700875 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800876 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100877 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800878 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100879 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100880 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700882 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100883 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700884 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
885 oat_method_offsets_offset, oat_file_.Size());
886 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100887 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700888 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800889 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700890
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100891 ScopedIndentation indent2(vios);
892 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
894 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100895 vios->Stream() << StringPrintf("WARNING: "
896 "code offset 0x%08x is past end of file 0x%08zx.\n",
897 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700898 success = false;
899 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100900 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700901
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 vios->Stream() << "gc_map: ";
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.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700905 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800906 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100907 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909 vios->Stream() << StringPrintf("WARNING: "
910 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
911 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800913 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100914 ScopedIndentation indent3(vios);
915 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800916 }
917 }
918 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100919 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
921 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700922
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800923 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100924 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700925 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100926 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100928 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700929 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
930 method_header_offset, oat_file_.Size());
931 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100932 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 return false;
934 }
935
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100936 ScopedIndentation indent2(vios);
937 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800938 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100939 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 }
941 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100942 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700943 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100944 vios->Stream() << StringPrintf("WARNING: "
945 "mapping table offset 0x%08x is past end of file 0x%08zx. "
946 "mapping table offset was loaded from offset 0x%08x.\n",
947 mapping_table_offset, oat_file_.Size(),
948 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700949 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800950 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100951 ScopedIndentation indent3(vios);
952 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700953 }
954
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100955 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800956 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100957 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700958 }
959 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100960 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 vios->Stream() << StringPrintf("WARNING: "
963 "vmap table offset 0x%08x is past end of file 0x%08zx. "
964 "vmap table offset was loaded from offset 0x%08x.\n",
965 vmap_table_offset, oat_file_.Size(),
966 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700967 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800968 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100969 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700970 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800971 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700972 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100973 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700974
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100975 ScopedIndentation indent2(vios);
976 vios->Stream()
977 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
978 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
979 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
980 vios->Stream() << "\n";
981 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
982 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
983 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700984 }
985 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100986 // Based on spill masks from QuickMethodFrameInfo so placed
987 // after it is dumped, but useful for understanding quick
988 // code, so dumped here.
989 ScopedIndentation indent2(vios);
990 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700991 }
992 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100993 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700994 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
995 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100996 ScopedIndentation indent2(vios);
997 vios->Stream() << StringPrintf("WARNING: "
998 "code size offset 0x%08x is past end of file 0x%08zx.",
999 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001000 success = false;
1001 } else {
1002 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001003 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1004 uint64_t aligned_code_end = aligned_code_begin + code_size;
1005
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001006 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001007 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001008 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001009 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1010 code_offset,
1011 code_size_offset,
1012 code_size,
1013 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001014
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001015 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001016 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001017 vios->Stream() << StringPrintf("WARNING: "
1018 "start of code at 0x%08x is past end of file 0x%08zx.",
1019 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001020 success = false;
1021 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 vios->Stream() << StringPrintf(
1023 "WARNING: "
1024 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1025 "code size is 0x%08x loaded from offset 0x%08x.\n",
1026 aligned_code_end, oat_file_.Size(),
1027 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001028 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001029 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001030 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001031 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001032 }
1033 }
1034 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001035 vios->Stream() << StringPrintf(
1036 "WARNING: "
1037 "code size %d is bigger than max expected threshold of %d. "
1038 "code size is 0x%08x loaded from offset 0x%08x.\n",
1039 code_size, kMaxCodeSize,
1040 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001041 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001042 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001043 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001044 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001045 }
1046 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001047 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001048 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001049 }
1050 }
1051 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001052 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001053 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001054 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001055
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001056 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1057 if (spill_mask == 0) {
1058 return;
1059 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001060 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001061 for (size_t i = 0; i < 32; i++) {
1062 if ((spill_mask & (1 << i)) != 0) {
1063 if (is_float) {
1064 os << "fr" << i;
1065 } else {
1066 os << "r" << i;
1067 }
1068 spill_mask ^= 1 << i; // clear bit
1069 if (spill_mask != 0) {
1070 os << ", ";
1071 } else {
1072 break;
1073 }
1074 }
1075 }
1076 os << ")";
1077 }
1078
Roland Levillain442b46a2015-02-18 16:54:21 +00001079 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001080 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001081 const OatFile::OatMethod& oat_method,
1082 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001083 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1084 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001085 const void* raw_code_info = oat_method.GetVmapTable();
1086 if (raw_code_info != nullptr) {
1087 CodeInfo code_info(raw_code_info);
1088 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001089 ScopedIndentation indent1(vios);
1090 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001091 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001092 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1093 // We don't encode the size in the table, so just emit that we have quickened
1094 // information.
1095 ScopedIndentation indent(vios);
1096 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001097 } else {
1098 // Otherwise, display the vmap table.
1099 const uint8_t* raw_table = oat_method.GetVmapTable();
1100 if (raw_table != nullptr) {
1101 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001102 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001103 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001104 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001105 }
1106
1107 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001108 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001109 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001110 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001111 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001112 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001113 oat_method.GetCodeOffset(),
1114 code_item.registers_size_,
1115 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001116 }
1117
Roland Levillain442b46a2015-02-18 16:54:21 +00001118 // Display a vmap table.
1119 void DumpVmapTable(std::ostream& os,
1120 const OatFile::OatMethod& oat_method,
1121 const VmapTable& vmap_table) {
1122 bool first = true;
1123 bool processing_fp = false;
1124 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1125 for (size_t i = 0; i < vmap_table.Size(); i++) {
1126 uint16_t dex_reg = vmap_table[i];
1127 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1128 processing_fp ? kFloatVReg : kIntVReg);
1129 os << (first ? "v" : ", v") << dex_reg;
1130 if (!processing_fp) {
1131 os << "/r" << cpu_reg;
1132 } else {
1133 os << "/fr" << cpu_reg;
1134 }
1135 first = false;
1136 if (!processing_fp && dex_reg == 0xFFFF) {
1137 processing_fp = true;
1138 spill_mask = oat_method.GetFpSpillMask();
1139 }
1140 }
1141 os << "\n";
1142 }
1143
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001144 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1145 const DexFile::CodeItem* code_item) {
1146 if (code_item != nullptr) {
1147 size_t num_locals_ins = code_item->registers_size_;
1148 size_t num_ins = code_item->ins_size_;
1149 size_t num_locals = num_locals_ins - num_ins;
1150 size_t num_outs = code_item->outs_size_;
1151
1152 os << "vr_stack_locations:";
1153 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1154 // For readability, delimit the different kinds of VRs.
1155 if (reg == num_locals_ins) {
1156 os << "\n\tmethod*:";
1157 } else if (reg == num_locals && num_ins > 0) {
1158 os << "\n\tins:";
1159 } else if (reg == 0 && num_locals > 0) {
1160 os << "\n\tlocals:";
1161 }
1162
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001163 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1164 code_item,
1165 oat_method.GetCoreSpillMask(),
1166 oat_method.GetFpSpillMask(),
1167 oat_method.GetFrameSizeInBytes(),
1168 reg,
1169 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001170 os << " v" << reg << "[sp + #" << offset << "]";
1171 }
1172
1173 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1174 if (out_reg == 0) {
1175 os << "\n\touts:";
1176 }
1177
1178 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1179 os << " v" << out_reg << "[sp + #" << offset << "]";
1180 }
1181
1182 os << "\n";
1183 }
1184 }
1185
Ian Rogersb23a7722012-10-09 16:54:26 -07001186 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001187 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001188 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001189 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001190 const VmapTable vmap_table(raw_table);
1191 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001192 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001193 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1194 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1195 : oat_method.GetCoreSpillMask();
1196 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001197 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001198 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1199 code_item,
1200 oat_method.GetCoreSpillMask(),
1201 oat_method.GetFpSpillMask(),
1202 oat_method.GetFrameSizeInBytes(),
1203 reg,
1204 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001205 os << "[sp + #" << offset << "]";
1206 }
1207 }
1208 }
1209
Ian Rogersef7d42f2014-01-06 12:55:46 -08001210 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1211 const DexFile::CodeItem* code_item,
1212 size_t num_regs, const uint8_t* reg_bitmap) {
1213 bool first = true;
1214 for (size_t reg = 0; reg < num_regs; reg++) {
1215 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1216 if (first) {
1217 os << " v" << reg << " (";
1218 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1219 os << ")";
1220 first = false;
1221 } else {
1222 os << ", v" << reg << " (";
1223 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1224 os << ")";
1225 }
1226 }
1227 }
1228 if (first) {
1229 os << "No registers in GC map\n";
1230 } else {
1231 os << "\n";
1232 }
1233 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001234 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1235 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001236 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001237 if (gc_map_raw == nullptr) {
1238 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001239 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001240 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001241 NativePcOffsetToReferenceMap map(gc_map_raw);
1242 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1243 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1244 map.GetNativePcOffset(entry);
1245 os << StringPrintf("%p", native_pc);
1246 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001247 }
1248 }
1249
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001250 void DumpMappingTable(VariableIndentationOutputStream* vios,
1251 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001252 const void* quick_code = oat_method.GetQuickCode();
1253 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001254 return;
1255 }
Ian Rogers1809a722013-08-09 22:05:32 -07001256 MappingTable table(oat_method.GetMappingTable());
1257 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001258 if (table.PcToDexSize() != 0) {
1259 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001260 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001261 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001262 ScopedIndentation indent1(vios);
1263 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001264 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001265 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001266 }
1267 if (table.DexToPcSize() != 0) {
1268 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001269 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001270 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001271 ScopedIndentation indent1(vios);
1272 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001273 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001274 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001275 }
1276 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001277 }
1278
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001279 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001280 const OatFile::OatMethod& oat_method,
1281 const DexFile::CodeItem* code_item,
1282 size_t offset,
1283 bool suspend_point_mapping) {
1284 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1285 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001286 ScopedIndentation indent1(vios);
1287 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001288 }
1289 // The return value is not used in the case of a method compiled
1290 // with the optimizing compiler.
1291 return DexFile::kDexNoIndex;
1292 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001293 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001294 }
1295 }
1296
Ian Rogers1809a722013-08-09 22:05:32 -07001297 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1298 size_t offset, bool suspend_point_mapping) {
1299 MappingTable table(oat_method.GetMappingTable());
1300 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1301 typedef MappingTable::PcToDexIterator It;
1302 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1303 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001304 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001305 return cur.DexPc();
1306 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001307 }
Ian Rogers1809a722013-08-09 22:05:32 -07001308 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1309 typedef MappingTable::DexToPcIterator It;
1310 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1311 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001312 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001313 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001314 }
1315 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001316 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001317 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001318 }
1319
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001320 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1321 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001322 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001323 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001324 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001325 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001326 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001327 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001328 bool first = true;
1329 for (size_t reg = 0; reg < num_regs; reg++) {
1330 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1331 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001332 os << "GC map objects: v" << reg << " (";
1333 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001334 os << ")";
1335 first = false;
1336 } else {
1337 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001338 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001339 os << ")";
1340 }
1341 }
1342 }
1343 if (!first) {
1344 os << "\n";
1345 }
1346 }
1347 }
1348 }
1349
Mathieu Chartier590fee92013-09-13 13:46:47 -07001350 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1351 const OatFile::OatMethod& oat_method,
1352 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1353 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001354 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001355 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001356 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1357 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1358 if (kind != kUndefined) {
1359 if (first) {
1360 os << "VRegs: v";
1361 first = false;
1362 } else {
1363 os << ", v";
1364 }
1365 os << reg << " (";
1366 switch (kind) {
1367 case kImpreciseConstant:
1368 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1369 DescribeVReg(os, oat_method, code_item, reg, kind);
1370 break;
1371 case kConstant:
1372 os << "Constant: " << kinds.at((reg * 2) + 1);
1373 break;
1374 default:
1375 DescribeVReg(os, oat_method, code_item, reg, kind);
1376 break;
1377 }
1378 os << ")";
1379 }
1380 }
1381 if (!first) {
1382 os << "\n";
1383 }
1384 }
1385
1386
Ian Rogersb23a7722012-10-09 16:54:26 -07001387 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001388 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001389 size_t i = 0;
1390 while (i < code_item->insns_size_in_code_units_) {
1391 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001392 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1393 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001394 i += instruction->SizeInCodeUnits();
1395 }
1396 }
1397 }
1398
Roland Levillainf2650d12015-05-28 14:53:28 +01001399 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1400 // the optimizing compiler?
1401 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1402 const DexFile::CodeItem* code_item) {
1403 // If the native GC map is null and the Dex `code_item` is not
1404 // null, then this method has been compiled with the optimizing
1405 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001406 return oat_method.GetQuickCode() != nullptr &&
1407 oat_method.GetGcMap() == nullptr &&
1408 code_item != nullptr;
1409 }
1410
1411 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1412 // the dextodex compiler?
1413 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1414 const DexFile::CodeItem* code_item) {
1415 // If the quick code is null, the Dex `code_item` is not
1416 // null, and the vmap table is not null, then this method has been compiled
1417 // with the dextodex compiler.
1418 return oat_method.GetQuickCode() == nullptr &&
1419 oat_method.GetVmapTable() != nullptr &&
1420 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001421 }
1422
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001423 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001424 const OatFile::OatMethod& oat_method,
1425 const DexFile::CodeItem* code_item,
1426 size_t offset) {
1427 // This method is only relevant for oat methods compiled with the
1428 // optimizing compiler.
1429 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1430
1431 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1432 const void* raw_code_info = oat_method.GetVmapTable();
1433 if (raw_code_info != nullptr) {
1434 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001435 StackMapEncoding encoding = code_info.ExtractEncoding();
1436 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001437 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001438 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1439 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001440 }
1441 }
1442 }
1443
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001444 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001445 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001446 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001447 const DexFile* dex_file,
1448 const DexFile::ClassDef& class_def,
1449 const DexFile::CodeItem* code_item,
1450 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001451 if ((method_access_flags & kAccNative) == 0) {
1452 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001453 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001454 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001455 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1456 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001457 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001458 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001459 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1460 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001462
1463 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 }
1465
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001466 void DumpCode(VariableIndentationOutputStream* vios,
1467 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001468 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1469 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001470 const void* quick_code = oat_method.GetQuickCode();
1471
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001472 if (code_size == 0) {
1473 code_size = oat_method.GetQuickCodeSize();
1474 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001475 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001476 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001477 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001478 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001479 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1480 size_t offset = 0;
1481 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001482 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001483 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001484 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001485 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001486 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001487 uint32_t dex_pc =
1488 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001489 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001490 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001491 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001492 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001493 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001494 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001495 }
1496 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001497 }
1498 }
1499
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001500 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001501 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001502 const OatDumperOptions& options_;
1503 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001504 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001505 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001506 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001507};
1508
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001509class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001510 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001511 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1512 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001513 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001514 vios_(os),
1515 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001516 image_space_(image_space),
1517 image_header_(image_header),
1518 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001519
Mathieu Chartier90443472015-07-16 20:32:27 -07001520 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001522 std::ostream& indent_os = vios_.Stream();
1523
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001524 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001525
Jeff Haodcdc85b2015-12-04 14:06:18 -08001526 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1527
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001528 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001529
Mathieu Chartiere401d142015-04-22 13:56:20 -07001530 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1531
1532 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1533 auto section = static_cast<ImageHeader::ImageSections>(i);
1534 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1535 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001536
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001537 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001538
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001539 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001540
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001541 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1542
1543 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1544
1545 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001546
Alex Lighta59dd802014-07-02 16:28:08 -07001547 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1548
Igor Murashkin46774762014-10-22 11:37:02 -07001549 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1550
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 {
1552 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001553 static_assert(arraysize(image_roots_descriptions_) ==
1554 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1556 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1557 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001558 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001559 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001560 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001561 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001562 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001563 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001564 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1565 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001566 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001567 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1568 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001569 run++;
1570 } else {
1571 break;
1572 }
1573 }
1574 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001575 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001576 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001577 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001578 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001579 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001580 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001581 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001582 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001583 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001584 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001585 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001586 }
1587 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001588 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001589
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001590 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001591 os << "METHOD ROOTS\n";
1592 static_assert(arraysize(image_methods_descriptions_) ==
1593 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1594 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1595 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1596 const char* description = image_methods_descriptions_[i];
1597 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001598 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001599 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001600 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001601 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001602
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001603 Runtime* const runtime = Runtime::Current();
1604 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001605 std::string image_filename = image_space_.GetImageFilename();
1606 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001607 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001608 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001609 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001610 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1611 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001612 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001613 oat_file = OatFile::Open(oat_location, oat_location,
1614 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001615 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001616 if (oat_file == nullptr) {
1617 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001618 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001619 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001620 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001622
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001623 stats_.oat_file_bytes = oat_file->Size();
1624
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001625 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001626
Mathieu Chartier02e25112013-08-14 16:14:24 -07001627 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001628 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001629 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1630 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001631 }
1632
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001634
Jeff Haodcdc85b2015-12-04 14:06:18 -08001635 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001636 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001637 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001638 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001639 {
1640 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1641 heap->FlushAllocStack();
1642 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001643 // Since FlushAllocStack() above resets the (active) allocation
1644 // stack. Need to revoke the thread-local allocation stacks that
1645 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001646 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001647 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001648 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001649 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001650 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001651 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001652 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001653 {
1654 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001655 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001656 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001657 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001658 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001659 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001660 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001661 }
1662 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001663 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001664 // Dump the normal objects before ArtMethods.
1665 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1666 indent_os << "\n";
1667 // TODO: Dump fields.
1668 // Dump methods after.
1669 const auto& methods_section = image_header_.GetMethodsSection();
1670 const size_t pointer_size =
1671 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1672 DumpArtMethodVisitor visitor(this);
1673 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001674 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001675 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001676 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001677 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001678 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001679 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001680 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001681 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001682 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001683 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001684 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001685 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001686 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001687 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001688 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1689 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001690 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1691 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001692 const auto& intern_section = image_header_.GetImageSection(
1693 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001694 const auto& class_table_section = image_header_.GetImageSection(
1695 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001696 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1697
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001698 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001699
1700 // Objects are kObjectAlignment-aligned.
1701 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1702 if (object_section.Offset() > header_bytes) {
1703 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1704 }
1705
1706 // Field section is 4-byte aligned.
1707 constexpr size_t kFieldSectionAlignment = 4U;
1708 uint32_t end_objects = object_section.Offset() + object_section.Size();
1709 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1710 stats_.alignment_bytes += field_section.Offset() - end_objects;
1711
1712 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1713 uint32_t end_fields = field_section.Offset() + field_section.Size();
1714 CHECK_ALIGNED(method_section.Offset(), 4);
1715 stats_.alignment_bytes += method_section.Offset() - end_fields;
1716
1717 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1718 uint32_t end_methods = method_section.Offset() + method_section.Size();
1719 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1720 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1721
1722 // Intern table is 8-byte aligned.
1723 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1724 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1725 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1726
1727 // Add space between intern table and class table.
1728 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1729 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1730
1731 // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1732 uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1733 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1734 stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1735
Mathieu Chartiere401d142015-04-22 13:56:20 -07001736 stats_.bitmap_bytes += bitmap_section.Size();
1737 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001738 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001739 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001740 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001741 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001742 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001743 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001744
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001745 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001746
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001747 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001748 }
1749
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001750 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001751 class DumpArtMethodVisitor : public ArtMethodVisitor {
1752 public:
1753 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1754
1755 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1756 std::ostream& indent_os = image_dumper_->vios_.Stream();
1757 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1758 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1759 indent_os << "\n";
1760 }
1761
1762 private:
1763 ImageDumper* const image_dumper_;
1764 };
1765
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001766 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001767 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001768 CHECK(type != nullptr);
1769 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001770 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001771 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001772 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001773 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001774 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001775 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001776 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001777 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001778 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001779 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001780 }
1781 }
1782
Mathieu Chartierc7853442015-03-27 14:35:38 -07001783 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001784 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001785 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001786 switch (field->GetTypeAsPrimitiveType()) {
1787 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001788 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001789 break;
1790 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001791 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001792 break;
1793 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001794 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001795 break;
1796 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001797 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001798 break;
1799 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001800 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001801 break;
1802 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001803 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001804 break;
1805 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001806 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1807 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001808 break;
1809 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001810 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001811 break;
1812 case Primitive::kPrimNot: {
1813 // Get the value, don't compute the type unless it is non-null as we don't want
1814 // to cause class loading.
1815 mirror::Object* value = field->GetObj(obj);
1816 if (value == nullptr) {
1817 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001818 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001819 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001820 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001821 if (field_type != nullptr) {
1822 PrettyObjectValue(os, field_type, value);
1823 } else {
1824 os << StringPrintf("%p %s\n", value,
1825 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1826 }
Ian Rogers50239c72013-06-17 14:53:22 -07001827 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001828 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001829 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001830 default:
1831 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1832 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001833 }
1834 }
1835
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001836 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001837 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001838 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001839 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001840 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001841 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001842 for (ArtField& field : klass->GetIFields()) {
1843 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001844 }
1845 }
1846
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001847 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001848 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001849 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001850
Mathieu Chartiere401d142015-04-22 13:56:20 -07001851 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001852 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001853 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1854 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001855 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001856 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001857 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001858 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001859 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001860 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001861 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001862 }
1863
Mathieu Chartiere401d142015-04-22 13:56:20 -07001864 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001865 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001866 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1867 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001868 return 0;
1869 }
1870 return oat_code_begin[-1];
1871 }
1872
Mathieu Chartiere401d142015-04-22 13:56:20 -07001873 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001874 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001875 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001876 if (oat_code_begin == nullptr) {
1877 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001878 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001879 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001880 }
1881
Mathieu Chartier90443472015-07-16 20:32:27 -07001882 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001883 DCHECK(obj != nullptr);
1884 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001885 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001886 if (!state->InDumpSpace(obj)) {
1887 return;
1888 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001889
1890 size_t object_bytes = obj->SizeOf();
1891 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1892 state->stats_.object_bytes += object_bytes;
1893 state->stats_.alignment_bytes += alignment_bytes;
1894
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001895 std::ostream& os = state->vios_.Stream();
1896
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001897 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001898 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001899 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1900 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001901 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001902 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001903 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1904 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001905 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001906 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001907 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001908 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001909 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001910 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001911 ScopedIndentation indent1(&state->vios_);
1912 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001913 const auto image_pointer_size =
1914 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001915 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001916 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1917 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001918 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001919 size_t run = 0;
1920 for (int32_t j = i + 1; j < length; j++) {
1921 if (value == obj_array->Get(j)) {
1922 run++;
1923 } else {
1924 break;
1925 }
1926 }
1927 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001928 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001929 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001930 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001931 i = i + run;
1932 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001933 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001934 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001935 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001936 }
1937 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001938 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001939 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001940 os << "STATICS:\n";
1941 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001942 for (ArtField& field : klass->GetSFields()) {
1943 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001944 }
1945 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001946 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001947 auto it = state->dex_caches_.find(obj);
1948 if (it != state->dex_caches_.end()) {
1949 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001950 const auto& field_section = state->image_header_.GetImageSection(
1951 ImageHeader::kSectionArtFields);
1952 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001953 size_t num_methods = dex_cache->NumResolvedMethods();
1954 if (num_methods != 0u) {
1955 os << "Methods (size=" << num_methods << "):";
1956 ScopedIndentation indent2(&state->vios_);
1957 auto* resolved_methods = dex_cache->GetResolvedMethods();
1958 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1959 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1960 size_t run = 0;
1961 for (size_t j = i + 1;
1962 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1963 j,
1964 image_pointer_size);
1965 ++j, ++run) {}
1966 if (run == 0) {
1967 os << StringPrintf("%zd: ", i);
1968 } else {
1969 os << StringPrintf("%zd to %zd: ", i, i + run);
1970 i = i + run;
1971 }
1972 std::string msg;
1973 if (elem == nullptr) {
1974 msg = "null";
1975 } else if (method_section.Contains(
1976 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1977 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1978 } else {
1979 msg = "<not in method section>";
1980 }
1981 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001982 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001983 }
1984 size_t num_fields = dex_cache->NumResolvedFields();
1985 if (num_fields != 0u) {
1986 os << "Fields (size=" << num_fields << "):";
1987 ScopedIndentation indent2(&state->vios_);
1988 auto* resolved_fields = dex_cache->GetResolvedFields();
1989 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1990 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1991 size_t run = 0;
1992 for (size_t j = i + 1;
1993 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1994 j,
1995 image_pointer_size);
1996 ++j, ++run) {}
1997 if (run == 0) {
1998 os << StringPrintf("%zd: ", i);
1999 } else {
2000 os << StringPrintf("%zd to %zd: ", i, i + run);
2001 i = i + run;
2002 }
2003 std::string msg;
2004 if (elem == nullptr) {
2005 msg = "null";
2006 } else if (field_section.Contains(
2007 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2008 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
2009 } else {
2010 msg = "<not in field section>";
2011 }
2012 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002013 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002014 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002015 }
2016 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002017 std::string temp;
2018 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002019 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002020
Mathieu Chartiere401d142015-04-22 13:56:20 -07002021 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002022 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002023 DCHECK(method != nullptr);
2024 const auto image_pointer_size =
2025 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002026 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2027 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002028 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2029 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002030 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002031 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2032 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2033 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2034 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002036 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002037 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002038 if (first_occurrence) {
2039 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2040 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002041 if (quick_oat_code_begin !=
2042 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
2043 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002044 }
2045 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2046 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2047 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002048 } else {
2049 const DexFile::CodeItem* code_item = method->GetCodeItem();
2050 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2051 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2052
2053 bool first_occurrence;
2054 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002055 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002056 if (first_occurrence) {
2057 state->stats_.gc_map_bytes += gc_map_bytes;
2058 }
2059
2060 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002061 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002062 if (first_occurrence) {
2063 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2064 }
2065
Roland Levillain6d7f1792015-07-02 10:59:15 +01002066 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002067 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002068 // Method compiled with the optimizing compiler have no vmap table.
2069 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002070 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002071 if (first_occurrence) {
2072 state->stats_.vmap_table_bytes += vmap_table_bytes;
2073 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002074 }
2075
Mathieu Chartiere401d142015-04-22 13:56:20 -07002076 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2077 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2078 if (first_occurrence) {
2079 state->stats_.managed_code_bytes += quick_oat_code_size;
2080 if (method->IsConstructor()) {
2081 if (method->IsStatic()) {
2082 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2083 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2084 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2085 }
2086 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2087 state->stats_.large_method_code_bytes += quick_oat_code_size;
2088 }
2089 }
2090 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2091
Igor Murashkin7617abd2015-07-10 18:27:47 -07002092 uint32_t method_access_flags = method->GetAccessFlags();
2093
Mathieu Chartiere401d142015-04-22 13:56:20 -07002094 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002095 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2096 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2097 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002098
2099 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002100 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002101
2102 double expansion =
2103 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2104 state->stats_.ComputeOutliers(total_size, expansion, method);
2105 }
2106 }
2107
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002108 std::set<const void*> already_seen_;
2109 // Compute the size of the given data within the oat file and whether this is the first time
2110 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002111 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002112 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002113 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002114 already_seen_.insert(oat_data);
2115 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002116 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002117 }
2118 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002119 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002120
2121 public:
2122 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002123 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002124 size_t file_bytes;
2125
2126 size_t header_bytes;
2127 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002128 size_t art_field_bytes;
2129 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002130 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002131 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002132 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002133 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002134 size_t alignment_bytes;
2135
2136 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002137 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002138 size_t managed_to_native_code_bytes;
2139 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002140 size_t class_initializer_code_bytes;
2141 size_t large_initializer_code_bytes;
2142 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002143
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002144 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002145 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002146 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002147
2148 size_t dex_instruction_bytes;
2149
Mathieu Chartiere401d142015-04-22 13:56:20 -07002150 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002151 std::vector<size_t> method_outlier_size;
2152 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002153 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002154
Roland Levillain3887c462015-08-12 18:15:42 +01002155 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002156 : oat_file_bytes(0),
2157 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002158 header_bytes(0),
2159 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002160 art_field_bytes(0),
2161 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002162 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002163 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002164 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002165 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002166 alignment_bytes(0),
2167 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002168 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002169 managed_to_native_code_bytes(0),
2170 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002171 class_initializer_code_bytes(0),
2172 large_initializer_code_bytes(0),
2173 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002174 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002175 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002176 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002177 dex_instruction_bytes(0) {}
2178
Elliott Hughesa0e18062012-04-13 15:59:59 -07002179 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002180 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002181 size_t bytes;
2182 size_t count;
2183 };
2184 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2185 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002186
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002187 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002188 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2189 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002190 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002191 it->second.count += 1;
2192 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002193 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002194 }
2195 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002196
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002197 double PercentOfOatBytes(size_t size) {
2198 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2199 }
2200
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002201 double PercentOfFileBytes(size_t size) {
2202 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2203 }
2204
2205 double PercentOfObjectBytes(size_t size) {
2206 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2207 }
2208
Mathieu Chartiere401d142015-04-22 13:56:20 -07002209 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002210 method_outlier_size.push_back(total_size);
2211 method_outlier_expansion.push_back(expansion);
2212 method_outlier.push_back(method);
2213 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002214
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002215 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002216 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002217 size_t sum_of_sizes = 0;
2218 size_t sum_of_sizes_squared = 0;
2219 size_t sum_of_expansion = 0;
2220 size_t sum_of_expansion_squared = 0;
2221 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002222 if (n == 0) {
2223 return;
2224 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002225 for (size_t i = 0; i < n; i++) {
2226 size_t cur_size = method_outlier_size[i];
2227 sum_of_sizes += cur_size;
2228 sum_of_sizes_squared += cur_size * cur_size;
2229 double cur_expansion = method_outlier_expansion[i];
2230 sum_of_expansion += cur_expansion;
2231 sum_of_expansion_squared += cur_expansion * cur_expansion;
2232 }
2233 size_t size_mean = sum_of_sizes / n;
2234 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2235 double expansion_mean = sum_of_expansion / n;
2236 double expansion_variance =
2237 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2238
2239 // Dump methods whose size is a certain number of standard deviations from the mean
2240 size_t dumped_values = 0;
2241 size_t skipped_values = 0;
2242 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2243 size_t cur_size_variance = i * i * size_variance;
2244 bool first = true;
2245 for (size_t j = 0; j < n; j++) {
2246 size_t cur_size = method_outlier_size[j];
2247 if (cur_size > size_mean) {
2248 size_t cur_var = cur_size - size_mean;
2249 cur_var = cur_var * cur_var;
2250 if (cur_var > cur_size_variance) {
2251 if (dumped_values > 20) {
2252 if (i == 1) {
2253 skipped_values++;
2254 } else {
2255 i = 2; // jump to counting for 1 standard deviation
2256 break;
2257 }
2258 } else {
2259 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002260 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002261 first = false;
2262 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002263 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002264 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002265 method_outlier_size[j] = 0; // don't consider this method again
2266 dumped_values++;
2267 }
2268 }
2269 }
2270 }
2271 }
2272 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002274 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002275 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002276 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002277
2278 // Dump methods whose expansion is a certain number of standard deviations from the mean
2279 dumped_values = 0;
2280 skipped_values = 0;
2281 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2282 double cur_expansion_variance = i * i * expansion_variance;
2283 bool first = true;
2284 for (size_t j = 0; j < n; j++) {
2285 double cur_expansion = method_outlier_expansion[j];
2286 if (cur_expansion > expansion_mean) {
2287 size_t cur_var = cur_expansion - expansion_mean;
2288 cur_var = cur_var * cur_var;
2289 if (cur_var > cur_expansion_variance) {
2290 if (dumped_values > 20) {
2291 if (i == 1) {
2292 skipped_values++;
2293 } else {
2294 i = 2; // jump to counting for 1 standard deviation
2295 break;
2296 }
2297 } else {
2298 if (first) {
2299 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002300 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002301 first = false;
2302 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002303 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002304 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002305 method_outlier_expansion[j] = 0.0; // don't consider this method again
2306 dumped_values++;
2307 }
2308 }
2309 }
2310 }
2311 }
2312 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002314 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002315 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002316 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002317 }
2318
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002319 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002320 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 {
2322 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2323 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002324 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2325 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2326 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2327 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2328 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2329 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002330 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002331 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2332 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002333 header_bytes, PercentOfFileBytes(header_bytes),
2334 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002335 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2336 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002337 dex_cache_arrays_bytes,
2338 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002339 interned_strings_bytes,
2340 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002341 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002342 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2344 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002345 CHECK_EQ(file_bytes,
2346 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2347 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2348 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002349 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002350
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002352 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002353 for (const auto& sizes_and_count : sizes_and_counts) {
2354 const std::string& descriptor(sizes_and_count.first);
2355 double average = static_cast<double>(sizes_and_count.second.bytes) /
2356 static_cast<double>(sizes_and_count.second.count);
2357 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002358 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002359 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002360 descriptor.c_str(), sizes_and_count.second.bytes,
2361 sizes_and_count.second.count, average, percent);
2362 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002363 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002364 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002365 CHECK_EQ(object_bytes, object_bytes_total);
2366
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002367 os << StringPrintf("oat_file_bytes = %8zd\n"
2368 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2369 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2370 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2371 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2372 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2373 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002374 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002375 managed_code_bytes,
2376 PercentOfOatBytes(managed_code_bytes),
2377 managed_to_native_code_bytes,
2378 PercentOfOatBytes(managed_to_native_code_bytes),
2379 native_to_managed_code_bytes,
2380 PercentOfOatBytes(native_to_managed_code_bytes),
2381 class_initializer_code_bytes,
2382 PercentOfOatBytes(class_initializer_code_bytes),
2383 large_initializer_code_bytes,
2384 PercentOfOatBytes(large_initializer_code_bytes),
2385 large_method_code_bytes,
2386 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002387 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002388 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002389 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002390 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2391 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002392 }
2393
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002394 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2395 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2396 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002397 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2398 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2399 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002400 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002401
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002402 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2403 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002404 static_cast<double>(managed_code_bytes) /
2405 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002406 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002407 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002408 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002409
2410 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002411 }
2412 } stats_;
2413
2414 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002415 enum {
2416 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2417 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2418 kLargeConstructorDexBytes = 4000,
2419 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2420 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2421 kLargeMethodDexBytes = 16000
2422 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002423
2424 // For performance, use the *os_ directly for anything that doesn't need indentation
2425 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002426 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002427 VariableIndentationOutputStream vios_;
2428 ScopedIndentation indent1_;
2429
Ian Rogers1d54e732013-05-02 21:10:01 -07002430 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002431 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002432 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002433 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002434 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002435
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002436 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002437};
2438
Jeff Haodcdc85b2015-12-04 14:06:18 -08002439static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002440 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002441 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002442 options->class_loader_ = &null_class_loader;
2443
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002444 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002445 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002446 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2447 CHECK(!image_spaces.empty());
2448 for (gc::space::ImageSpace* image_space : image_spaces) {
2449 const ImageHeader& image_header = image_space->GetImageHeader();
2450 if (!image_header.IsValid()) {
2451 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2452 return EXIT_FAILURE;
2453 }
2454
2455 ImageDumper image_dumper(os, *image_space, image_header, options);
2456 if (!image_dumper.Dump()) {
2457 return EXIT_FAILURE;
2458 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002459 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002460 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002461}
2462
Andreas Gampe00b25f32014-09-17 21:49:05 -07002463static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2464 std::ostream* os) {
2465 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2466
2467 Thread* self = Thread::Current();
2468 CHECK(self != nullptr);
2469 // Need well-known-classes.
2470 WellKnownClasses::Init(self->GetJniEnv());
2471
2472 // Need to register dex files to get a working dex cache.
2473 ScopedObjectAccess soa(self);
2474 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002475 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002476 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002477 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2478 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002479 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002480 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002481 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002482 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002483 }
2484
2485 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002486 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002487 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002488
2489 // Use the class loader while dumping.
2490 StackHandleScope<1> scope(self);
2491 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2492 soa.Decode<mirror::ClassLoader*>(class_loader));
2493 options->class_loader_ = &loader_handle;
2494
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002495 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002496 bool success = oat_dumper.Dump(*os);
2497 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2498}
2499
2500static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002501 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002502 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002503 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002504 options->class_loader_ = &null_class_loader;
2505
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002506 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002507 bool success = oat_dumper.Dump(*os);
2508 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2509}
2510
2511static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2512 std::ostream* os) {
2513 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002514 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002515 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002516 if (oat_file == nullptr) {
2517 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2518 return EXIT_FAILURE;
2519 }
2520
2521 if (runtime != nullptr) {
2522 return DumpOatWithRuntime(runtime, oat_file, options, os);
2523 } else {
2524 return DumpOatWithoutRuntime(oat_file, options, os);
2525 }
2526}
2527
2528static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2529 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002530 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002531 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002532 if (oat_file == nullptr) {
2533 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2534 return EXIT_FAILURE;
2535 }
2536
2537 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002538 if (!oat_symbolizer.Symbolize()) {
2539 fprintf(stderr, "Failed to symbolize\n");
2540 return EXIT_FAILURE;
2541 }
2542
2543 return EXIT_SUCCESS;
2544}
2545
Igor Murashkin37743352014-11-13 14:38:00 -08002546struct OatdumpArgs : public CmdlineArgs {
2547 protected:
2548 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002549
Igor Murashkin37743352014-11-13 14:38:00 -08002550 virtual ParseStatus ParseCustom(const StringPiece& option,
2551 std::string* error_msg) OVERRIDE {
2552 {
2553 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2554 if (base_parse != kParseUnknownArgument) {
2555 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002556 }
2557 }
2558
Igor Murashkin37743352014-11-13 14:38:00 -08002559 if (option.starts_with("--oat-file=")) {
2560 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2561 } else if (option.starts_with("--image=")) {
2562 image_location_ = option.substr(strlen("--image=")).data();
2563 } else if (option =="--dump:raw_mapping_table") {
2564 dump_raw_mapping_table_ = true;
2565 } else if (option == "--dump:raw_gc_map") {
2566 dump_raw_gc_map_ = true;
2567 } else if (option == "--no-dump:vmap") {
2568 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002569 } else if (option =="--dump:code_info_stack_maps") {
2570 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002571 } else if (option == "--no-disassemble") {
2572 disassemble_code_ = false;
2573 } else if (option.starts_with("--symbolize=")) {
2574 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2575 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002576 } else if (option.starts_with("--class-filter=")) {
2577 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002578 } else if (option.starts_with("--method-filter=")) {
2579 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002580 } else if (option.starts_with("--list-classes")) {
2581 list_classes_ = true;
2582 } else if (option.starts_with("--list-methods")) {
2583 list_methods_ = true;
2584 } else if (option.starts_with("--export-dex-to=")) {
2585 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2586 } else if (option.starts_with("--addr2instr=")) {
2587 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2588 *error_msg = "Address conversion failed";
2589 return kParseError;
2590 }
Igor Murashkin37743352014-11-13 14:38:00 -08002591 } else {
2592 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002593 }
2594
Igor Murashkin37743352014-11-13 14:38:00 -08002595 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002596 }
2597
Igor Murashkin37743352014-11-13 14:38:00 -08002598 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2599 // Infer boot image location from the image location if possible.
2600 if (boot_image_location_ == nullptr) {
2601 boot_image_location_ = image_location_;
2602 }
2603
2604 // Perform the parent checks.
2605 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2606 if (parent_checks != kParseOk) {
2607 return parent_checks;
2608 }
2609
2610 // Perform our own checks.
2611 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2612 *error_msg = "Either --image or --oat-file must be specified";
2613 return kParseError;
2614 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2615 *error_msg = "Either --image or --oat-file must be specified but not both";
2616 return kParseError;
2617 }
2618
2619 return kParseOk;
2620 }
2621
2622 virtual std::string GetUsage() const {
2623 std::string usage;
2624
2625 usage +=
2626 "Usage: oatdump [options] ...\n"
2627 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2628 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2629 "\n"
2630 // Either oat-file or image is required.
2631 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2632 " Example: --oat-file=/system/framework/boot.oat\n"
2633 "\n"
2634 " --image=<file.art>: specifies an input image location.\n"
2635 " Example: --image=/system/framework/boot.art\n"
2636 "\n";
2637
2638 usage += Base::GetUsage();
2639
2640 usage += // Optional.
2641 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2642 " Example: --dump:raw_mapping_table\n"
2643 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002644 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002645 " Example: --dump:raw_gc_map\n"
2646 "\n"
2647 " --no-dump:vmap may be used to disable vmap dumping.\n"
2648 " Example: --no-dump:vmap\n"
2649 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002650 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2651 " Example: --dump:code_info_stack_maps\n"
2652 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002653 " --no-disassemble may be used to disable disassembly.\n"
2654 " Example: --no-disassemble\n"
2655 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002656 " --list-classes may be used to list target file classes (can be used with filters).\n"
2657 " Example: --list-classes\n"
2658 " Example: --list-classes --class-filter=com.example.foo\n"
2659 "\n"
2660 " --list-methods may be used to list target file methods (can be used with filters).\n"
2661 " Example: --list-methods\n"
2662 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2663 "\n"
2664 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2665 " Example: --symbolize=/system/framework/boot.oat\n"
2666 "\n"
2667 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2668 " Example: --class-filter=com.example.foo\n"
2669 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002670 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2671 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002672 "\n"
2673 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2674 " Example: --export-dex-to=/data/local/tmp\n"
2675 "\n"
2676 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2677 " address (e.g. PC from crash dump)\n"
2678 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002679 "\n";
2680
2681 return usage;
2682 }
2683
2684 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002685 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002686 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002687 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002688 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002689 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002690 bool dump_raw_mapping_table_ = false;
2691 bool dump_raw_gc_map_ = false;
2692 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002693 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002694 bool disassemble_code_ = true;
2695 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002696 bool list_classes_ = false;
2697 bool list_methods_ = false;
2698 uint32_t addr2instr_ = 0;
2699 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002700};
2701
Igor Murashkin37743352014-11-13 14:38:00 -08002702struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2703 virtual bool NeedsRuntime() OVERRIDE {
2704 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002705
Igor Murashkin37743352014-11-13 14:38:00 -08002706 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2707 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2708
2709 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2710 args_->dump_raw_mapping_table_,
2711 args_->dump_raw_gc_map_,
2712 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002713 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002714 args_->disassemble_code_,
2715 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002716 args_->class_filter_,
2717 args_->method_filter_,
2718 args_->list_classes_,
2719 args_->list_methods_,
2720 args_->export_dex_location_,
2721 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002722
2723 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2724 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002725 }
2726
Igor Murashkin37743352014-11-13 14:38:00 -08002727 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2728 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002729 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002730
Mathieu Chartierd424d082014-10-15 10:31:46 -07002731 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002732
Andreas Gampec24f3992014-12-17 20:40:11 -08002733 if (args_->symbolize_) {
2734 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2735 } else {
2736 return DumpOat(nullptr,
2737 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002738 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002739 args_->os_) == EXIT_SUCCESS;
2740 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002741 }
2742
Igor Murashkin37743352014-11-13 14:38:00 -08002743 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2744 CHECK(args_ != nullptr);
2745
2746 if (args_->oat_filename_ != nullptr) {
2747 return DumpOat(runtime,
2748 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002749 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002750 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002751 }
Igor Murashkin37743352014-11-13 14:38:00 -08002752
Jeff Haodcdc85b2015-12-04 14:06:18 -08002753 return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002754 }
2755
Igor Murashkin37743352014-11-13 14:38:00 -08002756 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2757};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002758
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002759} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002760
2761int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002762 art::OatdumpMain main;
2763 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002764}