blob: c1875366e951762d2e6204ea43b57103ad7282fc [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
Vladimir Marko944da602016-02-19 12:27:55 +0000148 builder_->PrepareDynamicSection(
149 elf_file->GetPath(), rodata_size, text_size, oat_file_->BssSize());
150 builder_->WriteDynamicSection();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700151
152 Walk(&art::OatSymbolizer::RegisterForDedup);
153
154 NormalizeState();
155
David Srbecky6d8c8f02015-10-26 10:57:09 +0000156 strtab->Start();
157 strtab->Write(""); // strtab should start with empty string.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000158 AddTrampolineSymbols();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700159 Walk(&art::OatSymbolizer::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000160 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700161
David Srbecky6d8c8f02015-10-26 10:57:09 +0000162 symtab->Start();
163 symtab->Write();
164 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700165
David Srbecky6d8c8f02015-10-26 10:57:09 +0000166 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700167
Vladimir Marko10c13562015-11-25 14:33:36 +0000168 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700169 }
170
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000171 void AddTrampolineSymbol(const char* name, uint32_t code_offset) {
172 if (code_offset != 0) {
173 uint32_t name_offset = builder_->GetStrTab()->Write(name);
174 uint64_t symbol_value = code_offset - oat_file_->GetOatHeader().GetExecutableOffset();
Tamas Berghammer9e49ab12016-01-28 16:30:14 +0000175 // Specifying 0 as the symbol size means that the symbol lasts until the next symbol or until
176 // the end of the section in case of the last symbol.
Tamas Berghammer8f75c452016-01-26 18:03:43 +0000177 builder_->GetSymTab()->Add(name_offset, builder_->GetText(), symbol_value,
178 /* is_relative */ true, /* size */ 0, STB_GLOBAL, STT_FUNC);
179 }
180 }
181
182 void AddTrampolineSymbols() {
183 const OatHeader& oat_header = oat_file_->GetOatHeader();
184 AddTrampolineSymbol("interpreterToInterpreterBridge",
185 oat_header.GetInterpreterToInterpreterBridgeOffset());
186 AddTrampolineSymbol("interpreterToCompiledCodeBridge",
187 oat_header.GetInterpreterToCompiledCodeBridgeOffset());
188 AddTrampolineSymbol("jniDlsymLookup",
189 oat_header.GetJniDlsymLookupOffset());
190 AddTrampolineSymbol("quickGenericJniTrampoline",
191 oat_header.GetQuickGenericJniTrampolineOffset());
192 AddTrampolineSymbol("quickImtConflictTrampoline",
193 oat_header.GetQuickImtConflictTrampolineOffset());
194 AddTrampolineSymbol("quickResolutionTrampoline",
195 oat_header.GetQuickResolutionTrampolineOffset());
196 AddTrampolineSymbol("quickToInterpreterBridge",
197 oat_header.GetQuickToInterpreterBridgeOffset());
198 }
199
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700200 void Walk(Callback callback) {
201 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
202 for (size_t i = 0; i < oat_dex_files.size(); i++) {
203 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700204 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700205 WalkOatDexFile(oat_dex_file, callback);
206 }
207 }
208
209 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
210 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700211 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
212 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700213 return;
214 }
215 for (size_t class_def_index = 0;
216 class_def_index < dex_file->NumClassDefs();
217 class_def_index++) {
218 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
219 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
220 OatClassType type = oat_class.GetType();
221 switch (type) {
222 case kOatClassAllCompiled:
223 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700224 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700225 break;
226
227 case kOatClassNoneCompiled:
228 case kOatClassMax:
229 // Ignore.
230 break;
231 }
232 }
233 }
234
235 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
236 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700237 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238 if (class_data == nullptr) { // empty class such as a marker interface?
239 return;
240 }
241 // Note: even if this is an interface or a native class, we still have to walk it, as there
242 // might be a static initializer.
243 ClassDataItemIterator it(dex_file, class_data);
244 SkipAllFields(&it);
245 uint32_t class_method_idx = 0;
246 while (it.HasNextDirectMethod()) {
247 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
248 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700249 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700250 class_method_idx++;
251 it.Next();
252 }
253 while (it.HasNextVirtualMethod()) {
254 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
255 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700256 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 class_method_idx++;
258 it.Next();
259 }
260 DCHECK(!it.HasNext());
261 }
262
263 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
264 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
265 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
266 uint32_t method_access_flags, Callback callback) {
267 if ((method_access_flags & kAccAbstract) != 0) {
268 // Abstract method, no code.
269 return;
270 }
271 if (oat_method.GetCodeOffset() == 0) {
272 // No code.
273 return;
274 }
275
276 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
277 method_access_flags);
278 }
279
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700280 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
281 uint32_t class_method_index ATTRIBUTE_UNUSED,
282 const OatFile::OatMethod& oat_method,
283 const DexFile& dex_file ATTRIBUTE_UNUSED,
284 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
285 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
286 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700287 state_[oat_method.GetCodeOffset()]++;
288 }
289
290 void NormalizeState() {
291 for (auto& x : state_) {
292 if (x.second == 1) {
293 state_[x.first] = 0;
294 }
295 }
296 }
297
298 enum class DedupState { // private
299 kNotDeduplicated,
300 kDeduplicatedFirst,
301 kDeduplicatedOther
302 };
303 DedupState IsDuplicated(uint32_t offset) {
304 if (state_[offset] == 0) {
305 return DedupState::kNotDeduplicated;
306 }
307 if (state_[offset] == 1) {
308 return DedupState::kDeduplicatedOther;
309 }
310 state_[offset] = 1;
311 return DedupState::kDeduplicatedFirst;
312 }
313
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700314 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
315 uint32_t class_method_index ATTRIBUTE_UNUSED,
316 const OatFile::OatMethod& oat_method,
317 const DexFile& dex_file,
318 uint32_t dex_method_idx,
319 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
320 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700321 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
322 if (dedup != DedupState::kDeduplicatedOther) {
323 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
324
325 if (dedup == DedupState::kDeduplicatedFirst) {
326 pretty_name = "[Dedup]" + pretty_name;
327 }
328
David Srbecky6d8c8f02015-10-26 10:57:09 +0000329 int name_offset = builder_->GetStrTab()->Write(pretty_name);
330 builder_->GetSymTab()->Add(name_offset, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700331 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
332 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700333 }
334 }
335
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700336 private:
337 static void SkipAllFields(ClassDataItemIterator* it) {
338 while (it->HasNextStaticField()) {
339 it->Next();
340 }
341 while (it->HasNextInstanceField()) {
342 it->Next();
343 }
344 }
345
346 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100347 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700348 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700349 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700350};
351
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352class OatDumperOptions {
353 public:
354 OatDumperOptions(bool dump_raw_mapping_table,
355 bool dump_raw_gc_map,
356 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100357 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700358 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700359 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800360 const char* class_filter,
361 const char* method_filter,
362 bool list_classes,
363 bool list_methods,
David Brazdilc03d7b62016-03-02 12:18:03 +0000364 bool dump_header_only,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800365 const char* export_dex_location,
366 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700367 : dump_raw_mapping_table_(dump_raw_mapping_table),
368 dump_raw_gc_map_(dump_raw_gc_map),
369 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100370 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700371 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700372 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800373 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000374 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800375 list_classes_(list_classes),
376 list_methods_(list_methods),
David Brazdilc03d7b62016-03-02 12:18:03 +0000377 dump_header_only_(dump_header_only),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800378 export_dex_location_(export_dex_location),
379 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800380 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381
382 const bool dump_raw_mapping_table_;
383 const bool dump_raw_gc_map_;
384 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100385 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700386 const bool disassemble_code_;
387 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800388 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000389 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800390 const bool list_classes_;
391 const bool list_methods_;
David Brazdilc03d7b62016-03-02 12:18:03 +0000392 const bool dump_header_only_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800393 const char* const export_dex_location_;
394 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700395 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700396};
397
Elliott Hughese3c845c2012-02-28 17:23:01 -0800398class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700399 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100400 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000401 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800402 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700403 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800404 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800405 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
406 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800407 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000408 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100409 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800410 CHECK(options_.class_loader_ != nullptr);
411 CHECK(options_.class_filter_ != nullptr);
412 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800413 AddAllOffsets();
414 }
415
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700416 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700417 delete disassembler_;
418 }
419
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800420 InstructionSet GetInstructionSet() {
421 return instruction_set_;
422 }
423
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700424 bool Dump(std::ostream& os) {
425 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800426 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700427
428 os << "MAGIC:\n";
429 os << oat_header.GetMagic() << "\n\n";
430
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800431 os << "LOCATION:\n";
432 os << oat_file_.GetLocation() << "\n\n";
433
Brian Carlstromaded5f72011-10-07 17:15:04 -0700434 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800435 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700436
Elliott Hughesa72ec822012-03-05 17:12:22 -0800437 os << "INSTRUCTION SET:\n";
438 os << oat_header.GetInstructionSet() << "\n\n";
439
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700440 {
441 std::unique_ptr<const InstructionSetFeatures> features(
442 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
443 oat_header.GetInstructionSetFeaturesBitmap()));
444 os << "INSTRUCTION SET FEATURES:\n";
445 os << features->GetFeatureString() << "\n\n";
446 }
Dave Allison70202782013-10-22 17:52:19 -0700447
Brian Carlstromaded5f72011-10-07 17:15:04 -0700448 os << "DEX FILE COUNT:\n";
449 os << oat_header.GetDexFileCount() << "\n\n";
450
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800451#define DUMP_OAT_HEADER_OFFSET(label, offset) \
452 os << label " OFFSET:\n"; \
453 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800454 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800455 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
456 } \
457 os << StringPrintf("\n\n");
458
459 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
460 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
461 GetInterpreterToInterpreterBridgeOffset);
462 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
463 GetInterpreterToCompiledCodeBridgeOffset);
464 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
465 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800466 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
467 GetQuickGenericJniTrampolineOffset);
468 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
469 GetQuickImtConflictTrampolineOffset);
470 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
471 GetQuickResolutionTrampolineOffset);
472 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
473 GetQuickToInterpreterBridgeOffset);
474#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700476 os << "IMAGE PATCH DELTA:\n";
477 os << StringPrintf("%d (0x%08x)\n\n",
478 oat_header.GetImagePatchDelta(),
479 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700480
Brian Carlstrom28db0122012-10-18 16:20:41 -0700481 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
482 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
483
484 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800485 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700486
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700487 // Print the key-value store.
488 {
489 os << "KEY VALUE STORE:\n";
490 size_t index = 0;
491 const char* key;
492 const char* value;
493 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
494 os << key << " = " << value << "\n";
495 index++;
496 }
497 os << "\n";
498 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700499
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800500 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700501 os << "BEGIN:\n";
502 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700503
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700504 os << "END:\n";
505 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
506 }
507
508 os << "SIZE:\n";
509 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700510
511 os << std::flush;
512
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800513 // If set, adjust relative address to be searched
514 if (options_.addr2instr_ != 0) {
515 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
516 os << "SEARCH ADDRESS (executable offset + input):\n";
517 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
518 }
519
David Brazdilc03d7b62016-03-02 12:18:03 +0000520 if (!options_.dump_header_only_) {
521 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
522 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
523 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800524
David Brazdilc03d7b62016-03-02 12:18:03 +0000525 // If file export selected skip file analysis
526 if (options_.export_dex_location_) {
527 if (!ExportDexFile(os, *oat_dex_file)) {
528 success = false;
529 }
530 } else {
531 if (!DumpOatDexFile(os, *oat_dex_file)) {
532 success = false;
533 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800534 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700535 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700536 }
David Brazdilc03d7b62016-03-02 12:18:03 +0000537
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700538 os << std::flush;
539 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700540 }
541
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700543 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
544 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 return 0; // Address not in oat file
546 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800547 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
548 reinterpret_cast<uintptr_t>(oat_file_.Begin());
549 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800551 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800552 return end_offset - begin_offset;
553 }
554
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800555 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700556 return oat_file_.GetOatHeader().GetInstructionSet();
557 }
558
Mathieu Chartier90443472015-07-16 20:32:27 -0700559 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800560 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
561 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700562 CHECK(oat_dex_file != nullptr);
563 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700564 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
565 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700566 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
567 << "': " << error_msg;
568 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800569 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700570 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800571 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700572 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700573 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100574 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800575 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100576 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800577 }
578 }
579 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700580 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800581 }
582
Brian Carlstromaded5f72011-10-07 17:15:04 -0700583 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800584 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800585 // We don't know the length of the code for each method, but we need to know where to stop
586 // when disassembling. What we do know is that a region of code will be followed by some other
587 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
588 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800589 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
590 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700591 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700592 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700593 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
594 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700595 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
596 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800597 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800598 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800599 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800600 for (size_t class_def_index = 0;
601 class_def_index < dex_file->NumClassDefs();
602 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800603 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100604 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700605 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700606 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800607 ClassDataItemIterator it(*dex_file, class_data);
608 SkipAllFields(it);
609 uint32_t class_method_index = 0;
610 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100611 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800612 it.Next();
613 }
614 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100615 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800616 it.Next();
617 }
618 }
619 }
620 }
621
622 // If the last thing in the file is code for a method, there won't be an offset for the "next"
623 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
624 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800625 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800626 }
627
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700628 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
629 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
630 }
631
Elliott Hughese3c845c2012-02-28 17:23:01 -0800632 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800633 uint32_t code_offset = oat_method.GetCodeOffset();
634 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
635 code_offset &= ~0x1;
636 }
637 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800638 offsets_.insert(oat_method.GetMappingTableOffset());
639 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800640 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800641 }
642
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700643 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
644 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800645 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700646 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800647 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800648 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700649
650 // Create the verifier early.
651
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700652 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700653 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
654 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700655 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700656 os << std::flush;
657 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700658 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100659
660 VariableIndentationOutputStream vios(&os);
661 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800662 for (size_t class_def_index = 0;
663 class_def_index < dex_file->NumClassDefs();
664 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700665 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
666 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800667
668 // TODO: Support regex
669 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
670 continue;
671 }
672
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700673 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100674 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700675 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
676 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100677 << " (" << oat_class.GetStatus() << ")"
678 << " (" << oat_class.GetType() << ")\n";
679 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800680 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700681 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700682 success = false;
683 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800684 if (stop_analysis) {
685 os << std::flush;
686 return success;
687 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700688 }
689
690 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700691 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700692 }
693
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800694 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
695 std::string error_msg;
696 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
697
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700698 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800699 if (dex_file == nullptr) {
700 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
701 return false;
702 }
703 size_t fsize = oat_dex_file.FileSize();
704
705 // Some quick checks just in case
706 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
707 os << "Invalid dex file\n";
708 return false;
709 }
710
711 // Verify output directory exists
712 if (!OS::DirectoryExists(options_.export_dex_location_)) {
713 // TODO: Extend OS::DirectoryExists if symlink support is required
714 os << options_.export_dex_location_ << " output directory not found or symlink\n";
715 return false;
716 }
717
718 // Beautify path names
719 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
720 return false;
721 }
722
723 std::string dex_orig_name;
724 size_t dex_orig_pos = dex_file_location.rfind('/');
725 if (dex_orig_pos == std::string::npos)
726 dex_orig_name = dex_file_location;
727 else
728 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
729
730 // A more elegant approach to efficiently name user installed apps is welcome
731 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
732 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
733 size_t apk_orig_pos = dex_file_location.rfind('/');
734 if (apk_orig_pos != std::string::npos) {
735 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
736 }
737 }
738
739 std::string out_dex_path(options_.export_dex_location_);
740 if (out_dex_path.back() != '/') {
741 out_dex_path.append("/");
742 }
743 out_dex_path.append(dex_orig_name);
744 out_dex_path.append("_export.dex");
745 if (out_dex_path.length() > PATH_MAX) {
746 return false;
747 }
748
749 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
750 if (file.get() == nullptr) {
751 os << "Failed to open output dex file " << out_dex_path;
752 return false;
753 }
754
755 if (!file->WriteFully(dex_file->Begin(), fsize)) {
756 os << "Failed to write dex file";
757 file->Erase();
758 return false;
759 }
760
761 if (file->FlushCloseOrErase() != 0) {
762 os << "Flush and close failed";
763 return false;
764 }
765
766 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
767 os << std::flush;
768
769 return true;
770 }
771
Elliott Hughese3c845c2012-02-28 17:23:01 -0800772 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700773 while (it.HasNextStaticField()) {
774 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700775 }
Ian Rogers0571d352011-11-03 19:51:38 -0700776 while (it.HasNextInstanceField()) {
777 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700778 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800779 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700780
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100781 bool DumpOatClass(VariableIndentationOutputStream* vios,
782 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800783 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700784 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800785 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700786 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700787 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100788 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700789 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800790 }
791 ClassDataItemIterator it(dex_file, class_data);
792 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700793 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700794 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100795 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700796 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800797 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700798 success = false;
799 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800800 if (addr_found) {
801 *stop_analysis = true;
802 return success;
803 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700804 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700805 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700806 }
Ian Rogers0571d352011-11-03 19:51:38 -0700807 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100808 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700809 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800810 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700811 success = false;
812 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800813 if (addr_found) {
814 *stop_analysis = true;
815 return success;
816 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700817 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700818 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700819 }
Ian Rogers0571d352011-11-03 19:51:38 -0700820 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100821 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700822 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700823 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800824
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700825 static constexpr uint32_t kPrologueBytes = 16;
826
827 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
828 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
829
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100830 bool DumpOatMethod(VariableIndentationOutputStream* vios,
831 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700832 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700833 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800834 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800835 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700836 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800837
838 // TODO: Support regex
839 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
840 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000841 return success;
842 }
843
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800844 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100845 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
846 class_method_index, pretty_method.c_str(),
847 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800848 if (options_.list_methods_) return success;
849
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800850 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
851 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
852 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
853 uint32_t code_offset = oat_method.GetCodeOffset();
854 uint32_t code_size = oat_method.GetQuickCodeSize();
855 if (resolved_addr2instr_ != 0) {
856 if (resolved_addr2instr_ > code_offset + code_size) {
857 return success;
858 } else {
859 *addr_found = true; // stop analyzing file at next iteration
860 }
861 }
862
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100863 // Everything below is indented at least once.
864 ScopedIndentation indent1(vios);
865
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800866 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100867 vios->Stream() << "DEX CODE:\n";
868 ScopedIndentation indent2(vios);
869 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700870 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700871
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700872 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700873 std::unique_ptr<verifier::MethodVerifier> verifier;
874 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700875 // We need to have the handle scope stay live until after the verifier since the verifier has
876 // a handle to the dex cache from hs.
877 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100878 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
879 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700880 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700882 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700883 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800884 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100885 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800886 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100887 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100888 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100889 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700890 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100891 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700892 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
893 oat_method_offsets_offset, oat_file_.Size());
894 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100895 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700896 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800897 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700898
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100899 ScopedIndentation indent2(vios);
900 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700901 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
902 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100903 vios->Stream() << StringPrintf("WARNING: "
904 "code offset 0x%08x is past end of file 0x%08zx.\n",
905 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700906 success = false;
907 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100908 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100910 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800911 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100912 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700913 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800914 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100915 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100917 vios->Stream() << StringPrintf("WARNING: "
918 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
919 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800921 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100922 ScopedIndentation indent3(vios);
923 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800924 }
925 }
926 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100927 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700928 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
929 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700930
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800931 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100932 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700933 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100934 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700935 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100936 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700937 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
938 method_header_offset, oat_file_.Size());
939 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100940 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700941 return false;
942 }
943
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100944 ScopedIndentation indent2(vios);
945 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800946 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100947 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700948 }
949 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100950 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700951 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100952 vios->Stream() << StringPrintf("WARNING: "
953 "mapping table offset 0x%08x is past end of file 0x%08zx. "
954 "mapping table offset was loaded from offset 0x%08x.\n",
955 mapping_table_offset, oat_file_.Size(),
956 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700957 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800958 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100959 ScopedIndentation indent3(vios);
960 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961 }
962
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100963 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800964 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100965 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700966 }
967 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100968 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700969 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100970 vios->Stream() << StringPrintf("WARNING: "
971 "vmap table offset 0x%08x is past end of file 0x%08zx. "
972 "vmap table offset was loaded from offset 0x%08x.\n",
973 vmap_table_offset, oat_file_.Size(),
974 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700975 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800976 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100977 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700978 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800979 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700980 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100981 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700982
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100983 ScopedIndentation indent2(vios);
984 vios->Stream()
985 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
986 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
987 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
988 vios->Stream() << "\n";
989 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
990 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
991 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700992 }
993 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100994 // Based on spill masks from QuickMethodFrameInfo so placed
995 // after it is dumped, but useful for understanding quick
996 // code, so dumped here.
997 ScopedIndentation indent2(vios);
998 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700999 }
1000 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001001 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001002 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
1003 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001004 ScopedIndentation indent2(vios);
1005 vios->Stream() << StringPrintf("WARNING: "
1006 "code size offset 0x%08x is past end of file 0x%08zx.",
1007 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001008 success = false;
1009 } else {
1010 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001011 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
1012 uint64_t aligned_code_end = aligned_code_begin + code_size;
1013
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001014 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001015 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001016 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001017 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
1018 code_offset,
1019 code_size_offset,
1020 code_size,
1021 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001022
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001023 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001024 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001025 vios->Stream() << StringPrintf("WARNING: "
1026 "start of code at 0x%08x is past end of file 0x%08zx.",
1027 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001028 success = false;
1029 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001030 vios->Stream() << StringPrintf(
1031 "WARNING: "
1032 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
1033 "code size is 0x%08x loaded from offset 0x%08x.\n",
1034 aligned_code_end, oat_file_.Size(),
1035 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001036 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001037 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001038 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001039 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001040 }
1041 }
1042 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001043 vios->Stream() << StringPrintf(
1044 "WARNING: "
1045 "code size %d is bigger than max expected threshold of %d. "
1046 "code size is 0x%08x loaded from offset 0x%08x.\n",
1047 code_size, kMaxCodeSize,
1048 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001049 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001050 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001051 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001052 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001053 }
1054 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001055 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001056 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001057 }
1058 }
1059 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001060 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001061 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001062 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001063
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001064 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1065 if (spill_mask == 0) {
1066 return;
1067 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001068 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001069 for (size_t i = 0; i < 32; i++) {
1070 if ((spill_mask & (1 << i)) != 0) {
1071 if (is_float) {
1072 os << "fr" << i;
1073 } else {
1074 os << "r" << i;
1075 }
1076 spill_mask ^= 1 << i; // clear bit
1077 if (spill_mask != 0) {
1078 os << ", ";
1079 } else {
1080 break;
1081 }
1082 }
1083 }
1084 os << ")";
1085 }
1086
Roland Levillain442b46a2015-02-18 16:54:21 +00001087 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001088 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001089 const OatFile::OatMethod& oat_method,
1090 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001091 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1092 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001093 const void* raw_code_info = oat_method.GetVmapTable();
1094 if (raw_code_info != nullptr) {
1095 CodeInfo code_info(raw_code_info);
1096 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001097 ScopedIndentation indent1(vios);
1098 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001099 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001100 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1101 // We don't encode the size in the table, so just emit that we have quickened
1102 // information.
1103 ScopedIndentation indent(vios);
1104 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001105 } else {
1106 // Otherwise, display the vmap table.
1107 const uint8_t* raw_table = oat_method.GetVmapTable();
1108 if (raw_table != nullptr) {
1109 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001110 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001111 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001112 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001113 }
1114
1115 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001116 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001117 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001118 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001119 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001120 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001121 oat_method.GetCodeOffset(),
1122 code_item.registers_size_,
1123 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001124 }
1125
Roland Levillain442b46a2015-02-18 16:54:21 +00001126 // Display a vmap table.
1127 void DumpVmapTable(std::ostream& os,
1128 const OatFile::OatMethod& oat_method,
1129 const VmapTable& vmap_table) {
1130 bool first = true;
1131 bool processing_fp = false;
1132 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1133 for (size_t i = 0; i < vmap_table.Size(); i++) {
1134 uint16_t dex_reg = vmap_table[i];
1135 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1136 processing_fp ? kFloatVReg : kIntVReg);
1137 os << (first ? "v" : ", v") << dex_reg;
1138 if (!processing_fp) {
1139 os << "/r" << cpu_reg;
1140 } else {
1141 os << "/fr" << cpu_reg;
1142 }
1143 first = false;
1144 if (!processing_fp && dex_reg == 0xFFFF) {
1145 processing_fp = true;
1146 spill_mask = oat_method.GetFpSpillMask();
1147 }
1148 }
1149 os << "\n";
1150 }
1151
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001152 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1153 const DexFile::CodeItem* code_item) {
1154 if (code_item != nullptr) {
1155 size_t num_locals_ins = code_item->registers_size_;
1156 size_t num_ins = code_item->ins_size_;
1157 size_t num_locals = num_locals_ins - num_ins;
1158 size_t num_outs = code_item->outs_size_;
1159
1160 os << "vr_stack_locations:";
1161 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1162 // For readability, delimit the different kinds of VRs.
1163 if (reg == num_locals_ins) {
1164 os << "\n\tmethod*:";
1165 } else if (reg == num_locals && num_ins > 0) {
1166 os << "\n\tins:";
1167 } else if (reg == 0 && num_locals > 0) {
1168 os << "\n\tlocals:";
1169 }
1170
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001171 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1172 code_item,
1173 oat_method.GetCoreSpillMask(),
1174 oat_method.GetFpSpillMask(),
1175 oat_method.GetFrameSizeInBytes(),
1176 reg,
1177 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001178 os << " v" << reg << "[sp + #" << offset << "]";
1179 }
1180
1181 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1182 if (out_reg == 0) {
1183 os << "\n\touts:";
1184 }
1185
1186 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1187 os << " v" << out_reg << "[sp + #" << offset << "]";
1188 }
1189
1190 os << "\n";
1191 }
1192 }
1193
Ian Rogersb23a7722012-10-09 16:54:26 -07001194 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001195 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001196 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001197 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001198 const VmapTable vmap_table(raw_table);
1199 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001200 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001201 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1202 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1203 : oat_method.GetCoreSpillMask();
1204 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001205 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001206 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1207 code_item,
1208 oat_method.GetCoreSpillMask(),
1209 oat_method.GetFpSpillMask(),
1210 oat_method.GetFrameSizeInBytes(),
1211 reg,
1212 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001213 os << "[sp + #" << offset << "]";
1214 }
1215 }
1216 }
1217
Ian Rogersef7d42f2014-01-06 12:55:46 -08001218 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1219 const DexFile::CodeItem* code_item,
1220 size_t num_regs, const uint8_t* reg_bitmap) {
1221 bool first = true;
1222 for (size_t reg = 0; reg < num_regs; reg++) {
1223 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1224 if (first) {
1225 os << " v" << reg << " (";
1226 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1227 os << ")";
1228 first = false;
1229 } else {
1230 os << ", v" << reg << " (";
1231 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1232 os << ")";
1233 }
1234 }
1235 }
1236 if (first) {
1237 os << "No registers in GC map\n";
1238 } else {
1239 os << "\n";
1240 }
1241 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001242 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1243 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001244 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001245 if (gc_map_raw == nullptr) {
1246 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001247 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001248 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001249 NativePcOffsetToReferenceMap map(gc_map_raw);
1250 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1251 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1252 map.GetNativePcOffset(entry);
1253 os << StringPrintf("%p", native_pc);
1254 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001255 }
1256 }
1257
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001258 void DumpMappingTable(VariableIndentationOutputStream* vios,
1259 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001260 const void* quick_code = oat_method.GetQuickCode();
1261 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001262 return;
1263 }
Ian Rogers1809a722013-08-09 22:05:32 -07001264 MappingTable table(oat_method.GetMappingTable());
1265 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001266 if (table.PcToDexSize() != 0) {
1267 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001268 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001269 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001270 ScopedIndentation indent1(vios);
1271 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001272 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001273 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001274 }
1275 if (table.DexToPcSize() != 0) {
1276 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001277 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001278 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001279 ScopedIndentation indent1(vios);
1280 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001281 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001282 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001283 }
1284 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001285 }
1286
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001287 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001288 const OatFile::OatMethod& oat_method,
1289 const DexFile::CodeItem* code_item,
1290 size_t offset,
1291 bool suspend_point_mapping) {
1292 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1293 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001294 ScopedIndentation indent1(vios);
1295 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001296 }
1297 // The return value is not used in the case of a method compiled
1298 // with the optimizing compiler.
1299 return DexFile::kDexNoIndex;
1300 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001301 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001302 }
1303 }
1304
Ian Rogers1809a722013-08-09 22:05:32 -07001305 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1306 size_t offset, bool suspend_point_mapping) {
1307 MappingTable table(oat_method.GetMappingTable());
1308 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1309 typedef MappingTable::PcToDexIterator It;
1310 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1311 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001312 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001313 return cur.DexPc();
1314 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001315 }
Ian Rogers1809a722013-08-09 22:05:32 -07001316 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1317 typedef MappingTable::DexToPcIterator It;
1318 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1319 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001320 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001321 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001322 }
1323 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001324 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001325 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001326 }
1327
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1329 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001330 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001331 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001332 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001333 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001334 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001335 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001336 bool first = true;
1337 for (size_t reg = 0; reg < num_regs; reg++) {
1338 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1339 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001340 os << "GC map objects: v" << reg << " (";
1341 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001342 os << ")";
1343 first = false;
1344 } else {
1345 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001346 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001347 os << ")";
1348 }
1349 }
1350 }
1351 if (!first) {
1352 os << "\n";
1353 }
1354 }
1355 }
1356 }
1357
Mathieu Chartier590fee92013-09-13 13:46:47 -07001358 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1359 const OatFile::OatMethod& oat_method,
1360 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1361 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001362 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001363 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001364 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1365 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1366 if (kind != kUndefined) {
1367 if (first) {
1368 os << "VRegs: v";
1369 first = false;
1370 } else {
1371 os << ", v";
1372 }
1373 os << reg << " (";
1374 switch (kind) {
1375 case kImpreciseConstant:
1376 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1377 DescribeVReg(os, oat_method, code_item, reg, kind);
1378 break;
1379 case kConstant:
1380 os << "Constant: " << kinds.at((reg * 2) + 1);
1381 break;
1382 default:
1383 DescribeVReg(os, oat_method, code_item, reg, kind);
1384 break;
1385 }
1386 os << ")";
1387 }
1388 }
1389 if (!first) {
1390 os << "\n";
1391 }
1392 }
1393
1394
Ian Rogersb23a7722012-10-09 16:54:26 -07001395 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001396 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001397 size_t i = 0;
1398 while (i < code_item->insns_size_in_code_units_) {
1399 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001400 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1401 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001402 i += instruction->SizeInCodeUnits();
1403 }
1404 }
1405 }
1406
Roland Levillainf2650d12015-05-28 14:53:28 +01001407 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1408 // the optimizing compiler?
1409 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1410 const DexFile::CodeItem* code_item) {
1411 // If the native GC map is null and the Dex `code_item` is not
1412 // null, then this method has been compiled with the optimizing
1413 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001414 return oat_method.GetQuickCode() != nullptr &&
1415 oat_method.GetGcMap() == nullptr &&
1416 code_item != nullptr;
1417 }
1418
1419 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1420 // the dextodex compiler?
1421 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1422 const DexFile::CodeItem* code_item) {
1423 // If the quick code is null, the Dex `code_item` is not
1424 // null, and the vmap table is not null, then this method has been compiled
1425 // with the dextodex compiler.
1426 return oat_method.GetQuickCode() == nullptr &&
1427 oat_method.GetVmapTable() != nullptr &&
1428 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001429 }
1430
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001431 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001432 const OatFile::OatMethod& oat_method,
1433 const DexFile::CodeItem* code_item,
1434 size_t offset) {
1435 // This method is only relevant for oat methods compiled with the
1436 // optimizing compiler.
1437 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1438
1439 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1440 const void* raw_code_info = oat_method.GetVmapTable();
1441 if (raw_code_info != nullptr) {
1442 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001443 StackMapEncoding encoding = code_info.ExtractEncoding();
1444 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001445 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001446 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1447 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001448 }
1449 }
1450 }
1451
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001452 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001453 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001454 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001455 const DexFile* dex_file,
1456 const DexFile::ClassDef& class_def,
1457 const DexFile::CodeItem* code_item,
1458 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001459 if ((method_access_flags & kAccNative) == 0) {
1460 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001461 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001462 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001463 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1464 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001465 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001466 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001467 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1468 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001469 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001470
1471 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001472 }
1473
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001474 void DumpCode(VariableIndentationOutputStream* vios,
1475 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001476 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1477 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001478 const void* quick_code = oat_method.GetQuickCode();
1479
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001480 if (code_size == 0) {
1481 code_size = oat_method.GetQuickCodeSize();
1482 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001483 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001484 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001485 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001486 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001487 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1488 size_t offset = 0;
1489 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001490 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001491 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001492 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001493 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001494 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001495 uint32_t dex_pc =
1496 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001497 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001498 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001499 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001500 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001501 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001502 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001503 }
1504 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001505 }
1506 }
1507
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001508 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001509 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001510 const OatDumperOptions& options_;
1511 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001512 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001513 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001514 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001515};
1516
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001517class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001518 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001519 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1520 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001521 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001522 vios_(os),
1523 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001524 image_space_(image_space),
1525 image_header_(image_header),
1526 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001527
Mathieu Chartier90443472015-07-16 20:32:27 -07001528 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001529 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001530 std::ostream& indent_os = vios_.Stream();
1531
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001532 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001533
Jeff Haodcdc85b2015-12-04 14:06:18 -08001534 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1535
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001536 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001537
Mathieu Chartiere401d142015-04-22 13:56:20 -07001538 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1539
1540 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1541 auto section = static_cast<ImageHeader::ImageSections>(i);
1542 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1543 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001544
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001546
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001547 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001548
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001549 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1550
1551 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1552
1553 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001554
Alex Lighta59dd802014-07-02 16:28:08 -07001555 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1556
Igor Murashkin46774762014-10-22 11:37:02 -07001557 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1558
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001559 {
1560 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001561 static_assert(arraysize(image_roots_descriptions_) ==
1562 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001563 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1564 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1565 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001566 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001567 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001569 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001570 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001571 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001572 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1573 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001574 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001575 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1576 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001577 run++;
1578 } else {
1579 break;
1580 }
1581 }
1582 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001583 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001584 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001585 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001586 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001587 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001588 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001589 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001590 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001591 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001593 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001594 }
1595 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001596 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001597
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001598 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001599 os << "METHOD ROOTS\n";
1600 static_assert(arraysize(image_methods_descriptions_) ==
1601 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1602 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1603 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1604 const char* description = image_methods_descriptions_[i];
1605 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001606 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001607 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001608 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001609 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001610
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001611 Runtime* const runtime = Runtime::Current();
1612 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001613 std::string image_filename = image_space_.GetImageFilename();
1614 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001617 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001618 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1619 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001620 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001621 oat_file = OatFile::Open(oat_location, oat_location,
1622 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001623 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001624 if (oat_file == nullptr) {
1625 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001626 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001627 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001628 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001630
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001631 stats_.oat_file_bytes = oat_file->Size();
1632
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001633 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001634
Mathieu Chartier02e25112013-08-14 16:14:24 -07001635 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001636 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001637 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1638 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001639 }
1640
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001642
Jeff Haodcdc85b2015-12-04 14:06:18 -08001643 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001644 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001645 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001646 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001647 {
1648 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1649 heap->FlushAllocStack();
1650 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001651 // Since FlushAllocStack() above resets the (active) allocation
1652 // stack. Need to revoke the thread-local allocation stacks that
1653 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001654 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001655 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001656 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001657 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001658 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001659 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001660 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001661 {
1662 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001663 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001664 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001665 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001666 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001667 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001668 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001669 }
1670 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001671 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001672 // Dump the normal objects before ArtMethods.
1673 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1674 indent_os << "\n";
1675 // TODO: Dump fields.
1676 // Dump methods after.
1677 const auto& methods_section = image_header_.GetMethodsSection();
1678 const size_t pointer_size =
1679 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1680 DumpArtMethodVisitor visitor(this);
1681 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001682 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001683 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001684 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001685 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001687 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001688 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001689 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001690 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001691 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001692 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001693 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001694 size_t header_bytes = sizeof(ImageHeader);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001695 const auto& object_section = image_header_.GetImageSection(ImageHeader::kSectionObjects);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001696 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1697 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001698 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1699 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001700 const auto& intern_section = image_header_.GetImageSection(
1701 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001702 const auto& class_table_section = image_header_.GetImageSection(
1703 ImageHeader::kSectionClassTable);
Andreas Gampeace0dc12016-01-20 13:33:13 -08001704 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1705
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001706 stats_.header_bytes = header_bytes;
Andreas Gampeace0dc12016-01-20 13:33:13 -08001707
1708 // Objects are kObjectAlignment-aligned.
1709 // CHECK_EQ(RoundUp(header_bytes, kObjectAlignment), object_section.Offset());
1710 if (object_section.Offset() > header_bytes) {
1711 stats_.alignment_bytes += object_section.Offset() - header_bytes;
1712 }
1713
1714 // Field section is 4-byte aligned.
1715 constexpr size_t kFieldSectionAlignment = 4U;
1716 uint32_t end_objects = object_section.Offset() + object_section.Size();
1717 CHECK_EQ(RoundUp(end_objects, kFieldSectionAlignment), field_section.Offset());
1718 stats_.alignment_bytes += field_section.Offset() - end_objects;
1719
1720 // Method section is 4/8 byte aligned depending on target. Just check for 4-byte alignment.
1721 uint32_t end_fields = field_section.Offset() + field_section.Size();
1722 CHECK_ALIGNED(method_section.Offset(), 4);
1723 stats_.alignment_bytes += method_section.Offset() - end_fields;
1724
1725 // Dex cache arrays section is aligned depending on the target. Just check for 4-byte alignment.
1726 uint32_t end_methods = method_section.Offset() + method_section.Size();
1727 CHECK_ALIGNED(dex_cache_arrays_section.Offset(), 4);
1728 stats_.alignment_bytes += dex_cache_arrays_section.Offset() - end_methods;
1729
1730 // Intern table is 8-byte aligned.
1731 uint32_t end_caches = dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size();
1732 CHECK_EQ(RoundUp(end_caches, 8U), intern_section.Offset());
1733 stats_.alignment_bytes += intern_section.Offset() - end_caches;
1734
1735 // Add space between intern table and class table.
1736 uint32_t end_intern = intern_section.Offset() + intern_section.Size();
1737 stats_.alignment_bytes += class_table_section.Offset() - end_intern;
1738
1739 // Add space between class table and bitmap. Expect the bitmap to be page-aligned.
1740 uint32_t end_ctable = class_table_section.Offset() + class_table_section.Size();
1741 CHECK_ALIGNED(bitmap_section.Offset(), kPageSize);
1742 stats_.alignment_bytes += bitmap_section.Offset() - end_ctable;
1743
Mathieu Chartiere401d142015-04-22 13:56:20 -07001744 stats_.bitmap_bytes += bitmap_section.Size();
1745 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001746 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001747 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001748 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001749 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001750 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001751 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001752
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001753 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001754
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001755 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001756 }
1757
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001758 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001759 class DumpArtMethodVisitor : public ArtMethodVisitor {
1760 public:
1761 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1762
1763 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1764 std::ostream& indent_os = image_dumper_->vios_.Stream();
1765 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1766 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1767 indent_os << "\n";
1768 }
1769
1770 private:
1771 ImageDumper* const image_dumper_;
1772 };
1773
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001774 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001775 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001776 CHECK(type != nullptr);
1777 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001778 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001779 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001780 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001782 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001783 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001784 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001785 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001786 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001787 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001788 }
1789 }
1790
Mathieu Chartierc7853442015-03-27 14:35:38 -07001791 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001792 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001793 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001794 switch (field->GetTypeAsPrimitiveType()) {
1795 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001796 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001797 break;
1798 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001799 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001800 break;
1801 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001802 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001803 break;
1804 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001805 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001806 break;
1807 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001808 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001809 break;
1810 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001811 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001812 break;
1813 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001814 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1815 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001816 break;
1817 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001818 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001819 break;
1820 case Primitive::kPrimNot: {
1821 // Get the value, don't compute the type unless it is non-null as we don't want
1822 // to cause class loading.
1823 mirror::Object* value = field->GetObj(obj);
1824 if (value == nullptr) {
1825 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001826 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001827 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001828 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001829 if (field_type != nullptr) {
1830 PrettyObjectValue(os, field_type, value);
1831 } else {
1832 os << StringPrintf("%p %s\n", value,
1833 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1834 }
Ian Rogers50239c72013-06-17 14:53:22 -07001835 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001836 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001837 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001838 default:
1839 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1840 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001841 }
1842 }
1843
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001844 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001845 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001846 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001847 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001848 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001849 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001850 for (ArtField& field : klass->GetIFields()) {
1851 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001852 }
1853 }
1854
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001855 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001856 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001857 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001858
Mathieu Chartiere401d142015-04-22 13:56:20 -07001859 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001860 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001861 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1862 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001863 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001864 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001865 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001866 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001867 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001868 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001869 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001870 }
1871
Mathieu Chartiere401d142015-04-22 13:56:20 -07001872 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001873 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001874 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1875 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001876 return 0;
1877 }
1878 return oat_code_begin[-1];
1879 }
1880
Mathieu Chartiere401d142015-04-22 13:56:20 -07001881 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001882 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001883 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001884 if (oat_code_begin == nullptr) {
1885 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001886 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001887 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001888 }
1889
Mathieu Chartier90443472015-07-16 20:32:27 -07001890 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001891 DCHECK(obj != nullptr);
1892 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001893 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001894 if (!state->InDumpSpace(obj)) {
1895 return;
1896 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001897
1898 size_t object_bytes = obj->SizeOf();
1899 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1900 state->stats_.object_bytes += object_bytes;
1901 state->stats_.alignment_bytes += alignment_bytes;
1902
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001903 std::ostream& os = state->vios_.Stream();
1904
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001905 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001906 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001907 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1908 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001909 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001910 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001911 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1912 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001913 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001914 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001915 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001916 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001917 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001918 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001919 ScopedIndentation indent1(&state->vios_);
1920 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001921 const auto image_pointer_size =
1922 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001923 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001924 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1925 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001926 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001927 size_t run = 0;
1928 for (int32_t j = i + 1; j < length; j++) {
1929 if (value == obj_array->Get(j)) {
1930 run++;
1931 } else {
1932 break;
1933 }
1934 }
1935 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001936 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001937 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001938 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001939 i = i + run;
1940 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001941 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001942 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001943 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001944 }
1945 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001946 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001947 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001948 os << "STATICS:\n";
1949 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001950 for (ArtField& field : klass->GetSFields()) {
1951 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001952 }
1953 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001954 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001955 auto it = state->dex_caches_.find(obj);
1956 if (it != state->dex_caches_.end()) {
1957 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001958 const auto& field_section = state->image_header_.GetImageSection(
1959 ImageHeader::kSectionArtFields);
1960 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001961 size_t num_methods = dex_cache->NumResolvedMethods();
1962 if (num_methods != 0u) {
1963 os << "Methods (size=" << num_methods << "):";
1964 ScopedIndentation indent2(&state->vios_);
1965 auto* resolved_methods = dex_cache->GetResolvedMethods();
1966 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1967 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1968 size_t run = 0;
1969 for (size_t j = i + 1;
1970 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1971 j,
1972 image_pointer_size);
1973 ++j, ++run) {}
1974 if (run == 0) {
1975 os << StringPrintf("%zd: ", i);
1976 } else {
1977 os << StringPrintf("%zd to %zd: ", i, i + run);
1978 i = i + run;
1979 }
1980 std::string msg;
1981 if (elem == nullptr) {
1982 msg = "null";
1983 } else if (method_section.Contains(
1984 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1985 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1986 } else {
1987 msg = "<not in method section>";
1988 }
1989 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001990 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001991 }
1992 size_t num_fields = dex_cache->NumResolvedFields();
1993 if (num_fields != 0u) {
1994 os << "Fields (size=" << num_fields << "):";
1995 ScopedIndentation indent2(&state->vios_);
1996 auto* resolved_fields = dex_cache->GetResolvedFields();
1997 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1998 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1999 size_t run = 0;
2000 for (size_t j = i + 1;
2001 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
2002 j,
2003 image_pointer_size);
2004 ++j, ++run) {}
2005 if (run == 0) {
2006 os << StringPrintf("%zd: ", i);
2007 } else {
2008 os << StringPrintf("%zd to %zd: ", i, i + run);
2009 i = i + run;
2010 }
2011 std::string msg;
2012 if (elem == nullptr) {
2013 msg = "null";
2014 } else if (field_section.Contains(
2015 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
2016 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
2017 } else {
2018 msg = "<not in field section>";
2019 }
2020 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07002021 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002022 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002023 }
2024 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002025 std::string temp;
2026 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002027 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002028
Mathieu Chartiere401d142015-04-22 13:56:20 -07002029 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002030 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031 DCHECK(method != nullptr);
2032 const auto image_pointer_size =
2033 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002034 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2035 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002036 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
2037 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002038 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002039 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
2040 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
2041 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
2042 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002043 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002044 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002045 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002046 if (first_occurrence) {
2047 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
2048 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01002049 if (quick_oat_code_begin !=
2050 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
2051 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002052 }
2053 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
2054 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
2055 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002056 } else {
2057 const DexFile::CodeItem* code_item = method->GetCodeItem();
2058 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
2059 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
2060
2061 bool first_occurrence;
2062 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002063 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002064 if (first_occurrence) {
2065 state->stats_.gc_map_bytes += gc_map_bytes;
2066 }
2067
2068 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002069 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002070 if (first_occurrence) {
2071 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2072 }
2073
Roland Levillain6d7f1792015-07-02 10:59:15 +01002074 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002075 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002076 // Method compiled with the optimizing compiler have no vmap table.
2077 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002078 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002079 if (first_occurrence) {
2080 state->stats_.vmap_table_bytes += vmap_table_bytes;
2081 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002082 }
2083
Mathieu Chartiere401d142015-04-22 13:56:20 -07002084 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2085 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2086 if (first_occurrence) {
2087 state->stats_.managed_code_bytes += quick_oat_code_size;
2088 if (method->IsConstructor()) {
2089 if (method->IsStatic()) {
2090 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2091 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2092 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2093 }
2094 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2095 state->stats_.large_method_code_bytes += quick_oat_code_size;
2096 }
2097 }
2098 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2099
Igor Murashkin7617abd2015-07-10 18:27:47 -07002100 uint32_t method_access_flags = method->GetAccessFlags();
2101
Mathieu Chartiere401d142015-04-22 13:56:20 -07002102 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002103 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2104 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2105 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002106
2107 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002108 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002109
2110 double expansion =
2111 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2112 state->stats_.ComputeOutliers(total_size, expansion, method);
2113 }
2114 }
2115
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002116 std::set<const void*> already_seen_;
2117 // Compute the size of the given data within the oat file and whether this is the first time
2118 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002119 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002120 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002121 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002122 already_seen_.insert(oat_data);
2123 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002124 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002125 }
2126 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002127 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002128
2129 public:
2130 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002131 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002132 size_t file_bytes;
2133
2134 size_t header_bytes;
2135 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002136 size_t art_field_bytes;
2137 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002138 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002139 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002140 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002141 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002142 size_t alignment_bytes;
2143
2144 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002145 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002146 size_t managed_to_native_code_bytes;
2147 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002148 size_t class_initializer_code_bytes;
2149 size_t large_initializer_code_bytes;
2150 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002151
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002152 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002153 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002154 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002155
2156 size_t dex_instruction_bytes;
2157
Mathieu Chartiere401d142015-04-22 13:56:20 -07002158 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002159 std::vector<size_t> method_outlier_size;
2160 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002161 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002162
Roland Levillain3887c462015-08-12 18:15:42 +01002163 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002164 : oat_file_bytes(0),
2165 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002166 header_bytes(0),
2167 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002168 art_field_bytes(0),
2169 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002170 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002171 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002172 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002173 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002174 alignment_bytes(0),
2175 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002176 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002177 managed_to_native_code_bytes(0),
2178 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002179 class_initializer_code_bytes(0),
2180 large_initializer_code_bytes(0),
2181 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002182 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002183 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002184 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002185 dex_instruction_bytes(0) {}
2186
Elliott Hughesa0e18062012-04-13 15:59:59 -07002187 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002188 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002189 size_t bytes;
2190 size_t count;
2191 };
2192 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2193 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002194
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002195 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002196 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2197 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002198 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002199 it->second.count += 1;
2200 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002201 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002202 }
2203 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002204
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002205 double PercentOfOatBytes(size_t size) {
2206 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2207 }
2208
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002209 double PercentOfFileBytes(size_t size) {
2210 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2211 }
2212
2213 double PercentOfObjectBytes(size_t size) {
2214 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2215 }
2216
Mathieu Chartiere401d142015-04-22 13:56:20 -07002217 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002218 method_outlier_size.push_back(total_size);
2219 method_outlier_expansion.push_back(expansion);
2220 method_outlier.push_back(method);
2221 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002222
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002223 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002224 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002225 size_t sum_of_sizes = 0;
2226 size_t sum_of_sizes_squared = 0;
2227 size_t sum_of_expansion = 0;
2228 size_t sum_of_expansion_squared = 0;
2229 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002230 if (n == 0) {
2231 return;
2232 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002233 for (size_t i = 0; i < n; i++) {
2234 size_t cur_size = method_outlier_size[i];
2235 sum_of_sizes += cur_size;
2236 sum_of_sizes_squared += cur_size * cur_size;
2237 double cur_expansion = method_outlier_expansion[i];
2238 sum_of_expansion += cur_expansion;
2239 sum_of_expansion_squared += cur_expansion * cur_expansion;
2240 }
2241 size_t size_mean = sum_of_sizes / n;
2242 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2243 double expansion_mean = sum_of_expansion / n;
2244 double expansion_variance =
2245 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2246
2247 // Dump methods whose size is a certain number of standard deviations from the mean
2248 size_t dumped_values = 0;
2249 size_t skipped_values = 0;
2250 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2251 size_t cur_size_variance = i * i * size_variance;
2252 bool first = true;
2253 for (size_t j = 0; j < n; j++) {
2254 size_t cur_size = method_outlier_size[j];
2255 if (cur_size > size_mean) {
2256 size_t cur_var = cur_size - size_mean;
2257 cur_var = cur_var * cur_var;
2258 if (cur_var > cur_size_variance) {
2259 if (dumped_values > 20) {
2260 if (i == 1) {
2261 skipped_values++;
2262 } else {
2263 i = 2; // jump to counting for 1 standard deviation
2264 break;
2265 }
2266 } else {
2267 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002268 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002269 first = false;
2270 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002271 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002272 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002273 method_outlier_size[j] = 0; // don't consider this method again
2274 dumped_values++;
2275 }
2276 }
2277 }
2278 }
2279 }
2280 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002282 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002283 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002284 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002285
2286 // Dump methods whose expansion is a certain number of standard deviations from the mean
2287 dumped_values = 0;
2288 skipped_values = 0;
2289 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2290 double cur_expansion_variance = i * i * expansion_variance;
2291 bool first = true;
2292 for (size_t j = 0; j < n; j++) {
2293 double cur_expansion = method_outlier_expansion[j];
2294 if (cur_expansion > expansion_mean) {
2295 size_t cur_var = cur_expansion - expansion_mean;
2296 cur_var = cur_var * cur_var;
2297 if (cur_var > cur_expansion_variance) {
2298 if (dumped_values > 20) {
2299 if (i == 1) {
2300 skipped_values++;
2301 } else {
2302 i = 2; // jump to counting for 1 standard deviation
2303 break;
2304 }
2305 } else {
2306 if (first) {
2307 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002308 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002309 first = false;
2310 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002312 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002313 method_outlier_expansion[j] = 0.0; // don't consider this method again
2314 dumped_values++;
2315 }
2316 }
2317 }
2318 }
2319 }
2320 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002322 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002323 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002324 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002325 }
2326
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002327 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002328 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 {
2330 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2331 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002332 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2333 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2334 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2335 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2336 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2337 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002338 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002339 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2340 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 header_bytes, PercentOfFileBytes(header_bytes),
2342 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002343 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2344 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002345 dex_cache_arrays_bytes,
2346 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002347 interned_strings_bytes,
2348 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002349 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002350 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2352 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002353 CHECK_EQ(file_bytes,
2354 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2355 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2356 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002357 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002358
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002360 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002361 for (const auto& sizes_and_count : sizes_and_counts) {
2362 const std::string& descriptor(sizes_and_count.first);
2363 double average = static_cast<double>(sizes_and_count.second.bytes) /
2364 static_cast<double>(sizes_and_count.second.count);
2365 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002366 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002367 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002368 descriptor.c_str(), sizes_and_count.second.bytes,
2369 sizes_and_count.second.count, average, percent);
2370 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002371 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002372 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002373 CHECK_EQ(object_bytes, object_bytes_total);
2374
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002375 os << StringPrintf("oat_file_bytes = %8zd\n"
2376 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2377 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2378 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2379 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2380 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2381 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002382 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002383 managed_code_bytes,
2384 PercentOfOatBytes(managed_code_bytes),
2385 managed_to_native_code_bytes,
2386 PercentOfOatBytes(managed_to_native_code_bytes),
2387 native_to_managed_code_bytes,
2388 PercentOfOatBytes(native_to_managed_code_bytes),
2389 class_initializer_code_bytes,
2390 PercentOfOatBytes(class_initializer_code_bytes),
2391 large_initializer_code_bytes,
2392 PercentOfOatBytes(large_initializer_code_bytes),
2393 large_method_code_bytes,
2394 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002395 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002396 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002397 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002398 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2399 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002400 }
2401
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002402 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2403 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2404 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002405 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2406 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2407 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002408 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002409
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002410 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2411 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002412 static_cast<double>(managed_code_bytes) /
2413 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002414 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002415 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002416 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002417
2418 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002419 }
2420 } stats_;
2421
2422 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002423 enum {
2424 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2425 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2426 kLargeConstructorDexBytes = 4000,
2427 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2428 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2429 kLargeMethodDexBytes = 16000
2430 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002431
2432 // For performance, use the *os_ directly for anything that doesn't need indentation
2433 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002434 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002435 VariableIndentationOutputStream vios_;
2436 ScopedIndentation indent1_;
2437
Ian Rogers1d54e732013-05-02 21:10:01 -07002438 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002439 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002440 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002441 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002442 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002443
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002444 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002445};
2446
Jeff Haodcdc85b2015-12-04 14:06:18 -08002447static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002448 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002449 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002450 options->class_loader_ = &null_class_loader;
2451
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002452 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002453 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002454 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2455 CHECK(!image_spaces.empty());
2456 for (gc::space::ImageSpace* image_space : image_spaces) {
2457 const ImageHeader& image_header = image_space->GetImageHeader();
2458 if (!image_header.IsValid()) {
2459 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2460 return EXIT_FAILURE;
2461 }
2462
2463 ImageDumper image_dumper(os, *image_space, image_header, options);
2464 if (!image_dumper.Dump()) {
2465 return EXIT_FAILURE;
2466 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002467 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002468 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002469}
2470
Andreas Gampe00b25f32014-09-17 21:49:05 -07002471static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2472 std::ostream* os) {
2473 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2474
2475 Thread* self = Thread::Current();
2476 CHECK(self != nullptr);
2477 // Need well-known-classes.
2478 WellKnownClasses::Init(self->GetJniEnv());
2479
2480 // Need to register dex files to get a working dex cache.
2481 ScopedObjectAccess soa(self);
2482 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002483 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002484 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002485 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2486 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002487 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002488 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002489 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002490 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002491 }
2492
2493 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002494 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002495 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002496
2497 // Use the class loader while dumping.
2498 StackHandleScope<1> scope(self);
2499 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2500 soa.Decode<mirror::ClassLoader*>(class_loader));
2501 options->class_loader_ = &loader_handle;
2502
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002503 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002504 bool success = oat_dumper.Dump(*os);
2505 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2506}
2507
2508static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002509 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002510 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002511 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002512 options->class_loader_ = &null_class_loader;
2513
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002514 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002515 bool success = oat_dumper.Dump(*os);
2516 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2517}
2518
2519static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2520 std::ostream* os) {
2521 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002522 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002523 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002524 if (oat_file == nullptr) {
2525 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2526 return EXIT_FAILURE;
2527 }
2528
2529 if (runtime != nullptr) {
2530 return DumpOatWithRuntime(runtime, oat_file, options, os);
2531 } else {
2532 return DumpOatWithoutRuntime(oat_file, options, os);
2533 }
2534}
2535
2536static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2537 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002538 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002539 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002540 if (oat_file == nullptr) {
2541 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2542 return EXIT_FAILURE;
2543 }
2544
2545 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002546 if (!oat_symbolizer.Symbolize()) {
2547 fprintf(stderr, "Failed to symbolize\n");
2548 return EXIT_FAILURE;
2549 }
2550
2551 return EXIT_SUCCESS;
2552}
2553
Igor Murashkin37743352014-11-13 14:38:00 -08002554struct OatdumpArgs : public CmdlineArgs {
2555 protected:
2556 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002557
Igor Murashkin37743352014-11-13 14:38:00 -08002558 virtual ParseStatus ParseCustom(const StringPiece& option,
2559 std::string* error_msg) OVERRIDE {
2560 {
2561 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2562 if (base_parse != kParseUnknownArgument) {
2563 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002564 }
2565 }
2566
Igor Murashkin37743352014-11-13 14:38:00 -08002567 if (option.starts_with("--oat-file=")) {
2568 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2569 } else if (option.starts_with("--image=")) {
2570 image_location_ = option.substr(strlen("--image=")).data();
2571 } else if (option =="--dump:raw_mapping_table") {
2572 dump_raw_mapping_table_ = true;
2573 } else if (option == "--dump:raw_gc_map") {
2574 dump_raw_gc_map_ = true;
2575 } else if (option == "--no-dump:vmap") {
2576 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002577 } else if (option =="--dump:code_info_stack_maps") {
2578 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002579 } else if (option == "--no-disassemble") {
2580 disassemble_code_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002581 } else if (option =="--header-only") {
2582 dump_header_only_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002583 } else if (option.starts_with("--symbolize=")) {
2584 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2585 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002586 } else if (option.starts_with("--class-filter=")) {
2587 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002588 } else if (option.starts_with("--method-filter=")) {
2589 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002590 } else if (option.starts_with("--list-classes")) {
2591 list_classes_ = true;
2592 } else if (option.starts_with("--list-methods")) {
2593 list_methods_ = true;
2594 } else if (option.starts_with("--export-dex-to=")) {
2595 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2596 } else if (option.starts_with("--addr2instr=")) {
2597 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2598 *error_msg = "Address conversion failed";
2599 return kParseError;
2600 }
Igor Murashkin37743352014-11-13 14:38:00 -08002601 } else {
2602 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002603 }
2604
Igor Murashkin37743352014-11-13 14:38:00 -08002605 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002606 }
2607
Igor Murashkin37743352014-11-13 14:38:00 -08002608 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2609 // Infer boot image location from the image location if possible.
2610 if (boot_image_location_ == nullptr) {
2611 boot_image_location_ = image_location_;
2612 }
2613
2614 // Perform the parent checks.
2615 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2616 if (parent_checks != kParseOk) {
2617 return parent_checks;
2618 }
2619
2620 // Perform our own checks.
2621 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2622 *error_msg = "Either --image or --oat-file must be specified";
2623 return kParseError;
2624 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2625 *error_msg = "Either --image or --oat-file must be specified but not both";
2626 return kParseError;
2627 }
2628
2629 return kParseOk;
2630 }
2631
2632 virtual std::string GetUsage() const {
2633 std::string usage;
2634
2635 usage +=
2636 "Usage: oatdump [options] ...\n"
2637 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2638 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2639 "\n"
2640 // Either oat-file or image is required.
2641 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2642 " Example: --oat-file=/system/framework/boot.oat\n"
2643 "\n"
2644 " --image=<file.art>: specifies an input image location.\n"
2645 " Example: --image=/system/framework/boot.art\n"
2646 "\n";
2647
2648 usage += Base::GetUsage();
2649
2650 usage += // Optional.
2651 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2652 " Example: --dump:raw_mapping_table\n"
2653 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002654 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002655 " Example: --dump:raw_gc_map\n"
2656 "\n"
2657 " --no-dump:vmap may be used to disable vmap dumping.\n"
2658 " Example: --no-dump:vmap\n"
2659 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002660 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2661 " Example: --dump:code_info_stack_maps\n"
2662 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002663 " --no-disassemble may be used to disable disassembly.\n"
2664 " Example: --no-disassemble\n"
2665 "\n"
David Brazdilc03d7b62016-03-02 12:18:03 +00002666 " --header-only may be used to print only the oat header.\n"
2667 " Example: --header-only\n"
2668 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002669 " --list-classes may be used to list target file classes (can be used with filters).\n"
2670 " Example: --list-classes\n"
2671 " Example: --list-classes --class-filter=com.example.foo\n"
2672 "\n"
2673 " --list-methods may be used to list target file methods (can be used with filters).\n"
2674 " Example: --list-methods\n"
2675 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2676 "\n"
2677 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2678 " Example: --symbolize=/system/framework/boot.oat\n"
2679 "\n"
2680 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2681 " Example: --class-filter=com.example.foo\n"
2682 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002683 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2684 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002685 "\n"
2686 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2687 " Example: --export-dex-to=/data/local/tmp\n"
2688 "\n"
2689 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2690 " address (e.g. PC from crash dump)\n"
2691 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002692 "\n";
2693
2694 return usage;
2695 }
2696
2697 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002698 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002699 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002700 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002701 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002702 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002703 bool dump_raw_mapping_table_ = false;
2704 bool dump_raw_gc_map_ = false;
2705 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002706 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002707 bool disassemble_code_ = true;
2708 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002709 bool list_classes_ = false;
2710 bool list_methods_ = false;
David Brazdilc03d7b62016-03-02 12:18:03 +00002711 bool dump_header_only_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002712 uint32_t addr2instr_ = 0;
2713 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002714};
2715
Igor Murashkin37743352014-11-13 14:38:00 -08002716struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2717 virtual bool NeedsRuntime() OVERRIDE {
2718 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002719
Igor Murashkin37743352014-11-13 14:38:00 -08002720 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2721 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2722
2723 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2724 args_->dump_raw_mapping_table_,
2725 args_->dump_raw_gc_map_,
2726 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002727 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002728 args_->disassemble_code_,
2729 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002730 args_->class_filter_,
2731 args_->method_filter_,
2732 args_->list_classes_,
2733 args_->list_methods_,
David Brazdilc03d7b62016-03-02 12:18:03 +00002734 args_->dump_header_only_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002735 args_->export_dex_location_,
2736 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002737
2738 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2739 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002740 }
2741
Igor Murashkin37743352014-11-13 14:38:00 -08002742 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2743 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002744 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002745
Mathieu Chartierd424d082014-10-15 10:31:46 -07002746 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002747
Andreas Gampec24f3992014-12-17 20:40:11 -08002748 if (args_->symbolize_) {
2749 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2750 } else {
2751 return DumpOat(nullptr,
2752 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002753 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002754 args_->os_) == EXIT_SUCCESS;
2755 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002756 }
2757
Igor Murashkin37743352014-11-13 14:38:00 -08002758 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2759 CHECK(args_ != nullptr);
2760
2761 if (args_->oat_filename_ != nullptr) {
2762 return DumpOat(runtime,
2763 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002764 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002765 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002766 }
Igor Murashkin37743352014-11-13 14:38:00 -08002767
Jeff Haodcdc85b2015-12-04 14:06:18 -08002768 return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002769 }
2770
Igor Murashkin37743352014-11-13 14:38:00 -08002771 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2772};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002773
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002774} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002775
2776int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002777 art::OatdumpMain main;
2778 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002779}