blob: e2486041af9eb629e59116ad84298a82dadbd5aa [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"
Vladimir Marko05792b92015-08-03 11:56:49 +010047#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/object-inl.h"
49#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010051#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080052#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070053#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070054#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070056#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080057#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080058#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080059#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070060#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070061#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070062
Igor Murashkin37743352014-11-13 14:38:00 -080063#include <sys/stat.h>
64#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070065
Igor Murashkin37743352014-11-13 14:38:00 -080066namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070067
Mathieu Chartiere401d142015-04-22 13:56:20 -070068const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080069 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070070 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070071 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070072 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070073 "kRefsOnlySaveMethod",
74 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070075};
76
77const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070078 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070079 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070080};
81
Mathieu Chartierac8f4392015-08-27 13:54:20 -070082// Map is so that we don't allocate multiple dex files for the same OatDexFile.
83static std::map<const OatFile::OatDexFile*,
84 std::unique_ptr<const DexFile>> opened_dex_files;
85
86const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
87 DCHECK(oat_dex_file != nullptr);
88 auto it = opened_dex_files.find(oat_dex_file);
89 if (it != opened_dex_files.end()) {
90 return it->second.get();
91 }
92 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
93 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
94 return ret;
95}
96
David Srbeckybc90fd02015-04-22 19:40:27 +010097class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070098 public:
David Srbeckybc90fd02015-04-22 19:40:27 +010099 class RodataWriter FINAL : public CodeOutput {
100 public:
101 explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700102
David Srbeckybc90fd02015-04-22 19:40:27 +0100103 bool Write(OutputStream* out) OVERRIDE {
104 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
105 return out->WriteFully(oat_file_->Begin(), rodata_size);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700106 }
107
David Srbeckybc90fd02015-04-22 19:40:27 +0100108 private:
109 const OatFile* oat_file_;
110 };
111
112 class TextWriter FINAL : public CodeOutput {
113 public:
114 explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
115
116 bool Write(OutputStream* out) OVERRIDE {
117 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
118 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
119 return out->WriteFully(text_begin, oat_file_->End() - text_begin);
120 }
121
122 private:
123 const OatFile* oat_file_;
124 };
125
Roland Levillain3887c462015-08-12 18:15:42 +0100126 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100127 oat_file_(oat_file), builder_(nullptr),
128 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700129 }
130
131 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
132 uint32_t,
133 const OatFile::OatMethod&,
134 const DexFile&,
135 uint32_t,
136 const DexFile::CodeItem*,
137 uint32_t);
138
139 bool Symbolize() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100140 Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
141 uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
142 uint32_t text_size = size - rodata_size;
143 uint32_t bss_size = oat_file_->BssSize();
144 RodataWriter rodata_writer(oat_file_);
145 TextWriter text_writer(oat_file_);
146 builder_.reset(new ElfBuilder<ElfTypes32>(
147 oat_file_->GetOatHeader().GetInstructionSet(),
148 rodata_size, &rodata_writer,
149 text_size, &text_writer,
150 bss_size));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700151
152 Walk(&art::OatSymbolizer::RegisterForDedup);
153
154 NormalizeState();
155
156 Walk(&art::OatSymbolizer::AddSymbol);
157
David Srbeckybc90fd02015-04-22 19:40:27 +0100158 File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
159 bool result = builder_->Write(elf_output);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700160
Andreas Gampe4303ba92014-11-06 01:00:46 -0800161 // Ignore I/O errors.
David Srbeckybc90fd02015-04-22 19:40:27 +0100162 UNUSED(elf_output->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700163
164 return result;
165 }
166
167 void Walk(Callback callback) {
168 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
169 for (size_t i = 0; i < oat_dex_files.size(); i++) {
170 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700171 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700172 WalkOatDexFile(oat_dex_file, callback);
173 }
174 }
175
176 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
177 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700178 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
179 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700180 return;
181 }
182 for (size_t class_def_index = 0;
183 class_def_index < dex_file->NumClassDefs();
184 class_def_index++) {
185 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
186 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
187 OatClassType type = oat_class.GetType();
188 switch (type) {
189 case kOatClassAllCompiled:
190 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700191 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700192 break;
193
194 case kOatClassNoneCompiled:
195 case kOatClassMax:
196 // Ignore.
197 break;
198 }
199 }
200 }
201
202 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
203 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700204 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700205 if (class_data == nullptr) { // empty class such as a marker interface?
206 return;
207 }
208 // Note: even if this is an interface or a native class, we still have to walk it, as there
209 // might be a static initializer.
210 ClassDataItemIterator it(dex_file, class_data);
211 SkipAllFields(&it);
212 uint32_t class_method_idx = 0;
213 while (it.HasNextDirectMethod()) {
214 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
215 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700216 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700217 class_method_idx++;
218 it.Next();
219 }
220 while (it.HasNextVirtualMethod()) {
221 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
222 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700223 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700224 class_method_idx++;
225 it.Next();
226 }
227 DCHECK(!it.HasNext());
228 }
229
230 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
231 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
232 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
233 uint32_t method_access_flags, Callback callback) {
234 if ((method_access_flags & kAccAbstract) != 0) {
235 // Abstract method, no code.
236 return;
237 }
238 if (oat_method.GetCodeOffset() == 0) {
239 // No code.
240 return;
241 }
242
243 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
244 method_access_flags);
245 }
246
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700247 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
248 uint32_t class_method_index ATTRIBUTE_UNUSED,
249 const OatFile::OatMethod& oat_method,
250 const DexFile& dex_file ATTRIBUTE_UNUSED,
251 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
252 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
253 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700254 state_[oat_method.GetCodeOffset()]++;
255 }
256
257 void NormalizeState() {
258 for (auto& x : state_) {
259 if (x.second == 1) {
260 state_[x.first] = 0;
261 }
262 }
263 }
264
265 enum class DedupState { // private
266 kNotDeduplicated,
267 kDeduplicatedFirst,
268 kDeduplicatedOther
269 };
270 DedupState IsDuplicated(uint32_t offset) {
271 if (state_[offset] == 0) {
272 return DedupState::kNotDeduplicated;
273 }
274 if (state_[offset] == 1) {
275 return DedupState::kDeduplicatedOther;
276 }
277 state_[offset] = 1;
278 return DedupState::kDeduplicatedFirst;
279 }
280
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700281 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
282 uint32_t class_method_index ATTRIBUTE_UNUSED,
283 const OatFile::OatMethod& oat_method,
284 const DexFile& dex_file,
285 uint32_t dex_method_idx,
286 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
287 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700288 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
289 if (dedup != DedupState::kDeduplicatedOther) {
290 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
291
292 if (dedup == DedupState::kDeduplicatedFirst) {
293 pretty_name = "[Dedup]" + pretty_name;
294 }
295
David Srbeckybc90fd02015-04-22 19:40:27 +0100296 auto* symtab = builder_->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700297
David Srbeckybc90fd02015-04-22 19:40:27 +0100298 symtab->AddSymbol(pretty_name, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700299 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
300 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700301 }
302 }
303
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700304 private:
305 static void SkipAllFields(ClassDataItemIterator* it) {
306 while (it->HasNextStaticField()) {
307 it->Next();
308 }
309 while (it->HasNextInstanceField()) {
310 it->Next();
311 }
312 }
313
314 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100315 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700316 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700317 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700318};
319
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700320class OatDumperOptions {
321 public:
322 OatDumperOptions(bool dump_raw_mapping_table,
323 bool dump_raw_gc_map,
324 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100325 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700326 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700327 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800328 const char* class_filter,
329 const char* method_filter,
330 bool list_classes,
331 bool list_methods,
332 const char* export_dex_location,
333 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700334 : dump_raw_mapping_table_(dump_raw_mapping_table),
335 dump_raw_gc_map_(dump_raw_gc_map),
336 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100337 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700338 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700339 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800340 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000341 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800342 list_classes_(list_classes),
343 list_methods_(list_methods),
344 export_dex_location_(export_dex_location),
345 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800346 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700347
348 const bool dump_raw_mapping_table_;
349 const bool dump_raw_gc_map_;
350 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100351 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352 const bool disassemble_code_;
353 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800354 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000355 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 const bool list_classes_;
357 const bool list_methods_;
358 const char* const export_dex_location_;
359 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700360 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700361};
362
Elliott Hughese3c845c2012-02-28 17:23:01 -0800363class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700364 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100365 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000366 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800367 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700368 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800369 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800370 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
371 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800372 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000373 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100374 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800375 CHECK(options_.class_loader_ != nullptr);
376 CHECK(options_.class_filter_ != nullptr);
377 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800378 AddAllOffsets();
379 }
380
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700381 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700382 delete disassembler_;
383 }
384
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800385 InstructionSet GetInstructionSet() {
386 return instruction_set_;
387 }
388
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700389 bool Dump(std::ostream& os) {
390 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800391 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700392
393 os << "MAGIC:\n";
394 os << oat_header.GetMagic() << "\n\n";
395
396 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800397 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700398
Elliott Hughesa72ec822012-03-05 17:12:22 -0800399 os << "INSTRUCTION SET:\n";
400 os << oat_header.GetInstructionSet() << "\n\n";
401
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700402 {
403 std::unique_ptr<const InstructionSetFeatures> features(
404 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
405 oat_header.GetInstructionSetFeaturesBitmap()));
406 os << "INSTRUCTION SET FEATURES:\n";
407 os << features->GetFeatureString() << "\n\n";
408 }
Dave Allison70202782013-10-22 17:52:19 -0700409
Brian Carlstromaded5f72011-10-07 17:15:04 -0700410 os << "DEX FILE COUNT:\n";
411 os << oat_header.GetDexFileCount() << "\n\n";
412
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800413#define DUMP_OAT_HEADER_OFFSET(label, offset) \
414 os << label " OFFSET:\n"; \
415 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800416 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800417 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
418 } \
419 os << StringPrintf("\n\n");
420
421 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
422 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
423 GetInterpreterToInterpreterBridgeOffset);
424 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
425 GetInterpreterToCompiledCodeBridgeOffset);
426 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
427 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800428 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
429 GetQuickGenericJniTrampolineOffset);
430 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
431 GetQuickImtConflictTrampolineOffset);
432 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
433 GetQuickResolutionTrampolineOffset);
434 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
435 GetQuickToInterpreterBridgeOffset);
436#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700437
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700438 os << "IMAGE PATCH DELTA:\n";
439 os << StringPrintf("%d (0x%08x)\n\n",
440 oat_header.GetImagePatchDelta(),
441 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700442
Brian Carlstrom28db0122012-10-18 16:20:41 -0700443 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
444 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
445
446 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800447 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700448
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700449 // Print the key-value store.
450 {
451 os << "KEY VALUE STORE:\n";
452 size_t index = 0;
453 const char* key;
454 const char* value;
455 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
456 os << key << " = " << value << "\n";
457 index++;
458 }
459 os << "\n";
460 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700461
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800462 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700463 os << "BEGIN:\n";
464 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700465
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700466 os << "END:\n";
467 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
468 }
469
470 os << "SIZE:\n";
471 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700472
473 os << std::flush;
474
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800475 // If set, adjust relative address to be searched
476 if (options_.addr2instr_ != 0) {
477 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
478 os << "SEARCH ADDRESS (executable offset + input):\n";
479 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
480 }
481
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800482 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
483 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700484 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800485
486 // If file export selected skip file analysis
487 if (options_.export_dex_location_) {
488 if (!ExportDexFile(os, *oat_dex_file)) {
489 success = false;
490 }
491 } else {
492 if (!DumpOatDexFile(os, *oat_dex_file)) {
493 success = false;
494 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700495 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700496 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700497 os << std::flush;
498 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700499 }
500
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800501 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700502 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
503 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800504 return 0; // Address not in oat file
505 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800506 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
507 reinterpret_cast<uintptr_t>(oat_file_.Begin());
508 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800509 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800510 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800511 return end_offset - begin_offset;
512 }
513
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800514 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700515 return oat_file_.GetOatHeader().GetInstructionSet();
516 }
517
Mathieu Chartier90443472015-07-16 20:32:27 -0700518 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800519 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
520 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700521 CHECK(oat_dex_file != nullptr);
522 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700523 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
524 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700525 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
526 << "': " << error_msg;
527 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800528 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700529 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800530 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700531 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700532 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100533 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800534 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100535 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800536 }
537 }
538 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700539 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800540 }
541
Brian Carlstromaded5f72011-10-07 17:15:04 -0700542 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800543 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800544 // We don't know the length of the code for each method, but we need to know where to stop
545 // when disassembling. What we do know is that a region of code will be followed by some other
546 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
547 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800548 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
549 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700550 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700551 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700552 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
553 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700554 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
555 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800556 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800557 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800558 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800559 for (size_t class_def_index = 0;
560 class_def_index < dex_file->NumClassDefs();
561 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800562 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100563 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700564 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700565 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800566 ClassDataItemIterator it(*dex_file, class_data);
567 SkipAllFields(it);
568 uint32_t class_method_index = 0;
569 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100570 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800571 it.Next();
572 }
573 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100574 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800575 it.Next();
576 }
577 }
578 }
579 }
580
581 // If the last thing in the file is code for a method, there won't be an offset for the "next"
582 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
583 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800584 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800585 }
586
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700587 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
588 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
589 }
590
Elliott Hughese3c845c2012-02-28 17:23:01 -0800591 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800592 uint32_t code_offset = oat_method.GetCodeOffset();
593 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
594 code_offset &= ~0x1;
595 }
596 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800597 offsets_.insert(oat_method.GetMappingTableOffset());
598 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800599 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800600 }
601
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700602 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
603 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800604 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700605 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800606 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800607 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700608
609 // Create the verifier early.
610
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700611 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700612 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
613 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700614 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700615 os << std::flush;
616 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700617 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100618
619 VariableIndentationOutputStream vios(&os);
620 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800621 for (size_t class_def_index = 0;
622 class_def_index < dex_file->NumClassDefs();
623 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700624 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
625 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800626
627 // TODO: Support regex
628 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
629 continue;
630 }
631
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700632 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100633 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700634 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
635 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100636 << " (" << oat_class.GetStatus() << ")"
637 << " (" << oat_class.GetType() << ")\n";
638 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800639 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700640 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700641 success = false;
642 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800643 if (stop_analysis) {
644 os << std::flush;
645 return success;
646 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700647 }
648
649 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700650 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700651 }
652
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800653 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
654 std::string error_msg;
655 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
656
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700657 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800658 if (dex_file == nullptr) {
659 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
660 return false;
661 }
662 size_t fsize = oat_dex_file.FileSize();
663
664 // Some quick checks just in case
665 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
666 os << "Invalid dex file\n";
667 return false;
668 }
669
670 // Verify output directory exists
671 if (!OS::DirectoryExists(options_.export_dex_location_)) {
672 // TODO: Extend OS::DirectoryExists if symlink support is required
673 os << options_.export_dex_location_ << " output directory not found or symlink\n";
674 return false;
675 }
676
677 // Beautify path names
678 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
679 return false;
680 }
681
682 std::string dex_orig_name;
683 size_t dex_orig_pos = dex_file_location.rfind('/');
684 if (dex_orig_pos == std::string::npos)
685 dex_orig_name = dex_file_location;
686 else
687 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
688
689 // A more elegant approach to efficiently name user installed apps is welcome
690 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
691 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
692 size_t apk_orig_pos = dex_file_location.rfind('/');
693 if (apk_orig_pos != std::string::npos) {
694 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
695 }
696 }
697
698 std::string out_dex_path(options_.export_dex_location_);
699 if (out_dex_path.back() != '/') {
700 out_dex_path.append("/");
701 }
702 out_dex_path.append(dex_orig_name);
703 out_dex_path.append("_export.dex");
704 if (out_dex_path.length() > PATH_MAX) {
705 return false;
706 }
707
708 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
709 if (file.get() == nullptr) {
710 os << "Failed to open output dex file " << out_dex_path;
711 return false;
712 }
713
714 if (!file->WriteFully(dex_file->Begin(), fsize)) {
715 os << "Failed to write dex file";
716 file->Erase();
717 return false;
718 }
719
720 if (file->FlushCloseOrErase() != 0) {
721 os << "Flush and close failed";
722 return false;
723 }
724
725 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
726 os << std::flush;
727
728 return true;
729 }
730
Elliott Hughese3c845c2012-02-28 17:23:01 -0800731 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700732 while (it.HasNextStaticField()) {
733 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700734 }
Ian Rogers0571d352011-11-03 19:51:38 -0700735 while (it.HasNextInstanceField()) {
736 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700737 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800738 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700739
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100740 bool DumpOatClass(VariableIndentationOutputStream* vios,
741 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800742 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700743 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800744 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700745 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700746 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100747 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700748 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800749 }
750 ClassDataItemIterator it(dex_file, class_data);
751 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700752 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700753 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100754 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700755 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800756 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700757 success = false;
758 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800759 if (addr_found) {
760 *stop_analysis = true;
761 return success;
762 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700763 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700764 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700765 }
Ian Rogers0571d352011-11-03 19:51:38 -0700766 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100767 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700768 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800769 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700770 success = false;
771 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800772 if (addr_found) {
773 *stop_analysis = true;
774 return success;
775 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700776 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700777 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700778 }
Ian Rogers0571d352011-11-03 19:51:38 -0700779 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100780 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700781 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700782 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800783
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700784 static constexpr uint32_t kPrologueBytes = 16;
785
786 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
787 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
788
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100789 bool DumpOatMethod(VariableIndentationOutputStream* vios,
790 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700791 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700792 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800793 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800794 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700795 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800796
797 // TODO: Support regex
798 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
799 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000800 return success;
801 }
802
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800803 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100804 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
805 class_method_index, pretty_method.c_str(),
806 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800807 if (options_.list_methods_) return success;
808
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800809 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
810 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
811 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
812 uint32_t code_offset = oat_method.GetCodeOffset();
813 uint32_t code_size = oat_method.GetQuickCodeSize();
814 if (resolved_addr2instr_ != 0) {
815 if (resolved_addr2instr_ > code_offset + code_size) {
816 return success;
817 } else {
818 *addr_found = true; // stop analyzing file at next iteration
819 }
820 }
821
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100822 // Everything below is indented at least once.
823 ScopedIndentation indent1(vios);
824
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800825 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100826 vios->Stream() << "DEX CODE:\n";
827 ScopedIndentation indent2(vios);
828 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700829 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700830
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700831 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700832 std::unique_ptr<verifier::MethodVerifier> verifier;
833 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700834 // We need to have the handle scope stay live until after the verifier since the verifier has
835 // a handle to the dex cache from hs.
836 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100837 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
838 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700839 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100840 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700841 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700842 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800843 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100844 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800845 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100846 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100847 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100848 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700849 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100850 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700851 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
852 oat_method_offsets_offset, oat_file_.Size());
853 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100854 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700855 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800856 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700857
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100858 ScopedIndentation indent2(vios);
859 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700860 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
861 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100862 vios->Stream() << StringPrintf("WARNING: "
863 "code offset 0x%08x is past end of file 0x%08zx.\n",
864 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700865 success = false;
866 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100867 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700868
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100869 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800870 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100871 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700872 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800873 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100874 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700875 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100876 vios->Stream() << StringPrintf("WARNING: "
877 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
878 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700879 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800880 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100881 ScopedIndentation indent3(vios);
882 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800883 }
884 }
885 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100886 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700887 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
888 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700889
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800890 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100891 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700892 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100893 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700894 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100895 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700896 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
897 method_header_offset, oat_file_.Size());
898 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100899 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700900 return false;
901 }
902
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100903 ScopedIndentation indent2(vios);
904 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800905 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100906 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700907 }
908 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100911 vios->Stream() << StringPrintf("WARNING: "
912 "mapping table offset 0x%08x is past end of file 0x%08zx. "
913 "mapping table offset was loaded from offset 0x%08x.\n",
914 mapping_table_offset, oat_file_.Size(),
915 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700916 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800917 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100918 ScopedIndentation indent3(vios);
919 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700920 }
921
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100922 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800923 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100924 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700925 }
926 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100927 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700928 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100929 vios->Stream() << StringPrintf("WARNING: "
930 "vmap table offset 0x%08x is past end of file 0x%08zx. "
931 "vmap table offset was loaded from offset 0x%08x.\n",
932 vmap_table_offset, oat_file_.Size(),
933 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700934 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800935 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100936 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700937 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800938 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700939 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100940 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700941
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100942 ScopedIndentation indent2(vios);
943 vios->Stream()
944 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
945 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
946 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
947 vios->Stream() << "\n";
948 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
949 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
950 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700951 }
952 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100953 // Based on spill masks from QuickMethodFrameInfo so placed
954 // after it is dumped, but useful for understanding quick
955 // code, so dumped here.
956 ScopedIndentation indent2(vios);
957 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700958 }
959 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100960 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
962 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100963 ScopedIndentation indent2(vios);
964 vios->Stream() << StringPrintf("WARNING: "
965 "code size offset 0x%08x is past end of file 0x%08zx.",
966 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700967 success = false;
968 } else {
969 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700970 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
971 uint64_t aligned_code_end = aligned_code_begin + code_size;
972
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800973 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100974 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700975 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100976 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
977 code_offset,
978 code_size_offset,
979 code_size,
980 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700981
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100982 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700983 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100984 vios->Stream() << StringPrintf("WARNING: "
985 "start of code at 0x%08x is past end of file 0x%08zx.",
986 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700987 success = false;
988 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100989 vios->Stream() << StringPrintf(
990 "WARNING: "
991 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
992 "code size is 0x%08x loaded from offset 0x%08x.\n",
993 aligned_code_end, oat_file_.Size(),
994 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700995 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800996 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700997 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100998 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700999 }
1000 }
1001 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001002 vios->Stream() << StringPrintf(
1003 "WARNING: "
1004 "code size %d is bigger than max expected threshold of %d. "
1005 "code size is 0x%08x loaded from offset 0x%08x.\n",
1006 code_size, kMaxCodeSize,
1007 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001008 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001009 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001010 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001011 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001012 }
1013 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001014 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001015 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001016 }
1017 }
1018 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001019 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001020 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001021 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001022
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001023 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1024 if (spill_mask == 0) {
1025 return;
1026 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001027 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001028 for (size_t i = 0; i < 32; i++) {
1029 if ((spill_mask & (1 << i)) != 0) {
1030 if (is_float) {
1031 os << "fr" << i;
1032 } else {
1033 os << "r" << i;
1034 }
1035 spill_mask ^= 1 << i; // clear bit
1036 if (spill_mask != 0) {
1037 os << ", ";
1038 } else {
1039 break;
1040 }
1041 }
1042 }
1043 os << ")";
1044 }
1045
Roland Levillain442b46a2015-02-18 16:54:21 +00001046 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001047 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001048 const OatFile::OatMethod& oat_method,
1049 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001050 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1051 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001052 const void* raw_code_info = oat_method.GetVmapTable();
1053 if (raw_code_info != nullptr) {
1054 CodeInfo code_info(raw_code_info);
1055 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001056 ScopedIndentation indent1(vios);
1057 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001058 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001059 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1060 // We don't encode the size in the table, so just emit that we have quickened
1061 // information.
1062 ScopedIndentation indent(vios);
1063 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001064 } else {
1065 // Otherwise, display the vmap table.
1066 const uint8_t* raw_table = oat_method.GetVmapTable();
1067 if (raw_table != nullptr) {
1068 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001069 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001070 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001071 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001072 }
1073
1074 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001075 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001076 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001077 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001078 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001079 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001080 oat_method.GetCodeOffset(),
1081 code_item.registers_size_,
1082 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001083 }
1084
Roland Levillain442b46a2015-02-18 16:54:21 +00001085 // Display a vmap table.
1086 void DumpVmapTable(std::ostream& os,
1087 const OatFile::OatMethod& oat_method,
1088 const VmapTable& vmap_table) {
1089 bool first = true;
1090 bool processing_fp = false;
1091 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1092 for (size_t i = 0; i < vmap_table.Size(); i++) {
1093 uint16_t dex_reg = vmap_table[i];
1094 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1095 processing_fp ? kFloatVReg : kIntVReg);
1096 os << (first ? "v" : ", v") << dex_reg;
1097 if (!processing_fp) {
1098 os << "/r" << cpu_reg;
1099 } else {
1100 os << "/fr" << cpu_reg;
1101 }
1102 first = false;
1103 if (!processing_fp && dex_reg == 0xFFFF) {
1104 processing_fp = true;
1105 spill_mask = oat_method.GetFpSpillMask();
1106 }
1107 }
1108 os << "\n";
1109 }
1110
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001111 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1112 const DexFile::CodeItem* code_item) {
1113 if (code_item != nullptr) {
1114 size_t num_locals_ins = code_item->registers_size_;
1115 size_t num_ins = code_item->ins_size_;
1116 size_t num_locals = num_locals_ins - num_ins;
1117 size_t num_outs = code_item->outs_size_;
1118
1119 os << "vr_stack_locations:";
1120 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1121 // For readability, delimit the different kinds of VRs.
1122 if (reg == num_locals_ins) {
1123 os << "\n\tmethod*:";
1124 } else if (reg == num_locals && num_ins > 0) {
1125 os << "\n\tins:";
1126 } else if (reg == 0 && num_locals > 0) {
1127 os << "\n\tlocals:";
1128 }
1129
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001130 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1131 code_item,
1132 oat_method.GetCoreSpillMask(),
1133 oat_method.GetFpSpillMask(),
1134 oat_method.GetFrameSizeInBytes(),
1135 reg,
1136 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001137 os << " v" << reg << "[sp + #" << offset << "]";
1138 }
1139
1140 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1141 if (out_reg == 0) {
1142 os << "\n\touts:";
1143 }
1144
1145 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1146 os << " v" << out_reg << "[sp + #" << offset << "]";
1147 }
1148
1149 os << "\n";
1150 }
1151 }
1152
Ian Rogersb23a7722012-10-09 16:54:26 -07001153 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001154 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001155 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001156 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001157 const VmapTable vmap_table(raw_table);
1158 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001159 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001160 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1161 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1162 : oat_method.GetCoreSpillMask();
1163 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001164 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001165 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1166 code_item,
1167 oat_method.GetCoreSpillMask(),
1168 oat_method.GetFpSpillMask(),
1169 oat_method.GetFrameSizeInBytes(),
1170 reg,
1171 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001172 os << "[sp + #" << offset << "]";
1173 }
1174 }
1175 }
1176
Ian Rogersef7d42f2014-01-06 12:55:46 -08001177 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1178 const DexFile::CodeItem* code_item,
1179 size_t num_regs, const uint8_t* reg_bitmap) {
1180 bool first = true;
1181 for (size_t reg = 0; reg < num_regs; reg++) {
1182 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1183 if (first) {
1184 os << " v" << reg << " (";
1185 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1186 os << ")";
1187 first = false;
1188 } else {
1189 os << ", v" << reg << " (";
1190 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1191 os << ")";
1192 }
1193 }
1194 }
1195 if (first) {
1196 os << "No registers in GC map\n";
1197 } else {
1198 os << "\n";
1199 }
1200 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001201 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1202 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001203 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001204 if (gc_map_raw == nullptr) {
1205 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001206 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001207 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001208 NativePcOffsetToReferenceMap map(gc_map_raw);
1209 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1210 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1211 map.GetNativePcOffset(entry);
1212 os << StringPrintf("%p", native_pc);
1213 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001214 }
1215 }
1216
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001217 void DumpMappingTable(VariableIndentationOutputStream* vios,
1218 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001219 const void* quick_code = oat_method.GetQuickCode();
1220 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001221 return;
1222 }
Ian Rogers1809a722013-08-09 22:05:32 -07001223 MappingTable table(oat_method.GetMappingTable());
1224 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001225 if (table.PcToDexSize() != 0) {
1226 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001227 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001228 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001229 ScopedIndentation indent1(vios);
1230 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001231 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001232 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001233 }
1234 if (table.DexToPcSize() != 0) {
1235 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001236 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001237 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001238 ScopedIndentation indent1(vios);
1239 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001240 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001241 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001242 }
1243 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001244 }
1245
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001246 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001247 const OatFile::OatMethod& oat_method,
1248 const DexFile::CodeItem* code_item,
1249 size_t offset,
1250 bool suspend_point_mapping) {
1251 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1252 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001253 ScopedIndentation indent1(vios);
1254 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001255 }
1256 // The return value is not used in the case of a method compiled
1257 // with the optimizing compiler.
1258 return DexFile::kDexNoIndex;
1259 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001260 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001261 }
1262 }
1263
Ian Rogers1809a722013-08-09 22:05:32 -07001264 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1265 size_t offset, bool suspend_point_mapping) {
1266 MappingTable table(oat_method.GetMappingTable());
1267 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1268 typedef MappingTable::PcToDexIterator It;
1269 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1270 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001271 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001272 return cur.DexPc();
1273 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001274 }
Ian Rogers1809a722013-08-09 22:05:32 -07001275 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1276 typedef MappingTable::DexToPcIterator It;
1277 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1278 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001279 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001280 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001281 }
1282 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001283 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001284 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001285 }
1286
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001287 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1288 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001289 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001290 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001291 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001292 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001293 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001294 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001295 bool first = true;
1296 for (size_t reg = 0; reg < num_regs; reg++) {
1297 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1298 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001299 os << "GC map objects: v" << reg << " (";
1300 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001301 os << ")";
1302 first = false;
1303 } else {
1304 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001305 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001306 os << ")";
1307 }
1308 }
1309 }
1310 if (!first) {
1311 os << "\n";
1312 }
1313 }
1314 }
1315 }
1316
Mathieu Chartier590fee92013-09-13 13:46:47 -07001317 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1318 const OatFile::OatMethod& oat_method,
1319 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1320 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001321 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001322 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001323 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1324 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1325 if (kind != kUndefined) {
1326 if (first) {
1327 os << "VRegs: v";
1328 first = false;
1329 } else {
1330 os << ", v";
1331 }
1332 os << reg << " (";
1333 switch (kind) {
1334 case kImpreciseConstant:
1335 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1336 DescribeVReg(os, oat_method, code_item, reg, kind);
1337 break;
1338 case kConstant:
1339 os << "Constant: " << kinds.at((reg * 2) + 1);
1340 break;
1341 default:
1342 DescribeVReg(os, oat_method, code_item, reg, kind);
1343 break;
1344 }
1345 os << ")";
1346 }
1347 }
1348 if (!first) {
1349 os << "\n";
1350 }
1351 }
1352
1353
Ian Rogersb23a7722012-10-09 16:54:26 -07001354 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001355 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001356 size_t i = 0;
1357 while (i < code_item->insns_size_in_code_units_) {
1358 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001359 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1360 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001361 i += instruction->SizeInCodeUnits();
1362 }
1363 }
1364 }
1365
Roland Levillainf2650d12015-05-28 14:53:28 +01001366 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1367 // the optimizing compiler?
1368 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1369 const DexFile::CodeItem* code_item) {
1370 // If the native GC map is null and the Dex `code_item` is not
1371 // null, then this method has been compiled with the optimizing
1372 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001373 return oat_method.GetQuickCode() != nullptr &&
1374 oat_method.GetGcMap() == nullptr &&
1375 code_item != nullptr;
1376 }
1377
1378 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1379 // the dextodex compiler?
1380 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1381 const DexFile::CodeItem* code_item) {
1382 // If the quick code is null, the Dex `code_item` is not
1383 // null, and the vmap table is not null, then this method has been compiled
1384 // with the dextodex compiler.
1385 return oat_method.GetQuickCode() == nullptr &&
1386 oat_method.GetVmapTable() != nullptr &&
1387 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001388 }
1389
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001390 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001391 const OatFile::OatMethod& oat_method,
1392 const DexFile::CodeItem* code_item,
1393 size_t offset) {
1394 // This method is only relevant for oat methods compiled with the
1395 // optimizing compiler.
1396 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1397
1398 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1399 const void* raw_code_info = oat_method.GetVmapTable();
1400 if (raw_code_info != nullptr) {
1401 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001402 StackMapEncoding encoding = code_info.ExtractEncoding();
1403 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001404 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001405 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1406 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001407 }
1408 }
1409 }
1410
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001411 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001412 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001413 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001414 const DexFile* dex_file,
1415 const DexFile::ClassDef& class_def,
1416 const DexFile::CodeItem* code_item,
1417 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001418 if ((method_access_flags & kAccNative) == 0) {
1419 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001420 Handle<mirror::DexCache> dex_cache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001421 hs->NewHandle(Runtime::Current()->GetClassLinker()->RegisterDexFile(*dex_file)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001422 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001423 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001424 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1425 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001426 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001427
1428 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001429 }
1430
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001431 void DumpCode(VariableIndentationOutputStream* vios,
1432 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001433 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1434 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001435 const void* quick_code = oat_method.GetQuickCode();
1436
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001437 if (code_size == 0) {
1438 code_size = oat_method.GetQuickCodeSize();
1439 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001440 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001441 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001442 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001443 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001444 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1445 size_t offset = 0;
1446 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001447 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001449 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001450 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001451 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001452 uint32_t dex_pc =
1453 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001454 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001455 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001456 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001457 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001458 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001459 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001460 }
1461 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001462 }
1463 }
1464
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001465 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001466 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001467 const OatDumperOptions& options_;
1468 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001469 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001470 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001471 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001472};
1473
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001474class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001475 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001476 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1477 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001478 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001479 vios_(os),
1480 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001481 image_space_(image_space),
1482 image_header_(image_header),
1483 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001484
Mathieu Chartier90443472015-07-16 20:32:27 -07001485 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001486 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001487 std::ostream& indent_os = vios_.Stream();
1488
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001489 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001490
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001492
Mathieu Chartiere401d142015-04-22 13:56:20 -07001493 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1494
1495 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1496 auto section = static_cast<ImageHeader::ImageSections>(i);
1497 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1498 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001499
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001500 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001501
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001502 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001503
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001504 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1505
1506 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1507
1508 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001509
Alex Lighta59dd802014-07-02 16:28:08 -07001510 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1511
Igor Murashkin46774762014-10-22 11:37:02 -07001512 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1513
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001514 {
1515 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001516 static_assert(arraysize(image_roots_descriptions_) ==
1517 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001518 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1519 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1520 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001521 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001522 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001523 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001524 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001525 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001526 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001527 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1528 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001529 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001530 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1531 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001532 run++;
1533 } else {
1534 break;
1535 }
1536 }
1537 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001538 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001539 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001540 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001541 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001542 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001543 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001544 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001546 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001547 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001548 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001549 }
1550 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001551 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001552
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001553 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001554 os << "METHOD ROOTS\n";
1555 static_assert(arraysize(image_methods_descriptions_) ==
1556 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1557 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1558 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1559 const char* description = image_methods_descriptions_[i];
1560 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001561 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001562 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001563 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001565
Brian Carlstromaded5f72011-10-07 17:15:04 -07001566 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001567 std::string image_filename = image_space_.GetImageFilename();
1568 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001569 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001570 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001571 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001572 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1573 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001574 oat_file = OatFile::Open(oat_location, oat_location,
1575 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001576 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001577 if (oat_file == nullptr) {
1578 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001579 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001580 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001581 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001582 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001583
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001584 stats_.oat_file_bytes = oat_file->Size();
1585
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001586 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001587
Mathieu Chartier02e25112013-08-14 16:14:24 -07001588 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001589 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001590 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1591 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001592 }
1593
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001594 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001595
1596 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001597 gc::Heap* heap = Runtime::Current()->GetHeap();
1598 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001599 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001600 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001601 {
1602 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1603 heap->FlushAllocStack();
1604 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001605 // Since FlushAllocStack() above resets the (active) allocation
1606 // stack. Need to revoke the thread-local allocation stacks that
1607 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001608 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001609 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001610 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001611 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001612 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001613 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001614 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001615 {
1616 ReaderMutexLock mu(self, *class_linker->DexLock());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001617 for (jobject weak_root : class_linker->GetDexCaches()) {
1618 mirror::DexCache* dex_cache =
1619 down_cast<mirror::DexCache*>(self->DecodeJObject(weak_root));
1620 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001621 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001622 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001623 }
1624 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001626 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001627 if (space->IsImageSpace()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001628 auto* image_space = space->AsImageSpace();
1629 // Dump the normal objects before ArtMethods.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1631 indent_os << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001632 // TODO: Dump fields.
1633 // Dump methods after.
1634 const auto& methods_section = image_header_.GetMethodsSection();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001635 const size_t pointer_size =
Mathieu Chartiere401d142015-04-22 13:56:20 -07001636 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001637 DumpArtMethodVisitor visitor(this);
Vladimir Markocf36d492015-08-12 19:27:26 +01001638 methods_section.VisitPackedArtMethods(&visitor, image_space->Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001639 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001640 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001642 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001643 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001644 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001645 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001646 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001647 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001648 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001649 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001650 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001651 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001652 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001653 size_t header_bytes = sizeof(ImageHeader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001654 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1655 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1656 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001657 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1658 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001659 const auto& intern_section = image_header_.GetImageSection(
1660 ImageHeader::kSectionInternedStrings);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001661 stats_.header_bytes = header_bytes;
Vladimir Markocf36d492015-08-12 19:27:26 +01001662 stats_.alignment_bytes += RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1663 // Add padding between the field and method section.
1664 // (Field section is 4-byte aligned, method section is 8-byte aligned on 64-bit targets.)
Vladimir Marko05792b92015-08-03 11:56:49 +01001665 stats_.alignment_bytes += method_section.Offset() -
1666 (field_section.Offset() + field_section.Size());
1667 // Add padding between the dex cache arrays section and the intern table. (Dex cache
1668 // arrays section is 4-byte aligned on 32-bit targets, intern table is 8-byte aligned.)
1669 stats_.alignment_bytes += intern_section.Offset() -
1670 (dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001671 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1672 stats_.bitmap_bytes += bitmap_section.Size();
1673 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001674 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001675 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001676 stats_.interned_strings_bytes += intern_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001677 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001678 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001679
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001680 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001681
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001682 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001683 }
1684
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001685 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001686 class DumpArtMethodVisitor : public ArtMethodVisitor {
1687 public:
1688 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1689
1690 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1691 std::ostream& indent_os = image_dumper_->vios_.Stream();
1692 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1693 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1694 indent_os << "\n";
1695 }
1696
1697 private:
1698 ImageDumper* const image_dumper_;
1699 };
1700
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001701 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001702 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001703 CHECK(type != nullptr);
1704 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001705 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001706 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001707 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001708 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001709 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001710 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001711 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001712 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001713 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001715 }
1716 }
1717
Mathieu Chartierc7853442015-03-27 14:35:38 -07001718 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001719 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001720 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001721 switch (field->GetTypeAsPrimitiveType()) {
1722 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001723 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001724 break;
1725 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001726 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001727 break;
1728 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001729 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001730 break;
1731 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001732 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001733 break;
1734 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001735 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001736 break;
1737 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001738 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001739 break;
1740 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001741 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1742 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001743 break;
1744 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001745 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001746 break;
1747 case Primitive::kPrimNot: {
1748 // Get the value, don't compute the type unless it is non-null as we don't want
1749 // to cause class loading.
1750 mirror::Object* value = field->GetObj(obj);
1751 if (value == nullptr) {
1752 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001753 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001754 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001755 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001756 if (field_type != nullptr) {
1757 PrettyObjectValue(os, field_type, value);
1758 } else {
1759 os << StringPrintf("%p %s\n", value,
1760 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1761 }
Ian Rogers50239c72013-06-17 14:53:22 -07001762 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001763 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001764 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001765 default:
1766 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1767 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001768 }
1769 }
1770
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001771 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001772 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001773 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001774 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001775 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001776 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001777 for (ArtField& field : klass->GetIFields()) {
1778 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001779 }
1780 }
1781
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001782 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001783 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001784 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001785
Mathieu Chartiere401d142015-04-22 13:56:20 -07001786 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001787 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001788 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1789 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001790 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001791 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001792 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001793 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001794 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001795 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001796 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001797 }
1798
Mathieu Chartiere401d142015-04-22 13:56:20 -07001799 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001800 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001801 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1802 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001803 return 0;
1804 }
1805 return oat_code_begin[-1];
1806 }
1807
Mathieu Chartiere401d142015-04-22 13:56:20 -07001808 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001809 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001810 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001811 if (oat_code_begin == nullptr) {
1812 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001813 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001814 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001815 }
1816
Mathieu Chartier90443472015-07-16 20:32:27 -07001817 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001818 DCHECK(obj != nullptr);
1819 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001820 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001821 if (!state->InDumpSpace(obj)) {
1822 return;
1823 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001824
1825 size_t object_bytes = obj->SizeOf();
1826 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1827 state->stats_.object_bytes += object_bytes;
1828 state->stats_.alignment_bytes += alignment_bytes;
1829
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001830 std::ostream& os = state->vios_.Stream();
1831
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001832 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001833 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001834 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1835 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001836 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001837 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001838 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1839 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001840 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001841 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001842 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001843 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001844 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001845 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001846 ScopedIndentation indent1(&state->vios_);
1847 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001848 const auto image_pointer_size =
1849 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001850 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001851 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1852 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001853 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001854 size_t run = 0;
1855 for (int32_t j = i + 1; j < length; j++) {
1856 if (value == obj_array->Get(j)) {
1857 run++;
1858 } else {
1859 break;
1860 }
1861 }
1862 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001863 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001864 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001865 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001866 i = i + run;
1867 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001868 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001869 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001870 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001871 }
1872 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001873 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001874 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001875 os << "STATICS:\n";
1876 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001877 for (ArtField& field : klass->GetSFields()) {
1878 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001879 }
1880 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001881 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001882 auto it = state->dex_caches_.find(obj);
1883 if (it != state->dex_caches_.end()) {
1884 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001885 const auto& field_section = state->image_header_.GetImageSection(
1886 ImageHeader::kSectionArtFields);
1887 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001888 size_t num_methods = dex_cache->NumResolvedMethods();
1889 if (num_methods != 0u) {
1890 os << "Methods (size=" << num_methods << "):";
1891 ScopedIndentation indent2(&state->vios_);
1892 auto* resolved_methods = dex_cache->GetResolvedMethods();
1893 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1894 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1895 size_t run = 0;
1896 for (size_t j = i + 1;
1897 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1898 j,
1899 image_pointer_size);
1900 ++j, ++run) {}
1901 if (run == 0) {
1902 os << StringPrintf("%zd: ", i);
1903 } else {
1904 os << StringPrintf("%zd to %zd: ", i, i + run);
1905 i = i + run;
1906 }
1907 std::string msg;
1908 if (elem == nullptr) {
1909 msg = "null";
1910 } else if (method_section.Contains(
1911 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1912 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1913 } else {
1914 msg = "<not in method section>";
1915 }
1916 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001917 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001918 }
1919 size_t num_fields = dex_cache->NumResolvedFields();
1920 if (num_fields != 0u) {
1921 os << "Fields (size=" << num_fields << "):";
1922 ScopedIndentation indent2(&state->vios_);
1923 auto* resolved_fields = dex_cache->GetResolvedFields();
1924 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1925 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1926 size_t run = 0;
1927 for (size_t j = i + 1;
1928 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1929 j,
1930 image_pointer_size);
1931 ++j, ++run) {}
1932 if (run == 0) {
1933 os << StringPrintf("%zd: ", i);
1934 } else {
1935 os << StringPrintf("%zd to %zd: ", i, i + run);
1936 i = i + run;
1937 }
1938 std::string msg;
1939 if (elem == nullptr) {
1940 msg = "null";
1941 } else if (field_section.Contains(
1942 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1943 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1944 } else {
1945 msg = "<not in field section>";
1946 }
1947 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001948 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001949 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001950 }
1951 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001952 std::string temp;
1953 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001954 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001955
Mathieu Chartiere401d142015-04-22 13:56:20 -07001956 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001957 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001958 DCHECK(method != nullptr);
1959 const auto image_pointer_size =
1960 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1961 if (method->IsNative()) {
1962 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1963 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1964 bool first_occurrence;
1965 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1966 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1967 state->ComputeOatSize(quick_oat_code, &first_occurrence);
1968 if (first_occurrence) {
1969 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1970 }
1971 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1972 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
1973 }
1974 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1975 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1976 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1977 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1978 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1979 } else {
1980 const DexFile::CodeItem* code_item = method->GetCodeItem();
1981 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1982 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1983
1984 bool first_occurrence;
1985 size_t gc_map_bytes = state->ComputeOatSize(
1986 method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1987 if (first_occurrence) {
1988 state->stats_.gc_map_bytes += gc_map_bytes;
1989 }
1990
1991 size_t pc_mapping_table_bytes = state->ComputeOatSize(
1992 method->GetMappingTable(image_pointer_size), &first_occurrence);
1993 if (first_occurrence) {
1994 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1995 }
1996
Roland Levillain6d7f1792015-07-02 10:59:15 +01001997 size_t vmap_table_bytes = 0u;
1998 if (!method->IsOptimized(image_pointer_size)) {
1999 // Method compiled with the optimizing compiler have no vmap table.
2000 vmap_table_bytes = state->ComputeOatSize(
2001 method->GetVmapTable(image_pointer_size), &first_occurrence);
2002 if (first_occurrence) {
2003 state->stats_.vmap_table_bytes += vmap_table_bytes;
2004 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002005 }
2006
2007 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2008 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
2009 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2010 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2011 if (first_occurrence) {
2012 state->stats_.managed_code_bytes += quick_oat_code_size;
2013 if (method->IsConstructor()) {
2014 if (method->IsStatic()) {
2015 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2016 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2017 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2018 }
2019 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2020 state->stats_.large_method_code_bytes += quick_oat_code_size;
2021 }
2022 }
2023 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2024
Igor Murashkin7617abd2015-07-10 18:27:47 -07002025 uint32_t method_access_flags = method->GetAccessFlags();
2026
Mathieu Chartiere401d142015-04-22 13:56:20 -07002027 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002028 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2029 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2030 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002031
2032 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002033 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034
2035 double expansion =
2036 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2037 state->stats_.ComputeOutliers(total_size, expansion, method);
2038 }
2039 }
2040
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002041 std::set<const void*> already_seen_;
2042 // Compute the size of the given data within the oat file and whether this is the first time
2043 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002044 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002045 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002046 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002047 already_seen_.insert(oat_data);
2048 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002049 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002050 }
2051 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002052 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002053
2054 public:
2055 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002056 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002057 size_t file_bytes;
2058
2059 size_t header_bytes;
2060 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002061 size_t art_field_bytes;
2062 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002063 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002064 size_t interned_strings_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002065 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002066 size_t alignment_bytes;
2067
2068 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002069 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002070 size_t managed_to_native_code_bytes;
2071 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002072 size_t class_initializer_code_bytes;
2073 size_t large_initializer_code_bytes;
2074 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002075
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002076 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002077 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002078 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002079
2080 size_t dex_instruction_bytes;
2081
Mathieu Chartiere401d142015-04-22 13:56:20 -07002082 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002083 std::vector<size_t> method_outlier_size;
2084 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002085 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002086
Roland Levillain3887c462015-08-12 18:15:42 +01002087 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002088 : oat_file_bytes(0),
2089 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002090 header_bytes(0),
2091 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002092 art_field_bytes(0),
2093 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002094 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002095 interned_strings_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002096 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002097 alignment_bytes(0),
2098 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002099 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002100 managed_to_native_code_bytes(0),
2101 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002102 class_initializer_code_bytes(0),
2103 large_initializer_code_bytes(0),
2104 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002105 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002106 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002107 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002108 dex_instruction_bytes(0) {}
2109
Elliott Hughesa0e18062012-04-13 15:59:59 -07002110 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002111 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002112 size_t bytes;
2113 size_t count;
2114 };
2115 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2116 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002117
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002118 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002119 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2120 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002121 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002122 it->second.count += 1;
2123 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002124 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002125 }
2126 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002127
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002128 double PercentOfOatBytes(size_t size) {
2129 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2130 }
2131
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002132 double PercentOfFileBytes(size_t size) {
2133 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2134 }
2135
2136 double PercentOfObjectBytes(size_t size) {
2137 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2138 }
2139
Mathieu Chartiere401d142015-04-22 13:56:20 -07002140 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002141 method_outlier_size.push_back(total_size);
2142 method_outlier_expansion.push_back(expansion);
2143 method_outlier.push_back(method);
2144 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002145
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002146 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002147 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002148 size_t sum_of_sizes = 0;
2149 size_t sum_of_sizes_squared = 0;
2150 size_t sum_of_expansion = 0;
2151 size_t sum_of_expansion_squared = 0;
2152 size_t n = method_outlier_size.size();
2153 for (size_t i = 0; i < n; i++) {
2154 size_t cur_size = method_outlier_size[i];
2155 sum_of_sizes += cur_size;
2156 sum_of_sizes_squared += cur_size * cur_size;
2157 double cur_expansion = method_outlier_expansion[i];
2158 sum_of_expansion += cur_expansion;
2159 sum_of_expansion_squared += cur_expansion * cur_expansion;
2160 }
2161 size_t size_mean = sum_of_sizes / n;
2162 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2163 double expansion_mean = sum_of_expansion / n;
2164 double expansion_variance =
2165 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2166
2167 // Dump methods whose size is a certain number of standard deviations from the mean
2168 size_t dumped_values = 0;
2169 size_t skipped_values = 0;
2170 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2171 size_t cur_size_variance = i * i * size_variance;
2172 bool first = true;
2173 for (size_t j = 0; j < n; j++) {
2174 size_t cur_size = method_outlier_size[j];
2175 if (cur_size > size_mean) {
2176 size_t cur_var = cur_size - size_mean;
2177 cur_var = cur_var * cur_var;
2178 if (cur_var > cur_size_variance) {
2179 if (dumped_values > 20) {
2180 if (i == 1) {
2181 skipped_values++;
2182 } else {
2183 i = 2; // jump to counting for 1 standard deviation
2184 break;
2185 }
2186 } else {
2187 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002188 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002189 first = false;
2190 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002191 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002192 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002193 method_outlier_size[j] = 0; // don't consider this method again
2194 dumped_values++;
2195 }
2196 }
2197 }
2198 }
2199 }
2200 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002201 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002202 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002203 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002204 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002205
2206 // Dump methods whose expansion is a certain number of standard deviations from the mean
2207 dumped_values = 0;
2208 skipped_values = 0;
2209 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2210 double cur_expansion_variance = i * i * expansion_variance;
2211 bool first = true;
2212 for (size_t j = 0; j < n; j++) {
2213 double cur_expansion = method_outlier_expansion[j];
2214 if (cur_expansion > expansion_mean) {
2215 size_t cur_var = cur_expansion - expansion_mean;
2216 cur_var = cur_var * cur_var;
2217 if (cur_var > cur_expansion_variance) {
2218 if (dumped_values > 20) {
2219 if (i == 1) {
2220 skipped_values++;
2221 } else {
2222 i = 2; // jump to counting for 1 standard deviation
2223 break;
2224 }
2225 } else {
2226 if (first) {
2227 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002228 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002229 first = false;
2230 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002231 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002232 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002233 method_outlier_expansion[j] = 0.0; // don't consider this method again
2234 dumped_values++;
2235 }
2236 }
2237 }
2238 }
2239 }
2240 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002241 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002242 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002243 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002244 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002245 }
2246
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002247 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002248 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002249 {
2250 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2251 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002252 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2253 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2254 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2255 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2256 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2257 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
2258 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2259 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002260 header_bytes, PercentOfFileBytes(header_bytes),
2261 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002262 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2263 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002264 dex_cache_arrays_bytes,
2265 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002266 interned_strings_bytes,
2267 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002268 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002269 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2270 << std::flush;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002271 CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
Vladimir Marko05792b92015-08-03 11:56:49 +01002272 dex_cache_arrays_bytes + interned_strings_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002274
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002275 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002276 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002277 for (const auto& sizes_and_count : sizes_and_counts) {
2278 const std::string& descriptor(sizes_and_count.first);
2279 double average = static_cast<double>(sizes_and_count.second.bytes) /
2280 static_cast<double>(sizes_and_count.second.count);
2281 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002282 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002283 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002284 descriptor.c_str(), sizes_and_count.second.bytes,
2285 sizes_and_count.second.count, average, percent);
2286 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002287 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002288 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002289 CHECK_EQ(object_bytes, object_bytes_total);
2290
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002291 os << StringPrintf("oat_file_bytes = %8zd\n"
2292 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2293 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2294 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2295 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2296 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2297 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002298 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002299 managed_code_bytes,
2300 PercentOfOatBytes(managed_code_bytes),
2301 managed_to_native_code_bytes,
2302 PercentOfOatBytes(managed_to_native_code_bytes),
2303 native_to_managed_code_bytes,
2304 PercentOfOatBytes(native_to_managed_code_bytes),
2305 class_initializer_code_bytes,
2306 PercentOfOatBytes(class_initializer_code_bytes),
2307 large_initializer_code_bytes,
2308 PercentOfOatBytes(large_initializer_code_bytes),
2309 large_method_code_bytes,
2310 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002312 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002314 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2315 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002316 }
2317
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2319 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2320 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002321 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2322 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2323 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002324 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002325
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2327 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002328 static_cast<double>(managed_code_bytes) /
2329 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002330 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002331 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002332 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002333
2334 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002335 }
2336 } stats_;
2337
2338 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002339 enum {
2340 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2341 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2342 kLargeConstructorDexBytes = 4000,
2343 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2344 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2345 kLargeMethodDexBytes = 16000
2346 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002347
2348 // For performance, use the *os_ directly for anything that doesn't need indentation
2349 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002350 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002351 VariableIndentationOutputStream vios_;
2352 ScopedIndentation indent1_;
2353
Ian Rogers1d54e732013-05-02 21:10:01 -07002354 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002355 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002356 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002357 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002358 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002359
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002360 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002361};
2362
Andreas Gampe00b25f32014-09-17 21:49:05 -07002363static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2364 std::ostream* os) {
2365 // Dumping the image, no explicit class loader.
2366 NullHandle<mirror::ClassLoader> null_class_loader;
2367 options->class_loader_ = &null_class_loader;
2368
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002369 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002370 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002371 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002372 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002373 const ImageHeader& image_header = image_space->GetImageHeader();
2374 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002375 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002376 return EXIT_FAILURE;
2377 }
Igor Murashkin37743352014-11-13 14:38:00 -08002378
Andreas Gampe00b25f32014-09-17 21:49:05 -07002379 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002380
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002381 bool success = image_dumper.Dump();
2382 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002383}
2384
Andreas Gampe00b25f32014-09-17 21:49:05 -07002385static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2386 std::ostream* os) {
2387 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2388
2389 Thread* self = Thread::Current();
2390 CHECK(self != nullptr);
2391 // Need well-known-classes.
2392 WellKnownClasses::Init(self->GetJniEnv());
2393
2394 // Need to register dex files to get a working dex cache.
2395 ScopedObjectAccess soa(self);
2396 ClassLinker* class_linker = runtime->GetClassLinker();
2397 class_linker->RegisterOatFile(oat_file);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002398 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002399 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2400 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002401 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002402 CHECK(dex_file != nullptr) << error_msg;
2403 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002404 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002405 }
2406
2407 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002408 // Fake that we're a compiler.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07002409 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002410
2411 // Use the class loader while dumping.
2412 StackHandleScope<1> scope(self);
2413 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2414 soa.Decode<mirror::ClassLoader*>(class_loader));
2415 options->class_loader_ = &loader_handle;
2416
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002417 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002418 bool success = oat_dumper.Dump(*os);
2419 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2420}
2421
2422static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002423 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002424 // No image = no class loader.
2425 NullHandle<mirror::ClassLoader> null_class_loader;
2426 options->class_loader_ = &null_class_loader;
2427
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002428 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002429 bool success = oat_dumper.Dump(*os);
2430 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2431}
2432
2433static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2434 std::ostream* os) {
2435 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002436 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002437 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002438 if (oat_file == nullptr) {
2439 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2440 return EXIT_FAILURE;
2441 }
2442
2443 if (runtime != nullptr) {
2444 return DumpOatWithRuntime(runtime, oat_file, options, os);
2445 } else {
2446 return DumpOatWithoutRuntime(oat_file, options, os);
2447 }
2448}
2449
2450static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2451 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002452 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002453 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002454 if (oat_file == nullptr) {
2455 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2456 return EXIT_FAILURE;
2457 }
2458
2459 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002460 if (!oat_symbolizer.Symbolize()) {
2461 fprintf(stderr, "Failed to symbolize\n");
2462 return EXIT_FAILURE;
2463 }
2464
2465 return EXIT_SUCCESS;
2466}
2467
Igor Murashkin37743352014-11-13 14:38:00 -08002468struct OatdumpArgs : public CmdlineArgs {
2469 protected:
2470 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002471
Igor Murashkin37743352014-11-13 14:38:00 -08002472 virtual ParseStatus ParseCustom(const StringPiece& option,
2473 std::string* error_msg) OVERRIDE {
2474 {
2475 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2476 if (base_parse != kParseUnknownArgument) {
2477 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002478 }
2479 }
2480
Igor Murashkin37743352014-11-13 14:38:00 -08002481 if (option.starts_with("--oat-file=")) {
2482 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2483 } else if (option.starts_with("--image=")) {
2484 image_location_ = option.substr(strlen("--image=")).data();
2485 } else if (option =="--dump:raw_mapping_table") {
2486 dump_raw_mapping_table_ = true;
2487 } else if (option == "--dump:raw_gc_map") {
2488 dump_raw_gc_map_ = true;
2489 } else if (option == "--no-dump:vmap") {
2490 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002491 } else if (option =="--dump:code_info_stack_maps") {
2492 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002493 } else if (option == "--no-disassemble") {
2494 disassemble_code_ = false;
2495 } else if (option.starts_with("--symbolize=")) {
2496 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2497 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002498 } else if (option.starts_with("--class-filter=")) {
2499 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002500 } else if (option.starts_with("--method-filter=")) {
2501 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002502 } else if (option.starts_with("--list-classes")) {
2503 list_classes_ = true;
2504 } else if (option.starts_with("--list-methods")) {
2505 list_methods_ = true;
2506 } else if (option.starts_with("--export-dex-to=")) {
2507 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2508 } else if (option.starts_with("--addr2instr=")) {
2509 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2510 *error_msg = "Address conversion failed";
2511 return kParseError;
2512 }
Igor Murashkin37743352014-11-13 14:38:00 -08002513 } else {
2514 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002515 }
2516
Igor Murashkin37743352014-11-13 14:38:00 -08002517 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002518 }
2519
Igor Murashkin37743352014-11-13 14:38:00 -08002520 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2521 // Infer boot image location from the image location if possible.
2522 if (boot_image_location_ == nullptr) {
2523 boot_image_location_ = image_location_;
2524 }
2525
2526 // Perform the parent checks.
2527 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2528 if (parent_checks != kParseOk) {
2529 return parent_checks;
2530 }
2531
2532 // Perform our own checks.
2533 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2534 *error_msg = "Either --image or --oat-file must be specified";
2535 return kParseError;
2536 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2537 *error_msg = "Either --image or --oat-file must be specified but not both";
2538 return kParseError;
2539 }
2540
2541 return kParseOk;
2542 }
2543
2544 virtual std::string GetUsage() const {
2545 std::string usage;
2546
2547 usage +=
2548 "Usage: oatdump [options] ...\n"
2549 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2550 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2551 "\n"
2552 // Either oat-file or image is required.
2553 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2554 " Example: --oat-file=/system/framework/boot.oat\n"
2555 "\n"
2556 " --image=<file.art>: specifies an input image location.\n"
2557 " Example: --image=/system/framework/boot.art\n"
2558 "\n";
2559
2560 usage += Base::GetUsage();
2561
2562 usage += // Optional.
2563 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2564 " Example: --dump:raw_mapping_table\n"
2565 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002566 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002567 " Example: --dump:raw_gc_map\n"
2568 "\n"
2569 " --no-dump:vmap may be used to disable vmap dumping.\n"
2570 " Example: --no-dump:vmap\n"
2571 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002572 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2573 " Example: --dump:code_info_stack_maps\n"
2574 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002575 " --no-disassemble may be used to disable disassembly.\n"
2576 " Example: --no-disassemble\n"
2577 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002578 " --list-classes may be used to list target file classes (can be used with filters).\n"
2579 " Example: --list-classes\n"
2580 " Example: --list-classes --class-filter=com.example.foo\n"
2581 "\n"
2582 " --list-methods may be used to list target file methods (can be used with filters).\n"
2583 " Example: --list-methods\n"
2584 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2585 "\n"
2586 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2587 " Example: --symbolize=/system/framework/boot.oat\n"
2588 "\n"
2589 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2590 " Example: --class-filter=com.example.foo\n"
2591 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002592 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2593 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002594 "\n"
2595 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2596 " Example: --export-dex-to=/data/local/tmp\n"
2597 "\n"
2598 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2599 " address (e.g. PC from crash dump)\n"
2600 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002601 "\n";
2602
2603 return usage;
2604 }
2605
2606 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002607 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002608 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002609 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002610 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002611 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002612 bool dump_raw_mapping_table_ = false;
2613 bool dump_raw_gc_map_ = false;
2614 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002615 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002616 bool disassemble_code_ = true;
2617 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002618 bool list_classes_ = false;
2619 bool list_methods_ = false;
2620 uint32_t addr2instr_ = 0;
2621 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002622};
2623
Igor Murashkin37743352014-11-13 14:38:00 -08002624struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2625 virtual bool NeedsRuntime() OVERRIDE {
2626 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002627
Igor Murashkin37743352014-11-13 14:38:00 -08002628 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2629 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2630
2631 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2632 args_->dump_raw_mapping_table_,
2633 args_->dump_raw_gc_map_,
2634 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002635 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002636 args_->disassemble_code_,
2637 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002638 args_->class_filter_,
2639 args_->method_filter_,
2640 args_->list_classes_,
2641 args_->list_methods_,
2642 args_->export_dex_location_,
2643 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002644
2645 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2646 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002647 }
2648
Igor Murashkin37743352014-11-13 14:38:00 -08002649 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2650 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002651 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002652
Mathieu Chartierd424d082014-10-15 10:31:46 -07002653 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002654
Andreas Gampec24f3992014-12-17 20:40:11 -08002655 if (args_->symbolize_) {
2656 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2657 } else {
2658 return DumpOat(nullptr,
2659 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002660 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002661 args_->os_) == EXIT_SUCCESS;
2662 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002663 }
2664
Igor Murashkin37743352014-11-13 14:38:00 -08002665 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2666 CHECK(args_ != nullptr);
2667
2668 if (args_->oat_filename_ != nullptr) {
2669 return DumpOat(runtime,
2670 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002671 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002672 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002673 }
Igor Murashkin37743352014-11-13 14:38:00 -08002674
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002675 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002676 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002677 }
2678
Igor Murashkin37743352014-11-13 14:38:00 -08002679 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2680};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002681
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002682} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002683
2684int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002685 art::OatdumpMain main;
2686 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002687}