blob: 61d415276bfee37cd259e7a949ca3c805ab170f4 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080036#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080037#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070038#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070040#include "gc/space/image_space.h"
41#include "gc/space/large_object_space.h"
42#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070043#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000045#include "linker/buffered_output_stream.h"
46#include "linker/file_output_stream.h"
Ian Rogers1809a722013-08-09 22:05:32 -070047#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/array-inl.h"
49#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010050#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080056#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070057#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058#include "scoped_thread_state_change.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010059#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080061#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080062#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080063#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070064#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070065#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Igor Murashkin37743352014-11-13 14:38:00 -080067#include <sys/stat.h>
68#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070069
Igor Murashkin37743352014-11-13 14:38:00 -080070namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Mathieu Chartiere401d142015-04-22 13:56:20 -070072const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080073 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070074 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070075 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070077 "kRefsOnlySaveMethod",
78 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070079};
80
81const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Mathieu Chartierac8f4392015-08-27 13:54:20 -070086// Map is so that we don't allocate multiple dex files for the same OatDexFile.
87static std::map<const OatFile::OatDexFile*,
88 std::unique_ptr<const DexFile>> opened_dex_files;
89
90const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
91 DCHECK(oat_dex_file != nullptr);
92 auto it = opened_dex_files.find(oat_dex_file);
93 if (it != opened_dex_files.end()) {
94 return it->second.get();
95 }
96 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
97 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
98 return ret;
99}
100
David Srbeckybc90fd02015-04-22 19:40:27 +0100101class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700102 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100103 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100104 oat_file_(oat_file), builder_(nullptr),
105 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700106 }
107
108 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
109 uint32_t,
110 const OatFile::OatMethod&,
111 const DexFile&,
112 uint32_t,
113 const DexFile::CodeItem*,
114 uint32_t);
115
116 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000117 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
118
119 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
120 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000121 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000122 builder_.reset(new ElfBuilder<ElfTypes32>(isa, output_stream.get()));
123
124 builder_->Start();
125
126 auto* rodata = builder_->GetRoData();
127 auto* text = builder_->GetText();
128 auto* bss = builder_->GetBss();
129 auto* strtab = builder_->GetStrTab();
130 auto* symtab = builder_->GetSymTab();
131
132 rodata->Start();
133 const uint8_t* rodata_begin = oat_file_->Begin();
134 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
135 rodata->WriteFully(rodata_begin, rodata_size);
136 rodata->End();
137
138 text->Start();
139 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
140 const size_t text_size = oat_file_->End() - text_begin;
141 text->WriteFully(text_begin, text_size);
142 text->End();
143
144 if (oat_file_->BssSize() != 0) {
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000145 bss->WriteNoBitsSection(oat_file_->BssSize());
David Srbecky6d8c8f02015-10-26 10:57:09 +0000146 }
147
148 builder_->WriteDynamicSection(elf_file->GetPath());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700149
150 Walk(&art::OatSymbolizer::RegisterForDedup);
151
152 NormalizeState();
153
David Srbecky6d8c8f02015-10-26 10:57:09 +0000154 strtab->Start();
155 strtab->Write(""); // strtab should start with empty string.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700156 Walk(&art::OatSymbolizer::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000157 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700158
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 symtab->Start();
160 symtab->Write();
161 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700162
David Srbecky6d8c8f02015-10-26 10:57:09 +0000163 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700164
Vladimir Marko10c13562015-11-25 14:33:36 +0000165 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700166 }
167
168 void Walk(Callback callback) {
169 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
170 for (size_t i = 0; i < oat_dex_files.size(); i++) {
171 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700172 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700173 WalkOatDexFile(oat_dex_file, callback);
174 }
175 }
176
177 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
178 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700179 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
180 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700181 return;
182 }
183 for (size_t class_def_index = 0;
184 class_def_index < dex_file->NumClassDefs();
185 class_def_index++) {
186 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
187 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
188 OatClassType type = oat_class.GetType();
189 switch (type) {
190 case kOatClassAllCompiled:
191 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700192 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700193 break;
194
195 case kOatClassNoneCompiled:
196 case kOatClassMax:
197 // Ignore.
198 break;
199 }
200 }
201 }
202
203 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
204 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700205 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700206 if (class_data == nullptr) { // empty class such as a marker interface?
207 return;
208 }
209 // Note: even if this is an interface or a native class, we still have to walk it, as there
210 // might be a static initializer.
211 ClassDataItemIterator it(dex_file, class_data);
212 SkipAllFields(&it);
213 uint32_t class_method_idx = 0;
214 while (it.HasNextDirectMethod()) {
215 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
216 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700217 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700218 class_method_idx++;
219 it.Next();
220 }
221 while (it.HasNextVirtualMethod()) {
222 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
223 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700224 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700225 class_method_idx++;
226 it.Next();
227 }
228 DCHECK(!it.HasNext());
229 }
230
231 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
232 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
233 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
234 uint32_t method_access_flags, Callback callback) {
235 if ((method_access_flags & kAccAbstract) != 0) {
236 // Abstract method, no code.
237 return;
238 }
239 if (oat_method.GetCodeOffset() == 0) {
240 // No code.
241 return;
242 }
243
244 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
245 method_access_flags);
246 }
247
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700248 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
249 uint32_t class_method_index ATTRIBUTE_UNUSED,
250 const OatFile::OatMethod& oat_method,
251 const DexFile& dex_file ATTRIBUTE_UNUSED,
252 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
253 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
254 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700255 state_[oat_method.GetCodeOffset()]++;
256 }
257
258 void NormalizeState() {
259 for (auto& x : state_) {
260 if (x.second == 1) {
261 state_[x.first] = 0;
262 }
263 }
264 }
265
266 enum class DedupState { // private
267 kNotDeduplicated,
268 kDeduplicatedFirst,
269 kDeduplicatedOther
270 };
271 DedupState IsDuplicated(uint32_t offset) {
272 if (state_[offset] == 0) {
273 return DedupState::kNotDeduplicated;
274 }
275 if (state_[offset] == 1) {
276 return DedupState::kDeduplicatedOther;
277 }
278 state_[offset] = 1;
279 return DedupState::kDeduplicatedFirst;
280 }
281
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700282 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
283 uint32_t class_method_index ATTRIBUTE_UNUSED,
284 const OatFile::OatMethod& oat_method,
285 const DexFile& dex_file,
286 uint32_t dex_method_idx,
287 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
288 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700289 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
290 if (dedup != DedupState::kDeduplicatedOther) {
291 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
292
293 if (dedup == DedupState::kDeduplicatedFirst) {
294 pretty_name = "[Dedup]" + pretty_name;
295 }
296
David Srbecky6d8c8f02015-10-26 10:57:09 +0000297 int name_offset = builder_->GetStrTab()->Write(pretty_name);
298 builder_->GetSymTab()->Add(name_offset, 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
Jeff Hao4b07a6e2016-01-15 12:50:44 -0800396 os << "LOCATION:\n";
397 os << oat_file_.GetLocation() << "\n\n";
398
Brian Carlstromaded5f72011-10-07 17:15:04 -0700399 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800400 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700401
Elliott Hughesa72ec822012-03-05 17:12:22 -0800402 os << "INSTRUCTION SET:\n";
403 os << oat_header.GetInstructionSet() << "\n\n";
404
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700405 {
406 std::unique_ptr<const InstructionSetFeatures> features(
407 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
408 oat_header.GetInstructionSetFeaturesBitmap()));
409 os << "INSTRUCTION SET FEATURES:\n";
410 os << features->GetFeatureString() << "\n\n";
411 }
Dave Allison70202782013-10-22 17:52:19 -0700412
Brian Carlstromaded5f72011-10-07 17:15:04 -0700413 os << "DEX FILE COUNT:\n";
414 os << oat_header.GetDexFileCount() << "\n\n";
415
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800416#define DUMP_OAT_HEADER_OFFSET(label, offset) \
417 os << label " OFFSET:\n"; \
418 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800419 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800420 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
421 } \
422 os << StringPrintf("\n\n");
423
424 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
425 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
426 GetInterpreterToInterpreterBridgeOffset);
427 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
428 GetInterpreterToCompiledCodeBridgeOffset);
429 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
430 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800431 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
432 GetQuickGenericJniTrampolineOffset);
433 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
434 GetQuickImtConflictTrampolineOffset);
435 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
436 GetQuickResolutionTrampolineOffset);
437 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
438 GetQuickToInterpreterBridgeOffset);
439#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700440
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700441 os << "IMAGE PATCH DELTA:\n";
442 os << StringPrintf("%d (0x%08x)\n\n",
443 oat_header.GetImagePatchDelta(),
444 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700445
Brian Carlstrom28db0122012-10-18 16:20:41 -0700446 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
447 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
448
449 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800450 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700451
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700452 // Print the key-value store.
453 {
454 os << "KEY VALUE STORE:\n";
455 size_t index = 0;
456 const char* key;
457 const char* value;
458 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
459 os << key << " = " << value << "\n";
460 index++;
461 }
462 os << "\n";
463 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700464
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800465 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700466 os << "BEGIN:\n";
467 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700468
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700469 os << "END:\n";
470 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
471 }
472
473 os << "SIZE:\n";
474 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700475
476 os << std::flush;
477
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800478 // If set, adjust relative address to be searched
479 if (options_.addr2instr_ != 0) {
480 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
481 os << "SEARCH ADDRESS (executable offset + input):\n";
482 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
483 }
484
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800485 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
486 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700487 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800488
489 // If file export selected skip file analysis
490 if (options_.export_dex_location_) {
491 if (!ExportDexFile(os, *oat_dex_file)) {
492 success = false;
493 }
494 } else {
495 if (!DumpOatDexFile(os, *oat_dex_file)) {
496 success = false;
497 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700498 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700499 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700500 os << std::flush;
501 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700502 }
503
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800504 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700505 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
506 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800507 return 0; // Address not in oat file
508 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800509 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
510 reinterpret_cast<uintptr_t>(oat_file_.Begin());
511 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800513 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800514 return end_offset - begin_offset;
515 }
516
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800517 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700518 return oat_file_.GetOatHeader().GetInstructionSet();
519 }
520
Mathieu Chartier90443472015-07-16 20:32:27 -0700521 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800522 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
523 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700524 CHECK(oat_dex_file != nullptr);
525 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700526 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
527 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700528 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
529 << "': " << error_msg;
530 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800531 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700532 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800533 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700534 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700535 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100536 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100538 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800539 }
540 }
541 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700542 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800543 }
544
Brian Carlstromaded5f72011-10-07 17:15:04 -0700545 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800546 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800547 // We don't know the length of the code for each method, but we need to know where to stop
548 // when disassembling. What we do know is that a region of code will be followed by some other
549 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
550 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800551 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
552 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700553 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700554 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700555 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
556 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700557 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
558 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800559 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800560 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800561 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800562 for (size_t class_def_index = 0;
563 class_def_index < dex_file->NumClassDefs();
564 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800565 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100566 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700567 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700568 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 ClassDataItemIterator it(*dex_file, class_data);
570 SkipAllFields(it);
571 uint32_t class_method_index = 0;
572 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100573 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800574 it.Next();
575 }
576 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100577 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800578 it.Next();
579 }
580 }
581 }
582 }
583
584 // If the last thing in the file is code for a method, there won't be an offset for the "next"
585 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
586 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800587 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800588 }
589
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700590 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
591 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
592 }
593
Elliott Hughese3c845c2012-02-28 17:23:01 -0800594 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800595 uint32_t code_offset = oat_method.GetCodeOffset();
596 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
597 code_offset &= ~0x1;
598 }
599 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800600 offsets_.insert(oat_method.GetMappingTableOffset());
601 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800602 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800603 }
604
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700605 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
606 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800607 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700608 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800609 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800610 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700611
612 // Create the verifier early.
613
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700614 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700615 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
616 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700617 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700618 os << std::flush;
619 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700620 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100621
622 VariableIndentationOutputStream vios(&os);
623 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800624 for (size_t class_def_index = 0;
625 class_def_index < dex_file->NumClassDefs();
626 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700627 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
628 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800629
630 // TODO: Support regex
631 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
632 continue;
633 }
634
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700635 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100636 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700637 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
638 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100639 << " (" << oat_class.GetStatus() << ")"
640 << " (" << oat_class.GetType() << ")\n";
641 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800642 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700643 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700644 success = false;
645 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800646 if (stop_analysis) {
647 os << std::flush;
648 return success;
649 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700650 }
651
652 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700653 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700654 }
655
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800656 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
657 std::string error_msg;
658 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
659
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700660 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800661 if (dex_file == nullptr) {
662 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
663 return false;
664 }
665 size_t fsize = oat_dex_file.FileSize();
666
667 // Some quick checks just in case
668 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
669 os << "Invalid dex file\n";
670 return false;
671 }
672
673 // Verify output directory exists
674 if (!OS::DirectoryExists(options_.export_dex_location_)) {
675 // TODO: Extend OS::DirectoryExists if symlink support is required
676 os << options_.export_dex_location_ << " output directory not found or symlink\n";
677 return false;
678 }
679
680 // Beautify path names
681 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
682 return false;
683 }
684
685 std::string dex_orig_name;
686 size_t dex_orig_pos = dex_file_location.rfind('/');
687 if (dex_orig_pos == std::string::npos)
688 dex_orig_name = dex_file_location;
689 else
690 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
691
692 // A more elegant approach to efficiently name user installed apps is welcome
693 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
694 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
695 size_t apk_orig_pos = dex_file_location.rfind('/');
696 if (apk_orig_pos != std::string::npos) {
697 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
698 }
699 }
700
701 std::string out_dex_path(options_.export_dex_location_);
702 if (out_dex_path.back() != '/') {
703 out_dex_path.append("/");
704 }
705 out_dex_path.append(dex_orig_name);
706 out_dex_path.append("_export.dex");
707 if (out_dex_path.length() > PATH_MAX) {
708 return false;
709 }
710
711 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
712 if (file.get() == nullptr) {
713 os << "Failed to open output dex file " << out_dex_path;
714 return false;
715 }
716
717 if (!file->WriteFully(dex_file->Begin(), fsize)) {
718 os << "Failed to write dex file";
719 file->Erase();
720 return false;
721 }
722
723 if (file->FlushCloseOrErase() != 0) {
724 os << "Flush and close failed";
725 return false;
726 }
727
728 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
729 os << std::flush;
730
731 return true;
732 }
733
Elliott Hughese3c845c2012-02-28 17:23:01 -0800734 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700735 while (it.HasNextStaticField()) {
736 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700737 }
Ian Rogers0571d352011-11-03 19:51:38 -0700738 while (it.HasNextInstanceField()) {
739 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700740 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800741 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700742
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100743 bool DumpOatClass(VariableIndentationOutputStream* vios,
744 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800745 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700746 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800747 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700748 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700749 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100750 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700751 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800752 }
753 ClassDataItemIterator it(dex_file, class_data);
754 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700755 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700756 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100757 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700758 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800759 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700760 success = false;
761 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800762 if (addr_found) {
763 *stop_analysis = true;
764 return success;
765 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700766 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700767 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700768 }
Ian Rogers0571d352011-11-03 19:51:38 -0700769 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100770 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700771 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800772 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700773 success = false;
774 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800775 if (addr_found) {
776 *stop_analysis = true;
777 return success;
778 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700779 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700780 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700781 }
Ian Rogers0571d352011-11-03 19:51:38 -0700782 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100783 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700784 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700785 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800786
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700787 static constexpr uint32_t kPrologueBytes = 16;
788
789 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
790 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
791
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100792 bool DumpOatMethod(VariableIndentationOutputStream* vios,
793 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700794 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700795 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800796 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800797 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700798 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800799
800 // TODO: Support regex
801 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
802 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000803 return success;
804 }
805
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800806 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100807 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
808 class_method_index, pretty_method.c_str(),
809 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800810 if (options_.list_methods_) return success;
811
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800812 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
813 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
814 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
815 uint32_t code_offset = oat_method.GetCodeOffset();
816 uint32_t code_size = oat_method.GetQuickCodeSize();
817 if (resolved_addr2instr_ != 0) {
818 if (resolved_addr2instr_ > code_offset + code_size) {
819 return success;
820 } else {
821 *addr_found = true; // stop analyzing file at next iteration
822 }
823 }
824
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100825 // Everything below is indented at least once.
826 ScopedIndentation indent1(vios);
827
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800828 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100829 vios->Stream() << "DEX CODE:\n";
830 ScopedIndentation indent2(vios);
831 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700832 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700833
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700834 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700835 std::unique_ptr<verifier::MethodVerifier> verifier;
836 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700837 // We need to have the handle scope stay live until after the verifier since the verifier has
838 // a handle to the dex cache from hs.
839 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100840 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
841 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700842 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100843 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700844 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700845 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800846 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100847 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800848 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100849 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100850 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100851 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700852 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100853 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700854 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
855 oat_method_offsets_offset, oat_file_.Size());
856 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100857 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700858 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800859 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700860
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100861 ScopedIndentation indent2(vios);
862 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700863 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
864 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100865 vios->Stream() << StringPrintf("WARNING: "
866 "code offset 0x%08x is past end of file 0x%08zx.\n",
867 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700868 success = false;
869 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100870 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700871
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100872 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800873 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100874 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700875 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800876 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100877 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700878 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100879 vios->Stream() << StringPrintf("WARNING: "
880 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
881 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700882 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800883 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100884 ScopedIndentation indent3(vios);
885 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800886 }
887 }
888 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100889 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700890 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
891 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700892
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800893 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100894 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100896 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700897 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100898 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700899 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
900 method_header_offset, oat_file_.Size());
901 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700903 return false;
904 }
905
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100906 ScopedIndentation indent2(vios);
907 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800908 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700910 }
911 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100912 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700913 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100914 vios->Stream() << StringPrintf("WARNING: "
915 "mapping table offset 0x%08x is past end of file 0x%08zx. "
916 "mapping table offset was loaded from offset 0x%08x.\n",
917 mapping_table_offset, oat_file_.Size(),
918 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800920 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100921 ScopedIndentation indent3(vios);
922 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700923 }
924
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100925 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800926 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100927 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700928 }
929 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100930 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100932 vios->Stream() << StringPrintf("WARNING: "
933 "vmap table offset 0x%08x is past end of file 0x%08zx. "
934 "vmap table offset was loaded from offset 0x%08x.\n",
935 vmap_table_offset, oat_file_.Size(),
936 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700937 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800938 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100939 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800941 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700942 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100943 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700944
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100945 ScopedIndentation indent2(vios);
946 vios->Stream()
947 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
948 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
949 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
950 vios->Stream() << "\n";
951 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
952 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
953 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700954 }
955 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100956 // Based on spill masks from QuickMethodFrameInfo so placed
957 // after it is dumped, but useful for understanding quick
958 // code, so dumped here.
959 ScopedIndentation indent2(vios);
960 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961 }
962 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100963 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700964 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
965 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100966 ScopedIndentation indent2(vios);
967 vios->Stream() << StringPrintf("WARNING: "
968 "code size offset 0x%08x is past end of file 0x%08zx.",
969 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700970 success = false;
971 } else {
972 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700973 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
974 uint64_t aligned_code_end = aligned_code_begin + code_size;
975
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800976 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100977 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700978 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100979 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
980 code_offset,
981 code_size_offset,
982 code_size,
983 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700984
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100985 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700986 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100987 vios->Stream() << StringPrintf("WARNING: "
988 "start of code at 0x%08x is past end of file 0x%08zx.",
989 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700990 success = false;
991 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100992 vios->Stream() << StringPrintf(
993 "WARNING: "
994 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
995 "code size is 0x%08x loaded from offset 0x%08x.\n",
996 aligned_code_end, oat_file_.Size(),
997 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700998 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800999 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001000 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001001 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001002 }
1003 }
1004 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001005 vios->Stream() << StringPrintf(
1006 "WARNING: "
1007 "code size %d is bigger than max expected threshold of %d. "
1008 "code size is 0x%08x loaded from offset 0x%08x.\n",
1009 code_size, kMaxCodeSize,
1010 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001011 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001012 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001013 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001014 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001015 }
1016 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001017 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001018 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001019 }
1020 }
1021 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001022 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001023 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001024 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001025
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001026 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1027 if (spill_mask == 0) {
1028 return;
1029 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001030 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001031 for (size_t i = 0; i < 32; i++) {
1032 if ((spill_mask & (1 << i)) != 0) {
1033 if (is_float) {
1034 os << "fr" << i;
1035 } else {
1036 os << "r" << i;
1037 }
1038 spill_mask ^= 1 << i; // clear bit
1039 if (spill_mask != 0) {
1040 os << ", ";
1041 } else {
1042 break;
1043 }
1044 }
1045 }
1046 os << ")";
1047 }
1048
Roland Levillain442b46a2015-02-18 16:54:21 +00001049 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001050 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001051 const OatFile::OatMethod& oat_method,
1052 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001053 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1054 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001055 const void* raw_code_info = oat_method.GetVmapTable();
1056 if (raw_code_info != nullptr) {
1057 CodeInfo code_info(raw_code_info);
1058 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001059 ScopedIndentation indent1(vios);
1060 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001061 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001062 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1063 // We don't encode the size in the table, so just emit that we have quickened
1064 // information.
1065 ScopedIndentation indent(vios);
1066 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001067 } else {
1068 // Otherwise, display the vmap table.
1069 const uint8_t* raw_table = oat_method.GetVmapTable();
1070 if (raw_table != nullptr) {
1071 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001072 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001073 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001074 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001075 }
1076
1077 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001078 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001079 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001080 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001081 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001082 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001083 oat_method.GetCodeOffset(),
1084 code_item.registers_size_,
1085 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001086 }
1087
Roland Levillain442b46a2015-02-18 16:54:21 +00001088 // Display a vmap table.
1089 void DumpVmapTable(std::ostream& os,
1090 const OatFile::OatMethod& oat_method,
1091 const VmapTable& vmap_table) {
1092 bool first = true;
1093 bool processing_fp = false;
1094 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1095 for (size_t i = 0; i < vmap_table.Size(); i++) {
1096 uint16_t dex_reg = vmap_table[i];
1097 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1098 processing_fp ? kFloatVReg : kIntVReg);
1099 os << (first ? "v" : ", v") << dex_reg;
1100 if (!processing_fp) {
1101 os << "/r" << cpu_reg;
1102 } else {
1103 os << "/fr" << cpu_reg;
1104 }
1105 first = false;
1106 if (!processing_fp && dex_reg == 0xFFFF) {
1107 processing_fp = true;
1108 spill_mask = oat_method.GetFpSpillMask();
1109 }
1110 }
1111 os << "\n";
1112 }
1113
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001114 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1115 const DexFile::CodeItem* code_item) {
1116 if (code_item != nullptr) {
1117 size_t num_locals_ins = code_item->registers_size_;
1118 size_t num_ins = code_item->ins_size_;
1119 size_t num_locals = num_locals_ins - num_ins;
1120 size_t num_outs = code_item->outs_size_;
1121
1122 os << "vr_stack_locations:";
1123 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1124 // For readability, delimit the different kinds of VRs.
1125 if (reg == num_locals_ins) {
1126 os << "\n\tmethod*:";
1127 } else if (reg == num_locals && num_ins > 0) {
1128 os << "\n\tins:";
1129 } else if (reg == 0 && num_locals > 0) {
1130 os << "\n\tlocals:";
1131 }
1132
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001133 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1134 code_item,
1135 oat_method.GetCoreSpillMask(),
1136 oat_method.GetFpSpillMask(),
1137 oat_method.GetFrameSizeInBytes(),
1138 reg,
1139 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001140 os << " v" << reg << "[sp + #" << offset << "]";
1141 }
1142
1143 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1144 if (out_reg == 0) {
1145 os << "\n\touts:";
1146 }
1147
1148 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1149 os << " v" << out_reg << "[sp + #" << offset << "]";
1150 }
1151
1152 os << "\n";
1153 }
1154 }
1155
Ian Rogersb23a7722012-10-09 16:54:26 -07001156 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001157 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001158 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001159 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001160 const VmapTable vmap_table(raw_table);
1161 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001162 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001163 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1164 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1165 : oat_method.GetCoreSpillMask();
1166 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001167 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001168 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1169 code_item,
1170 oat_method.GetCoreSpillMask(),
1171 oat_method.GetFpSpillMask(),
1172 oat_method.GetFrameSizeInBytes(),
1173 reg,
1174 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001175 os << "[sp + #" << offset << "]";
1176 }
1177 }
1178 }
1179
Ian Rogersef7d42f2014-01-06 12:55:46 -08001180 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1181 const DexFile::CodeItem* code_item,
1182 size_t num_regs, const uint8_t* reg_bitmap) {
1183 bool first = true;
1184 for (size_t reg = 0; reg < num_regs; reg++) {
1185 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1186 if (first) {
1187 os << " v" << reg << " (";
1188 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1189 os << ")";
1190 first = false;
1191 } else {
1192 os << ", v" << reg << " (";
1193 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1194 os << ")";
1195 }
1196 }
1197 }
1198 if (first) {
1199 os << "No registers in GC map\n";
1200 } else {
1201 os << "\n";
1202 }
1203 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001204 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1205 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001206 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001207 if (gc_map_raw == nullptr) {
1208 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001209 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001210 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001211 NativePcOffsetToReferenceMap map(gc_map_raw);
1212 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1213 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1214 map.GetNativePcOffset(entry);
1215 os << StringPrintf("%p", native_pc);
1216 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001217 }
1218 }
1219
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001220 void DumpMappingTable(VariableIndentationOutputStream* vios,
1221 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001222 const void* quick_code = oat_method.GetQuickCode();
1223 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001224 return;
1225 }
Ian Rogers1809a722013-08-09 22:05:32 -07001226 MappingTable table(oat_method.GetMappingTable());
1227 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001228 if (table.PcToDexSize() != 0) {
1229 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001230 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001231 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001232 ScopedIndentation indent1(vios);
1233 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001234 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001235 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001236 }
1237 if (table.DexToPcSize() != 0) {
1238 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001239 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001240 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001241 ScopedIndentation indent1(vios);
1242 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001243 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001244 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001245 }
1246 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001247 }
1248
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001249 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001250 const OatFile::OatMethod& oat_method,
1251 const DexFile::CodeItem* code_item,
1252 size_t offset,
1253 bool suspend_point_mapping) {
1254 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1255 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001256 ScopedIndentation indent1(vios);
1257 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001258 }
1259 // The return value is not used in the case of a method compiled
1260 // with the optimizing compiler.
1261 return DexFile::kDexNoIndex;
1262 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001263 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001264 }
1265 }
1266
Ian Rogers1809a722013-08-09 22:05:32 -07001267 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1268 size_t offset, bool suspend_point_mapping) {
1269 MappingTable table(oat_method.GetMappingTable());
1270 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1271 typedef MappingTable::PcToDexIterator It;
1272 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1273 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001274 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001275 return cur.DexPc();
1276 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001277 }
Ian Rogers1809a722013-08-09 22:05:32 -07001278 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1279 typedef MappingTable::DexToPcIterator It;
1280 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1281 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001282 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001283 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001284 }
1285 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001286 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001287 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001288 }
1289
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001290 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1291 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001292 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001293 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001294 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001295 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001296 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001297 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001298 bool first = true;
1299 for (size_t reg = 0; reg < num_regs; reg++) {
1300 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1301 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001302 os << "GC map objects: v" << reg << " (";
1303 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001304 os << ")";
1305 first = false;
1306 } else {
1307 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001308 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001309 os << ")";
1310 }
1311 }
1312 }
1313 if (!first) {
1314 os << "\n";
1315 }
1316 }
1317 }
1318 }
1319
Mathieu Chartier590fee92013-09-13 13:46:47 -07001320 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1321 const OatFile::OatMethod& oat_method,
1322 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1323 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001324 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001325 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001326 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1327 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1328 if (kind != kUndefined) {
1329 if (first) {
1330 os << "VRegs: v";
1331 first = false;
1332 } else {
1333 os << ", v";
1334 }
1335 os << reg << " (";
1336 switch (kind) {
1337 case kImpreciseConstant:
1338 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1339 DescribeVReg(os, oat_method, code_item, reg, kind);
1340 break;
1341 case kConstant:
1342 os << "Constant: " << kinds.at((reg * 2) + 1);
1343 break;
1344 default:
1345 DescribeVReg(os, oat_method, code_item, reg, kind);
1346 break;
1347 }
1348 os << ")";
1349 }
1350 }
1351 if (!first) {
1352 os << "\n";
1353 }
1354 }
1355
1356
Ian Rogersb23a7722012-10-09 16:54:26 -07001357 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001358 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001359 size_t i = 0;
1360 while (i < code_item->insns_size_in_code_units_) {
1361 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001362 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1363 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001364 i += instruction->SizeInCodeUnits();
1365 }
1366 }
1367 }
1368
Roland Levillainf2650d12015-05-28 14:53:28 +01001369 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1370 // the optimizing compiler?
1371 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1372 const DexFile::CodeItem* code_item) {
1373 // If the native GC map is null and the Dex `code_item` is not
1374 // null, then this method has been compiled with the optimizing
1375 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001376 return oat_method.GetQuickCode() != nullptr &&
1377 oat_method.GetGcMap() == nullptr &&
1378 code_item != nullptr;
1379 }
1380
1381 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1382 // the dextodex compiler?
1383 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1384 const DexFile::CodeItem* code_item) {
1385 // If the quick code is null, the Dex `code_item` is not
1386 // null, and the vmap table is not null, then this method has been compiled
1387 // with the dextodex compiler.
1388 return oat_method.GetQuickCode() == nullptr &&
1389 oat_method.GetVmapTable() != nullptr &&
1390 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001391 }
1392
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001393 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001394 const OatFile::OatMethod& oat_method,
1395 const DexFile::CodeItem* code_item,
1396 size_t offset) {
1397 // This method is only relevant for oat methods compiled with the
1398 // optimizing compiler.
1399 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1400
1401 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1402 const void* raw_code_info = oat_method.GetVmapTable();
1403 if (raw_code_info != nullptr) {
1404 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001405 StackMapEncoding encoding = code_info.ExtractEncoding();
1406 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001407 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001408 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1409 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001410 }
1411 }
1412 }
1413
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001414 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001415 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001416 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001417 const DexFile* dex_file,
1418 const DexFile::ClassDef& class_def,
1419 const DexFile::CodeItem* code_item,
1420 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001421 if ((method_access_flags & kAccNative) == 0) {
1422 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001423 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001424 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001425 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1426 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001427 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001428 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001429 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1430 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001431 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001432
1433 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001434 }
1435
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001436 void DumpCode(VariableIndentationOutputStream* vios,
1437 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001438 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1439 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001440 const void* quick_code = oat_method.GetQuickCode();
1441
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001442 if (code_size == 0) {
1443 code_size = oat_method.GetQuickCodeSize();
1444 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001445 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001446 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001447 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001448 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001449 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1450 size_t offset = 0;
1451 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001452 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001453 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001454 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001455 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001456 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001457 uint32_t dex_pc =
1458 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001459 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001460 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001461 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001462 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001463 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001464 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001465 }
1466 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001467 }
1468 }
1469
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001470 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001471 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001472 const OatDumperOptions& options_;
1473 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001474 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001475 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001476 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001477};
1478
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001479class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001480 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001481 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1482 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001483 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001484 vios_(os),
1485 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001486 image_space_(image_space),
1487 image_header_(image_header),
1488 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001489
Mathieu Chartier90443472015-07-16 20:32:27 -07001490 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001492 std::ostream& indent_os = vios_.Stream();
1493
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001494 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001495
Jeff Haodcdc85b2015-12-04 14:06:18 -08001496 os << "IMAGE LOCATION: " << image_space_.GetImageLocation() << "\n\n";
1497
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001498 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001499
Mathieu Chartiere401d142015-04-22 13:56:20 -07001500 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1501
1502 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1503 auto section = static_cast<ImageHeader::ImageSections>(i);
1504 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1505 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001506
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001507 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001508
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001509 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001510
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001511 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1512
1513 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1514
1515 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001516
Alex Lighta59dd802014-07-02 16:28:08 -07001517 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1518
Igor Murashkin46774762014-10-22 11:37:02 -07001519 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1520
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001521 {
1522 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001523 static_assert(arraysize(image_roots_descriptions_) ==
1524 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001525 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1526 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1527 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001528 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001529 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001531 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001532 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001533 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001534 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1535 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001536 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001537 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1538 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001539 run++;
1540 } else {
1541 break;
1542 }
1543 }
1544 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001545 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001546 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001547 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001548 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001549 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001550 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001551 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001552 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001553 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001554 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001555 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001556 }
1557 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001558 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001559
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001560 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001561 os << "METHOD ROOTS\n";
1562 static_assert(arraysize(image_methods_descriptions_) ==
1563 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1564 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1565 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1566 const char* description = image_methods_descriptions_[i];
1567 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001568 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001569 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001570 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001572
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001573 Runtime* const runtime = Runtime::Current();
1574 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001575 std::string image_filename = image_space_.GetImageFilename();
1576 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001577 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001578 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001579 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001580 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1581 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001582 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001583 oat_file = OatFile::Open(oat_location, oat_location,
1584 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001585 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001586 if (oat_file == nullptr) {
1587 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001588 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001589 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001590 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001591 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001592
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001593 stats_.oat_file_bytes = oat_file->Size();
1594
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001595 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001596
Mathieu Chartier02e25112013-08-14 16:14:24 -07001597 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001598 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001599 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1600 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001601 }
1602
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001603 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001604
Jeff Haodcdc85b2015-12-04 14:06:18 -08001605 // Loop through the image space and dump its objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001606 gc::Heap* heap = runtime->GetHeap();
Ian Rogers50b35e22012-10-04 10:09:15 -07001607 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001608 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001609 {
1610 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1611 heap->FlushAllocStack();
1612 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001613 // Since FlushAllocStack() above resets the (active) allocation
1614 // stack. Need to revoke the thread-local allocation stacks that
1615 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001616 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001617 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001618 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001619 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001620 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001621 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001622 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001623 {
1624 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001625 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001626 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001627 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001628 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001629 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001630 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001631 }
1632 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Jeff Haodcdc85b2015-12-04 14:06:18 -08001634 // Dump the normal objects before ArtMethods.
1635 image_space_.GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1636 indent_os << "\n";
1637 // TODO: Dump fields.
1638 // Dump methods after.
1639 const auto& methods_section = image_header_.GetMethodsSection();
1640 const size_t pointer_size =
1641 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1642 DumpArtMethodVisitor visitor(this);
1643 methods_section.VisitPackedArtMethods(&visitor, image_space_.Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001645 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001646 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001647 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001648 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001649 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001650 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001651 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001652 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001653 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001654 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001655 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001656 size_t header_bytes = sizeof(ImageHeader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001657 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1658 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1659 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001660 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1661 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001662 const auto& intern_section = image_header_.GetImageSection(
1663 ImageHeader::kSectionInternedStrings);
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001664 const auto& class_table_section = image_header_.GetImageSection(
1665 ImageHeader::kSectionClassTable);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001666 stats_.header_bytes = header_bytes;
Vladimir Markocf36d492015-08-12 19:27:26 +01001667 stats_.alignment_bytes += RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1668 // Add padding between the field and method section.
1669 // (Field section is 4-byte aligned, method section is 8-byte aligned on 64-bit targets.)
Vladimir Marko05792b92015-08-03 11:56:49 +01001670 stats_.alignment_bytes += method_section.Offset() -
1671 (field_section.Offset() + field_section.Size());
1672 // Add padding between the dex cache arrays section and the intern table. (Dex cache
1673 // arrays section is 4-byte aligned on 32-bit targets, intern table is 8-byte aligned.)
1674 stats_.alignment_bytes += intern_section.Offset() -
1675 (dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001676 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1677 stats_.bitmap_bytes += bitmap_section.Size();
1678 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001679 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001680 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001681 stats_.interned_strings_bytes += intern_section.Size();
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08001682 stats_.class_table_bytes += class_table_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001683 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001684 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001685
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001687
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001688 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001689 }
1690
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001691 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001692 class DumpArtMethodVisitor : public ArtMethodVisitor {
1693 public:
1694 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1695
1696 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1697 std::ostream& indent_os = image_dumper_->vios_.Stream();
1698 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1699 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1700 indent_os << "\n";
1701 }
1702
1703 private:
1704 ImageDumper* const image_dumper_;
1705 };
1706
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001707 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001708 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001709 CHECK(type != nullptr);
1710 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001711 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001712 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001713 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001715 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001716 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001717 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001718 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001719 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001720 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001721 }
1722 }
1723
Mathieu Chartierc7853442015-03-27 14:35:38 -07001724 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001725 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001726 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001727 switch (field->GetTypeAsPrimitiveType()) {
1728 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001729 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001730 break;
1731 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001732 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001733 break;
1734 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001735 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001736 break;
1737 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001738 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001739 break;
1740 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001741 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001742 break;
1743 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001744 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001745 break;
1746 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001747 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1748 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001749 break;
1750 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001751 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001752 break;
1753 case Primitive::kPrimNot: {
1754 // Get the value, don't compute the type unless it is non-null as we don't want
1755 // to cause class loading.
1756 mirror::Object* value = field->GetObj(obj);
1757 if (value == nullptr) {
1758 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001759 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001760 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001761 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001762 if (field_type != nullptr) {
1763 PrettyObjectValue(os, field_type, value);
1764 } else {
1765 os << StringPrintf("%p %s\n", value,
1766 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1767 }
Ian Rogers50239c72013-06-17 14:53:22 -07001768 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001769 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001770 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001771 default:
1772 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1773 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001774 }
1775 }
1776
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001777 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001778 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001779 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001780 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001782 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001783 for (ArtField& field : klass->GetIFields()) {
1784 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001785 }
1786 }
1787
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001788 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001789 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001790 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001791
Mathieu Chartiere401d142015-04-22 13:56:20 -07001792 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001793 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001794 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1795 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001796 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001797 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001798 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001799 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001800 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001801 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001802 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001803 }
1804
Mathieu Chartiere401d142015-04-22 13:56:20 -07001805 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001806 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001807 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1808 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001809 return 0;
1810 }
1811 return oat_code_begin[-1];
1812 }
1813
Mathieu Chartiere401d142015-04-22 13:56:20 -07001814 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001815 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001816 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001817 if (oat_code_begin == nullptr) {
1818 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001819 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001820 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001821 }
1822
Mathieu Chartier90443472015-07-16 20:32:27 -07001823 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001824 DCHECK(obj != nullptr);
1825 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001826 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001827 if (!state->InDumpSpace(obj)) {
1828 return;
1829 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001830
1831 size_t object_bytes = obj->SizeOf();
1832 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1833 state->stats_.object_bytes += object_bytes;
1834 state->stats_.alignment_bytes += alignment_bytes;
1835
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001836 std::ostream& os = state->vios_.Stream();
1837
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001838 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001839 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001840 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1841 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001842 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001843 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001844 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1845 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001846 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001848 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001849 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001850 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001851 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001852 ScopedIndentation indent1(&state->vios_);
1853 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001854 const auto image_pointer_size =
1855 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001856 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001857 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1858 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001859 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001860 size_t run = 0;
1861 for (int32_t j = i + 1; j < length; j++) {
1862 if (value == obj_array->Get(j)) {
1863 run++;
1864 } else {
1865 break;
1866 }
1867 }
1868 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001869 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001870 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001871 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001872 i = i + run;
1873 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001874 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001875 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001876 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001877 }
1878 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001879 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001880 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001881 os << "STATICS:\n";
1882 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001883 for (ArtField& field : klass->GetSFields()) {
1884 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001885 }
1886 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001887 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001888 auto it = state->dex_caches_.find(obj);
1889 if (it != state->dex_caches_.end()) {
1890 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001891 const auto& field_section = state->image_header_.GetImageSection(
1892 ImageHeader::kSectionArtFields);
1893 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001894 size_t num_methods = dex_cache->NumResolvedMethods();
1895 if (num_methods != 0u) {
1896 os << "Methods (size=" << num_methods << "):";
1897 ScopedIndentation indent2(&state->vios_);
1898 auto* resolved_methods = dex_cache->GetResolvedMethods();
1899 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1900 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1901 size_t run = 0;
1902 for (size_t j = i + 1;
1903 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1904 j,
1905 image_pointer_size);
1906 ++j, ++run) {}
1907 if (run == 0) {
1908 os << StringPrintf("%zd: ", i);
1909 } else {
1910 os << StringPrintf("%zd to %zd: ", i, i + run);
1911 i = i + run;
1912 }
1913 std::string msg;
1914 if (elem == nullptr) {
1915 msg = "null";
1916 } else if (method_section.Contains(
1917 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1918 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1919 } else {
1920 msg = "<not in method section>";
1921 }
1922 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001923 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001924 }
1925 size_t num_fields = dex_cache->NumResolvedFields();
1926 if (num_fields != 0u) {
1927 os << "Fields (size=" << num_fields << "):";
1928 ScopedIndentation indent2(&state->vios_);
1929 auto* resolved_fields = dex_cache->GetResolvedFields();
1930 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1931 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1932 size_t run = 0;
1933 for (size_t j = i + 1;
1934 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1935 j,
1936 image_pointer_size);
1937 ++j, ++run) {}
1938 if (run == 0) {
1939 os << StringPrintf("%zd: ", i);
1940 } else {
1941 os << StringPrintf("%zd to %zd: ", i, i + run);
1942 i = i + run;
1943 }
1944 std::string msg;
1945 if (elem == nullptr) {
1946 msg = "null";
1947 } else if (field_section.Contains(
1948 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1949 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1950 } else {
1951 msg = "<not in field section>";
1952 }
1953 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001954 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001955 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001956 }
1957 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001958 std::string temp;
1959 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001960 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001961
Mathieu Chartiere401d142015-04-22 13:56:20 -07001962 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001963 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001964 DCHECK(method != nullptr);
1965 const auto image_pointer_size =
1966 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001967 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1968 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001969 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1970 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001971 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001972 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
1973 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1974 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
1975 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001976 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001977 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001978 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001979 if (first_occurrence) {
1980 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1981 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001982 if (quick_oat_code_begin !=
1983 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1984 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001985 }
1986 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1987 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1988 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001989 } else {
1990 const DexFile::CodeItem* code_item = method->GetCodeItem();
1991 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1992 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1993
1994 bool first_occurrence;
1995 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001996 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001997 if (first_occurrence) {
1998 state->stats_.gc_map_bytes += gc_map_bytes;
1999 }
2000
2001 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002002 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002003 if (first_occurrence) {
2004 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2005 }
2006
Roland Levillain6d7f1792015-07-02 10:59:15 +01002007 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002008 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002009 // Method compiled with the optimizing compiler have no vmap table.
2010 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002011 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002012 if (first_occurrence) {
2013 state->stats_.vmap_table_bytes += vmap_table_bytes;
2014 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015 }
2016
Mathieu Chartiere401d142015-04-22 13:56:20 -07002017 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2018 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2019 if (first_occurrence) {
2020 state->stats_.managed_code_bytes += quick_oat_code_size;
2021 if (method->IsConstructor()) {
2022 if (method->IsStatic()) {
2023 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2024 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2025 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2026 }
2027 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2028 state->stats_.large_method_code_bytes += quick_oat_code_size;
2029 }
2030 }
2031 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2032
Igor Murashkin7617abd2015-07-10 18:27:47 -07002033 uint32_t method_access_flags = method->GetAccessFlags();
2034
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002036 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2037 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2038 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002039
2040 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002041 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002042
2043 double expansion =
2044 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2045 state->stats_.ComputeOutliers(total_size, expansion, method);
2046 }
2047 }
2048
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002049 std::set<const void*> already_seen_;
2050 // Compute the size of the given data within the oat file and whether this is the first time
2051 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002052 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002053 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002054 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002055 already_seen_.insert(oat_data);
2056 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002057 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002058 }
2059 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002060 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002061
2062 public:
2063 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002064 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002065 size_t file_bytes;
2066
2067 size_t header_bytes;
2068 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002069 size_t art_field_bytes;
2070 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002071 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002072 size_t interned_strings_bytes;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002073 size_t class_table_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002074 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002075 size_t alignment_bytes;
2076
2077 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002078 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002079 size_t managed_to_native_code_bytes;
2080 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002081 size_t class_initializer_code_bytes;
2082 size_t large_initializer_code_bytes;
2083 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002084
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002085 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002086 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002087 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002088
2089 size_t dex_instruction_bytes;
2090
Mathieu Chartiere401d142015-04-22 13:56:20 -07002091 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002092 std::vector<size_t> method_outlier_size;
2093 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002094 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002095
Roland Levillain3887c462015-08-12 18:15:42 +01002096 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002097 : oat_file_bytes(0),
2098 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002099 header_bytes(0),
2100 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002101 art_field_bytes(0),
2102 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002103 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002104 interned_strings_bytes(0),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002105 class_table_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002106 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002107 alignment_bytes(0),
2108 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002109 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002110 managed_to_native_code_bytes(0),
2111 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002112 class_initializer_code_bytes(0),
2113 large_initializer_code_bytes(0),
2114 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002115 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002116 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002117 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002118 dex_instruction_bytes(0) {}
2119
Elliott Hughesa0e18062012-04-13 15:59:59 -07002120 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002121 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002122 size_t bytes;
2123 size_t count;
2124 };
2125 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2126 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002127
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002128 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002129 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2130 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002131 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002132 it->second.count += 1;
2133 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002134 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002135 }
2136 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002137
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002138 double PercentOfOatBytes(size_t size) {
2139 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2140 }
2141
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002142 double PercentOfFileBytes(size_t size) {
2143 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2144 }
2145
2146 double PercentOfObjectBytes(size_t size) {
2147 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2148 }
2149
Mathieu Chartiere401d142015-04-22 13:56:20 -07002150 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002151 method_outlier_size.push_back(total_size);
2152 method_outlier_expansion.push_back(expansion);
2153 method_outlier.push_back(method);
2154 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002155
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002156 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002157 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002158 size_t sum_of_sizes = 0;
2159 size_t sum_of_sizes_squared = 0;
2160 size_t sum_of_expansion = 0;
2161 size_t sum_of_expansion_squared = 0;
2162 size_t n = method_outlier_size.size();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002163 if (n == 0) {
2164 return;
2165 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002166 for (size_t i = 0; i < n; i++) {
2167 size_t cur_size = method_outlier_size[i];
2168 sum_of_sizes += cur_size;
2169 sum_of_sizes_squared += cur_size * cur_size;
2170 double cur_expansion = method_outlier_expansion[i];
2171 sum_of_expansion += cur_expansion;
2172 sum_of_expansion_squared += cur_expansion * cur_expansion;
2173 }
2174 size_t size_mean = sum_of_sizes / n;
2175 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2176 double expansion_mean = sum_of_expansion / n;
2177 double expansion_variance =
2178 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2179
2180 // Dump methods whose size is a certain number of standard deviations from the mean
2181 size_t dumped_values = 0;
2182 size_t skipped_values = 0;
2183 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2184 size_t cur_size_variance = i * i * size_variance;
2185 bool first = true;
2186 for (size_t j = 0; j < n; j++) {
2187 size_t cur_size = method_outlier_size[j];
2188 if (cur_size > size_mean) {
2189 size_t cur_var = cur_size - size_mean;
2190 cur_var = cur_var * cur_var;
2191 if (cur_var > cur_size_variance) {
2192 if (dumped_values > 20) {
2193 if (i == 1) {
2194 skipped_values++;
2195 } else {
2196 i = 2; // jump to counting for 1 standard deviation
2197 break;
2198 }
2199 } else {
2200 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002201 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002202 first = false;
2203 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002204 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002205 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002206 method_outlier_size[j] = 0; // don't consider this method again
2207 dumped_values++;
2208 }
2209 }
2210 }
2211 }
2212 }
2213 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002214 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002215 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002216 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002217 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002218
2219 // Dump methods whose expansion is a certain number of standard deviations from the mean
2220 dumped_values = 0;
2221 skipped_values = 0;
2222 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2223 double cur_expansion_variance = i * i * expansion_variance;
2224 bool first = true;
2225 for (size_t j = 0; j < n; j++) {
2226 double cur_expansion = method_outlier_expansion[j];
2227 if (cur_expansion > expansion_mean) {
2228 size_t cur_var = cur_expansion - expansion_mean;
2229 cur_var = cur_var * cur_var;
2230 if (cur_var > cur_expansion_variance) {
2231 if (dumped_values > 20) {
2232 if (i == 1) {
2233 skipped_values++;
2234 } else {
2235 i = 2; // jump to counting for 1 standard deviation
2236 break;
2237 }
2238 } else {
2239 if (first) {
2240 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002241 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002242 first = false;
2243 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002244 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002245 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002246 method_outlier_expansion[j] = 0.0; // don't consider this method again
2247 dumped_values++;
2248 }
2249 }
2250 }
2251 }
2252 }
2253 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002254 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002255 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002256 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002257 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002258 }
2259
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002260 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002261 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002262 {
2263 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2264 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002265 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2266 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2267 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2268 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2269 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2270 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002271 "class_table_bytes = %8zd (%2.0f%% of art file bytes)\n"
Vladimir Marko05792b92015-08-03 11:56:49 +01002272 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2273 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002274 header_bytes, PercentOfFileBytes(header_bytes),
2275 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002276 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2277 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002278 dex_cache_arrays_bytes,
2279 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002280 interned_strings_bytes,
2281 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002282 class_table_bytes, PercentOfFileBytes(class_table_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002283 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002284 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2285 << std::flush;
Mathieu Chartier208a5cb2015-12-02 15:44:07 -08002286 CHECK_EQ(file_bytes,
2287 header_bytes + object_bytes + art_field_bytes + art_method_bytes +
2288 dex_cache_arrays_bytes + interned_strings_bytes + class_table_bytes +
2289 bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002290 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002291
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002292 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002293 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002294 for (const auto& sizes_and_count : sizes_and_counts) {
2295 const std::string& descriptor(sizes_and_count.first);
2296 double average = static_cast<double>(sizes_and_count.second.bytes) /
2297 static_cast<double>(sizes_and_count.second.count);
2298 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002299 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002300 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002301 descriptor.c_str(), sizes_and_count.second.bytes,
2302 sizes_and_count.second.count, average, percent);
2303 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002304 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002305 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002306 CHECK_EQ(object_bytes, object_bytes_total);
2307
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002308 os << StringPrintf("oat_file_bytes = %8zd\n"
2309 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2310 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2311 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2312 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2313 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2314 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002315 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002316 managed_code_bytes,
2317 PercentOfOatBytes(managed_code_bytes),
2318 managed_to_native_code_bytes,
2319 PercentOfOatBytes(managed_to_native_code_bytes),
2320 native_to_managed_code_bytes,
2321 PercentOfOatBytes(native_to_managed_code_bytes),
2322 class_initializer_code_bytes,
2323 PercentOfOatBytes(class_initializer_code_bytes),
2324 large_initializer_code_bytes,
2325 PercentOfOatBytes(large_initializer_code_bytes),
2326 large_method_code_bytes,
2327 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002329 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002331 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2332 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002333 }
2334
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002335 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2336 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2337 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002338 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2339 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2340 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002341 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002342
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002343 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2344 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002345 static_cast<double>(managed_code_bytes) /
2346 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002347 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002348 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002349 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002350
2351 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002352 }
2353 } stats_;
2354
2355 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002356 enum {
2357 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2358 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2359 kLargeConstructorDexBytes = 4000,
2360 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2361 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2362 kLargeMethodDexBytes = 16000
2363 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002364
2365 // For performance, use the *os_ directly for anything that doesn't need indentation
2366 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002367 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002368 VariableIndentationOutputStream vios_;
2369 ScopedIndentation indent1_;
2370
Ian Rogers1d54e732013-05-02 21:10:01 -07002371 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002372 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002373 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002374 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002375 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002376
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002377 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002378};
2379
Jeff Haodcdc85b2015-12-04 14:06:18 -08002380static int DumpImage(Runtime* runtime, OatDumperOptions* options, std::ostream* os) {
Andreas Gampe00b25f32014-09-17 21:49:05 -07002381 // Dumping the image, no explicit class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002382 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002383 options->class_loader_ = &null_class_loader;
2384
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002385 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002386 gc::Heap* heap = runtime->GetHeap();
Jeff Haodcdc85b2015-12-04 14:06:18 -08002387 std::vector<gc::space::ImageSpace*> image_spaces = heap->GetBootImageSpaces();
2388 CHECK(!image_spaces.empty());
2389 for (gc::space::ImageSpace* image_space : image_spaces) {
2390 const ImageHeader& image_header = image_space->GetImageHeader();
2391 if (!image_header.IsValid()) {
2392 fprintf(stderr, "Invalid image header %s\n", image_space->GetImageLocation().c_str());
2393 return EXIT_FAILURE;
2394 }
2395
2396 ImageDumper image_dumper(os, *image_space, image_header, options);
2397 if (!image_dumper.Dump()) {
2398 return EXIT_FAILURE;
2399 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07002400 }
Jeff Haodcdc85b2015-12-04 14:06:18 -08002401 return EXIT_SUCCESS;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002402}
2403
Andreas Gampe00b25f32014-09-17 21:49:05 -07002404static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2405 std::ostream* os) {
2406 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2407
2408 Thread* self = Thread::Current();
2409 CHECK(self != nullptr);
2410 // Need well-known-classes.
2411 WellKnownClasses::Init(self->GetJniEnv());
2412
2413 // Need to register dex files to get a working dex cache.
2414 ScopedObjectAccess soa(self);
2415 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002416 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002417 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002418 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2419 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002420 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002421 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002422 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002423 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002424 }
2425
2426 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002427 // Fake that we're a compiler.
Mathieu Chartier966878d2016-01-14 14:33:29 -08002428 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002429
2430 // Use the class loader while dumping.
2431 StackHandleScope<1> scope(self);
2432 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2433 soa.Decode<mirror::ClassLoader*>(class_loader));
2434 options->class_loader_ = &loader_handle;
2435
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002436 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002437 bool success = oat_dumper.Dump(*os);
2438 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2439}
2440
2441static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002442 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002443 // No image = no class loader.
Mathieu Chartier9865bde2015-12-21 09:58:16 -08002444 ScopedNullHandle<mirror::ClassLoader> null_class_loader;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002445 options->class_loader_ = &null_class_loader;
2446
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002447 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002448 bool success = oat_dumper.Dump(*os);
2449 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2450}
2451
2452static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2453 std::ostream* os) {
2454 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002455 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002456 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002457 if (oat_file == nullptr) {
2458 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2459 return EXIT_FAILURE;
2460 }
2461
2462 if (runtime != nullptr) {
2463 return DumpOatWithRuntime(runtime, oat_file, options, os);
2464 } else {
2465 return DumpOatWithoutRuntime(oat_file, options, os);
2466 }
2467}
2468
2469static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2470 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002471 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002472 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002473 if (oat_file == nullptr) {
2474 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2475 return EXIT_FAILURE;
2476 }
2477
2478 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002479 if (!oat_symbolizer.Symbolize()) {
2480 fprintf(stderr, "Failed to symbolize\n");
2481 return EXIT_FAILURE;
2482 }
2483
2484 return EXIT_SUCCESS;
2485}
2486
Igor Murashkin37743352014-11-13 14:38:00 -08002487struct OatdumpArgs : public CmdlineArgs {
2488 protected:
2489 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002490
Igor Murashkin37743352014-11-13 14:38:00 -08002491 virtual ParseStatus ParseCustom(const StringPiece& option,
2492 std::string* error_msg) OVERRIDE {
2493 {
2494 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2495 if (base_parse != kParseUnknownArgument) {
2496 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002497 }
2498 }
2499
Igor Murashkin37743352014-11-13 14:38:00 -08002500 if (option.starts_with("--oat-file=")) {
2501 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2502 } else if (option.starts_with("--image=")) {
2503 image_location_ = option.substr(strlen("--image=")).data();
2504 } else if (option =="--dump:raw_mapping_table") {
2505 dump_raw_mapping_table_ = true;
2506 } else if (option == "--dump:raw_gc_map") {
2507 dump_raw_gc_map_ = true;
2508 } else if (option == "--no-dump:vmap") {
2509 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002510 } else if (option =="--dump:code_info_stack_maps") {
2511 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002512 } else if (option == "--no-disassemble") {
2513 disassemble_code_ = false;
2514 } else if (option.starts_with("--symbolize=")) {
2515 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2516 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002517 } else if (option.starts_with("--class-filter=")) {
2518 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002519 } else if (option.starts_with("--method-filter=")) {
2520 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002521 } else if (option.starts_with("--list-classes")) {
2522 list_classes_ = true;
2523 } else if (option.starts_with("--list-methods")) {
2524 list_methods_ = true;
2525 } else if (option.starts_with("--export-dex-to=")) {
2526 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2527 } else if (option.starts_with("--addr2instr=")) {
2528 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2529 *error_msg = "Address conversion failed";
2530 return kParseError;
2531 }
Igor Murashkin37743352014-11-13 14:38:00 -08002532 } else {
2533 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002534 }
2535
Igor Murashkin37743352014-11-13 14:38:00 -08002536 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002537 }
2538
Igor Murashkin37743352014-11-13 14:38:00 -08002539 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2540 // Infer boot image location from the image location if possible.
2541 if (boot_image_location_ == nullptr) {
2542 boot_image_location_ = image_location_;
2543 }
2544
2545 // Perform the parent checks.
2546 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2547 if (parent_checks != kParseOk) {
2548 return parent_checks;
2549 }
2550
2551 // Perform our own checks.
2552 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2553 *error_msg = "Either --image or --oat-file must be specified";
2554 return kParseError;
2555 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2556 *error_msg = "Either --image or --oat-file must be specified but not both";
2557 return kParseError;
2558 }
2559
2560 return kParseOk;
2561 }
2562
2563 virtual std::string GetUsage() const {
2564 std::string usage;
2565
2566 usage +=
2567 "Usage: oatdump [options] ...\n"
2568 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2569 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2570 "\n"
2571 // Either oat-file or image is required.
2572 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2573 " Example: --oat-file=/system/framework/boot.oat\n"
2574 "\n"
2575 " --image=<file.art>: specifies an input image location.\n"
2576 " Example: --image=/system/framework/boot.art\n"
2577 "\n";
2578
2579 usage += Base::GetUsage();
2580
2581 usage += // Optional.
2582 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2583 " Example: --dump:raw_mapping_table\n"
2584 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002585 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002586 " Example: --dump:raw_gc_map\n"
2587 "\n"
2588 " --no-dump:vmap may be used to disable vmap dumping.\n"
2589 " Example: --no-dump:vmap\n"
2590 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002591 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2592 " Example: --dump:code_info_stack_maps\n"
2593 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002594 " --no-disassemble may be used to disable disassembly.\n"
2595 " Example: --no-disassemble\n"
2596 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002597 " --list-classes may be used to list target file classes (can be used with filters).\n"
2598 " Example: --list-classes\n"
2599 " Example: --list-classes --class-filter=com.example.foo\n"
2600 "\n"
2601 " --list-methods may be used to list target file methods (can be used with filters).\n"
2602 " Example: --list-methods\n"
2603 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2604 "\n"
2605 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2606 " Example: --symbolize=/system/framework/boot.oat\n"
2607 "\n"
2608 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2609 " Example: --class-filter=com.example.foo\n"
2610 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002611 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2612 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002613 "\n"
2614 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2615 " Example: --export-dex-to=/data/local/tmp\n"
2616 "\n"
2617 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2618 " address (e.g. PC from crash dump)\n"
2619 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002620 "\n";
2621
2622 return usage;
2623 }
2624
2625 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002626 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002627 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002628 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002629 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002630 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002631 bool dump_raw_mapping_table_ = false;
2632 bool dump_raw_gc_map_ = false;
2633 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002634 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002635 bool disassemble_code_ = true;
2636 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002637 bool list_classes_ = false;
2638 bool list_methods_ = false;
2639 uint32_t addr2instr_ = 0;
2640 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002641};
2642
Igor Murashkin37743352014-11-13 14:38:00 -08002643struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2644 virtual bool NeedsRuntime() OVERRIDE {
2645 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002646
Igor Murashkin37743352014-11-13 14:38:00 -08002647 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2648 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2649
2650 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2651 args_->dump_raw_mapping_table_,
2652 args_->dump_raw_gc_map_,
2653 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002654 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002655 args_->disassemble_code_,
2656 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002657 args_->class_filter_,
2658 args_->method_filter_,
2659 args_->list_classes_,
2660 args_->list_methods_,
2661 args_->export_dex_location_,
2662 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002663
2664 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2665 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002666 }
2667
Igor Murashkin37743352014-11-13 14:38:00 -08002668 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2669 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002670 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002671
Mathieu Chartierd424d082014-10-15 10:31:46 -07002672 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002673
Andreas Gampec24f3992014-12-17 20:40:11 -08002674 if (args_->symbolize_) {
2675 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2676 } else {
2677 return DumpOat(nullptr,
2678 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002679 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002680 args_->os_) == EXIT_SUCCESS;
2681 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002682 }
2683
Igor Murashkin37743352014-11-13 14:38:00 -08002684 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2685 CHECK(args_ != nullptr);
2686
2687 if (args_->oat_filename_ != nullptr) {
2688 return DumpOat(runtime,
2689 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002690 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002691 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002692 }
Igor Murashkin37743352014-11-13 14:38:00 -08002693
Jeff Haodcdc85b2015-12-04 14:06:18 -08002694 return DumpImage(runtime, oat_dumper_options_.get(), args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002695 }
2696
Igor Murashkin37743352014-11-13 14:38:00 -08002697 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2698};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002699
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002700} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002701
2702int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002703 art::OatdumpMain main;
2704 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002705}