blob: b9d81a7b2bbfa9982bae020f43eec1b4ead79011 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Elliott Hughes76160052012-12-12 16:31:20 -080031#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070032#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080035#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080036#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070037#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080038#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070039#include "gc/space/image_space.h"
40#include "gc/space/large_object_space.h"
41#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070042#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080043#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070044#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/array-inl.h"
46#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010047#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/object-inl.h"
49#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010051#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070052#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080053#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070054#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070055#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070057#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080058#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080059#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080060#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070061#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070062#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070063
Igor Murashkin37743352014-11-13 14:38:00 -080064#include <sys/stat.h>
65#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Igor Murashkin37743352014-11-13 14:38:00 -080067namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070068
Mathieu Chartiere401d142015-04-22 13:56:20 -070069const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080070 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070071 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070072 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070073 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070074 "kRefsOnlySaveMethod",
75 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070076};
77
78const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070079 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070080 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070081};
82
Mathieu Chartierac8f4392015-08-27 13:54:20 -070083// Map is so that we don't allocate multiple dex files for the same OatDexFile.
84static std::map<const OatFile::OatDexFile*,
85 std::unique_ptr<const DexFile>> opened_dex_files;
86
87const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
88 DCHECK(oat_dex_file != nullptr);
89 auto it = opened_dex_files.find(oat_dex_file);
90 if (it != opened_dex_files.end()) {
91 return it->second.get();
92 }
93 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
94 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
95 return ret;
96}
97
David Srbeckybc90fd02015-04-22 19:40:27 +010098class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070099 public:
David Srbeckybc90fd02015-04-22 19:40:27 +0100100 class RodataWriter FINAL : public CodeOutput {
101 public:
102 explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700103
David Srbeckybc90fd02015-04-22 19:40:27 +0100104 bool Write(OutputStream* out) OVERRIDE {
105 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
106 return out->WriteFully(oat_file_->Begin(), rodata_size);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700107 }
108
David Srbeckybc90fd02015-04-22 19:40:27 +0100109 private:
110 const OatFile* oat_file_;
111 };
112
113 class TextWriter FINAL : public CodeOutput {
114 public:
115 explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
116
117 bool Write(OutputStream* out) OVERRIDE {
118 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
119 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
120 return out->WriteFully(text_begin, oat_file_->End() - text_begin);
121 }
122
123 private:
124 const OatFile* oat_file_;
125 };
126
Roland Levillain3887c462015-08-12 18:15:42 +0100127 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100128 oat_file_(oat_file), builder_(nullptr),
129 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700130 }
131
132 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
133 uint32_t,
134 const OatFile::OatMethod&,
135 const DexFile&,
136 uint32_t,
137 const DexFile::CodeItem*,
138 uint32_t);
139
140 bool Symbolize() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100141 Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
142 uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
143 uint32_t text_size = size - rodata_size;
144 uint32_t bss_size = oat_file_->BssSize();
145 RodataWriter rodata_writer(oat_file_);
146 TextWriter text_writer(oat_file_);
147 builder_.reset(new ElfBuilder<ElfTypes32>(
148 oat_file_->GetOatHeader().GetInstructionSet(),
149 rodata_size, &rodata_writer,
150 text_size, &text_writer,
151 bss_size));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700152
153 Walk(&art::OatSymbolizer::RegisterForDedup);
154
155 NormalizeState();
156
157 Walk(&art::OatSymbolizer::AddSymbol);
158
David Srbeckybc90fd02015-04-22 19:40:27 +0100159 File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
160 bool result = builder_->Write(elf_output);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700161
Andreas Gampe4303ba92014-11-06 01:00:46 -0800162 // Ignore I/O errors.
David Srbeckybc90fd02015-04-22 19:40:27 +0100163 UNUSED(elf_output->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700164
165 return result;
166 }
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 Srbeckybc90fd02015-04-22 19:40:27 +0100297 auto* symtab = builder_->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700298
David Srbeckybc90fd02015-04-22 19:40:27 +0100299 symtab->AddSymbol(pretty_name, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700300 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
301 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700302 }
303 }
304
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700305 private:
306 static void SkipAllFields(ClassDataItemIterator* it) {
307 while (it->HasNextStaticField()) {
308 it->Next();
309 }
310 while (it->HasNextInstanceField()) {
311 it->Next();
312 }
313 }
314
315 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100316 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700317 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700318 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700319};
320
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700321class OatDumperOptions {
322 public:
323 OatDumperOptions(bool dump_raw_mapping_table,
324 bool dump_raw_gc_map,
325 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100326 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700327 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700328 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800329 const char* class_filter,
330 const char* method_filter,
331 bool list_classes,
332 bool list_methods,
333 const char* export_dex_location,
334 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700335 : dump_raw_mapping_table_(dump_raw_mapping_table),
336 dump_raw_gc_map_(dump_raw_gc_map),
337 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100338 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700339 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700340 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800341 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000342 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800343 list_classes_(list_classes),
344 list_methods_(list_methods),
345 export_dex_location_(export_dex_location),
346 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800347 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700348
349 const bool dump_raw_mapping_table_;
350 const bool dump_raw_gc_map_;
351 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100352 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700353 const bool disassemble_code_;
354 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800355 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000356 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800357 const bool list_classes_;
358 const bool list_methods_;
359 const char* const export_dex_location_;
360 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700361 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700362};
363
Elliott Hughese3c845c2012-02-28 17:23:01 -0800364class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700365 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100366 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000367 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800368 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700369 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800370 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800371 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
372 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800373 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000374 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100375 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800376 CHECK(options_.class_loader_ != nullptr);
377 CHECK(options_.class_filter_ != nullptr);
378 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800379 AddAllOffsets();
380 }
381
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700382 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700383 delete disassembler_;
384 }
385
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800386 InstructionSet GetInstructionSet() {
387 return instruction_set_;
388 }
389
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700390 bool Dump(std::ostream& os) {
391 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800392 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700393
394 os << "MAGIC:\n";
395 os << oat_header.GetMagic() << "\n\n";
396
397 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800398 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700399
Elliott Hughesa72ec822012-03-05 17:12:22 -0800400 os << "INSTRUCTION SET:\n";
401 os << oat_header.GetInstructionSet() << "\n\n";
402
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700403 {
404 std::unique_ptr<const InstructionSetFeatures> features(
405 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
406 oat_header.GetInstructionSetFeaturesBitmap()));
407 os << "INSTRUCTION SET FEATURES:\n";
408 os << features->GetFeatureString() << "\n\n";
409 }
Dave Allison70202782013-10-22 17:52:19 -0700410
Brian Carlstromaded5f72011-10-07 17:15:04 -0700411 os << "DEX FILE COUNT:\n";
412 os << oat_header.GetDexFileCount() << "\n\n";
413
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800414#define DUMP_OAT_HEADER_OFFSET(label, offset) \
415 os << label " OFFSET:\n"; \
416 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800417 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800418 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
419 } \
420 os << StringPrintf("\n\n");
421
422 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
423 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
424 GetInterpreterToInterpreterBridgeOffset);
425 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
426 GetInterpreterToCompiledCodeBridgeOffset);
427 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
428 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800429 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
430 GetQuickGenericJniTrampolineOffset);
431 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
432 GetQuickImtConflictTrampolineOffset);
433 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
434 GetQuickResolutionTrampolineOffset);
435 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
436 GetQuickToInterpreterBridgeOffset);
437#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700438
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700439 os << "IMAGE PATCH DELTA:\n";
440 os << StringPrintf("%d (0x%08x)\n\n",
441 oat_header.GetImagePatchDelta(),
442 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700443
Brian Carlstrom28db0122012-10-18 16:20:41 -0700444 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
445 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
446
447 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800448 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700449
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700450 // Print the key-value store.
451 {
452 os << "KEY VALUE STORE:\n";
453 size_t index = 0;
454 const char* key;
455 const char* value;
456 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
457 os << key << " = " << value << "\n";
458 index++;
459 }
460 os << "\n";
461 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700462
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800463 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700464 os << "BEGIN:\n";
465 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700466
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700467 os << "END:\n";
468 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
469 }
470
471 os << "SIZE:\n";
472 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700473
474 os << std::flush;
475
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800476 // If set, adjust relative address to be searched
477 if (options_.addr2instr_ != 0) {
478 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
479 os << "SEARCH ADDRESS (executable offset + input):\n";
480 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
481 }
482
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800483 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
484 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700485 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800486
487 // If file export selected skip file analysis
488 if (options_.export_dex_location_) {
489 if (!ExportDexFile(os, *oat_dex_file)) {
490 success = false;
491 }
492 } else {
493 if (!DumpOatDexFile(os, *oat_dex_file)) {
494 success = false;
495 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700496 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700497 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700498 os << std::flush;
499 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700500 }
501
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800502 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700503 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
504 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800505 return 0; // Address not in oat file
506 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800507 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
508 reinterpret_cast<uintptr_t>(oat_file_.Begin());
509 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800510 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800511 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800512 return end_offset - begin_offset;
513 }
514
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800515 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700516 return oat_file_.GetOatHeader().GetInstructionSet();
517 }
518
Mathieu Chartier90443472015-07-16 20:32:27 -0700519 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800520 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
521 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700522 CHECK(oat_dex_file != nullptr);
523 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700524 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
525 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700526 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
527 << "': " << error_msg;
528 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800529 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700530 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800531 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700532 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700533 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100534 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800535 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100536 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800537 }
538 }
539 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700540 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800541 }
542
Brian Carlstromaded5f72011-10-07 17:15:04 -0700543 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800544 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800545 // We don't know the length of the code for each method, but we need to know where to stop
546 // when disassembling. What we do know is that a region of code will be followed by some other
547 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
548 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800549 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
550 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700551 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700552 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700553 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
554 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700555 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
556 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800557 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800558 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800559 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800560 for (size_t class_def_index = 0;
561 class_def_index < dex_file->NumClassDefs();
562 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800563 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100564 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700565 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700566 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800567 ClassDataItemIterator it(*dex_file, class_data);
568 SkipAllFields(it);
569 uint32_t class_method_index = 0;
570 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100571 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800572 it.Next();
573 }
574 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100575 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800576 it.Next();
577 }
578 }
579 }
580 }
581
582 // If the last thing in the file is code for a method, there won't be an offset for the "next"
583 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
584 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800585 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800586 }
587
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700588 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
589 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
590 }
591
Elliott Hughese3c845c2012-02-28 17:23:01 -0800592 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800593 uint32_t code_offset = oat_method.GetCodeOffset();
594 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
595 code_offset &= ~0x1;
596 }
597 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800598 offsets_.insert(oat_method.GetMappingTableOffset());
599 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800600 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800601 }
602
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700603 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
604 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800605 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700606 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800607 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800608 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700609
610 // Create the verifier early.
611
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700612 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700613 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
614 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700615 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700616 os << std::flush;
617 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700618 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100619
620 VariableIndentationOutputStream vios(&os);
621 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800622 for (size_t class_def_index = 0;
623 class_def_index < dex_file->NumClassDefs();
624 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700625 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
626 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800627
628 // TODO: Support regex
629 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
630 continue;
631 }
632
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700633 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100634 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700635 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
636 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100637 << " (" << oat_class.GetStatus() << ")"
638 << " (" << oat_class.GetType() << ")\n";
639 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800640 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700641 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700642 success = false;
643 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800644 if (stop_analysis) {
645 os << std::flush;
646 return success;
647 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700648 }
649
650 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700651 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700652 }
653
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800654 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
655 std::string error_msg;
656 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
657
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700658 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800659 if (dex_file == nullptr) {
660 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
661 return false;
662 }
663 size_t fsize = oat_dex_file.FileSize();
664
665 // Some quick checks just in case
666 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
667 os << "Invalid dex file\n";
668 return false;
669 }
670
671 // Verify output directory exists
672 if (!OS::DirectoryExists(options_.export_dex_location_)) {
673 // TODO: Extend OS::DirectoryExists if symlink support is required
674 os << options_.export_dex_location_ << " output directory not found or symlink\n";
675 return false;
676 }
677
678 // Beautify path names
679 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
680 return false;
681 }
682
683 std::string dex_orig_name;
684 size_t dex_orig_pos = dex_file_location.rfind('/');
685 if (dex_orig_pos == std::string::npos)
686 dex_orig_name = dex_file_location;
687 else
688 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
689
690 // A more elegant approach to efficiently name user installed apps is welcome
691 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
692 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
693 size_t apk_orig_pos = dex_file_location.rfind('/');
694 if (apk_orig_pos != std::string::npos) {
695 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
696 }
697 }
698
699 std::string out_dex_path(options_.export_dex_location_);
700 if (out_dex_path.back() != '/') {
701 out_dex_path.append("/");
702 }
703 out_dex_path.append(dex_orig_name);
704 out_dex_path.append("_export.dex");
705 if (out_dex_path.length() > PATH_MAX) {
706 return false;
707 }
708
709 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
710 if (file.get() == nullptr) {
711 os << "Failed to open output dex file " << out_dex_path;
712 return false;
713 }
714
715 if (!file->WriteFully(dex_file->Begin(), fsize)) {
716 os << "Failed to write dex file";
717 file->Erase();
718 return false;
719 }
720
721 if (file->FlushCloseOrErase() != 0) {
722 os << "Flush and close failed";
723 return false;
724 }
725
726 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
727 os << std::flush;
728
729 return true;
730 }
731
Elliott Hughese3c845c2012-02-28 17:23:01 -0800732 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700733 while (it.HasNextStaticField()) {
734 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700735 }
Ian Rogers0571d352011-11-03 19:51:38 -0700736 while (it.HasNextInstanceField()) {
737 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700738 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800739 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700740
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100741 bool DumpOatClass(VariableIndentationOutputStream* vios,
742 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800743 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700744 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800745 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700746 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700747 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100748 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700749 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800750 }
751 ClassDataItemIterator it(dex_file, class_data);
752 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700753 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700754 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100755 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700756 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800757 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700758 success = false;
759 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800760 if (addr_found) {
761 *stop_analysis = true;
762 return success;
763 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700764 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700765 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700766 }
Ian Rogers0571d352011-11-03 19:51:38 -0700767 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100768 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700769 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800770 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700771 success = false;
772 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800773 if (addr_found) {
774 *stop_analysis = true;
775 return success;
776 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700777 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700778 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700779 }
Ian Rogers0571d352011-11-03 19:51:38 -0700780 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100781 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700782 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700783 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800784
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700785 static constexpr uint32_t kPrologueBytes = 16;
786
787 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
788 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
789
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100790 bool DumpOatMethod(VariableIndentationOutputStream* vios,
791 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700792 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700793 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800794 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800795 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700796 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800797
798 // TODO: Support regex
799 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
800 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000801 return success;
802 }
803
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800804 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100805 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
806 class_method_index, pretty_method.c_str(),
807 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800808 if (options_.list_methods_) return success;
809
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800810 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
811 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
812 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
813 uint32_t code_offset = oat_method.GetCodeOffset();
814 uint32_t code_size = oat_method.GetQuickCodeSize();
815 if (resolved_addr2instr_ != 0) {
816 if (resolved_addr2instr_ > code_offset + code_size) {
817 return success;
818 } else {
819 *addr_found = true; // stop analyzing file at next iteration
820 }
821 }
822
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100823 // Everything below is indented at least once.
824 ScopedIndentation indent1(vios);
825
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800826 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100827 vios->Stream() << "DEX CODE:\n";
828 ScopedIndentation indent2(vios);
829 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700830 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700831
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700832 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700833 std::unique_ptr<verifier::MethodVerifier> verifier;
834 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700835 // We need to have the handle scope stay live until after the verifier since the verifier has
836 // a handle to the dex cache from hs.
837 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100838 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
839 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700840 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100841 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700842 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700843 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800844 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100845 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800846 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100847 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100848 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100849 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700850 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100851 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700852 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
853 oat_method_offsets_offset, oat_file_.Size());
854 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100855 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700856 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800857 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700858
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100859 ScopedIndentation indent2(vios);
860 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700861 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
862 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100863 vios->Stream() << StringPrintf("WARNING: "
864 "code offset 0x%08x is past end of file 0x%08zx.\n",
865 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700866 success = false;
867 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100868 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700869
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100870 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800871 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100872 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700873 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800874 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100875 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700876 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100877 vios->Stream() << StringPrintf("WARNING: "
878 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
879 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700880 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800881 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100882 ScopedIndentation indent3(vios);
883 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800884 }
885 }
886 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100887 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700888 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
889 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700890
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800891 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100892 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700893 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100894 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700895 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100896 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700897 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
898 method_header_offset, oat_file_.Size());
899 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100900 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700901 return false;
902 }
903
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100904 ScopedIndentation indent2(vios);
905 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800906 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100907 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 }
909 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100910 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700911 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100912 vios->Stream() << StringPrintf("WARNING: "
913 "mapping table offset 0x%08x is past end of file 0x%08zx. "
914 "mapping table offset was loaded from offset 0x%08x.\n",
915 mapping_table_offset, oat_file_.Size(),
916 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700917 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800918 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100919 ScopedIndentation indent3(vios);
920 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700921 }
922
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100923 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800924 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100925 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700926 }
927 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100928 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700929 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100930 vios->Stream() << StringPrintf("WARNING: "
931 "vmap table offset 0x%08x is past end of file 0x%08zx. "
932 "vmap table offset was loaded from offset 0x%08x.\n",
933 vmap_table_offset, oat_file_.Size(),
934 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700935 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800936 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100937 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700938 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800939 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700940 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100941 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700942
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100943 ScopedIndentation indent2(vios);
944 vios->Stream()
945 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
946 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
947 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
948 vios->Stream() << "\n";
949 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
950 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
951 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700952 }
953 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100954 // Based on spill masks from QuickMethodFrameInfo so placed
955 // after it is dumped, but useful for understanding quick
956 // code, so dumped here.
957 ScopedIndentation indent2(vios);
958 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700959 }
960 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100961 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700962 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
963 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100964 ScopedIndentation indent2(vios);
965 vios->Stream() << StringPrintf("WARNING: "
966 "code size offset 0x%08x is past end of file 0x%08zx.",
967 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700968 success = false;
969 } else {
970 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700971 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
972 uint64_t aligned_code_end = aligned_code_begin + code_size;
973
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800974 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100975 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700976 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100977 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
978 code_offset,
979 code_size_offset,
980 code_size,
981 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700982
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100983 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700984 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100985 vios->Stream() << StringPrintf("WARNING: "
986 "start of code at 0x%08x is past end of file 0x%08zx.",
987 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700988 success = false;
989 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100990 vios->Stream() << StringPrintf(
991 "WARNING: "
992 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
993 "code size is 0x%08x loaded from offset 0x%08x.\n",
994 aligned_code_end, oat_file_.Size(),
995 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800997 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700998 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100999 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001000 }
1001 }
1002 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001003 vios->Stream() << StringPrintf(
1004 "WARNING: "
1005 "code size %d is bigger than max expected threshold of %d. "
1006 "code size is 0x%08x loaded from offset 0x%08x.\n",
1007 code_size, kMaxCodeSize,
1008 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001009 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001010 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001011 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001012 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001013 }
1014 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001015 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001016 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001017 }
1018 }
1019 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001020 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001021 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001022 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001023
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001024 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1025 if (spill_mask == 0) {
1026 return;
1027 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001028 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001029 for (size_t i = 0; i < 32; i++) {
1030 if ((spill_mask & (1 << i)) != 0) {
1031 if (is_float) {
1032 os << "fr" << i;
1033 } else {
1034 os << "r" << i;
1035 }
1036 spill_mask ^= 1 << i; // clear bit
1037 if (spill_mask != 0) {
1038 os << ", ";
1039 } else {
1040 break;
1041 }
1042 }
1043 }
1044 os << ")";
1045 }
1046
Roland Levillain442b46a2015-02-18 16:54:21 +00001047 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001048 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001049 const OatFile::OatMethod& oat_method,
1050 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001051 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1052 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001053 const void* raw_code_info = oat_method.GetVmapTable();
1054 if (raw_code_info != nullptr) {
1055 CodeInfo code_info(raw_code_info);
1056 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001057 ScopedIndentation indent1(vios);
1058 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001059 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001060 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1061 // We don't encode the size in the table, so just emit that we have quickened
1062 // information.
1063 ScopedIndentation indent(vios);
1064 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001065 } else {
1066 // Otherwise, display the vmap table.
1067 const uint8_t* raw_table = oat_method.GetVmapTable();
1068 if (raw_table != nullptr) {
1069 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001070 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001071 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001072 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001073 }
1074
1075 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001076 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001077 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001078 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001079 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001080 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001081 oat_method.GetCodeOffset(),
1082 code_item.registers_size_,
1083 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001084 }
1085
Roland Levillain442b46a2015-02-18 16:54:21 +00001086 // Display a vmap table.
1087 void DumpVmapTable(std::ostream& os,
1088 const OatFile::OatMethod& oat_method,
1089 const VmapTable& vmap_table) {
1090 bool first = true;
1091 bool processing_fp = false;
1092 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1093 for (size_t i = 0; i < vmap_table.Size(); i++) {
1094 uint16_t dex_reg = vmap_table[i];
1095 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1096 processing_fp ? kFloatVReg : kIntVReg);
1097 os << (first ? "v" : ", v") << dex_reg;
1098 if (!processing_fp) {
1099 os << "/r" << cpu_reg;
1100 } else {
1101 os << "/fr" << cpu_reg;
1102 }
1103 first = false;
1104 if (!processing_fp && dex_reg == 0xFFFF) {
1105 processing_fp = true;
1106 spill_mask = oat_method.GetFpSpillMask();
1107 }
1108 }
1109 os << "\n";
1110 }
1111
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001112 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1113 const DexFile::CodeItem* code_item) {
1114 if (code_item != nullptr) {
1115 size_t num_locals_ins = code_item->registers_size_;
1116 size_t num_ins = code_item->ins_size_;
1117 size_t num_locals = num_locals_ins - num_ins;
1118 size_t num_outs = code_item->outs_size_;
1119
1120 os << "vr_stack_locations:";
1121 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1122 // For readability, delimit the different kinds of VRs.
1123 if (reg == num_locals_ins) {
1124 os << "\n\tmethod*:";
1125 } else if (reg == num_locals && num_ins > 0) {
1126 os << "\n\tins:";
1127 } else if (reg == 0 && num_locals > 0) {
1128 os << "\n\tlocals:";
1129 }
1130
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001131 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1132 code_item,
1133 oat_method.GetCoreSpillMask(),
1134 oat_method.GetFpSpillMask(),
1135 oat_method.GetFrameSizeInBytes(),
1136 reg,
1137 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001138 os << " v" << reg << "[sp + #" << offset << "]";
1139 }
1140
1141 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1142 if (out_reg == 0) {
1143 os << "\n\touts:";
1144 }
1145
1146 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1147 os << " v" << out_reg << "[sp + #" << offset << "]";
1148 }
1149
1150 os << "\n";
1151 }
1152 }
1153
Ian Rogersb23a7722012-10-09 16:54:26 -07001154 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001155 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001156 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001157 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001158 const VmapTable vmap_table(raw_table);
1159 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001160 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001161 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1162 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1163 : oat_method.GetCoreSpillMask();
1164 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001165 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001166 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1167 code_item,
1168 oat_method.GetCoreSpillMask(),
1169 oat_method.GetFpSpillMask(),
1170 oat_method.GetFrameSizeInBytes(),
1171 reg,
1172 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001173 os << "[sp + #" << offset << "]";
1174 }
1175 }
1176 }
1177
Ian Rogersef7d42f2014-01-06 12:55:46 -08001178 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1179 const DexFile::CodeItem* code_item,
1180 size_t num_regs, const uint8_t* reg_bitmap) {
1181 bool first = true;
1182 for (size_t reg = 0; reg < num_regs; reg++) {
1183 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1184 if (first) {
1185 os << " v" << reg << " (";
1186 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1187 os << ")";
1188 first = false;
1189 } else {
1190 os << ", v" << reg << " (";
1191 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1192 os << ")";
1193 }
1194 }
1195 }
1196 if (first) {
1197 os << "No registers in GC map\n";
1198 } else {
1199 os << "\n";
1200 }
1201 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001202 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1203 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001204 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001205 if (gc_map_raw == nullptr) {
1206 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001207 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001208 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001209 NativePcOffsetToReferenceMap map(gc_map_raw);
1210 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1211 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1212 map.GetNativePcOffset(entry);
1213 os << StringPrintf("%p", native_pc);
1214 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001215 }
1216 }
1217
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001218 void DumpMappingTable(VariableIndentationOutputStream* vios,
1219 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001220 const void* quick_code = oat_method.GetQuickCode();
1221 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001222 return;
1223 }
Ian Rogers1809a722013-08-09 22:05:32 -07001224 MappingTable table(oat_method.GetMappingTable());
1225 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001226 if (table.PcToDexSize() != 0) {
1227 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001229 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001230 ScopedIndentation indent1(vios);
1231 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001232 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001233 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001234 }
1235 if (table.DexToPcSize() != 0) {
1236 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001237 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001238 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001239 ScopedIndentation indent1(vios);
1240 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001241 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001242 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001243 }
1244 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001245 }
1246
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001247 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001248 const OatFile::OatMethod& oat_method,
1249 const DexFile::CodeItem* code_item,
1250 size_t offset,
1251 bool suspend_point_mapping) {
1252 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1253 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001254 ScopedIndentation indent1(vios);
1255 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001256 }
1257 // The return value is not used in the case of a method compiled
1258 // with the optimizing compiler.
1259 return DexFile::kDexNoIndex;
1260 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001261 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001262 }
1263 }
1264
Ian Rogers1809a722013-08-09 22:05:32 -07001265 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1266 size_t offset, bool suspend_point_mapping) {
1267 MappingTable table(oat_method.GetMappingTable());
1268 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1269 typedef MappingTable::PcToDexIterator It;
1270 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1271 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001272 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001273 return cur.DexPc();
1274 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001275 }
Ian Rogers1809a722013-08-09 22:05:32 -07001276 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1277 typedef MappingTable::DexToPcIterator It;
1278 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1279 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001280 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001281 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001282 }
1283 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001284 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001285 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001286 }
1287
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001288 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1289 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001290 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001291 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001292 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001293 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001294 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001295 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001296 bool first = true;
1297 for (size_t reg = 0; reg < num_regs; reg++) {
1298 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1299 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001300 os << "GC map objects: v" << reg << " (";
1301 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001302 os << ")";
1303 first = false;
1304 } else {
1305 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001306 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001307 os << ")";
1308 }
1309 }
1310 }
1311 if (!first) {
1312 os << "\n";
1313 }
1314 }
1315 }
1316 }
1317
Mathieu Chartier590fee92013-09-13 13:46:47 -07001318 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1319 const OatFile::OatMethod& oat_method,
1320 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1321 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001322 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001323 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001324 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1325 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1326 if (kind != kUndefined) {
1327 if (first) {
1328 os << "VRegs: v";
1329 first = false;
1330 } else {
1331 os << ", v";
1332 }
1333 os << reg << " (";
1334 switch (kind) {
1335 case kImpreciseConstant:
1336 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1337 DescribeVReg(os, oat_method, code_item, reg, kind);
1338 break;
1339 case kConstant:
1340 os << "Constant: " << kinds.at((reg * 2) + 1);
1341 break;
1342 default:
1343 DescribeVReg(os, oat_method, code_item, reg, kind);
1344 break;
1345 }
1346 os << ")";
1347 }
1348 }
1349 if (!first) {
1350 os << "\n";
1351 }
1352 }
1353
1354
Ian Rogersb23a7722012-10-09 16:54:26 -07001355 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001356 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001357 size_t i = 0;
1358 while (i < code_item->insns_size_in_code_units_) {
1359 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001360 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1361 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001362 i += instruction->SizeInCodeUnits();
1363 }
1364 }
1365 }
1366
Roland Levillainf2650d12015-05-28 14:53:28 +01001367 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1368 // the optimizing compiler?
1369 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1370 const DexFile::CodeItem* code_item) {
1371 // If the native GC map is null and the Dex `code_item` is not
1372 // null, then this method has been compiled with the optimizing
1373 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001374 return oat_method.GetQuickCode() != nullptr &&
1375 oat_method.GetGcMap() == nullptr &&
1376 code_item != nullptr;
1377 }
1378
1379 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1380 // the dextodex compiler?
1381 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1382 const DexFile::CodeItem* code_item) {
1383 // If the quick code is null, the Dex `code_item` is not
1384 // null, and the vmap table is not null, then this method has been compiled
1385 // with the dextodex compiler.
1386 return oat_method.GetQuickCode() == nullptr &&
1387 oat_method.GetVmapTable() != nullptr &&
1388 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001389 }
1390
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001391 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001392 const OatFile::OatMethod& oat_method,
1393 const DexFile::CodeItem* code_item,
1394 size_t offset) {
1395 // This method is only relevant for oat methods compiled with the
1396 // optimizing compiler.
1397 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1398
1399 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1400 const void* raw_code_info = oat_method.GetVmapTable();
1401 if (raw_code_info != nullptr) {
1402 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001403 StackMapEncoding encoding = code_info.ExtractEncoding();
1404 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001405 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001406 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1407 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001408 }
1409 }
1410 }
1411
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001412 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001413 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001414 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001415 const DexFile* dex_file,
1416 const DexFile::ClassDef& class_def,
1417 const DexFile::CodeItem* code_item,
1418 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001419 if ((method_access_flags & kAccNative) == 0) {
1420 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001421 Handle<mirror::DexCache> dex_cache(
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001422 hs->NewHandle(Runtime::Current()->GetClassLinker()->RegisterDexFile(*dex_file)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001423 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001424 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001425 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1426 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001427 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001428
1429 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 }
1431
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001432 void DumpCode(VariableIndentationOutputStream* vios,
1433 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001434 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1435 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001436 const void* quick_code = oat_method.GetQuickCode();
1437
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001438 if (code_size == 0) {
1439 code_size = oat_method.GetQuickCodeSize();
1440 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001441 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001442 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001443 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001444 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001445 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1446 size_t offset = 0;
1447 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001448 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001449 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001450 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001451 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001452 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001453 uint32_t dex_pc =
1454 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001455 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001456 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001457 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001458 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001459 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001460 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 }
1462 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001463 }
1464 }
1465
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001466 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001467 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001468 const OatDumperOptions& options_;
1469 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001470 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001471 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001472 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001473};
1474
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001475class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001476 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001477 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1478 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001479 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001480 vios_(os),
1481 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001482 image_space_(image_space),
1483 image_header_(image_header),
1484 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001485
Mathieu Chartier90443472015-07-16 20:32:27 -07001486 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001487 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001488 std::ostream& indent_os = vios_.Stream();
1489
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001490 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001491
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001492 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001493
Mathieu Chartiere401d142015-04-22 13:56:20 -07001494 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1495
1496 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1497 auto section = static_cast<ImageHeader::ImageSections>(i);
1498 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1499 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001500
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001501 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001502
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001503 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001504
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001505 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1506
1507 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1508
1509 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001510
Alex Lighta59dd802014-07-02 16:28:08 -07001511 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1512
Igor Murashkin46774762014-10-22 11:37:02 -07001513 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1514
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001515 {
1516 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001517 static_assert(arraysize(image_roots_descriptions_) ==
1518 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001519 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1520 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1521 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001522 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001523 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001524 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001525 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001526 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001527 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001528 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1529 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001530 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001531 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1532 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001533 run++;
1534 } else {
1535 break;
1536 }
1537 }
1538 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001539 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001540 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001541 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001542 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001543 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001544 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001545 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001546 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001547 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001549 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001550 }
1551 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001552 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001553
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001554 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001555 os << "METHOD ROOTS\n";
1556 static_assert(arraysize(image_methods_descriptions_) ==
1557 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1558 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1559 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1560 const char* description = image_methods_descriptions_[i];
1561 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001562 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001563 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001564 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001565 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001566
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001567 Runtime* const runtime = Runtime::Current();
1568 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001569 std::string image_filename = image_space_.GetImageFilename();
1570 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001572 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001573 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001574 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1575 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001576 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001577 oat_file = OatFile::Open(oat_location, oat_location,
1578 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001579 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001580 if (oat_file == nullptr) {
1581 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001582 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001583 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001584 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001585 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001586
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001587 stats_.oat_file_bytes = oat_file->Size();
1588
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001589 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001590
Mathieu Chartier02e25112013-08-14 16:14:24 -07001591 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001592 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001593 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1594 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001595 }
1596
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001597 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001598
1599 // Loop through all the image spaces and dump their objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001600 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07001601 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001602 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001603 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001604 {
1605 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1606 heap->FlushAllocStack();
1607 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001608 // Since FlushAllocStack() above resets the (active) allocation
1609 // stack. Need to revoke the thread-local allocation stacks that
1610 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001611 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001612 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001613 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001614 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001616 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001617 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001618 {
1619 ReaderMutexLock mu(self, *class_linker->DexLock());
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001620 for (jobject weak_root : class_linker->GetDexCaches()) {
1621 mirror::DexCache* dex_cache =
1622 down_cast<mirror::DexCache*>(self->DecodeJObject(weak_root));
1623 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001624 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001625 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001626 }
1627 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001629 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 if (space->IsImageSpace()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001631 auto* image_space = space->AsImageSpace();
1632 // Dump the normal objects before ArtMethods.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1634 indent_os << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001635 // TODO: Dump fields.
1636 // Dump methods after.
1637 const auto& methods_section = image_header_.GetMethodsSection();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001638 const size_t pointer_size =
Mathieu Chartiere401d142015-04-22 13:56:20 -07001639 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001640 DumpArtMethodVisitor visitor(this);
Vladimir Markocf36d492015-08-12 19:27:26 +01001641 methods_section.VisitPackedArtMethods(&visitor, image_space->Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001642 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001643 }
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);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001664 stats_.header_bytes = header_bytes;
Vladimir Markocf36d492015-08-12 19:27:26 +01001665 stats_.alignment_bytes += RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1666 // Add padding between the field and method section.
1667 // (Field section is 4-byte aligned, method section is 8-byte aligned on 64-bit targets.)
Vladimir Marko05792b92015-08-03 11:56:49 +01001668 stats_.alignment_bytes += method_section.Offset() -
1669 (field_section.Offset() + field_section.Size());
1670 // Add padding between the dex cache arrays section and the intern table. (Dex cache
1671 // arrays section is 4-byte aligned on 32-bit targets, intern table is 8-byte aligned.)
1672 stats_.alignment_bytes += intern_section.Offset() -
1673 (dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001674 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1675 stats_.bitmap_bytes += bitmap_section.Size();
1676 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001677 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001678 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001679 stats_.interned_strings_bytes += intern_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001680 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001681 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001682
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001683 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001684
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001685 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001686 }
1687
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001688 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001689 class DumpArtMethodVisitor : public ArtMethodVisitor {
1690 public:
1691 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1692
1693 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1694 std::ostream& indent_os = image_dumper_->vios_.Stream();
1695 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1696 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1697 indent_os << "\n";
1698 }
1699
1700 private:
1701 ImageDumper* const image_dumper_;
1702 };
1703
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001704 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001705 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001706 CHECK(type != nullptr);
1707 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001708 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001709 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001710 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001711 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001712 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001713 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001714 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001715 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001716 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001717 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001718 }
1719 }
1720
Mathieu Chartierc7853442015-03-27 14:35:38 -07001721 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001722 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001723 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001724 switch (field->GetTypeAsPrimitiveType()) {
1725 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001726 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001727 break;
1728 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001729 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001730 break;
1731 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001732 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001733 break;
1734 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001735 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001736 break;
1737 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001738 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001739 break;
1740 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001741 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001742 break;
1743 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001744 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1745 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001746 break;
1747 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001748 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001749 break;
1750 case Primitive::kPrimNot: {
1751 // Get the value, don't compute the type unless it is non-null as we don't want
1752 // to cause class loading.
1753 mirror::Object* value = field->GetObj(obj);
1754 if (value == nullptr) {
1755 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001756 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001757 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001758 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001759 if (field_type != nullptr) {
1760 PrettyObjectValue(os, field_type, value);
1761 } else {
1762 os << StringPrintf("%p %s\n", value,
1763 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1764 }
Ian Rogers50239c72013-06-17 14:53:22 -07001765 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001766 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001767 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001768 default:
1769 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1770 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001771 }
1772 }
1773
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001774 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001775 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001776 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001777 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001778 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001779 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001780 for (ArtField& field : klass->GetIFields()) {
1781 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001782 }
1783 }
1784
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001785 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001786 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001787 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001788
Mathieu Chartiere401d142015-04-22 13:56:20 -07001789 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001790 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001791 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1792 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001793 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001794 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001795 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001796 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001797 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001798 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001799 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001800 }
1801
Mathieu Chartiere401d142015-04-22 13:56:20 -07001802 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001803 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001804 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1805 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001806 return 0;
1807 }
1808 return oat_code_begin[-1];
1809 }
1810
Mathieu Chartiere401d142015-04-22 13:56:20 -07001811 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001812 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001813 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001814 if (oat_code_begin == nullptr) {
1815 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001816 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001817 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001818 }
1819
Mathieu Chartier90443472015-07-16 20:32:27 -07001820 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001821 DCHECK(obj != nullptr);
1822 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001823 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001824 if (!state->InDumpSpace(obj)) {
1825 return;
1826 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001827
1828 size_t object_bytes = obj->SizeOf();
1829 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1830 state->stats_.object_bytes += object_bytes;
1831 state->stats_.alignment_bytes += alignment_bytes;
1832
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001833 std::ostream& os = state->vios_.Stream();
1834
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001835 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001836 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001837 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1838 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001839 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001840 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001841 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1842 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001843 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001844 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001845 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001846 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001848 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001849 ScopedIndentation indent1(&state->vios_);
1850 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001851 const auto image_pointer_size =
1852 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001853 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001854 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1855 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001856 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001857 size_t run = 0;
1858 for (int32_t j = i + 1; j < length; j++) {
1859 if (value == obj_array->Get(j)) {
1860 run++;
1861 } else {
1862 break;
1863 }
1864 }
1865 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001866 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001867 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001868 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001869 i = i + run;
1870 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001871 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001872 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001873 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001874 }
1875 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001876 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001877 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001878 os << "STATICS:\n";
1879 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001880 for (ArtField& field : klass->GetSFields()) {
1881 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001882 }
1883 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001884 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001885 auto it = state->dex_caches_.find(obj);
1886 if (it != state->dex_caches_.end()) {
1887 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001888 const auto& field_section = state->image_header_.GetImageSection(
1889 ImageHeader::kSectionArtFields);
1890 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001891 size_t num_methods = dex_cache->NumResolvedMethods();
1892 if (num_methods != 0u) {
1893 os << "Methods (size=" << num_methods << "):";
1894 ScopedIndentation indent2(&state->vios_);
1895 auto* resolved_methods = dex_cache->GetResolvedMethods();
1896 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1897 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1898 size_t run = 0;
1899 for (size_t j = i + 1;
1900 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1901 j,
1902 image_pointer_size);
1903 ++j, ++run) {}
1904 if (run == 0) {
1905 os << StringPrintf("%zd: ", i);
1906 } else {
1907 os << StringPrintf("%zd to %zd: ", i, i + run);
1908 i = i + run;
1909 }
1910 std::string msg;
1911 if (elem == nullptr) {
1912 msg = "null";
1913 } else if (method_section.Contains(
1914 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1915 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1916 } else {
1917 msg = "<not in method section>";
1918 }
1919 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001920 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001921 }
1922 size_t num_fields = dex_cache->NumResolvedFields();
1923 if (num_fields != 0u) {
1924 os << "Fields (size=" << num_fields << "):";
1925 ScopedIndentation indent2(&state->vios_);
1926 auto* resolved_fields = dex_cache->GetResolvedFields();
1927 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1928 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1929 size_t run = 0;
1930 for (size_t j = i + 1;
1931 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1932 j,
1933 image_pointer_size);
1934 ++j, ++run) {}
1935 if (run == 0) {
1936 os << StringPrintf("%zd: ", i);
1937 } else {
1938 os << StringPrintf("%zd to %zd: ", i, i + run);
1939 i = i + run;
1940 }
1941 std::string msg;
1942 if (elem == nullptr) {
1943 msg = "null";
1944 } else if (field_section.Contains(
1945 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1946 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1947 } else {
1948 msg = "<not in field section>";
1949 }
1950 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001951 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001952 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001953 }
1954 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001955 std::string temp;
1956 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001957 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001958
Mathieu Chartiere401d142015-04-22 13:56:20 -07001959 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001960 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001961 DCHECK(method != nullptr);
1962 const auto image_pointer_size =
1963 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1964 if (method->IsNative()) {
1965 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1966 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1967 bool first_occurrence;
1968 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1969 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1970 state->ComputeOatSize(quick_oat_code, &first_occurrence);
1971 if (first_occurrence) {
1972 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1973 }
1974 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1975 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
1976 }
1977 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1978 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1979 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1980 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1981 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1982 } else {
1983 const DexFile::CodeItem* code_item = method->GetCodeItem();
1984 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1985 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1986
1987 bool first_occurrence;
1988 size_t gc_map_bytes = state->ComputeOatSize(
1989 method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1990 if (first_occurrence) {
1991 state->stats_.gc_map_bytes += gc_map_bytes;
1992 }
1993
1994 size_t pc_mapping_table_bytes = state->ComputeOatSize(
1995 method->GetMappingTable(image_pointer_size), &first_occurrence);
1996 if (first_occurrence) {
1997 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1998 }
1999
Roland Levillain6d7f1792015-07-02 10:59:15 +01002000 size_t vmap_table_bytes = 0u;
2001 if (!method->IsOptimized(image_pointer_size)) {
2002 // Method compiled with the optimizing compiler have no vmap table.
2003 vmap_table_bytes = state->ComputeOatSize(
2004 method->GetVmapTable(image_pointer_size), &first_occurrence);
2005 if (first_occurrence) {
2006 state->stats_.vmap_table_bytes += vmap_table_bytes;
2007 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008 }
2009
2010 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
2011 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
2012 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2013 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2014 if (first_occurrence) {
2015 state->stats_.managed_code_bytes += quick_oat_code_size;
2016 if (method->IsConstructor()) {
2017 if (method->IsStatic()) {
2018 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2019 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2020 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2021 }
2022 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2023 state->stats_.large_method_code_bytes += quick_oat_code_size;
2024 }
2025 }
2026 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2027
Igor Murashkin7617abd2015-07-10 18:27:47 -07002028 uint32_t method_access_flags = method->GetAccessFlags();
2029
Mathieu Chartiere401d142015-04-22 13:56:20 -07002030 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002031 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2032 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2033 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002034
2035 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002036 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002037
2038 double expansion =
2039 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2040 state->stats_.ComputeOutliers(total_size, expansion, method);
2041 }
2042 }
2043
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002044 std::set<const void*> already_seen_;
2045 // Compute the size of the given data within the oat file and whether this is the first time
2046 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002047 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002048 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002049 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002050 already_seen_.insert(oat_data);
2051 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002052 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002053 }
2054 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002055 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002056
2057 public:
2058 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002059 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002060 size_t file_bytes;
2061
2062 size_t header_bytes;
2063 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002064 size_t art_field_bytes;
2065 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002066 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002067 size_t interned_strings_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002068 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002069 size_t alignment_bytes;
2070
2071 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002072 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002073 size_t managed_to_native_code_bytes;
2074 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002075 size_t class_initializer_code_bytes;
2076 size_t large_initializer_code_bytes;
2077 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002078
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002079 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002080 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002081 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002082
2083 size_t dex_instruction_bytes;
2084
Mathieu Chartiere401d142015-04-22 13:56:20 -07002085 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002086 std::vector<size_t> method_outlier_size;
2087 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002088 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002089
Roland Levillain3887c462015-08-12 18:15:42 +01002090 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002091 : oat_file_bytes(0),
2092 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002093 header_bytes(0),
2094 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002095 art_field_bytes(0),
2096 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002097 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002098 interned_strings_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002099 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002100 alignment_bytes(0),
2101 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002102 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002103 managed_to_native_code_bytes(0),
2104 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002105 class_initializer_code_bytes(0),
2106 large_initializer_code_bytes(0),
2107 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002108 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002109 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002110 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002111 dex_instruction_bytes(0) {}
2112
Elliott Hughesa0e18062012-04-13 15:59:59 -07002113 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002114 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002115 size_t bytes;
2116 size_t count;
2117 };
2118 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2119 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002120
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002121 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002122 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2123 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002124 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002125 it->second.count += 1;
2126 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002127 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002128 }
2129 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002130
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002131 double PercentOfOatBytes(size_t size) {
2132 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2133 }
2134
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002135 double PercentOfFileBytes(size_t size) {
2136 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2137 }
2138
2139 double PercentOfObjectBytes(size_t size) {
2140 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2141 }
2142
Mathieu Chartiere401d142015-04-22 13:56:20 -07002143 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002144 method_outlier_size.push_back(total_size);
2145 method_outlier_expansion.push_back(expansion);
2146 method_outlier.push_back(method);
2147 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002148
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002149 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002150 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002151 size_t sum_of_sizes = 0;
2152 size_t sum_of_sizes_squared = 0;
2153 size_t sum_of_expansion = 0;
2154 size_t sum_of_expansion_squared = 0;
2155 size_t n = method_outlier_size.size();
2156 for (size_t i = 0; i < n; i++) {
2157 size_t cur_size = method_outlier_size[i];
2158 sum_of_sizes += cur_size;
2159 sum_of_sizes_squared += cur_size * cur_size;
2160 double cur_expansion = method_outlier_expansion[i];
2161 sum_of_expansion += cur_expansion;
2162 sum_of_expansion_squared += cur_expansion * cur_expansion;
2163 }
2164 size_t size_mean = sum_of_sizes / n;
2165 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2166 double expansion_mean = sum_of_expansion / n;
2167 double expansion_variance =
2168 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2169
2170 // Dump methods whose size is a certain number of standard deviations from the mean
2171 size_t dumped_values = 0;
2172 size_t skipped_values = 0;
2173 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2174 size_t cur_size_variance = i * i * size_variance;
2175 bool first = true;
2176 for (size_t j = 0; j < n; j++) {
2177 size_t cur_size = method_outlier_size[j];
2178 if (cur_size > size_mean) {
2179 size_t cur_var = cur_size - size_mean;
2180 cur_var = cur_var * cur_var;
2181 if (cur_var > cur_size_variance) {
2182 if (dumped_values > 20) {
2183 if (i == 1) {
2184 skipped_values++;
2185 } else {
2186 i = 2; // jump to counting for 1 standard deviation
2187 break;
2188 }
2189 } else {
2190 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002191 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002192 first = false;
2193 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002194 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002195 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002196 method_outlier_size[j] = 0; // don't consider this method again
2197 dumped_values++;
2198 }
2199 }
2200 }
2201 }
2202 }
2203 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002204 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002205 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002206 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002207 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002208
2209 // Dump methods whose expansion is a certain number of standard deviations from the mean
2210 dumped_values = 0;
2211 skipped_values = 0;
2212 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2213 double cur_expansion_variance = i * i * expansion_variance;
2214 bool first = true;
2215 for (size_t j = 0; j < n; j++) {
2216 double cur_expansion = method_outlier_expansion[j];
2217 if (cur_expansion > expansion_mean) {
2218 size_t cur_var = cur_expansion - expansion_mean;
2219 cur_var = cur_var * cur_var;
2220 if (cur_var > cur_expansion_variance) {
2221 if (dumped_values > 20) {
2222 if (i == 1) {
2223 skipped_values++;
2224 } else {
2225 i = 2; // jump to counting for 1 standard deviation
2226 break;
2227 }
2228 } else {
2229 if (first) {
2230 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002231 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002232 first = false;
2233 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002234 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002235 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002236 method_outlier_expansion[j] = 0.0; // don't consider this method again
2237 dumped_values++;
2238 }
2239 }
2240 }
2241 }
2242 }
2243 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002244 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002245 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002246 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002247 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002248 }
2249
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002250 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002251 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002252 {
2253 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2254 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002255 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2256 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2257 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2258 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2259 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2260 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
2261 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2262 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002263 header_bytes, PercentOfFileBytes(header_bytes),
2264 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002265 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2266 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002267 dex_cache_arrays_bytes,
2268 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002269 interned_strings_bytes,
2270 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002271 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002272 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2273 << std::flush;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002274 CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
Vladimir Marko05792b92015-08-03 11:56:49 +01002275 dex_cache_arrays_bytes + interned_strings_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002276 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002277
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002278 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002279 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002280 for (const auto& sizes_and_count : sizes_and_counts) {
2281 const std::string& descriptor(sizes_and_count.first);
2282 double average = static_cast<double>(sizes_and_count.second.bytes) /
2283 static_cast<double>(sizes_and_count.second.count);
2284 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002285 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002286 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002287 descriptor.c_str(), sizes_and_count.second.bytes,
2288 sizes_and_count.second.count, average, percent);
2289 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002290 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002291 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002292 CHECK_EQ(object_bytes, object_bytes_total);
2293
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002294 os << StringPrintf("oat_file_bytes = %8zd\n"
2295 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2296 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2297 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2298 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2299 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2300 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002301 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002302 managed_code_bytes,
2303 PercentOfOatBytes(managed_code_bytes),
2304 managed_to_native_code_bytes,
2305 PercentOfOatBytes(managed_to_native_code_bytes),
2306 native_to_managed_code_bytes,
2307 PercentOfOatBytes(native_to_managed_code_bytes),
2308 class_initializer_code_bytes,
2309 PercentOfOatBytes(class_initializer_code_bytes),
2310 large_initializer_code_bytes,
2311 PercentOfOatBytes(large_initializer_code_bytes),
2312 large_method_code_bytes,
2313 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002315 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002316 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002317 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2318 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002319 }
2320
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2322 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2323 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002324 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2325 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2326 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002327 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002328
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2330 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002331 static_cast<double>(managed_code_bytes) /
2332 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002333 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002334 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002335 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002336
2337 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002338 }
2339 } stats_;
2340
2341 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002342 enum {
2343 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2344 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2345 kLargeConstructorDexBytes = 4000,
2346 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2347 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2348 kLargeMethodDexBytes = 16000
2349 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002350
2351 // For performance, use the *os_ directly for anything that doesn't need indentation
2352 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002353 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002354 VariableIndentationOutputStream vios_;
2355 ScopedIndentation indent1_;
2356
Ian Rogers1d54e732013-05-02 21:10:01 -07002357 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002358 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002359 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002360 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002361 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002362
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002363 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002364};
2365
Andreas Gampe00b25f32014-09-17 21:49:05 -07002366static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2367 std::ostream* os) {
2368 // Dumping the image, no explicit class loader.
2369 NullHandle<mirror::ClassLoader> null_class_loader;
2370 options->class_loader_ = &null_class_loader;
2371
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002372 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002373 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002374 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002375 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002376 const ImageHeader& image_header = image_space->GetImageHeader();
2377 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002378 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002379 return EXIT_FAILURE;
2380 }
Igor Murashkin37743352014-11-13 14:38:00 -08002381
Andreas Gampe00b25f32014-09-17 21:49:05 -07002382 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002383
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002384 bool success = image_dumper.Dump();
2385 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002386}
2387
Andreas Gampe00b25f32014-09-17 21:49:05 -07002388static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2389 std::ostream* os) {
2390 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2391
2392 Thread* self = Thread::Current();
2393 CHECK(self != nullptr);
2394 // Need well-known-classes.
2395 WellKnownClasses::Init(self->GetJniEnv());
2396
2397 // Need to register dex files to get a working dex cache.
2398 ScopedObjectAccess soa(self);
2399 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07002400 Runtime::Current()->GetOatFileManager().RegisterOatFile(
2401 std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002402 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002403 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2404 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002405 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002406 CHECK(dex_file != nullptr) << error_msg;
2407 class_linker->RegisterDexFile(*dex_file);
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002408 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002409 }
2410
2411 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002412 // Fake that we're a compiler.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07002413 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002414
2415 // Use the class loader while dumping.
2416 StackHandleScope<1> scope(self);
2417 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2418 soa.Decode<mirror::ClassLoader*>(class_loader));
2419 options->class_loader_ = &loader_handle;
2420
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002421 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002422 bool success = oat_dumper.Dump(*os);
2423 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2424}
2425
2426static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002427 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002428 // No image = no class loader.
2429 NullHandle<mirror::ClassLoader> null_class_loader;
2430 options->class_loader_ = &null_class_loader;
2431
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002432 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002433 bool success = oat_dumper.Dump(*os);
2434 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2435}
2436
2437static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2438 std::ostream* os) {
2439 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002440 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002441 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002442 if (oat_file == nullptr) {
2443 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2444 return EXIT_FAILURE;
2445 }
2446
2447 if (runtime != nullptr) {
2448 return DumpOatWithRuntime(runtime, oat_file, options, os);
2449 } else {
2450 return DumpOatWithoutRuntime(oat_file, options, os);
2451 }
2452}
2453
2454static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2455 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002456 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002457 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002458 if (oat_file == nullptr) {
2459 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2460 return EXIT_FAILURE;
2461 }
2462
2463 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002464 if (!oat_symbolizer.Symbolize()) {
2465 fprintf(stderr, "Failed to symbolize\n");
2466 return EXIT_FAILURE;
2467 }
2468
2469 return EXIT_SUCCESS;
2470}
2471
Igor Murashkin37743352014-11-13 14:38:00 -08002472struct OatdumpArgs : public CmdlineArgs {
2473 protected:
2474 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002475
Igor Murashkin37743352014-11-13 14:38:00 -08002476 virtual ParseStatus ParseCustom(const StringPiece& option,
2477 std::string* error_msg) OVERRIDE {
2478 {
2479 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2480 if (base_parse != kParseUnknownArgument) {
2481 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002482 }
2483 }
2484
Igor Murashkin37743352014-11-13 14:38:00 -08002485 if (option.starts_with("--oat-file=")) {
2486 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2487 } else if (option.starts_with("--image=")) {
2488 image_location_ = option.substr(strlen("--image=")).data();
2489 } else if (option =="--dump:raw_mapping_table") {
2490 dump_raw_mapping_table_ = true;
2491 } else if (option == "--dump:raw_gc_map") {
2492 dump_raw_gc_map_ = true;
2493 } else if (option == "--no-dump:vmap") {
2494 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002495 } else if (option =="--dump:code_info_stack_maps") {
2496 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002497 } else if (option == "--no-disassemble") {
2498 disassemble_code_ = false;
2499 } else if (option.starts_with("--symbolize=")) {
2500 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2501 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002502 } else if (option.starts_with("--class-filter=")) {
2503 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002504 } else if (option.starts_with("--method-filter=")) {
2505 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002506 } else if (option.starts_with("--list-classes")) {
2507 list_classes_ = true;
2508 } else if (option.starts_with("--list-methods")) {
2509 list_methods_ = true;
2510 } else if (option.starts_with("--export-dex-to=")) {
2511 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2512 } else if (option.starts_with("--addr2instr=")) {
2513 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2514 *error_msg = "Address conversion failed";
2515 return kParseError;
2516 }
Igor Murashkin37743352014-11-13 14:38:00 -08002517 } else {
2518 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002519 }
2520
Igor Murashkin37743352014-11-13 14:38:00 -08002521 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002522 }
2523
Igor Murashkin37743352014-11-13 14:38:00 -08002524 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2525 // Infer boot image location from the image location if possible.
2526 if (boot_image_location_ == nullptr) {
2527 boot_image_location_ = image_location_;
2528 }
2529
2530 // Perform the parent checks.
2531 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2532 if (parent_checks != kParseOk) {
2533 return parent_checks;
2534 }
2535
2536 // Perform our own checks.
2537 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2538 *error_msg = "Either --image or --oat-file must be specified";
2539 return kParseError;
2540 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2541 *error_msg = "Either --image or --oat-file must be specified but not both";
2542 return kParseError;
2543 }
2544
2545 return kParseOk;
2546 }
2547
2548 virtual std::string GetUsage() const {
2549 std::string usage;
2550
2551 usage +=
2552 "Usage: oatdump [options] ...\n"
2553 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2554 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2555 "\n"
2556 // Either oat-file or image is required.
2557 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2558 " Example: --oat-file=/system/framework/boot.oat\n"
2559 "\n"
2560 " --image=<file.art>: specifies an input image location.\n"
2561 " Example: --image=/system/framework/boot.art\n"
2562 "\n";
2563
2564 usage += Base::GetUsage();
2565
2566 usage += // Optional.
2567 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2568 " Example: --dump:raw_mapping_table\n"
2569 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002570 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002571 " Example: --dump:raw_gc_map\n"
2572 "\n"
2573 " --no-dump:vmap may be used to disable vmap dumping.\n"
2574 " Example: --no-dump:vmap\n"
2575 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002576 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2577 " Example: --dump:code_info_stack_maps\n"
2578 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002579 " --no-disassemble may be used to disable disassembly.\n"
2580 " Example: --no-disassemble\n"
2581 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002582 " --list-classes may be used to list target file classes (can be used with filters).\n"
2583 " Example: --list-classes\n"
2584 " Example: --list-classes --class-filter=com.example.foo\n"
2585 "\n"
2586 " --list-methods may be used to list target file methods (can be used with filters).\n"
2587 " Example: --list-methods\n"
2588 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2589 "\n"
2590 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2591 " Example: --symbolize=/system/framework/boot.oat\n"
2592 "\n"
2593 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2594 " Example: --class-filter=com.example.foo\n"
2595 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002596 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2597 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002598 "\n"
2599 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2600 " Example: --export-dex-to=/data/local/tmp\n"
2601 "\n"
2602 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2603 " address (e.g. PC from crash dump)\n"
2604 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002605 "\n";
2606
2607 return usage;
2608 }
2609
2610 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002611 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002612 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002613 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002614 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002615 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002616 bool dump_raw_mapping_table_ = false;
2617 bool dump_raw_gc_map_ = false;
2618 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002619 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002620 bool disassemble_code_ = true;
2621 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002622 bool list_classes_ = false;
2623 bool list_methods_ = false;
2624 uint32_t addr2instr_ = 0;
2625 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002626};
2627
Igor Murashkin37743352014-11-13 14:38:00 -08002628struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2629 virtual bool NeedsRuntime() OVERRIDE {
2630 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002631
Igor Murashkin37743352014-11-13 14:38:00 -08002632 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2633 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2634
2635 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2636 args_->dump_raw_mapping_table_,
2637 args_->dump_raw_gc_map_,
2638 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002639 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002640 args_->disassemble_code_,
2641 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002642 args_->class_filter_,
2643 args_->method_filter_,
2644 args_->list_classes_,
2645 args_->list_methods_,
2646 args_->export_dex_location_,
2647 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002648
2649 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2650 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002651 }
2652
Igor Murashkin37743352014-11-13 14:38:00 -08002653 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2654 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002655 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002656
Mathieu Chartierd424d082014-10-15 10:31:46 -07002657 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002658
Andreas Gampec24f3992014-12-17 20:40:11 -08002659 if (args_->symbolize_) {
2660 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2661 } else {
2662 return DumpOat(nullptr,
2663 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002664 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002665 args_->os_) == EXIT_SUCCESS;
2666 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002667 }
2668
Igor Murashkin37743352014-11-13 14:38:00 -08002669 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2670 CHECK(args_ != nullptr);
2671
2672 if (args_->oat_filename_ != nullptr) {
2673 return DumpOat(runtime,
2674 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002675 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002676 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002677 }
Igor Murashkin37743352014-11-13 14:38:00 -08002678
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002679 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002680 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002681 }
2682
Igor Murashkin37743352014-11-13 14:38:00 -08002683 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2684};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002685
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002686} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002687
2688int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002689 art::OatdumpMain main;
2690 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002691}