blob: e039d10583874e317d0491e9a18c3e00c3c096bc [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"
Elliott Hughes76160052012-12-12 16:31:20 -080031#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070032#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080035#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080036#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070037#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080038#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070039#include "gc/space/image_space.h"
40#include "gc/space/large_object_space.h"
41#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070042#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080043#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070044#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/array-inl.h"
46#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010050#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080051#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070052#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070055#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080056#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080057#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080058#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070059#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070061
Igor Murashkin37743352014-11-13 14:38:00 -080062#include <sys/stat.h>
63#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064
Igor Murashkin37743352014-11-13 14:38:00 -080065namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Mathieu Chartiere401d142015-04-22 13:56:20 -070067const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080068 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070069 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070070 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070072 "kRefsOnlySaveMethod",
73 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070074};
75
76const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070077 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070078 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070079};
80
David Srbeckybc90fd02015-04-22 19:40:27 +010081class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070082 public:
David Srbeckybc90fd02015-04-22 19:40:27 +010083 class RodataWriter FINAL : public CodeOutput {
84 public:
85 explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
Andreas Gampe54fc26c2014-09-04 21:47:42 -070086
David Srbeckybc90fd02015-04-22 19:40:27 +010087 bool Write(OutputStream* out) OVERRIDE {
88 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
89 return out->WriteFully(oat_file_->Begin(), rodata_size);
Andreas Gampe54fc26c2014-09-04 21:47:42 -070090 }
91
David Srbeckybc90fd02015-04-22 19:40:27 +010092 private:
93 const OatFile* oat_file_;
94 };
95
96 class TextWriter FINAL : public CodeOutput {
97 public:
98 explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
99
100 bool Write(OutputStream* out) OVERRIDE {
101 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
102 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
103 return out->WriteFully(text_begin, oat_file_->End() - text_begin);
104 }
105
106 private:
107 const OatFile* oat_file_;
108 };
109
110 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
111 oat_file_(oat_file), builder_(nullptr),
112 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700113 }
114
115 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
116 uint32_t,
117 const OatFile::OatMethod&,
118 const DexFile&,
119 uint32_t,
120 const DexFile::CodeItem*,
121 uint32_t);
122
123 bool Symbolize() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100124 Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
125 uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
126 uint32_t text_size = size - rodata_size;
127 uint32_t bss_size = oat_file_->BssSize();
128 RodataWriter rodata_writer(oat_file_);
129 TextWriter text_writer(oat_file_);
130 builder_.reset(new ElfBuilder<ElfTypes32>(
131 oat_file_->GetOatHeader().GetInstructionSet(),
132 rodata_size, &rodata_writer,
133 text_size, &text_writer,
134 bss_size));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700135
136 Walk(&art::OatSymbolizer::RegisterForDedup);
137
138 NormalizeState();
139
140 Walk(&art::OatSymbolizer::AddSymbol);
141
David Srbeckybc90fd02015-04-22 19:40:27 +0100142 File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
143 bool result = builder_->Write(elf_output);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700144
Andreas Gampe4303ba92014-11-06 01:00:46 -0800145 // Ignore I/O errors.
David Srbeckybc90fd02015-04-22 19:40:27 +0100146 UNUSED(elf_output->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700147
148 return result;
149 }
150
151 void Walk(Callback callback) {
152 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
153 for (size_t i = 0; i < oat_dex_files.size(); i++) {
154 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700156 WalkOatDexFile(oat_dex_file, callback);
157 }
158 }
159
160 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
161 std::string error_msg;
162 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
163 if (dex_file.get() == nullptr) {
164 return;
165 }
166 for (size_t class_def_index = 0;
167 class_def_index < dex_file->NumClassDefs();
168 class_def_index++) {
169 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
170 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
171 OatClassType type = oat_class.GetType();
172 switch (type) {
173 case kOatClassAllCompiled:
174 case kOatClassSomeCompiled:
175 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
176 break;
177
178 case kOatClassNoneCompiled:
179 case kOatClassMax:
180 // Ignore.
181 break;
182 }
183 }
184 }
185
186 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
187 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700188 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700189 if (class_data == nullptr) { // empty class such as a marker interface?
190 return;
191 }
192 // Note: even if this is an interface or a native class, we still have to walk it, as there
193 // might be a static initializer.
194 ClassDataItemIterator it(dex_file, class_data);
195 SkipAllFields(&it);
196 uint32_t class_method_idx = 0;
197 while (it.HasNextDirectMethod()) {
198 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
199 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700200 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700201 class_method_idx++;
202 it.Next();
203 }
204 while (it.HasNextVirtualMethod()) {
205 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
206 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700207 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700208 class_method_idx++;
209 it.Next();
210 }
211 DCHECK(!it.HasNext());
212 }
213
214 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
215 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
216 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
217 uint32_t method_access_flags, Callback callback) {
218 if ((method_access_flags & kAccAbstract) != 0) {
219 // Abstract method, no code.
220 return;
221 }
222 if (oat_method.GetCodeOffset() == 0) {
223 // No code.
224 return;
225 }
226
227 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
228 method_access_flags);
229 }
230
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700231 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
232 uint32_t class_method_index ATTRIBUTE_UNUSED,
233 const OatFile::OatMethod& oat_method,
234 const DexFile& dex_file ATTRIBUTE_UNUSED,
235 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
236 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
237 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238 state_[oat_method.GetCodeOffset()]++;
239 }
240
241 void NormalizeState() {
242 for (auto& x : state_) {
243 if (x.second == 1) {
244 state_[x.first] = 0;
245 }
246 }
247 }
248
249 enum class DedupState { // private
250 kNotDeduplicated,
251 kDeduplicatedFirst,
252 kDeduplicatedOther
253 };
254 DedupState IsDuplicated(uint32_t offset) {
255 if (state_[offset] == 0) {
256 return DedupState::kNotDeduplicated;
257 }
258 if (state_[offset] == 1) {
259 return DedupState::kDeduplicatedOther;
260 }
261 state_[offset] = 1;
262 return DedupState::kDeduplicatedFirst;
263 }
264
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700265 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
266 uint32_t class_method_index ATTRIBUTE_UNUSED,
267 const OatFile::OatMethod& oat_method,
268 const DexFile& dex_file,
269 uint32_t dex_method_idx,
270 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
271 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
273 if (dedup != DedupState::kDeduplicatedOther) {
274 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
275
276 if (dedup == DedupState::kDeduplicatedFirst) {
277 pretty_name = "[Dedup]" + pretty_name;
278 }
279
David Srbeckybc90fd02015-04-22 19:40:27 +0100280 auto* symtab = builder_->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281
David Srbeckybc90fd02015-04-22 19:40:27 +0100282 symtab->AddSymbol(pretty_name, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700283 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
284 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700285 }
286 }
287
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700288 private:
289 static void SkipAllFields(ClassDataItemIterator* it) {
290 while (it->HasNextStaticField()) {
291 it->Next();
292 }
293 while (it->HasNextInstanceField()) {
294 it->Next();
295 }
296 }
297
298 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100299 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700300 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700301 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700302};
303
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700304class OatDumperOptions {
305 public:
306 OatDumperOptions(bool dump_raw_mapping_table,
307 bool dump_raw_gc_map,
308 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100309 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700310 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700311 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800312 const char* class_filter,
313 const char* method_filter,
314 bool list_classes,
315 bool list_methods,
316 const char* export_dex_location,
317 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700318 : dump_raw_mapping_table_(dump_raw_mapping_table),
319 dump_raw_gc_map_(dump_raw_gc_map),
320 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100321 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700322 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700323 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000325 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800326 list_classes_(list_classes),
327 list_methods_(list_methods),
328 export_dex_location_(export_dex_location),
329 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800330 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700331
332 const bool dump_raw_mapping_table_;
333 const bool dump_raw_gc_map_;
334 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100335 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700336 const bool disassemble_code_;
337 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800338 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000339 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800340 const bool list_classes_;
341 const bool list_methods_;
342 const char* const export_dex_location_;
343 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700344 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700345};
346
Elliott Hughese3c845c2012-02-28 17:23:01 -0800347class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700348 public:
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800349 explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000350 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800351 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800354 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
355 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000357 oat_file.Begin(),
358 true /* can_read_litals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800359 CHECK(options_.class_loader_ != nullptr);
360 CHECK(options_.class_filter_ != nullptr);
361 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800362 AddAllOffsets();
363 }
364
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 delete disassembler_;
367 }
368
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800369 InstructionSet GetInstructionSet() {
370 return instruction_set_;
371 }
372
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700373 bool Dump(std::ostream& os) {
374 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800375 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700376
377 os << "MAGIC:\n";
378 os << oat_header.GetMagic() << "\n\n";
379
380 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800381 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700382
Elliott Hughesa72ec822012-03-05 17:12:22 -0800383 os << "INSTRUCTION SET:\n";
384 os << oat_header.GetInstructionSet() << "\n\n";
385
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700386 {
387 std::unique_ptr<const InstructionSetFeatures> features(
388 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
389 oat_header.GetInstructionSetFeaturesBitmap()));
390 os << "INSTRUCTION SET FEATURES:\n";
391 os << features->GetFeatureString() << "\n\n";
392 }
Dave Allison70202782013-10-22 17:52:19 -0700393
Brian Carlstromaded5f72011-10-07 17:15:04 -0700394 os << "DEX FILE COUNT:\n";
395 os << oat_header.GetDexFileCount() << "\n\n";
396
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800397#define DUMP_OAT_HEADER_OFFSET(label, offset) \
398 os << label " OFFSET:\n"; \
399 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800400 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800401 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
402 } \
403 os << StringPrintf("\n\n");
404
405 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
406 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
407 GetInterpreterToInterpreterBridgeOffset);
408 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
409 GetInterpreterToCompiledCodeBridgeOffset);
410 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
411 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800412 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
413 GetQuickGenericJniTrampolineOffset);
414 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
415 GetQuickImtConflictTrampolineOffset);
416 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
417 GetQuickResolutionTrampolineOffset);
418 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
419 GetQuickToInterpreterBridgeOffset);
420#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700421
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700422 os << "IMAGE PATCH DELTA:\n";
423 os << StringPrintf("%d (0x%08x)\n\n",
424 oat_header.GetImagePatchDelta(),
425 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700426
Brian Carlstrom28db0122012-10-18 16:20:41 -0700427 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
428 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
429
430 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800431 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700432
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700433 // Print the key-value store.
434 {
435 os << "KEY VALUE STORE:\n";
436 size_t index = 0;
437 const char* key;
438 const char* value;
439 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
440 os << key << " = " << value << "\n";
441 index++;
442 }
443 os << "\n";
444 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700445
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800446 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700447 os << "BEGIN:\n";
448 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700449
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700450 os << "END:\n";
451 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
452 }
453
454 os << "SIZE:\n";
455 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700456
457 os << std::flush;
458
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800459 // If set, adjust relative address to be searched
460 if (options_.addr2instr_ != 0) {
461 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
462 os << "SEARCH ADDRESS (executable offset + input):\n";
463 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
464 }
465
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800466 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
467 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700468 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800469
470 // If file export selected skip file analysis
471 if (options_.export_dex_location_) {
472 if (!ExportDexFile(os, *oat_dex_file)) {
473 success = false;
474 }
475 } else {
476 if (!DumpOatDexFile(os, *oat_dex_file)) {
477 success = false;
478 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700479 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700481 os << std::flush;
482 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700483 }
484
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800485 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700486 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
487 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 return 0; // Address not in oat file
489 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800490 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
491 reinterpret_cast<uintptr_t>(oat_file_.Begin());
492 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800493 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800494 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800495 return end_offset - begin_offset;
496 }
497
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800498 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700499 return oat_file_.GetOatHeader().GetInstructionSet();
500 }
501
Mathieu Chartiere401d142015-04-22 13:56:20 -0700502 const void* GetQuickOatCode(ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
504 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700505 CHECK(oat_dex_file != nullptr);
506 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700507 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700508 if (dex_file.get() == nullptr) {
509 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
510 << "': " << error_msg;
511 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800512 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700513 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800514 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700515 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700516 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100517 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100519 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800520 }
521 }
522 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700523 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800524 }
525
Brian Carlstromaded5f72011-10-07 17:15:04 -0700526 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800528 // We don't know the length of the code for each method, but we need to know where to stop
529 // when disassembling. What we do know is that a region of code will be followed by some other
530 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
531 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800532 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
533 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700534 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700535 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700536 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700537 if (dex_file.get() == nullptr) {
538 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
539 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800540 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800541 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800542 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800543 for (size_t class_def_index = 0;
544 class_def_index < dex_file->NumClassDefs();
545 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800546 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100547 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700548 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700549 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800550 ClassDataItemIterator it(*dex_file, class_data);
551 SkipAllFields(it);
552 uint32_t class_method_index = 0;
553 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100554 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800555 it.Next();
556 }
557 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100558 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800559 it.Next();
560 }
561 }
562 }
563 }
564
565 // If the last thing in the file is code for a method, there won't be an offset for the "next"
566 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
567 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800568 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 }
570
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700571 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
572 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
573 }
574
Elliott Hughese3c845c2012-02-28 17:23:01 -0800575 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800576 uint32_t code_offset = oat_method.GetCodeOffset();
577 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
578 code_offset &= ~0x1;
579 }
580 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800581 offsets_.insert(oat_method.GetMappingTableOffset());
582 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800583 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800584 }
585
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700586 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
587 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800588 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700589 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800590 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800591 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700592
593 // Create the verifier early.
594
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700595 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700596 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700597 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700598 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700599 os << std::flush;
600 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700601 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800602 for (size_t class_def_index = 0;
603 class_def_index < dex_file->NumClassDefs();
604 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700605 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
606 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800607
608 // TODO: Support regex
609 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
610 continue;
611 }
612
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700613 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100614 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700615 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
616 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100617 << " (" << oat_class.GetStatus() << ")"
618 << " (" << oat_class.GetType() << ")\n";
619 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800620 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
621 std::ostream indented_os(&indent_filter);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800622 if (options_.list_classes_) continue;
623 if (!DumpOatClass(indented_os, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700624 success = false;
625 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800626 if (stop_analysis) {
627 os << std::flush;
628 return success;
629 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700630 }
631
632 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700633 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700634 }
635
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800636 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
637 std::string error_msg;
638 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
639
640 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
641 if (dex_file == nullptr) {
642 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
643 return false;
644 }
645 size_t fsize = oat_dex_file.FileSize();
646
647 // Some quick checks just in case
648 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
649 os << "Invalid dex file\n";
650 return false;
651 }
652
653 // Verify output directory exists
654 if (!OS::DirectoryExists(options_.export_dex_location_)) {
655 // TODO: Extend OS::DirectoryExists if symlink support is required
656 os << options_.export_dex_location_ << " output directory not found or symlink\n";
657 return false;
658 }
659
660 // Beautify path names
661 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
662 return false;
663 }
664
665 std::string dex_orig_name;
666 size_t dex_orig_pos = dex_file_location.rfind('/');
667 if (dex_orig_pos == std::string::npos)
668 dex_orig_name = dex_file_location;
669 else
670 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
671
672 // A more elegant approach to efficiently name user installed apps is welcome
673 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
674 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
675 size_t apk_orig_pos = dex_file_location.rfind('/');
676 if (apk_orig_pos != std::string::npos) {
677 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
678 }
679 }
680
681 std::string out_dex_path(options_.export_dex_location_);
682 if (out_dex_path.back() != '/') {
683 out_dex_path.append("/");
684 }
685 out_dex_path.append(dex_orig_name);
686 out_dex_path.append("_export.dex");
687 if (out_dex_path.length() > PATH_MAX) {
688 return false;
689 }
690
691 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
692 if (file.get() == nullptr) {
693 os << "Failed to open output dex file " << out_dex_path;
694 return false;
695 }
696
697 if (!file->WriteFully(dex_file->Begin(), fsize)) {
698 os << "Failed to write dex file";
699 file->Erase();
700 return false;
701 }
702
703 if (file->FlushCloseOrErase() != 0) {
704 os << "Flush and close failed";
705 return false;
706 }
707
708 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
709 os << std::flush;
710
711 return true;
712 }
713
Elliott Hughese3c845c2012-02-28 17:23:01 -0800714 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700715 while (it.HasNextStaticField()) {
716 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700717 }
Ian Rogers0571d352011-11-03 19:51:38 -0700718 while (it.HasNextInstanceField()) {
719 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700720 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800721 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700722
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700723 bool DumpOatClass(std::ostream& os, const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800724 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700725 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800726 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700727 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700728 if (class_data == nullptr) { // empty class such as a marker interface?
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700729 os << std::flush;
730 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800731 }
732 ClassDataItemIterator it(dex_file, class_data);
733 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700734 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700735 while (it.HasNextDirectMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700736 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
737 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800738 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700739 success = false;
740 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800741 if (addr_found) {
742 *stop_analysis = true;
743 return success;
744 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700745 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700746 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700747 }
Ian Rogers0571d352011-11-03 19:51:38 -0700748 while (it.HasNextVirtualMethod()) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700749 if (!DumpOatMethod(os, class_def, class_method_index, oat_class, dex_file,
750 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800751 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700752 success = false;
753 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800754 if (addr_found) {
755 *stop_analysis = true;
756 return success;
757 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700758 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700759 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700760 }
Ian Rogers0571d352011-11-03 19:51:38 -0700761 DCHECK(!it.HasNext());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700762 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700763 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700764 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800765
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700766 static constexpr uint32_t kPrologueBytes = 16;
767
768 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
769 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
770
771 bool DumpOatMethod(std::ostream& os, const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700772 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700773 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800774 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800775 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700776 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800777
778 // TODO: Support regex
779 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
780 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000781 return success;
782 }
783
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800784 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800785 os << StringPrintf("%d: %s (dex_method_idx=%d)\n",
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000786 class_method_index, pretty_method.c_str(),
Ian Rogersdb7bdc12012-04-09 21:27:15 -0700787 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800788 if (options_.list_methods_) return success;
789
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800790 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700791 std::unique_ptr<std::ostream> indent1_os(new std::ostream(&indent1_filter));
792 Indenter indent2_filter(indent1_os->rdbuf(), kIndentChar, kIndentBy1Count);
793 std::unique_ptr<std::ostream> indent2_os(new std::ostream(&indent2_filter));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800794
795 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
796 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
797 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
798 uint32_t code_offset = oat_method.GetCodeOffset();
799 uint32_t code_size = oat_method.GetQuickCodeSize();
800 if (resolved_addr2instr_ != 0) {
801 if (resolved_addr2instr_ > code_offset + code_size) {
802 return success;
803 } else {
804 *addr_found = true; // stop analyzing file at next iteration
805 }
806 }
807
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800808 {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700809 *indent1_os << "DEX CODE:\n";
810 DumpDexCode(*indent2_os, dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700811 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700812
813 std::unique_ptr<verifier::MethodVerifier> verifier;
814 if (Runtime::Current() != nullptr) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700815 *indent1_os << "VERIFIER TYPE ANALYSIS:\n";
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700816 verifier.reset(DumpVerifier(*indent2_os, dex_method_idx, &dex_file, class_def, code_item,
817 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700818 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800819 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700820 *indent1_os << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800821 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700822 *indent1_os << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100823 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700824 *indent1_os << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
825 if (oat_method_offsets_offset > oat_file_.Size()) {
826 *indent1_os << StringPrintf(
827 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
828 oat_method_offsets_offset, oat_file_.Size());
829 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
830 os << std::flush;
831 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800832 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700833
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700834 *indent2_os << StringPrintf("code_offset: 0x%08x ", code_offset);
835 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
836 if (aligned_code_begin > oat_file_.Size()) {
837 *indent2_os << StringPrintf("WARNING: "
838 "code offset 0x%08x is past end of file 0x%08zx.\n",
839 aligned_code_begin, oat_file_.Size());
840 success = false;
841 }
842 *indent2_os << "\n";
843
844 *indent2_os << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800845 if (options_.absolute_addresses_) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800846 *indent2_os << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700847 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800848 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700849 *indent2_os << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
850 if (gc_map_offset > oat_file_.Size()) {
851 *indent2_os << StringPrintf("WARNING: "
852 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
853 gc_map_offset, oat_file_.Size());
854 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800855 } else if (options_.dump_raw_gc_map_) {
Andreas Gampea8b41aa2014-06-04 22:42:28 -0700856 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800857 std::ostream indent3_os(&indent3_filter);
858 DumpGcMap(indent3_os, oat_method, code_item);
859 }
860 }
861 {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700862 *indent1_os << "OatQuickMethodHeader ";
863 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
864 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700865
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800866 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700867 *indent1_os << StringPrintf("%p ", method_header);
868 }
869 *indent1_os << StringPrintf("(offset=0x%08x)\n", method_header_offset);
870 if (method_header_offset > oat_file_.Size()) {
871 *indent1_os << StringPrintf(
872 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
873 method_header_offset, oat_file_.Size());
874 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
875 os << std::flush;
876 return false;
877 }
878
879 *indent2_os << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800880 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700881 *indent2_os << StringPrintf("%p ", oat_method.GetMappingTable());
882 }
883 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
884 *indent2_os << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
885 if (mapping_table_offset > oat_file_.Size()) {
886 *indent2_os << StringPrintf("WARNING: "
887 "mapping table offset 0x%08x is past end of file 0x%08zx. "
888 "mapping table offset was loaded from offset 0x%08x.\n",
889 mapping_table_offset, oat_file_.Size(),
890 oat_method.GetMappingTableOffsetOffset());
891 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800892 } else if (options_.dump_raw_mapping_table_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893 Indenter indent3_filter(indent2_os->rdbuf(), kIndentChar, kIndentBy1Count);
894 std::ostream indent3_os(&indent3_filter);
895 DumpMappingTable(indent3_os, oat_method);
896 }
897
898 *indent2_os << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800899 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700900 *indent2_os << StringPrintf("%p ", oat_method.GetVmapTable());
901 }
902 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
903 *indent2_os << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
904 if (vmap_table_offset > oat_file_.Size()) {
905 *indent2_os << StringPrintf("WARNING: "
906 "vmap table offset 0x%08x is past end of file 0x%08zx. "
907 "vmap table offset was loaded from offset 0x%08x.\n",
908 vmap_table_offset, oat_file_.Size(),
909 oat_method.GetVmapTableOffsetOffset());
910 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800911 } else if (options_.dump_vmap_) {
Roland Levillain442b46a2015-02-18 16:54:21 +0000912 DumpVmapData(*indent2_os, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700913 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800914 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700915 {
916 *indent1_os << "QuickMethodFrameInfo\n";
917
918 *indent2_os << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
919 *indent2_os << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
920 DumpSpillMask(*indent2_os, oat_method.GetCoreSpillMask(), false);
921 *indent2_os << "\n";
922 *indent2_os << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
923 DumpSpillMask(*indent2_os, oat_method.GetFpSpillMask(), true);
924 *indent2_os << "\n";
925 }
926 {
927 // Based on spill masks from QuickMethodFrameInfo so placed
928 // after it is dumped, but useful for understanding quick
929 // code, so dumped here.
930 DumpVregLocations(*indent2_os, oat_method, code_item);
931 }
932 {
933 *indent1_os << "CODE: ";
934 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
935 if (code_size_offset > oat_file_.Size()) {
936 *indent2_os << StringPrintf("WARNING: "
937 "code size offset 0x%08x is past end of file 0x%08zx.",
938 code_size_offset, oat_file_.Size());
939 success = false;
940 } else {
941 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700942 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
943 uint64_t aligned_code_end = aligned_code_begin + code_size;
944
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800945 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700946 *indent1_os << StringPrintf("%p ", code);
947 }
948 *indent1_os << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
949 code_offset,
950 code_size_offset,
951 code_size,
952 code != nullptr ? "..." : "");
953
954 if (aligned_code_begin > oat_file_.Size()) {
955 *indent2_os << StringPrintf("WARNING: "
956 "start of code at 0x%08x is past end of file 0x%08zx.",
957 aligned_code_begin, oat_file_.Size());
958 success = false;
959 } else if (aligned_code_end > oat_file_.Size()) {
960 *indent2_os << StringPrintf("WARNING: "
961 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
962 "code size is 0x%08x loaded from offset 0x%08x.\n",
963 aligned_code_end, oat_file_.Size(),
964 code_size, code_size_offset);
965 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800966 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700967 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
968 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
969 }
970 }
971 } else if (code_size > kMaxCodeSize) {
972 *indent2_os << StringPrintf("WARNING: "
973 "code size %d is bigger than max expected threshold of %d. "
974 "code size is 0x%08x loaded from offset 0x%08x.\n",
975 code_size, kMaxCodeSize,
976 code_size, code_size_offset);
977 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800978 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700979 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
980 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, true, kPrologueBytes);
981 }
982 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800983 } else if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700984 DumpCode(*indent2_os, verifier.get(), oat_method, code_item, !success, 0);
985 }
986 }
987 }
988 os << std::flush;
989 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700990 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800991
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800992 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
993 if (spill_mask == 0) {
994 return;
995 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800996 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800997 for (size_t i = 0; i < 32; i++) {
998 if ((spill_mask & (1 << i)) != 0) {
999 if (is_float) {
1000 os << "fr" << i;
1001 } else {
1002 os << "r" << i;
1003 }
1004 spill_mask ^= 1 << i; // clear bit
1005 if (spill_mask != 0) {
1006 os << ", ";
1007 } else {
1008 break;
1009 }
1010 }
1011 }
1012 os << ")";
1013 }
1014
Roland Levillain442b46a2015-02-18 16:54:21 +00001015 // Display data stored at the the vmap offset of an oat method.
1016 void DumpVmapData(std::ostream& os,
1017 const OatFile::OatMethod& oat_method,
1018 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001019 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1020 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001021 const void* raw_code_info = oat_method.GetVmapTable();
1022 if (raw_code_info != nullptr) {
1023 CodeInfo code_info(raw_code_info);
1024 DCHECK(code_item != nullptr);
Roland Levillainf2650d12015-05-28 14:53:28 +01001025 DumpCodeInfo(os, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001026 }
1027 } else {
1028 // Otherwise, display the vmap table.
1029 const uint8_t* raw_table = oat_method.GetVmapTable();
1030 if (raw_table != nullptr) {
1031 VmapTable vmap_table(raw_table);
1032 DumpVmapTable(os, oat_method, vmap_table);
1033 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001034 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001035 }
1036
1037 // Display a CodeInfo object emitted by the optimizing compiler.
1038 void DumpCodeInfo(std::ostream& os,
1039 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001040 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001041 const DexFile::CodeItem& code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001042 code_info.Dump(os,
1043 oat_method.GetCodeOffset(),
1044 code_item.registers_size_,
1045 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001046 }
1047
Roland Levillain442b46a2015-02-18 16:54:21 +00001048 // Display a vmap table.
1049 void DumpVmapTable(std::ostream& os,
1050 const OatFile::OatMethod& oat_method,
1051 const VmapTable& vmap_table) {
1052 bool first = true;
1053 bool processing_fp = false;
1054 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1055 for (size_t i = 0; i < vmap_table.Size(); i++) {
1056 uint16_t dex_reg = vmap_table[i];
1057 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1058 processing_fp ? kFloatVReg : kIntVReg);
1059 os << (first ? "v" : ", v") << dex_reg;
1060 if (!processing_fp) {
1061 os << "/r" << cpu_reg;
1062 } else {
1063 os << "/fr" << cpu_reg;
1064 }
1065 first = false;
1066 if (!processing_fp && dex_reg == 0xFFFF) {
1067 processing_fp = true;
1068 spill_mask = oat_method.GetFpSpillMask();
1069 }
1070 }
1071 os << "\n";
1072 }
1073
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001074 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1075 const DexFile::CodeItem* code_item) {
1076 if (code_item != nullptr) {
1077 size_t num_locals_ins = code_item->registers_size_;
1078 size_t num_ins = code_item->ins_size_;
1079 size_t num_locals = num_locals_ins - num_ins;
1080 size_t num_outs = code_item->outs_size_;
1081
1082 os << "vr_stack_locations:";
1083 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1084 // For readability, delimit the different kinds of VRs.
1085 if (reg == num_locals_ins) {
1086 os << "\n\tmethod*:";
1087 } else if (reg == num_locals && num_ins > 0) {
1088 os << "\n\tins:";
1089 } else if (reg == 0 && num_locals > 0) {
1090 os << "\n\tlocals:";
1091 }
1092
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001093 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1094 code_item,
1095 oat_method.GetCoreSpillMask(),
1096 oat_method.GetFpSpillMask(),
1097 oat_method.GetFrameSizeInBytes(),
1098 reg,
1099 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001100 os << " v" << reg << "[sp + #" << offset << "]";
1101 }
1102
1103 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1104 if (out_reg == 0) {
1105 os << "\n\touts:";
1106 }
1107
1108 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1109 os << " v" << out_reg << "[sp + #" << offset << "]";
1110 }
1111
1112 os << "\n";
1113 }
1114 }
1115
Ian Rogersb23a7722012-10-09 16:54:26 -07001116 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001117 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001118 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001119 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001120 const VmapTable vmap_table(raw_table);
1121 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001122 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001123 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1124 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1125 : oat_method.GetCoreSpillMask();
1126 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001127 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001128 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1129 code_item,
1130 oat_method.GetCoreSpillMask(),
1131 oat_method.GetFpSpillMask(),
1132 oat_method.GetFrameSizeInBytes(),
1133 reg,
1134 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001135 os << "[sp + #" << offset << "]";
1136 }
1137 }
1138 }
1139
Ian Rogersef7d42f2014-01-06 12:55:46 -08001140 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1141 const DexFile::CodeItem* code_item,
1142 size_t num_regs, const uint8_t* reg_bitmap) {
1143 bool first = true;
1144 for (size_t reg = 0; reg < num_regs; reg++) {
1145 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1146 if (first) {
1147 os << " v" << reg << " (";
1148 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1149 os << ")";
1150 first = false;
1151 } else {
1152 os << ", v" << reg << " (";
1153 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1154 os << ")";
1155 }
1156 }
1157 }
1158 if (first) {
1159 os << "No registers in GC map\n";
1160 } else {
1161 os << "\n";
1162 }
1163 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001164 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1165 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001166 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001167 if (gc_map_raw == nullptr) {
1168 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001169 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001170 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001171 NativePcOffsetToReferenceMap map(gc_map_raw);
1172 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1173 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1174 map.GetNativePcOffset(entry);
1175 os << StringPrintf("%p", native_pc);
1176 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001177 }
1178 }
1179
1180 void DumpMappingTable(std::ostream& os, const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001181 const void* quick_code = oat_method.GetQuickCode();
1182 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001183 return;
1184 }
Ian Rogers1809a722013-08-09 22:05:32 -07001185 MappingTable table(oat_method.GetMappingTable());
1186 if (table.TotalSize() != 0) {
1187 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1188 std::ostream indent_os(&indent_filter);
1189 if (table.PcToDexSize() != 0) {
1190 typedef MappingTable::PcToDexIterator It;
1191 os << "suspend point mappings {\n";
1192 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1193 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1194 }
1195 os << "}\n";
1196 }
1197 if (table.DexToPcSize() != 0) {
1198 typedef MappingTable::DexToPcIterator It;
1199 os << "catch entry mappings {\n";
1200 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1201 indent_os << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
1202 }
1203 os << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001204 }
1205 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001206 }
1207
Roland Levillainf2650d12015-05-28 14:53:28 +01001208 uint32_t DumpInformationAtOffset(std::ostream& os,
1209 const OatFile::OatMethod& oat_method,
1210 const DexFile::CodeItem* code_item,
1211 size_t offset,
1212 bool suspend_point_mapping) {
1213 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1214 if (suspend_point_mapping) {
1215 DumpDexRegisterMapAtOffset(os, oat_method, code_item, offset);
1216 }
1217 // The return value is not used in the case of a method compiled
1218 // with the optimizing compiler.
1219 return DexFile::kDexNoIndex;
1220 } else {
1221 return DumpMappingAtOffset(os, oat_method, offset, suspend_point_mapping);
1222 }
1223 }
1224
Ian Rogers1809a722013-08-09 22:05:32 -07001225 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1226 size_t offset, bool suspend_point_mapping) {
1227 MappingTable table(oat_method.GetMappingTable());
1228 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1229 typedef MappingTable::PcToDexIterator It;
1230 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1231 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001232 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001233 return cur.DexPc();
1234 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001235 }
Ian Rogers1809a722013-08-09 22:05:32 -07001236 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1237 typedef MappingTable::DexToPcIterator It;
1238 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1239 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001240 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001241 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001242 }
1243 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001244 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001245 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001246 }
1247
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001248 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1249 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001250 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001251 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001252 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001253 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001254 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001255 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001256 bool first = true;
1257 for (size_t reg = 0; reg < num_regs; reg++) {
1258 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1259 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001260 os << "GC map objects: v" << reg << " (";
1261 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001262 os << ")";
1263 first = false;
1264 } else {
1265 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001266 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001267 os << ")";
1268 }
1269 }
1270 }
1271 if (!first) {
1272 os << "\n";
1273 }
1274 }
1275 }
1276 }
1277
Mathieu Chartier590fee92013-09-13 13:46:47 -07001278 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1279 const OatFile::OatMethod& oat_method,
1280 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1281 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001282 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001283 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001284 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1285 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1286 if (kind != kUndefined) {
1287 if (first) {
1288 os << "VRegs: v";
1289 first = false;
1290 } else {
1291 os << ", v";
1292 }
1293 os << reg << " (";
1294 switch (kind) {
1295 case kImpreciseConstant:
1296 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1297 DescribeVReg(os, oat_method, code_item, reg, kind);
1298 break;
1299 case kConstant:
1300 os << "Constant: " << kinds.at((reg * 2) + 1);
1301 break;
1302 default:
1303 DescribeVReg(os, oat_method, code_item, reg, kind);
1304 break;
1305 }
1306 os << ")";
1307 }
1308 }
1309 if (!first) {
1310 os << "\n";
1311 }
1312 }
1313
1314
Ian Rogersb23a7722012-10-09 16:54:26 -07001315 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001316 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001317 size_t i = 0;
1318 while (i < code_item->insns_size_in_code_units_) {
1319 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001320 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1321 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001322 i += instruction->SizeInCodeUnits();
1323 }
1324 }
1325 }
1326
Roland Levillainf2650d12015-05-28 14:53:28 +01001327 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1328 // the optimizing compiler?
1329 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1330 const DexFile::CodeItem* code_item) {
1331 // If the native GC map is null and the Dex `code_item` is not
1332 // null, then this method has been compiled with the optimizing
1333 // compiler.
1334 return oat_method.GetGcMap() == nullptr && code_item != nullptr;
1335 }
1336
1337 void DumpDexRegisterMapAtOffset(std::ostream& os,
1338 const OatFile::OatMethod& oat_method,
1339 const DexFile::CodeItem* code_item,
1340 size_t offset) {
1341 // This method is only relevant for oat methods compiled with the
1342 // optimizing compiler.
1343 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1344
1345 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1346 const void* raw_code_info = oat_method.GetVmapTable();
1347 if (raw_code_info != nullptr) {
1348 CodeInfo code_info(raw_code_info);
1349 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset);
1350 if (stack_map.IsValid()) {
1351 stack_map.Dump(os, code_info, oat_method.GetCodeOffset(), code_item->registers_size_);
1352 }
1353 }
1354 }
1355
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001356 verifier::MethodVerifier* DumpVerifier(std::ostream& os, uint32_t dex_method_idx,
1357 const DexFile* dex_file,
1358 const DexFile::ClassDef& class_def,
1359 const DexFile::CodeItem* code_item,
1360 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001361 if ((method_access_flags & kAccNative) == 0) {
1362 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001363 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001364 Handle<mirror::DexCache> dex_cache(
1365 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001366 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001367 return verifier::MethodVerifier::VerifyMethodAndDump(
1368 soa.Self(), os, dex_method_idx, dex_file, dex_cache, *options_.class_loader_, &class_def,
1369 code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001370 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001371
1372 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001373 }
1374
Mathieu Chartier590fee92013-09-13 13:46:47 -07001375 void DumpCode(std::ostream& os, verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001376 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1377 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001378 const void* quick_code = oat_method.GetQuickCode();
1379
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001380 if (code_size == 0) {
1381 code_size = oat_method.GetQuickCodeSize();
1382 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001383 if (code_size == 0 || quick_code == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001384 os << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001385 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001386 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001387 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1388 size_t offset = 0;
1389 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001390 if (!bad_input) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001391 DumpInformationAtOffset(os, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001392 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001393 offset += disassembler_->Dump(os, quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001394 if (!bad_input) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001395 uint32_t dex_pc = DumpInformationAtOffset(os, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001396 if (dex_pc != DexFile::kDexNoIndex) {
1397 DumpGcMapAtNativePcOffset(os, oat_method, code_item, offset);
1398 if (verifier != nullptr) {
1399 DumpVRegsAtDexPc(os, verifier, oat_method, code_item, dex_pc);
1400 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001401 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001402 }
1403 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001404 }
1405 }
1406
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001407 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001408 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001409 const OatDumperOptions& options_;
1410 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001411 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001412 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001413 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001414};
1415
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001416class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001417 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001418 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001419 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1420 : os_(os),
1421 image_space_(image_space),
1422 image_header_(image_header),
1423 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001424
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001425 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001426 std::ostream& os = *os_;
1427 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001428
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001429 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001430
Mathieu Chartiere401d142015-04-22 13:56:20 -07001431 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1432
1433 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1434 auto section = static_cast<ImageHeader::ImageSections>(i);
1435 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1436 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001437
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001438 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001439
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001440 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001441
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001442 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1443
1444 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1445
1446 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001447
Alex Lighta59dd802014-07-02 16:28:08 -07001448 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1449
Igor Murashkin46774762014-10-22 11:37:02 -07001450 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1451
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001452 {
1453 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
1454 Indenter indent1_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1455 std::ostream indent1_os(&indent1_filter);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001456 static_assert(arraysize(image_roots_descriptions_) ==
1457 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001458 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1459 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1460 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001461 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001462 indent1_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
1463 if (image_root_object->IsObjectArray()) {
1464 Indenter indent2_filter(indent1_os.rdbuf(), kIndentChar, kIndentBy1Count);
1465 std::ostream indent2_os(&indent2_filter);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001466 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001467 = image_root_object->AsObjectArray<mirror::Object>();
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001468 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1469 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001470 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001471 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1472 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001473 run++;
1474 } else {
1475 break;
1476 }
1477 }
1478 if (run == 0) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001479 indent2_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001480 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001481 indent2_os << StringPrintf("%d to %zd: ", j, j + run);
1482 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001483 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001484 if (value != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001485 PrettyObjectValue(indent2_os, value->GetClass(), value);
1486 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001487 indent2_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001488 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001489 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001490 }
1491 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001492
1493 os << "METHOD ROOTS\n";
1494 static_assert(arraysize(image_methods_descriptions_) ==
1495 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1496 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1497 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1498 const char* description = image_methods_descriptions_[i];
1499 auto* image_method = image_header_.GetImageMethod(image_root);
1500 indent1_os << StringPrintf("%s: %p\n", description, image_method);
1501 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001502 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001503 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001504
Brian Carlstromaded5f72011-10-07 17:15:04 -07001505 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001506 std::string image_filename = image_space_.GetImageFilename();
1507 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001508 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001509 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001510 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001511 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1512 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001513 oat_file = OatFile::Open(oat_location, oat_location,
1514 nullptr, nullptr, false, nullptr,
1515 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001516 if (oat_file == nullptr) {
1517 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001518 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001519 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001520 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001522
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001523 stats_.oat_file_bytes = oat_file->Size();
1524
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001525 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001526
Mathieu Chartier02e25112013-08-14 16:14:24 -07001527 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001528 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001529 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1530 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001531 }
1532
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001533 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001534
1535 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001536 gc::Heap* heap = Runtime::Current()->GetHeap();
1537 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001538 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001539 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001540 {
1541 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1542 heap->FlushAllocStack();
1543 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001544 // Since FlushAllocStack() above resets the (active) allocation
1545 // stack. Need to revoke the thread-local allocation stacks that
1546 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001547 {
1548 self->TransitionFromRunnableToSuspended(kNative);
1549 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -07001550 thread_list->SuspendAll(__FUNCTION__);
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001551 heap->RevokeAllThreadLocalAllocationStacks(self);
1552 thread_list->ResumeAll();
1553 self->TransitionFromSuspendedToRunnable();
1554 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001555 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001556 {
1557 std::ostream* saved_os = os_;
1558 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1559 std::ostream indent_os(&indent_filter);
1560 os_ = &indent_os;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001561
1562 // Mark dex caches.
1563 dex_cache_arrays_.clear();
1564 {
1565 ReaderMutexLock mu(self, *class_linker->DexLock());
1566 for (size_t i = 0; i < class_linker->GetDexCacheCount(); ++i) {
1567 auto* dex_cache = class_linker->GetDexCache(i);
1568 dex_cache_arrays_.insert(dex_cache->GetResolvedFields());
1569 dex_cache_arrays_.insert(dex_cache->GetResolvedMethods());
1570 }
1571 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001573 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001574 if (space->IsImageSpace()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001575 auto* image_space = space->AsImageSpace();
1576 // Dump the normal objects before ArtMethods.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001577 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1578 indent_os << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001579 // TODO: Dump fields.
1580 // Dump methods after.
1581 const auto& methods_section = image_header_.GetMethodsSection();
1582 const auto pointer_size =
1583 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1584 const auto method_size = ArtMethod::ObjectSize(pointer_size);
1585 for (size_t pos = 0; pos < methods_section.Size(); pos += method_size) {
1586 auto* method = reinterpret_cast<ArtMethod*>(
1587 image_space->Begin() + pos + methods_section.Offset());
1588 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1589 DumpMethod(method, this, indent_os);
1590 indent_os << "\n";
1591 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001592 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001593 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001594 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001595 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001596 indent_os << "\n";
1597 os_ = saved_os;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001598 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001599 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001600 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001601 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001602 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001603 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001604 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001605 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001606 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001607 size_t header_bytes = sizeof(ImageHeader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001608 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1609 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1610 const auto& method_section = image_header_.GetMethodsSection();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001611 stats_.header_bytes = header_bytes;
1612 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1613 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1615 stats_.bitmap_bytes += bitmap_section.Size();
1616 stats_.art_field_bytes += field_section.Size();
1617 stats_.art_method_bytes += method_section.Size();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 stats_.Dump(os);
1619 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001620
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001622
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001623 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001624 }
1625
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001626 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001627 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001628 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001629 CHECK(type != nullptr);
1630 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001631 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001632 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001633 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001634 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001635 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001636 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001637 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001639 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001640 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001641 }
1642 }
1643
Mathieu Chartierc7853442015-03-27 14:35:38 -07001644 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001645 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001646 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001647 switch (field->GetTypeAsPrimitiveType()) {
1648 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001649 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001650 break;
1651 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001652 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001653 break;
1654 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001655 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001656 break;
1657 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001658 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001659 break;
1660 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001661 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001662 break;
1663 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001664 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001665 break;
1666 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001667 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1668 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001669 break;
1670 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001671 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001672 break;
1673 case Primitive::kPrimNot: {
1674 // Get the value, don't compute the type unless it is non-null as we don't want
1675 // to cause class loading.
1676 mirror::Object* value = field->GetObj(obj);
1677 if (value == nullptr) {
1678 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001679 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001680 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001681 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001682 if (field_type != nullptr) {
1683 PrettyObjectValue(os, field_type, value);
1684 } else {
1685 os << StringPrintf("%p %s\n", value,
1686 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1687 }
Ian Rogers50239c72013-06-17 14:53:22 -07001688 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001689 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001690 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001691 default:
1692 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1693 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001694 }
1695 }
1696
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001697 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001698 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001699 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001700 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001701 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001702 }
Mathieu Chartierc7853442015-03-27 14:35:38 -07001703 ArtField* fields = klass->GetIFields();
1704 for (size_t i = 0, count = klass->NumInstanceFields(); i < count; i++) {
1705 PrintField(os, &fields[i], obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001706 }
1707 }
1708
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001709 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001710 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001711 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001712
Mathieu Chartiere401d142015-04-22 13:56:20 -07001713 const void* GetQuickOatCodeBegin(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001714 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001715 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1716 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001717 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001718 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001719 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001720 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001721 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001722 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001723 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001724 }
1725
Mathieu Chartiere401d142015-04-22 13:56:20 -07001726 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001727 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001728 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1729 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001730 return 0;
1731 }
1732 return oat_code_begin[-1];
1733 }
1734
Mathieu Chartiere401d142015-04-22 13:56:20 -07001735 const void* GetQuickOatCodeEnd(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001736 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001737 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001738 if (oat_code_begin == nullptr) {
1739 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001740 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001741 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001742 }
1743
Mathieu Chartiere401d142015-04-22 13:56:20 -07001744 static void Callback(mirror::Object* obj, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001745 DCHECK(obj != nullptr);
1746 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001747 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001748 if (!state->InDumpSpace(obj)) {
1749 return;
1750 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001751
1752 size_t object_bytes = obj->SizeOf();
1753 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1754 state->stats_.object_bytes += object_bytes;
1755 state->stats_.alignment_bytes += alignment_bytes;
1756
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001757 std::ostream& os = *state->os_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001758 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001759 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001760 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1761 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001762 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001763 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001764 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1765 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001766 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001767 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001768 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001769 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001770 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001771 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001772 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1773 std::ostream indent_os(&indent_filter);
1774 DumpFields(indent_os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001775 const auto image_pointer_size =
1776 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001777 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001778 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1779 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001780 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001781 size_t run = 0;
1782 for (int32_t j = i + 1; j < length; j++) {
1783 if (value == obj_array->Get(j)) {
1784 run++;
1785 } else {
1786 break;
1787 }
1788 }
1789 if (run == 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001790 indent_os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001791 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001792 indent_os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001793 i = i + run;
1794 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001795 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001796 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001797 PrettyObjectValue(indent_os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001798 }
1799 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001800 mirror::Class* klass = obj->AsClass();
1801 ArtField* sfields = klass->GetSFields();
1802 const size_t num_fields = klass->NumStaticFields();
1803 if (num_fields != 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001804 indent_os << "STATICS:\n";
1805 Indenter indent2_filter(indent_os.rdbuf(), kIndentChar, kIndentBy1Count);
1806 std::ostream indent2_os(&indent2_filter);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001807 for (size_t i = 0; i < num_fields; i++) {
1808 PrintField(indent2_os, &sfields[i], sfields[i].GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001809 }
1810 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001811 } else {
1812 auto it = state->dex_cache_arrays_.find(obj);
1813 if (it != state->dex_cache_arrays_.end()) {
1814 const auto& field_section = state->image_header_.GetImageSection(
1815 ImageHeader::kSectionArtFields);
1816 const auto& method_section = state->image_header_.GetMethodsSection();
1817 auto* arr = down_cast<mirror::PointerArray*>(obj);
1818 for (int32_t i = 0, length = arr->GetLength(); i < length; i++) {
1819 void* elem = arr->GetElementPtrSize<void*>(i, image_pointer_size);
1820 size_t run = 0;
1821 for (int32_t j = i + 1; j < length &&
1822 elem == arr->GetElementPtrSize<void*>(j, image_pointer_size); j++, run++) { }
1823 if (run == 0) {
1824 indent_os << StringPrintf("%d: ", i);
1825 } else {
1826 indent_os << StringPrintf("%d to %zd: ", i, i + run);
1827 i = i + run;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001828 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001829 auto offset = reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin();
1830 std::string msg;
1831 if (field_section.Contains(offset)) {
1832 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1833 } else if (method_section.Contains(offset)) {
1834 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1835 } else {
1836 msg = "Unknown type";
1837 }
1838 indent_os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001839 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001840 }
1841 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001842 std::string temp;
1843 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001844 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001845
Mathieu Chartiere401d142015-04-22 13:56:20 -07001846 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
1847 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1848 DCHECK(method != nullptr);
1849 const auto image_pointer_size =
1850 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1851 if (method->IsNative()) {
1852 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1853 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1854 bool first_occurrence;
1855 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1856 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1857 state->ComputeOatSize(quick_oat_code, &first_occurrence);
1858 if (first_occurrence) {
1859 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1860 }
1861 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1862 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
1863 }
1864 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1865 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1866 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1867 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1868 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1869 } else {
1870 const DexFile::CodeItem* code_item = method->GetCodeItem();
1871 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1872 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1873
1874 bool first_occurrence;
1875 size_t gc_map_bytes = state->ComputeOatSize(
1876 method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1877 if (first_occurrence) {
1878 state->stats_.gc_map_bytes += gc_map_bytes;
1879 }
1880
1881 size_t pc_mapping_table_bytes = state->ComputeOatSize(
1882 method->GetMappingTable(image_pointer_size), &first_occurrence);
1883 if (first_occurrence) {
1884 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1885 }
1886
1887 size_t vmap_table_bytes = state->ComputeOatSize(
1888 method->GetVmapTable(image_pointer_size), &first_occurrence);
1889 if (first_occurrence) {
1890 state->stats_.vmap_table_bytes += vmap_table_bytes;
1891 }
1892
1893 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1894 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1895 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1896 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1897 if (first_occurrence) {
1898 state->stats_.managed_code_bytes += quick_oat_code_size;
1899 if (method->IsConstructor()) {
1900 if (method->IsStatic()) {
1901 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
1902 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1903 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
1904 }
1905 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1906 state->stats_.large_method_code_bytes += quick_oat_code_size;
1907 }
1908 }
1909 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
1910
1911 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
1912 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
1913 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
1914
1915 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1916 vmap_table_bytes + quick_oat_code_size + ArtMethod::ObjectSize(image_pointer_size);
1917
1918 double expansion =
1919 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
1920 state->stats_.ComputeOutliers(total_size, expansion, method);
1921 }
1922 }
1923
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001924 std::set<const void*> already_seen_;
1925 // Compute the size of the given data within the oat file and whether this is the first time
1926 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001927 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001928 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001929 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001930 already_seen_.insert(oat_data);
1931 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001932 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001933 }
1934 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001935 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001936
1937 public:
1938 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001939 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001940 size_t file_bytes;
1941
1942 size_t header_bytes;
1943 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001944 size_t art_field_bytes;
1945 size_t art_method_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001946 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001947 size_t alignment_bytes;
1948
1949 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001950 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001951 size_t managed_to_native_code_bytes;
1952 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001953 size_t class_initializer_code_bytes;
1954 size_t large_initializer_code_bytes;
1955 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001956
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001957 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001958 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001959 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001960
1961 size_t dex_instruction_bytes;
1962
Mathieu Chartiere401d142015-04-22 13:56:20 -07001963 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001964 std::vector<size_t> method_outlier_size;
1965 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001966 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001967
1968 explicit Stats()
1969 : oat_file_bytes(0),
1970 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001971 header_bytes(0),
1972 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001973 art_field_bytes(0),
1974 art_method_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07001975 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001976 alignment_bytes(0),
1977 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001978 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001979 managed_to_native_code_bytes(0),
1980 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07001981 class_initializer_code_bytes(0),
1982 large_initializer_code_bytes(0),
1983 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001984 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001985 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001986 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001987 dex_instruction_bytes(0) {}
1988
Elliott Hughesa0e18062012-04-13 15:59:59 -07001989 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001990 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07001991 size_t bytes;
1992 size_t count;
1993 };
1994 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
1995 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001996
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001997 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001998 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
1999 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002000 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002001 it->second.count += 1;
2002 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002003 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002004 }
2005 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002006
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002007 double PercentOfOatBytes(size_t size) {
2008 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2009 }
2010
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002011 double PercentOfFileBytes(size_t size) {
2012 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2013 }
2014
2015 double PercentOfObjectBytes(size_t size) {
2016 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2017 }
2018
Mathieu Chartiere401d142015-04-22 13:56:20 -07002019 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002020 method_outlier_size.push_back(total_size);
2021 method_outlier_expansion.push_back(expansion);
2022 method_outlier.push_back(method);
2023 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002024
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002025 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002026 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002027 size_t sum_of_sizes = 0;
2028 size_t sum_of_sizes_squared = 0;
2029 size_t sum_of_expansion = 0;
2030 size_t sum_of_expansion_squared = 0;
2031 size_t n = method_outlier_size.size();
2032 for (size_t i = 0; i < n; i++) {
2033 size_t cur_size = method_outlier_size[i];
2034 sum_of_sizes += cur_size;
2035 sum_of_sizes_squared += cur_size * cur_size;
2036 double cur_expansion = method_outlier_expansion[i];
2037 sum_of_expansion += cur_expansion;
2038 sum_of_expansion_squared += cur_expansion * cur_expansion;
2039 }
2040 size_t size_mean = sum_of_sizes / n;
2041 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2042 double expansion_mean = sum_of_expansion / n;
2043 double expansion_variance =
2044 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2045
2046 // Dump methods whose size is a certain number of standard deviations from the mean
2047 size_t dumped_values = 0;
2048 size_t skipped_values = 0;
2049 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2050 size_t cur_size_variance = i * i * size_variance;
2051 bool first = true;
2052 for (size_t j = 0; j < n; j++) {
2053 size_t cur_size = method_outlier_size[j];
2054 if (cur_size > size_mean) {
2055 size_t cur_var = cur_size - size_mean;
2056 cur_var = cur_var * cur_var;
2057 if (cur_var > cur_size_variance) {
2058 if (dumped_values > 20) {
2059 if (i == 1) {
2060 skipped_values++;
2061 } else {
2062 i = 2; // jump to counting for 1 standard deviation
2063 break;
2064 }
2065 } else {
2066 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002067 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002068 first = false;
2069 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002070 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002071 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002072 method_outlier_size[j] = 0; // don't consider this method again
2073 dumped_values++;
2074 }
2075 }
2076 }
2077 }
2078 }
2079 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002080 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002081 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002082 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002083 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002084
2085 // Dump methods whose expansion is a certain number of standard deviations from the mean
2086 dumped_values = 0;
2087 skipped_values = 0;
2088 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2089 double cur_expansion_variance = i * i * expansion_variance;
2090 bool first = true;
2091 for (size_t j = 0; j < n; j++) {
2092 double cur_expansion = method_outlier_expansion[j];
2093 if (cur_expansion > expansion_mean) {
2094 size_t cur_var = cur_expansion - expansion_mean;
2095 cur_var = cur_var * cur_var;
2096 if (cur_var > cur_expansion_variance) {
2097 if (dumped_values > 20) {
2098 if (i == 1) {
2099 skipped_values++;
2100 } else {
2101 i = 2; // jump to counting for 1 standard deviation
2102 break;
2103 }
2104 } else {
2105 if (first) {
2106 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002107 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002108 first = false;
2109 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002110 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002111 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002112 method_outlier_expansion[j] = 0.0; // don't consider this method again
2113 dumped_values++;
2114 }
2115 }
2116 }
2117 }
2118 }
2119 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002120 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002121 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002122 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002123 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002124 }
2125
Ian Rogersb726dcb2012-09-05 08:57:23 -07002126 void Dump(std::ostream& os) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002127 {
2128 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2129 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
2130 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
2131 std::ostream indent_os(&indent_filter);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002132 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2133 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2134 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2135 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2136 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2137 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002138 header_bytes, PercentOfFileBytes(header_bytes),
2139 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002140 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2141 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002142 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002143 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2144 << std::flush;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002145 CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2146 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002147 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002148
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002150 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002151 for (const auto& sizes_and_count : sizes_and_counts) {
2152 const std::string& descriptor(sizes_and_count.first);
2153 double average = static_cast<double>(sizes_and_count.second.bytes) /
2154 static_cast<double>(sizes_and_count.second.count);
2155 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002157 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002158 descriptor.c_str(), sizes_and_count.second.bytes,
2159 sizes_and_count.second.count, average, percent);
2160 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002161 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002162 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002163 CHECK_EQ(object_bytes, object_bytes_total);
2164
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002165 os << StringPrintf("oat_file_bytes = %8zd\n"
2166 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2167 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2168 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2169 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2170 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2171 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002172 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002173 managed_code_bytes,
2174 PercentOfOatBytes(managed_code_bytes),
2175 managed_to_native_code_bytes,
2176 PercentOfOatBytes(managed_to_native_code_bytes),
2177 native_to_managed_code_bytes,
2178 PercentOfOatBytes(native_to_managed_code_bytes),
2179 class_initializer_code_bytes,
2180 PercentOfOatBytes(class_initializer_code_bytes),
2181 large_initializer_code_bytes,
2182 PercentOfOatBytes(large_initializer_code_bytes),
2183 large_method_code_bytes,
2184 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002185 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002186 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002187 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002188 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2189 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002190 }
2191
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2193 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2194 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002195 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2196 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2197 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002198 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002199
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002200 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2201 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002202 static_cast<double>(managed_code_bytes) /
2203 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002204 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002205 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002206 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002207
2208 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002209 }
2210 } stats_;
2211
2212 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002213 enum {
2214 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2215 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2216 kLargeConstructorDexBytes = 4000,
2217 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2218 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2219 kLargeMethodDexBytes = 16000
2220 };
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002221 std::ostream* os_;
Ian Rogers1d54e732013-05-02 21:10:01 -07002222 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002223 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002224 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002225 OatDumperOptions* oat_dumper_options_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002226 std::set<mirror::Object*> dex_cache_arrays_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002227
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002228 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002229};
2230
Andreas Gampe00b25f32014-09-17 21:49:05 -07002231static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2232 std::ostream* os) {
2233 // Dumping the image, no explicit class loader.
2234 NullHandle<mirror::ClassLoader> null_class_loader;
2235 options->class_loader_ = &null_class_loader;
2236
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002237 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002238 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002239 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002240 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002241 const ImageHeader& image_header = image_space->GetImageHeader();
2242 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002243 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002244 return EXIT_FAILURE;
2245 }
Igor Murashkin37743352014-11-13 14:38:00 -08002246
Andreas Gampe00b25f32014-09-17 21:49:05 -07002247 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002248
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002249 bool success = image_dumper.Dump();
2250 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002251}
2252
Andreas Gampe00b25f32014-09-17 21:49:05 -07002253static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2254 std::ostream* os) {
2255 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2256
2257 Thread* self = Thread::Current();
2258 CHECK(self != nullptr);
2259 // Need well-known-classes.
2260 WellKnownClasses::Init(self->GetJniEnv());
2261
2262 // Need to register dex files to get a working dex cache.
2263 ScopedObjectAccess soa(self);
2264 ClassLinker* class_linker = runtime->GetClassLinker();
2265 class_linker->RegisterOatFile(oat_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002266 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002267 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2268 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002269 std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002270 CHECK(dex_file != nullptr) << error_msg;
2271 class_linker->RegisterDexFile(*dex_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002272 dex_files.push_back(std::move(dex_file));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002273 }
2274
2275 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002276 // Fake that we're a compiler.
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002277 std::vector<const DexFile*> class_path;
2278 for (auto& dex_file : dex_files) {
2279 class_path.push_back(dex_file.get());
2280 }
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07002281 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002282
2283 // Use the class loader while dumping.
2284 StackHandleScope<1> scope(self);
2285 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2286 soa.Decode<mirror::ClassLoader*>(class_loader));
2287 options->class_loader_ = &loader_handle;
2288
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002289 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002290 bool success = oat_dumper.Dump(*os);
2291 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2292}
2293
2294static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002295 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002296 // No image = no class loader.
2297 NullHandle<mirror::ClassLoader> null_class_loader;
2298 options->class_loader_ = &null_class_loader;
2299
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002300 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002301 bool success = oat_dumper.Dump(*os);
2302 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2303}
2304
2305static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2306 std::ostream* os) {
2307 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002308 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Richard Uhlere5fed032015-03-18 08:21:11 -07002309 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002310 if (oat_file == nullptr) {
2311 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2312 return EXIT_FAILURE;
2313 }
2314
2315 if (runtime != nullptr) {
2316 return DumpOatWithRuntime(runtime, oat_file, options, os);
2317 } else {
2318 return DumpOatWithoutRuntime(oat_file, options, os);
2319 }
2320}
2321
2322static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2323 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002324 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Richard Uhlere5fed032015-03-18 08:21:11 -07002325 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002326 if (oat_file == nullptr) {
2327 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2328 return EXIT_FAILURE;
2329 }
2330
2331 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002332 if (!oat_symbolizer.Symbolize()) {
2333 fprintf(stderr, "Failed to symbolize\n");
2334 return EXIT_FAILURE;
2335 }
2336
2337 return EXIT_SUCCESS;
2338}
2339
Igor Murashkin37743352014-11-13 14:38:00 -08002340struct OatdumpArgs : public CmdlineArgs {
2341 protected:
2342 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002343
Igor Murashkin37743352014-11-13 14:38:00 -08002344 virtual ParseStatus ParseCustom(const StringPiece& option,
2345 std::string* error_msg) OVERRIDE {
2346 {
2347 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2348 if (base_parse != kParseUnknownArgument) {
2349 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002350 }
2351 }
2352
Igor Murashkin37743352014-11-13 14:38:00 -08002353 if (option.starts_with("--oat-file=")) {
2354 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2355 } else if (option.starts_with("--image=")) {
2356 image_location_ = option.substr(strlen("--image=")).data();
2357 } else if (option =="--dump:raw_mapping_table") {
2358 dump_raw_mapping_table_ = true;
2359 } else if (option == "--dump:raw_gc_map") {
2360 dump_raw_gc_map_ = true;
2361 } else if (option == "--no-dump:vmap") {
2362 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002363 } else if (option =="--dump:code_info_stack_maps") {
2364 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002365 } else if (option == "--no-disassemble") {
2366 disassemble_code_ = false;
2367 } else if (option.starts_with("--symbolize=")) {
2368 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2369 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002370 } else if (option.starts_with("--class-filter=")) {
2371 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002372 } else if (option.starts_with("--method-filter=")) {
2373 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002374 } else if (option.starts_with("--list-classes")) {
2375 list_classes_ = true;
2376 } else if (option.starts_with("--list-methods")) {
2377 list_methods_ = true;
2378 } else if (option.starts_with("--export-dex-to=")) {
2379 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2380 } else if (option.starts_with("--addr2instr=")) {
2381 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2382 *error_msg = "Address conversion failed";
2383 return kParseError;
2384 }
Igor Murashkin37743352014-11-13 14:38:00 -08002385 } else {
2386 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002387 }
2388
Igor Murashkin37743352014-11-13 14:38:00 -08002389 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002390 }
2391
Igor Murashkin37743352014-11-13 14:38:00 -08002392 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2393 // Infer boot image location from the image location if possible.
2394 if (boot_image_location_ == nullptr) {
2395 boot_image_location_ = image_location_;
2396 }
2397
2398 // Perform the parent checks.
2399 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2400 if (parent_checks != kParseOk) {
2401 return parent_checks;
2402 }
2403
2404 // Perform our own checks.
2405 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2406 *error_msg = "Either --image or --oat-file must be specified";
2407 return kParseError;
2408 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2409 *error_msg = "Either --image or --oat-file must be specified but not both";
2410 return kParseError;
2411 }
2412
2413 return kParseOk;
2414 }
2415
2416 virtual std::string GetUsage() const {
2417 std::string usage;
2418
2419 usage +=
2420 "Usage: oatdump [options] ...\n"
2421 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2422 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2423 "\n"
2424 // Either oat-file or image is required.
2425 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2426 " Example: --oat-file=/system/framework/boot.oat\n"
2427 "\n"
2428 " --image=<file.art>: specifies an input image location.\n"
2429 " Example: --image=/system/framework/boot.art\n"
2430 "\n";
2431
2432 usage += Base::GetUsage();
2433
2434 usage += // Optional.
2435 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2436 " Example: --dump:raw_mapping_table\n"
2437 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002438 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002439 " Example: --dump:raw_gc_map\n"
2440 "\n"
2441 " --no-dump:vmap may be used to disable vmap dumping.\n"
2442 " Example: --no-dump:vmap\n"
2443 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002444 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2445 " Example: --dump:code_info_stack_maps\n"
2446 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002447 " --no-disassemble may be used to disable disassembly.\n"
2448 " Example: --no-disassemble\n"
2449 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002450 " --list-classes may be used to list target file classes (can be used with filters).\n"
2451 " Example: --list-classes\n"
2452 " Example: --list-classes --class-filter=com.example.foo\n"
2453 "\n"
2454 " --list-methods may be used to list target file methods (can be used with filters).\n"
2455 " Example: --list-methods\n"
2456 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2457 "\n"
2458 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2459 " Example: --symbolize=/system/framework/boot.oat\n"
2460 "\n"
2461 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2462 " Example: --class-filter=com.example.foo\n"
2463 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002464 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2465 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002466 "\n"
2467 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2468 " Example: --export-dex-to=/data/local/tmp\n"
2469 "\n"
2470 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2471 " address (e.g. PC from crash dump)\n"
2472 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002473 "\n";
2474
2475 return usage;
2476 }
2477
2478 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002479 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002480 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002481 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002482 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002483 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002484 bool dump_raw_mapping_table_ = false;
2485 bool dump_raw_gc_map_ = false;
2486 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002487 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002488 bool disassemble_code_ = true;
2489 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002490 bool list_classes_ = false;
2491 bool list_methods_ = false;
2492 uint32_t addr2instr_ = 0;
2493 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002494};
2495
Igor Murashkin37743352014-11-13 14:38:00 -08002496struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2497 virtual bool NeedsRuntime() OVERRIDE {
2498 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002499
Igor Murashkin37743352014-11-13 14:38:00 -08002500 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2501 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2502
2503 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2504 args_->dump_raw_mapping_table_,
2505 args_->dump_raw_gc_map_,
2506 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002507 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002508 args_->disassemble_code_,
2509 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002510 args_->class_filter_,
2511 args_->method_filter_,
2512 args_->list_classes_,
2513 args_->list_methods_,
2514 args_->export_dex_location_,
2515 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002516
2517 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2518 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002519 }
2520
Igor Murashkin37743352014-11-13 14:38:00 -08002521 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2522 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002523 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002524
Mathieu Chartierd424d082014-10-15 10:31:46 -07002525 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002526
Andreas Gampec24f3992014-12-17 20:40:11 -08002527 if (args_->symbolize_) {
2528 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2529 } else {
2530 return DumpOat(nullptr,
2531 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002532 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002533 args_->os_) == EXIT_SUCCESS;
2534 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002535 }
2536
Igor Murashkin37743352014-11-13 14:38:00 -08002537 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2538 CHECK(args_ != nullptr);
2539
2540 if (args_->oat_filename_ != nullptr) {
2541 return DumpOat(runtime,
2542 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002543 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002544 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002545 }
Igor Murashkin37743352014-11-13 14:38:00 -08002546
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002547 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002548 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002549 }
2550
Igor Murashkin37743352014-11-13 14:38:00 -08002551 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2552};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002553
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002554} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002555
2556int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002557 art::OatdumpMain main;
2558 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002559}