blob: a99bb00979e5a3855ab151d7fb4a1a0b5934f235 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Vladimir Marko10c13562015-11-25 14:33:36 +000031#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080032#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070033#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080036#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080037#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070038#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070040#include "gc/space/image_space.h"
41#include "gc/space/large_object_space.h"
42#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070043#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044#include "indenter.h"
Vladimir Marko131980f2015-12-03 18:29:23 +000045#include "linker/buffered_output_stream.h"
46#include "linker/file_output_stream.h"
Ian Rogers1809a722013-08-09 22:05:32 -070047#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048#include "mirror/array-inl.h"
49#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010050#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080053#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010054#include "oat_file-inl.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070055#include "oat_file_manager.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080056#include "os.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070057#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058#include "scoped_thread_state_change.h"
Nicolas Geoffray6bc43742015-10-12 18:11:10 +010059#include "stack_map.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080061#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080062#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080063#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070064#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070065#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Igor Murashkin37743352014-11-13 14:38:00 -080067#include <sys/stat.h>
68#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070069
Igor Murashkin37743352014-11-13 14:38:00 -080070namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070071
Mathieu Chartiere401d142015-04-22 13:56:20 -070072const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080073 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070074 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070075 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070076 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070077 "kRefsOnlySaveMethod",
78 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070079};
80
81const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070083 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070084};
85
Mathieu Chartierac8f4392015-08-27 13:54:20 -070086// Map is so that we don't allocate multiple dex files for the same OatDexFile.
87static std::map<const OatFile::OatDexFile*,
88 std::unique_ptr<const DexFile>> opened_dex_files;
89
90const DexFile* OpenDexFile(const OatFile::OatDexFile* oat_dex_file, std::string* error_msg) {
91 DCHECK(oat_dex_file != nullptr);
92 auto it = opened_dex_files.find(oat_dex_file);
93 if (it != opened_dex_files.end()) {
94 return it->second.get();
95 }
96 const DexFile* ret = oat_dex_file->OpenDexFile(error_msg).release();
97 opened_dex_files.emplace(oat_dex_file, std::unique_ptr<const DexFile>(ret));
98 return ret;
99}
100
David Srbeckybc90fd02015-04-22 19:40:27 +0100101class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700102 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100103 OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
David Srbeckybc90fd02015-04-22 19:40:27 +0100104 oat_file_(oat_file), builder_(nullptr),
105 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700106 }
107
108 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
109 uint32_t,
110 const OatFile::OatMethod&,
111 const DexFile&,
112 uint32_t,
113 const DexFile::CodeItem*,
114 uint32_t);
115
116 bool Symbolize() {
David Srbecky6d8c8f02015-10-26 10:57:09 +0000117 const InstructionSet isa = oat_file_->GetOatHeader().GetInstructionSet();
118
119 File* elf_file = OS::CreateEmptyFile(output_name_.c_str());
120 std::unique_ptr<BufferedOutputStream> output_stream(
Vladimir Marko10c13562015-11-25 14:33:36 +0000121 MakeUnique<BufferedOutputStream>(MakeUnique<FileOutputStream>(elf_file)));
David Srbecky6d8c8f02015-10-26 10:57:09 +0000122 builder_.reset(new ElfBuilder<ElfTypes32>(isa, output_stream.get()));
123
124 builder_->Start();
125
126 auto* rodata = builder_->GetRoData();
127 auto* text = builder_->GetText();
128 auto* bss = builder_->GetBss();
129 auto* strtab = builder_->GetStrTab();
130 auto* symtab = builder_->GetSymTab();
131
132 rodata->Start();
133 const uint8_t* rodata_begin = oat_file_->Begin();
134 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
135 rodata->WriteFully(rodata_begin, rodata_size);
136 rodata->End();
137
138 text->Start();
139 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
140 const size_t text_size = oat_file_->End() - text_begin;
141 text->WriteFully(text_begin, text_size);
142 text->End();
143
144 if (oat_file_->BssSize() != 0) {
145 bss->Start();
146 bss->SetSize(oat_file_->BssSize());
147 bss->End();
148 }
149
150 builder_->WriteDynamicSection(elf_file->GetPath());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700151
152 Walk(&art::OatSymbolizer::RegisterForDedup);
153
154 NormalizeState();
155
David Srbecky6d8c8f02015-10-26 10:57:09 +0000156 strtab->Start();
157 strtab->Write(""); // strtab should start with empty string.
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700158 Walk(&art::OatSymbolizer::AddSymbol);
David Srbecky6d8c8f02015-10-26 10:57:09 +0000159 strtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700160
David Srbecky6d8c8f02015-10-26 10:57:09 +0000161 symtab->Start();
162 symtab->Write();
163 symtab->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700164
David Srbecky6d8c8f02015-10-26 10:57:09 +0000165 builder_->End();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700166
Vladimir Marko10c13562015-11-25 14:33:36 +0000167 return builder_->Good();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700168 }
169
170 void Walk(Callback callback) {
171 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
172 for (size_t i = 0; i < oat_dex_files.size(); i++) {
173 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700174 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700175 WalkOatDexFile(oat_dex_file, callback);
176 }
177 }
178
179 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
180 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700181 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
182 if (dex_file == nullptr) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700183 return;
184 }
185 for (size_t class_def_index = 0;
186 class_def_index < dex_file->NumClassDefs();
187 class_def_index++) {
188 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
189 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
190 OatClassType type = oat_class.GetType();
191 switch (type) {
192 case kOatClassAllCompiled:
193 case kOatClassSomeCompiled:
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700194 WalkOatClass(oat_class, *dex_file, class_def, callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700195 break;
196
197 case kOatClassNoneCompiled:
198 case kOatClassMax:
199 // Ignore.
200 break;
201 }
202 }
203 }
204
205 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
206 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700207 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700208 if (class_data == nullptr) { // empty class such as a marker interface?
209 return;
210 }
211 // Note: even if this is an interface or a native class, we still have to walk it, as there
212 // might be a static initializer.
213 ClassDataItemIterator it(dex_file, class_data);
214 SkipAllFields(&it);
215 uint32_t class_method_idx = 0;
216 while (it.HasNextDirectMethod()) {
217 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
218 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700219 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700220 class_method_idx++;
221 it.Next();
222 }
223 while (it.HasNextVirtualMethod()) {
224 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
225 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700226 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700227 class_method_idx++;
228 it.Next();
229 }
230 DCHECK(!it.HasNext());
231 }
232
233 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
234 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
235 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
236 uint32_t method_access_flags, Callback callback) {
237 if ((method_access_flags & kAccAbstract) != 0) {
238 // Abstract method, no code.
239 return;
240 }
241 if (oat_method.GetCodeOffset() == 0) {
242 // No code.
243 return;
244 }
245
246 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
247 method_access_flags);
248 }
249
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700250 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
251 uint32_t class_method_index ATTRIBUTE_UNUSED,
252 const OatFile::OatMethod& oat_method,
253 const DexFile& dex_file ATTRIBUTE_UNUSED,
254 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
255 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
256 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700257 state_[oat_method.GetCodeOffset()]++;
258 }
259
260 void NormalizeState() {
261 for (auto& x : state_) {
262 if (x.second == 1) {
263 state_[x.first] = 0;
264 }
265 }
266 }
267
268 enum class DedupState { // private
269 kNotDeduplicated,
270 kDeduplicatedFirst,
271 kDeduplicatedOther
272 };
273 DedupState IsDuplicated(uint32_t offset) {
274 if (state_[offset] == 0) {
275 return DedupState::kNotDeduplicated;
276 }
277 if (state_[offset] == 1) {
278 return DedupState::kDeduplicatedOther;
279 }
280 state_[offset] = 1;
281 return DedupState::kDeduplicatedFirst;
282 }
283
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700284 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
285 uint32_t class_method_index ATTRIBUTE_UNUSED,
286 const OatFile::OatMethod& oat_method,
287 const DexFile& dex_file,
288 uint32_t dex_method_idx,
289 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
290 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700291 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
292 if (dedup != DedupState::kDeduplicatedOther) {
293 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
294
295 if (dedup == DedupState::kDeduplicatedFirst) {
296 pretty_name = "[Dedup]" + pretty_name;
297 }
298
David Srbecky6d8c8f02015-10-26 10:57:09 +0000299 int name_offset = builder_->GetStrTab()->Write(pretty_name);
300 builder_->GetSymTab()->Add(name_offset, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700301 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
302 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700303 }
304 }
305
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700306 private:
307 static void SkipAllFields(ClassDataItemIterator* it) {
308 while (it->HasNextStaticField()) {
309 it->Next();
310 }
311 while (it->HasNextInstanceField()) {
312 it->Next();
313 }
314 }
315
316 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100317 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700318 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700319 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700320};
321
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700322class OatDumperOptions {
323 public:
324 OatDumperOptions(bool dump_raw_mapping_table,
325 bool dump_raw_gc_map,
326 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100327 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700328 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700329 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800330 const char* class_filter,
331 const char* method_filter,
332 bool list_classes,
333 bool list_methods,
334 const char* export_dex_location,
335 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700336 : dump_raw_mapping_table_(dump_raw_mapping_table),
337 dump_raw_gc_map_(dump_raw_gc_map),
338 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100339 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700340 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700341 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800342 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000343 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800344 list_classes_(list_classes),
345 list_methods_(list_methods),
346 export_dex_location_(export_dex_location),
347 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800348 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700349
350 const bool dump_raw_mapping_table_;
351 const bool dump_raw_gc_map_;
352 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100353 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700354 const bool disassemble_code_;
355 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000357 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800358 const bool list_classes_;
359 const bool list_methods_;
360 const char* const export_dex_location_;
361 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700362 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700363};
364
Elliott Hughese3c845c2012-02-28 17:23:01 -0800365class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700366 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100367 OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000368 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800369 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700370 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800371 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800372 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
373 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800374 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000375 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100376 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800377 CHECK(options_.class_loader_ != nullptr);
378 CHECK(options_.class_filter_ != nullptr);
379 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800380 AddAllOffsets();
381 }
382
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700383 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700384 delete disassembler_;
385 }
386
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800387 InstructionSet GetInstructionSet() {
388 return instruction_set_;
389 }
390
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700391 bool Dump(std::ostream& os) {
392 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800393 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700394
395 os << "MAGIC:\n";
396 os << oat_header.GetMagic() << "\n\n";
397
398 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800399 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700400
Elliott Hughesa72ec822012-03-05 17:12:22 -0800401 os << "INSTRUCTION SET:\n";
402 os << oat_header.GetInstructionSet() << "\n\n";
403
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700404 {
405 std::unique_ptr<const InstructionSetFeatures> features(
406 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
407 oat_header.GetInstructionSetFeaturesBitmap()));
408 os << "INSTRUCTION SET FEATURES:\n";
409 os << features->GetFeatureString() << "\n\n";
410 }
Dave Allison70202782013-10-22 17:52:19 -0700411
Brian Carlstromaded5f72011-10-07 17:15:04 -0700412 os << "DEX FILE COUNT:\n";
413 os << oat_header.GetDexFileCount() << "\n\n";
414
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800415#define DUMP_OAT_HEADER_OFFSET(label, offset) \
416 os << label " OFFSET:\n"; \
417 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800418 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800419 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
420 } \
421 os << StringPrintf("\n\n");
422
423 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
424 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
425 GetInterpreterToInterpreterBridgeOffset);
426 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
427 GetInterpreterToCompiledCodeBridgeOffset);
428 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
429 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800430 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
431 GetQuickGenericJniTrampolineOffset);
432 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
433 GetQuickImtConflictTrampolineOffset);
434 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
435 GetQuickResolutionTrampolineOffset);
436 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
437 GetQuickToInterpreterBridgeOffset);
438#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700439
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700440 os << "IMAGE PATCH DELTA:\n";
441 os << StringPrintf("%d (0x%08x)\n\n",
442 oat_header.GetImagePatchDelta(),
443 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700444
Brian Carlstrom28db0122012-10-18 16:20:41 -0700445 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
446 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
447
448 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800449 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700450
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700451 // Print the key-value store.
452 {
453 os << "KEY VALUE STORE:\n";
454 size_t index = 0;
455 const char* key;
456 const char* value;
457 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
458 os << key << " = " << value << "\n";
459 index++;
460 }
461 os << "\n";
462 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700463
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800464 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700465 os << "BEGIN:\n";
466 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700467
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700468 os << "END:\n";
469 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
470 }
471
472 os << "SIZE:\n";
473 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700474
475 os << std::flush;
476
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800477 // If set, adjust relative address to be searched
478 if (options_.addr2instr_ != 0) {
479 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
480 os << "SEARCH ADDRESS (executable offset + input):\n";
481 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
482 }
483
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800484 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
485 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700486 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800487
488 // If file export selected skip file analysis
489 if (options_.export_dex_location_) {
490 if (!ExportDexFile(os, *oat_dex_file)) {
491 success = false;
492 }
493 } else {
494 if (!DumpOatDexFile(os, *oat_dex_file)) {
495 success = false;
496 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700497 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700498 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700499 os << std::flush;
500 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700501 }
502
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700504 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
505 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800506 return 0; // Address not in oat file
507 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800508 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
509 reinterpret_cast<uintptr_t>(oat_file_.Begin());
510 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800511 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800512 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800513 return end_offset - begin_offset;
514 }
515
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800516 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700517 return oat_file_.GetOatHeader().GetInstructionSet();
518 }
519
Mathieu Chartier90443472015-07-16 20:32:27 -0700520 const void* GetQuickOatCode(ArtMethod* m) SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800521 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
522 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700523 CHECK(oat_dex_file != nullptr);
524 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700525 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
526 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700527 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
528 << "': " << error_msg;
529 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800530 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700531 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800532 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700533 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700534 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100535 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800536 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100537 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800538 }
539 }
540 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700541 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800542 }
543
Brian Carlstromaded5f72011-10-07 17:15:04 -0700544 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800545 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800546 // We don't know the length of the code for each method, but we need to know where to stop
547 // when disassembling. What we do know is that a region of code will be followed by some other
548 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
549 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800550 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
551 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700552 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700553 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700554 const DexFile* const dex_file = OpenDexFile(oat_dex_file, &error_msg);
555 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700556 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
557 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800558 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800559 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800560 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800561 for (size_t class_def_index = 0;
562 class_def_index < dex_file->NumClassDefs();
563 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800564 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100565 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700566 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700567 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800568 ClassDataItemIterator it(*dex_file, class_data);
569 SkipAllFields(it);
570 uint32_t class_method_index = 0;
571 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100572 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800573 it.Next();
574 }
575 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100576 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800577 it.Next();
578 }
579 }
580 }
581 }
582
583 // If the last thing in the file is code for a method, there won't be an offset for the "next"
584 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
585 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800586 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800587 }
588
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700589 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
590 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
591 }
592
Elliott Hughese3c845c2012-02-28 17:23:01 -0800593 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800594 uint32_t code_offset = oat_method.GetCodeOffset();
595 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
596 code_offset &= ~0x1;
597 }
598 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800599 offsets_.insert(oat_method.GetMappingTableOffset());
600 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800601 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800602 }
603
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700604 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
605 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800606 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700607 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800608 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800609 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700610
611 // Create the verifier early.
612
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700613 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700614 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
615 if (dex_file == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700616 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700617 os << std::flush;
618 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700619 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100620
621 VariableIndentationOutputStream vios(&os);
622 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800623 for (size_t class_def_index = 0;
624 class_def_index < dex_file->NumClassDefs();
625 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700626 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
627 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800628
629 // TODO: Support regex
630 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
631 continue;
632 }
633
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700634 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100635 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700636 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
637 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100638 << " (" << oat_class.GetStatus() << ")"
639 << " (" << oat_class.GetType() << ")\n";
640 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800641 if (options_.list_classes_) continue;
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700642 if (!DumpOatClass(&vios, oat_class, *dex_file, class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700643 success = false;
644 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800645 if (stop_analysis) {
646 os << std::flush;
647 return success;
648 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700649 }
650
651 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700652 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700653 }
654
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800655 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
656 std::string error_msg;
657 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
658
Mathieu Chartierac8f4392015-08-27 13:54:20 -0700659 const DexFile* const dex_file = OpenDexFile(&oat_dex_file, &error_msg);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800660 if (dex_file == nullptr) {
661 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
662 return false;
663 }
664 size_t fsize = oat_dex_file.FileSize();
665
666 // Some quick checks just in case
667 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
668 os << "Invalid dex file\n";
669 return false;
670 }
671
672 // Verify output directory exists
673 if (!OS::DirectoryExists(options_.export_dex_location_)) {
674 // TODO: Extend OS::DirectoryExists if symlink support is required
675 os << options_.export_dex_location_ << " output directory not found or symlink\n";
676 return false;
677 }
678
679 // Beautify path names
680 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
681 return false;
682 }
683
684 std::string dex_orig_name;
685 size_t dex_orig_pos = dex_file_location.rfind('/');
686 if (dex_orig_pos == std::string::npos)
687 dex_orig_name = dex_file_location;
688 else
689 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
690
691 // A more elegant approach to efficiently name user installed apps is welcome
692 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
693 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
694 size_t apk_orig_pos = dex_file_location.rfind('/');
695 if (apk_orig_pos != std::string::npos) {
696 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
697 }
698 }
699
700 std::string out_dex_path(options_.export_dex_location_);
701 if (out_dex_path.back() != '/') {
702 out_dex_path.append("/");
703 }
704 out_dex_path.append(dex_orig_name);
705 out_dex_path.append("_export.dex");
706 if (out_dex_path.length() > PATH_MAX) {
707 return false;
708 }
709
710 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
711 if (file.get() == nullptr) {
712 os << "Failed to open output dex file " << out_dex_path;
713 return false;
714 }
715
716 if (!file->WriteFully(dex_file->Begin(), fsize)) {
717 os << "Failed to write dex file";
718 file->Erase();
719 return false;
720 }
721
722 if (file->FlushCloseOrErase() != 0) {
723 os << "Flush and close failed";
724 return false;
725 }
726
727 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
728 os << std::flush;
729
730 return true;
731 }
732
Elliott Hughese3c845c2012-02-28 17:23:01 -0800733 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700734 while (it.HasNextStaticField()) {
735 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700736 }
Ian Rogers0571d352011-11-03 19:51:38 -0700737 while (it.HasNextInstanceField()) {
738 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700739 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800740 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700741
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100742 bool DumpOatClass(VariableIndentationOutputStream* vios,
743 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800744 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700745 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800746 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700747 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700748 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100749 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700750 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800751 }
752 ClassDataItemIterator it(dex_file, class_data);
753 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700754 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700755 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100756 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700757 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800758 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700759 success = false;
760 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800761 if (addr_found) {
762 *stop_analysis = true;
763 return success;
764 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700765 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700766 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700767 }
Ian Rogers0571d352011-11-03 19:51:38 -0700768 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100769 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700770 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800771 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700772 success = false;
773 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800774 if (addr_found) {
775 *stop_analysis = true;
776 return success;
777 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700778 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700779 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700780 }
Ian Rogers0571d352011-11-03 19:51:38 -0700781 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100782 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700783 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700784 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800785
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700786 static constexpr uint32_t kPrologueBytes = 16;
787
788 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
789 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
790
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100791 bool DumpOatMethod(VariableIndentationOutputStream* vios,
792 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700793 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700794 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800795 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800796 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700797 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800798
799 // TODO: Support regex
800 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
801 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000802 return success;
803 }
804
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800805 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100806 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
807 class_method_index, pretty_method.c_str(),
808 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800809 if (options_.list_methods_) return success;
810
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800811 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
812 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
813 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
814 uint32_t code_offset = oat_method.GetCodeOffset();
815 uint32_t code_size = oat_method.GetQuickCodeSize();
816 if (resolved_addr2instr_ != 0) {
817 if (resolved_addr2instr_ > code_offset + code_size) {
818 return success;
819 } else {
820 *addr_found = true; // stop analyzing file at next iteration
821 }
822 }
823
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100824 // Everything below is indented at least once.
825 ScopedIndentation indent1(vios);
826
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800827 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100828 vios->Stream() << "DEX CODE:\n";
829 ScopedIndentation indent2(vios);
830 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700831 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700832
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700833 std::unique_ptr<StackHandleScope<1>> hs;
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700834 std::unique_ptr<verifier::MethodVerifier> verifier;
835 if (Runtime::Current() != nullptr) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700836 // We need to have the handle scope stay live until after the verifier since the verifier has
837 // a handle to the dex cache from hs.
838 hs.reset(new StackHandleScope<1>(Thread::Current()));
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100839 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
840 ScopedIndentation indent2(vios);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700841 verifier.reset(DumpVerifier(vios, hs.get(),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100842 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700843 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700844 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800845 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100846 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800847 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100848 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100849 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100850 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700851 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100852 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700853 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
854 oat_method_offsets_offset, oat_file_.Size());
855 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100856 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700857 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800858 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700859
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100860 ScopedIndentation indent2(vios);
861 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700862 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
863 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100864 vios->Stream() << StringPrintf("WARNING: "
865 "code offset 0x%08x is past end of file 0x%08zx.\n",
866 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700867 success = false;
868 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100869 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700870
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100871 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800872 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100873 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700874 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800875 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100876 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700877 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100878 vios->Stream() << StringPrintf("WARNING: "
879 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
880 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700881 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800882 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100883 ScopedIndentation indent3(vios);
884 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800885 }
886 }
887 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100888 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700889 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
890 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700891
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800892 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100893 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700894 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100895 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700896 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100897 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700898 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
899 method_header_offset, oat_file_.Size());
900 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100901 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700902 return false;
903 }
904
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100905 ScopedIndentation indent2(vios);
906 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800907 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100908 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700909 }
910 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100911 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700912 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100913 vios->Stream() << StringPrintf("WARNING: "
914 "mapping table offset 0x%08x is past end of file 0x%08zx. "
915 "mapping table offset was loaded from offset 0x%08x.\n",
916 mapping_table_offset, oat_file_.Size(),
917 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700918 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800919 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100920 ScopedIndentation indent3(vios);
921 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700922 }
923
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100924 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800925 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100926 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700927 }
928 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100929 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700930 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100931 vios->Stream() << StringPrintf("WARNING: "
932 "vmap table offset 0x%08x is past end of file 0x%08zx. "
933 "vmap table offset was loaded from offset 0x%08x.\n",
934 vmap_table_offset, oat_file_.Size(),
935 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700936 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800937 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100938 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700939 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800940 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700941 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100942 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700943
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100944 ScopedIndentation indent2(vios);
945 vios->Stream()
946 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
947 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
948 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
949 vios->Stream() << "\n";
950 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
951 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
952 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700953 }
954 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100955 // Based on spill masks from QuickMethodFrameInfo so placed
956 // after it is dumped, but useful for understanding quick
957 // code, so dumped here.
958 ScopedIndentation indent2(vios);
959 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700960 }
961 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700963 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
964 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100965 ScopedIndentation indent2(vios);
966 vios->Stream() << StringPrintf("WARNING: "
967 "code size offset 0x%08x is past end of file 0x%08zx.",
968 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700969 success = false;
970 } else {
971 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700972 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
973 uint64_t aligned_code_end = aligned_code_begin + code_size;
974
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800975 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100976 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700977 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100978 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
979 code_offset,
980 code_size_offset,
981 code_size,
982 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700983
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100984 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700985 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100986 vios->Stream() << StringPrintf("WARNING: "
987 "start of code at 0x%08x is past end of file 0x%08zx.",
988 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700989 success = false;
990 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100991 vios->Stream() << StringPrintf(
992 "WARNING: "
993 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
994 "code size is 0x%08x loaded from offset 0x%08x.\n",
995 aligned_code_end, oat_file_.Size(),
996 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700997 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800998 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700999 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001000 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001001 }
1002 }
1003 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001004 vios->Stream() << StringPrintf(
1005 "WARNING: "
1006 "code size %d is bigger than max expected threshold of %d. "
1007 "code size is 0x%08x loaded from offset 0x%08x.\n",
1008 code_size, kMaxCodeSize,
1009 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001010 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001011 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001012 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001013 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001014 }
1015 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001016 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001017 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001018 }
1019 }
1020 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001021 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001022 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001023 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001024
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001025 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1026 if (spill_mask == 0) {
1027 return;
1028 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001029 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001030 for (size_t i = 0; i < 32; i++) {
1031 if ((spill_mask & (1 << i)) != 0) {
1032 if (is_float) {
1033 os << "fr" << i;
1034 } else {
1035 os << "r" << i;
1036 }
1037 spill_mask ^= 1 << i; // clear bit
1038 if (spill_mask != 0) {
1039 os << ", ";
1040 } else {
1041 break;
1042 }
1043 }
1044 }
1045 os << ")";
1046 }
1047
Roland Levillain442b46a2015-02-18 16:54:21 +00001048 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001049 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001050 const OatFile::OatMethod& oat_method,
1051 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001052 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1053 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001054 const void* raw_code_info = oat_method.GetVmapTable();
1055 if (raw_code_info != nullptr) {
1056 CodeInfo code_info(raw_code_info);
1057 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001058 ScopedIndentation indent1(vios);
1059 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001060 }
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001061 } else if (IsMethodGeneratedByDexToDexCompiler(oat_method, code_item)) {
1062 // We don't encode the size in the table, so just emit that we have quickened
1063 // information.
1064 ScopedIndentation indent(vios);
1065 vios->Stream() << "quickened data\n";
Roland Levillain442b46a2015-02-18 16:54:21 +00001066 } else {
1067 // Otherwise, display the vmap table.
1068 const uint8_t* raw_table = oat_method.GetVmapTable();
1069 if (raw_table != nullptr) {
1070 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001071 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001072 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001073 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001074 }
1075
1076 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001077 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001078 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001079 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001080 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001081 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001082 oat_method.GetCodeOffset(),
1083 code_item.registers_size_,
1084 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001085 }
1086
Roland Levillain442b46a2015-02-18 16:54:21 +00001087 // Display a vmap table.
1088 void DumpVmapTable(std::ostream& os,
1089 const OatFile::OatMethod& oat_method,
1090 const VmapTable& vmap_table) {
1091 bool first = true;
1092 bool processing_fp = false;
1093 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1094 for (size_t i = 0; i < vmap_table.Size(); i++) {
1095 uint16_t dex_reg = vmap_table[i];
1096 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1097 processing_fp ? kFloatVReg : kIntVReg);
1098 os << (first ? "v" : ", v") << dex_reg;
1099 if (!processing_fp) {
1100 os << "/r" << cpu_reg;
1101 } else {
1102 os << "/fr" << cpu_reg;
1103 }
1104 first = false;
1105 if (!processing_fp && dex_reg == 0xFFFF) {
1106 processing_fp = true;
1107 spill_mask = oat_method.GetFpSpillMask();
1108 }
1109 }
1110 os << "\n";
1111 }
1112
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001113 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1114 const DexFile::CodeItem* code_item) {
1115 if (code_item != nullptr) {
1116 size_t num_locals_ins = code_item->registers_size_;
1117 size_t num_ins = code_item->ins_size_;
1118 size_t num_locals = num_locals_ins - num_ins;
1119 size_t num_outs = code_item->outs_size_;
1120
1121 os << "vr_stack_locations:";
1122 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1123 // For readability, delimit the different kinds of VRs.
1124 if (reg == num_locals_ins) {
1125 os << "\n\tmethod*:";
1126 } else if (reg == num_locals && num_ins > 0) {
1127 os << "\n\tins:";
1128 } else if (reg == 0 && num_locals > 0) {
1129 os << "\n\tlocals:";
1130 }
1131
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001132 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1133 code_item,
1134 oat_method.GetCoreSpillMask(),
1135 oat_method.GetFpSpillMask(),
1136 oat_method.GetFrameSizeInBytes(),
1137 reg,
1138 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001139 os << " v" << reg << "[sp + #" << offset << "]";
1140 }
1141
1142 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1143 if (out_reg == 0) {
1144 os << "\n\touts:";
1145 }
1146
1147 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1148 os << " v" << out_reg << "[sp + #" << offset << "]";
1149 }
1150
1151 os << "\n";
1152 }
1153 }
1154
Ian Rogersb23a7722012-10-09 16:54:26 -07001155 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001156 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001157 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001158 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001159 const VmapTable vmap_table(raw_table);
1160 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001161 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001162 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1163 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1164 : oat_method.GetCoreSpillMask();
1165 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001166 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001167 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1168 code_item,
1169 oat_method.GetCoreSpillMask(),
1170 oat_method.GetFpSpillMask(),
1171 oat_method.GetFrameSizeInBytes(),
1172 reg,
1173 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001174 os << "[sp + #" << offset << "]";
1175 }
1176 }
1177 }
1178
Ian Rogersef7d42f2014-01-06 12:55:46 -08001179 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1180 const DexFile::CodeItem* code_item,
1181 size_t num_regs, const uint8_t* reg_bitmap) {
1182 bool first = true;
1183 for (size_t reg = 0; reg < num_regs; reg++) {
1184 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1185 if (first) {
1186 os << " v" << reg << " (";
1187 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1188 os << ")";
1189 first = false;
1190 } else {
1191 os << ", v" << reg << " (";
1192 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1193 os << ")";
1194 }
1195 }
1196 }
1197 if (first) {
1198 os << "No registers in GC map\n";
1199 } else {
1200 os << "\n";
1201 }
1202 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001203 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1204 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001205 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001206 if (gc_map_raw == nullptr) {
1207 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001208 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001209 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001210 NativePcOffsetToReferenceMap map(gc_map_raw);
1211 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1212 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1213 map.GetNativePcOffset(entry);
1214 os << StringPrintf("%p", native_pc);
1215 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001216 }
1217 }
1218
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001219 void DumpMappingTable(VariableIndentationOutputStream* vios,
1220 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001221 const void* quick_code = oat_method.GetQuickCode();
1222 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001223 return;
1224 }
Ian Rogers1809a722013-08-09 22:05:32 -07001225 MappingTable table(oat_method.GetMappingTable());
1226 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001227 if (table.PcToDexSize() != 0) {
1228 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001229 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001230 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001231 ScopedIndentation indent1(vios);
1232 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001233 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001234 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001235 }
1236 if (table.DexToPcSize() != 0) {
1237 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001238 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001239 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001240 ScopedIndentation indent1(vios);
1241 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001242 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001243 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001244 }
1245 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001246 }
1247
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001248 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001249 const OatFile::OatMethod& oat_method,
1250 const DexFile::CodeItem* code_item,
1251 size_t offset,
1252 bool suspend_point_mapping) {
1253 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1254 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001255 ScopedIndentation indent1(vios);
1256 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001257 }
1258 // The return value is not used in the case of a method compiled
1259 // with the optimizing compiler.
1260 return DexFile::kDexNoIndex;
1261 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001262 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001263 }
1264 }
1265
Ian Rogers1809a722013-08-09 22:05:32 -07001266 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1267 size_t offset, bool suspend_point_mapping) {
1268 MappingTable table(oat_method.GetMappingTable());
1269 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1270 typedef MappingTable::PcToDexIterator It;
1271 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1272 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001273 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001274 return cur.DexPc();
1275 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001276 }
Ian Rogers1809a722013-08-09 22:05:32 -07001277 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1278 typedef MappingTable::DexToPcIterator It;
1279 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1280 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001281 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001282 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001283 }
1284 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001285 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001286 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001287 }
1288
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001289 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1290 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001291 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001292 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001293 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001294 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001295 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001296 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001297 bool first = true;
1298 for (size_t reg = 0; reg < num_regs; reg++) {
1299 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1300 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001301 os << "GC map objects: v" << reg << " (";
1302 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001303 os << ")";
1304 first = false;
1305 } else {
1306 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001307 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001308 os << ")";
1309 }
1310 }
1311 }
1312 if (!first) {
1313 os << "\n";
1314 }
1315 }
1316 }
1317 }
1318
Mathieu Chartier590fee92013-09-13 13:46:47 -07001319 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1320 const OatFile::OatMethod& oat_method,
1321 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1322 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001323 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001324 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001325 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1326 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1327 if (kind != kUndefined) {
1328 if (first) {
1329 os << "VRegs: v";
1330 first = false;
1331 } else {
1332 os << ", v";
1333 }
1334 os << reg << " (";
1335 switch (kind) {
1336 case kImpreciseConstant:
1337 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1338 DescribeVReg(os, oat_method, code_item, reg, kind);
1339 break;
1340 case kConstant:
1341 os << "Constant: " << kinds.at((reg * 2) + 1);
1342 break;
1343 default:
1344 DescribeVReg(os, oat_method, code_item, reg, kind);
1345 break;
1346 }
1347 os << ")";
1348 }
1349 }
1350 if (!first) {
1351 os << "\n";
1352 }
1353 }
1354
1355
Ian Rogersb23a7722012-10-09 16:54:26 -07001356 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001357 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001358 size_t i = 0;
1359 while (i < code_item->insns_size_in_code_units_) {
1360 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001361 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1362 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001363 i += instruction->SizeInCodeUnits();
1364 }
1365 }
1366 }
1367
Roland Levillainf2650d12015-05-28 14:53:28 +01001368 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1369 // the optimizing compiler?
1370 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1371 const DexFile::CodeItem* code_item) {
1372 // If the native GC map is null and the Dex `code_item` is not
1373 // null, then this method has been compiled with the optimizing
1374 // compiler.
Nicolas Geoffray0a5cd122015-07-16 14:15:05 +01001375 return oat_method.GetQuickCode() != nullptr &&
1376 oat_method.GetGcMap() == nullptr &&
1377 code_item != nullptr;
1378 }
1379
1380 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1381 // the dextodex compiler?
1382 static bool IsMethodGeneratedByDexToDexCompiler(const OatFile::OatMethod& oat_method,
1383 const DexFile::CodeItem* code_item) {
1384 // If the quick code is null, the Dex `code_item` is not
1385 // null, and the vmap table is not null, then this method has been compiled
1386 // with the dextodex compiler.
1387 return oat_method.GetQuickCode() == nullptr &&
1388 oat_method.GetVmapTable() != nullptr &&
1389 code_item != nullptr;
Roland Levillainf2650d12015-05-28 14:53:28 +01001390 }
1391
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001392 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001393 const OatFile::OatMethod& oat_method,
1394 const DexFile::CodeItem* code_item,
1395 size_t offset) {
1396 // This method is only relevant for oat methods compiled with the
1397 // optimizing compiler.
1398 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1399
1400 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1401 const void* raw_code_info = oat_method.GetVmapTable();
1402 if (raw_code_info != nullptr) {
1403 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001404 StackMapEncoding encoding = code_info.ExtractEncoding();
1405 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001406 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001407 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1408 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001409 }
1410 }
1411 }
1412
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001413 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001414 StackHandleScope<1>* hs,
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001415 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001416 const DexFile* dex_file,
1417 const DexFile::ClassDef& class_def,
1418 const DexFile::CodeItem* code_item,
1419 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001420 if ((method_access_flags & kAccNative) == 0) {
1421 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001422 Runtime* const runtime = Runtime::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001423 Handle<mirror::DexCache> dex_cache(
Mathieu Chartierd57d4542015-10-14 10:55:30 -07001424 hs->NewHandle(runtime->GetClassLinker()->RegisterDexFile(*dex_file,
1425 runtime->GetLinearAlloc())));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001426 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001427 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001428 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1429 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001430 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001431
1432 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001433 }
1434
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001435 void DumpCode(VariableIndentationOutputStream* vios,
1436 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001437 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1438 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001439 const void* quick_code = oat_method.GetQuickCode();
1440
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001441 if (code_size == 0) {
1442 code_size = oat_method.GetQuickCodeSize();
1443 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001444 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001445 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001446 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001447 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001448 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1449 size_t offset = 0;
1450 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001451 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001452 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001453 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001454 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001455 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001456 uint32_t dex_pc =
1457 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001458 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001459 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001460 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001461 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001462 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001463 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001464 }
1465 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001466 }
1467 }
1468
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001469 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001470 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001471 const OatDumperOptions& options_;
1472 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001473 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001474 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001475 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001476};
1477
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001478class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001479 public:
Roland Levillain3887c462015-08-12 18:15:42 +01001480 ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
1481 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001482 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001483 vios_(os),
1484 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001485 image_space_(image_space),
1486 image_header_(image_header),
1487 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001488
Mathieu Chartier90443472015-07-16 20:32:27 -07001489 bool Dump() SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001490 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001491 std::ostream& indent_os = vios_.Stream();
1492
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001493 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001494
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001495 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001496
Mathieu Chartiere401d142015-04-22 13:56:20 -07001497 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1498
1499 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1500 auto section = static_cast<ImageHeader::ImageSections>(i);
1501 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1502 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001503
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001504 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001505
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001506 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001507
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001508 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1509
1510 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1511
1512 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001513
Alex Lighta59dd802014-07-02 16:28:08 -07001514 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1515
Igor Murashkin46774762014-10-22 11:37:02 -07001516 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1517
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001518 {
1519 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001520 static_assert(arraysize(image_roots_descriptions_) ==
1521 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001522 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1523 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1524 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001525 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001526 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001527 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001528 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001529 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001530 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001531 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1532 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001533 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001534 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1535 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001536 run++;
1537 } else {
1538 break;
1539 }
1540 }
1541 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001542 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001543 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001544 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001545 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001546 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001547 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001548 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001550 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001552 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001553 }
1554 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001555 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001556
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001557 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001558 os << "METHOD ROOTS\n";
1559 static_assert(arraysize(image_methods_descriptions_) ==
1560 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1561 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1562 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1563 const char* description = image_methods_descriptions_[i];
1564 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001565 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001566 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001567 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001569
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001570 Runtime* const runtime = Runtime::Current();
1571 ClassLinker* class_linker = runtime->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001572 std::string image_filename = image_space_.GetImageFilename();
1573 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001574 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001575 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001576 std::string error_msg;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001577 const OatFile* oat_file = runtime->GetOatFileManager().FindOpenedOatFileFromOatLocation(
1578 oat_location);
Alex Lighta59dd802014-07-02 16:28:08 -07001579 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001580 oat_file = OatFile::Open(oat_location, oat_location,
1581 nullptr, nullptr, false, nullptr,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001582 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001583 if (oat_file == nullptr) {
1584 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001585 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001586 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001587 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001588 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001589
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001590 stats_.oat_file_bytes = oat_file->Size();
1591
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001592 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001593
Mathieu Chartier02e25112013-08-14 16:14:24 -07001594 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001595 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001596 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1597 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001598 }
1599
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001600 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001601
1602 // Loop through all the image spaces and dump their objects.
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001603 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07001604 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001605 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001606 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001607 {
1608 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1609 heap->FlushAllocStack();
1610 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001611 // Since FlushAllocStack() above resets the (active) allocation
1612 // stack. Need to revoke the thread-local allocation stacks that
1613 // point into it.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001614 ScopedThreadSuspension sts(self, kNative);
Mathieu Chartier4f55e222015-09-04 13:26:21 -07001615 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001616 heap->RevokeAllThreadLocalAllocationStacks(self);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001617 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001619 // Mark dex caches.
Vladimir Marko05792b92015-08-03 11:56:49 +01001620 dex_caches_.clear();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001621 {
1622 ReaderMutexLock mu(self, *class_linker->DexLock());
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001623 for (const ClassLinker::DexCacheData& data : class_linker->GetDexCachesData()) {
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001624 mirror::DexCache* dex_cache =
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -08001625 down_cast<mirror::DexCache*>(self->DecodeJObject(data.weak_root));
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001626 if (dex_cache != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01001627 dex_caches_.insert(dex_cache);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -07001628 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001629 }
1630 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001631 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001632 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 if (space->IsImageSpace()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001634 auto* image_space = space->AsImageSpace();
1635 // Dump the normal objects before ArtMethods.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1637 indent_os << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001638 // TODO: Dump fields.
1639 // Dump methods after.
1640 const auto& methods_section = image_header_.GetMethodsSection();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001641 const size_t pointer_size =
Mathieu Chartiere401d142015-04-22 13:56:20 -07001642 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001643 DumpArtMethodVisitor visitor(this);
Vladimir Markocf36d492015-08-12 19:27:26 +01001644 methods_section.VisitPackedArtMethods(&visitor, image_space->Begin(), pointer_size);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001645 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001646 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001647 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001648 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001649 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001650 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001651 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001652 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001653 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001654 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001655 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001656 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001657 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001658 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001659 size_t header_bytes = sizeof(ImageHeader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001660 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1661 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1662 const auto& method_section = image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001663 const auto& dex_cache_arrays_section = image_header_.GetImageSection(
1664 ImageHeader::kSectionDexCacheArrays);
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001665 const auto& intern_section = image_header_.GetImageSection(
1666 ImageHeader::kSectionInternedStrings);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001667 stats_.header_bytes = header_bytes;
Vladimir Markocf36d492015-08-12 19:27:26 +01001668 stats_.alignment_bytes += RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1669 // Add padding between the field and method section.
1670 // (Field section is 4-byte aligned, method section is 8-byte aligned on 64-bit targets.)
Vladimir Marko05792b92015-08-03 11:56:49 +01001671 stats_.alignment_bytes += method_section.Offset() -
1672 (field_section.Offset() + field_section.Size());
1673 // Add padding between the dex cache arrays section and the intern table. (Dex cache
1674 // arrays section is 4-byte aligned on 32-bit targets, intern table is 8-byte aligned.)
1675 stats_.alignment_bytes += intern_section.Offset() -
1676 (dex_cache_arrays_section.Offset() + dex_cache_arrays_section.Size());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001677 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1678 stats_.bitmap_bytes += bitmap_section.Size();
1679 stats_.art_field_bytes += field_section.Size();
Vladimir Markocf36d492015-08-12 19:27:26 +01001680 stats_.art_method_bytes += method_section.Size();
Vladimir Marko05792b92015-08-03 11:56:49 +01001681 stats_.dex_cache_arrays_bytes += dex_cache_arrays_section.Size();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001682 stats_.interned_strings_bytes += intern_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001683 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001684 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001685
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001686 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001687
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001688 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001689 }
1690
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001691 private:
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001692 class DumpArtMethodVisitor : public ArtMethodVisitor {
1693 public:
1694 explicit DumpArtMethodVisitor(ImageDumper* image_dumper) : image_dumper_(image_dumper) {}
1695
1696 virtual void Visit(ArtMethod* method) OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
1697 std::ostream& indent_os = image_dumper_->vios_.Stream();
1698 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1699 image_dumper_->DumpMethod(method, image_dumper_, indent_os);
1700 indent_os << "\n";
1701 }
1702
1703 private:
1704 ImageDumper* const image_dumper_;
1705 };
1706
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001707 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Mathieu Chartier90443472015-07-16 20:32:27 -07001708 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001709 CHECK(type != nullptr);
1710 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001711 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001712 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001713 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001715 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001716 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001717 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001718 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001719 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001720 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001721 }
1722 }
1723
Mathieu Chartierc7853442015-03-27 14:35:38 -07001724 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -07001725 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001726 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001727 switch (field->GetTypeAsPrimitiveType()) {
1728 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001729 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001730 break;
1731 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001732 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001733 break;
1734 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001735 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001736 break;
1737 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001738 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001739 break;
1740 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001741 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001742 break;
1743 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001744 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001745 break;
1746 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001747 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1748 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001749 break;
1750 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001751 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001752 break;
1753 case Primitive::kPrimNot: {
1754 // Get the value, don't compute the type unless it is non-null as we don't want
1755 // to cause class loading.
1756 mirror::Object* value = field->GetObj(obj);
1757 if (value == nullptr) {
1758 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001759 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001760 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001761 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001762 if (field_type != nullptr) {
1763 PrettyObjectValue(os, field_type, value);
1764 } else {
1765 os << StringPrintf("%p %s\n", value,
1766 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1767 }
Ian Rogers50239c72013-06-17 14:53:22 -07001768 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001769 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001770 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001771 default:
1772 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1773 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001774 }
1775 }
1776
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001777 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Mathieu Chartier90443472015-07-16 20:32:27 -07001778 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001779 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001780 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001782 }
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001783 for (ArtField& field : klass->GetIFields()) {
1784 PrintField(os, &field, obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001785 }
1786 }
1787
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001788 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001789 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001790 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001791
Mathieu Chartiere401d142015-04-22 13:56:20 -07001792 const void* GetQuickOatCodeBegin(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001793 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001794 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1795 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001796 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001797 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001798 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001799 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001800 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001801 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001802 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001803 }
1804
Mathieu Chartiere401d142015-04-22 13:56:20 -07001805 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001806 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001807 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1808 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001809 return 0;
1810 }
1811 return oat_code_begin[-1];
1812 }
1813
Mathieu Chartiere401d142015-04-22 13:56:20 -07001814 const void* GetQuickOatCodeEnd(ArtMethod* m)
Mathieu Chartier90443472015-07-16 20:32:27 -07001815 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001816 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001817 if (oat_code_begin == nullptr) {
1818 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001819 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001820 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001821 }
1822
Mathieu Chartier90443472015-07-16 20:32:27 -07001823 static void Callback(mirror::Object* obj, void* arg) SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001824 DCHECK(obj != nullptr);
1825 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001826 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001827 if (!state->InDumpSpace(obj)) {
1828 return;
1829 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001830
1831 size_t object_bytes = obj->SizeOf();
1832 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1833 state->stats_.object_bytes += object_bytes;
1834 state->stats_.alignment_bytes += alignment_bytes;
1835
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001836 std::ostream& os = state->vios_.Stream();
1837
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001838 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001839 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001840 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1841 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001842 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001843 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001844 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1845 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001846 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001848 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001849 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001850 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001851 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001852 ScopedIndentation indent1(&state->vios_);
1853 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001854 const auto image_pointer_size =
1855 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001856 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001857 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1858 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001859 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001860 size_t run = 0;
1861 for (int32_t j = i + 1; j < length; j++) {
1862 if (value == obj_array->Get(j)) {
1863 run++;
1864 } else {
1865 break;
1866 }
1867 }
1868 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001869 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001870 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001871 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001872 i = i + run;
1873 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001874 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001875 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001876 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001877 }
1878 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001879 mirror::Class* klass = obj->AsClass();
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001880 if (klass->NumStaticFields() != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001881 os << "STATICS:\n";
1882 ScopedIndentation indent2(&state->vios_);
Mathieu Chartier54d220e2015-07-30 16:20:06 -07001883 for (ArtField& field : klass->GetSFields()) {
1884 PrintField(os, &field, field.GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001885 }
1886 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001887 } else {
Vladimir Marko05792b92015-08-03 11:56:49 +01001888 auto it = state->dex_caches_.find(obj);
1889 if (it != state->dex_caches_.end()) {
1890 auto* dex_cache = down_cast<mirror::DexCache*>(obj);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001891 const auto& field_section = state->image_header_.GetImageSection(
1892 ImageHeader::kSectionArtFields);
1893 const auto& method_section = state->image_header_.GetMethodsSection();
Vladimir Marko05792b92015-08-03 11:56:49 +01001894 size_t num_methods = dex_cache->NumResolvedMethods();
1895 if (num_methods != 0u) {
1896 os << "Methods (size=" << num_methods << "):";
1897 ScopedIndentation indent2(&state->vios_);
1898 auto* resolved_methods = dex_cache->GetResolvedMethods();
1899 for (size_t i = 0, length = dex_cache->NumResolvedMethods(); i < length; ++i) {
1900 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_methods, i, image_pointer_size);
1901 size_t run = 0;
1902 for (size_t j = i + 1;
1903 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_methods,
1904 j,
1905 image_pointer_size);
1906 ++j, ++run) {}
1907 if (run == 0) {
1908 os << StringPrintf("%zd: ", i);
1909 } else {
1910 os << StringPrintf("%zd to %zd: ", i, i + run);
1911 i = i + run;
1912 }
1913 std::string msg;
1914 if (elem == nullptr) {
1915 msg = "null";
1916 } else if (method_section.Contains(
1917 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1918 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1919 } else {
1920 msg = "<not in method section>";
1921 }
1922 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers0d2d3782012-04-10 11:09:18 -07001923 }
Vladimir Marko05792b92015-08-03 11:56:49 +01001924 }
1925 size_t num_fields = dex_cache->NumResolvedFields();
1926 if (num_fields != 0u) {
1927 os << "Fields (size=" << num_fields << "):";
1928 ScopedIndentation indent2(&state->vios_);
1929 auto* resolved_fields = dex_cache->GetResolvedFields();
1930 for (size_t i = 0, length = dex_cache->NumResolvedFields(); i < length; ++i) {
1931 auto* elem = mirror::DexCache::GetElementPtrSize(resolved_fields, i, image_pointer_size);
1932 size_t run = 0;
1933 for (size_t j = i + 1;
1934 j != length && elem == mirror::DexCache::GetElementPtrSize(resolved_fields,
1935 j,
1936 image_pointer_size);
1937 ++j, ++run) {}
1938 if (run == 0) {
1939 os << StringPrintf("%zd: ", i);
1940 } else {
1941 os << StringPrintf("%zd to %zd: ", i, i + run);
1942 i = i + run;
1943 }
1944 std::string msg;
1945 if (elem == nullptr) {
1946 msg = "null";
1947 } else if (field_section.Contains(
1948 reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin())) {
1949 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1950 } else {
1951 msg = "<not in field section>";
1952 }
1953 os << StringPrintf("%p %s\n", elem, msg.c_str());
Mathieu Chartiere401d142015-04-22 13:56:20 -07001954 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001955 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001956 }
1957 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001958 std::string temp;
1959 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001960 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001961
Mathieu Chartiere401d142015-04-22 13:56:20 -07001962 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07001963 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001964 DCHECK(method != nullptr);
1965 const auto image_pointer_size =
1966 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001967 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1968 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001969 OatQuickMethodHeader* method_header = reinterpret_cast<OatQuickMethodHeader*>(
1970 reinterpret_cast<uintptr_t>(quick_oat_code_begin) - sizeof(OatQuickMethodHeader));
Mathieu Chartiere401d142015-04-22 13:56:20 -07001971 if (method->IsNative()) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001972 if (!Runtime::Current()->GetClassLinker()->IsQuickGenericJniStub(quick_oat_code_begin)) {
1973 DCHECK(method_header->GetNativeGcMap() == nullptr) << PrettyMethod(method);
1974 DCHECK(method_header->GetMappingTable() == nullptr) << PrettyMethod(method);
1975 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001976 bool first_occurrence;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001977 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001978 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001979 if (first_occurrence) {
1980 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1981 }
Nicolas Geoffray6bc43742015-10-12 18:11:10 +01001982 if (quick_oat_code_begin !=
1983 method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1984 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code_begin);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001985 }
1986 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1987 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1988 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001989 } else {
1990 const DexFile::CodeItem* code_item = method->GetCodeItem();
1991 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1992 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1993
1994 bool first_occurrence;
1995 size_t gc_map_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01001996 method_header->GetNativeGcMap(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001997 if (first_occurrence) {
1998 state->stats_.gc_map_bytes += gc_map_bytes;
1999 }
2000
2001 size_t pc_mapping_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002002 method_header->GetMappingTable(), &first_occurrence);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002003 if (first_occurrence) {
2004 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
2005 }
2006
Roland Levillain6d7f1792015-07-02 10:59:15 +01002007 size_t vmap_table_bytes = 0u;
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002008 if (!method_header->IsOptimized()) {
Roland Levillain6d7f1792015-07-02 10:59:15 +01002009 // Method compiled with the optimizing compiler have no vmap table.
2010 vmap_table_bytes = state->ComputeOatSize(
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +01002011 method_header->GetVmapTable(), &first_occurrence);
Roland Levillain6d7f1792015-07-02 10:59:15 +01002012 if (first_occurrence) {
2013 state->stats_.vmap_table_bytes += vmap_table_bytes;
2014 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015 }
2016
Mathieu Chartiere401d142015-04-22 13:56:20 -07002017 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
2018 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
2019 if (first_occurrence) {
2020 state->stats_.managed_code_bytes += quick_oat_code_size;
2021 if (method->IsConstructor()) {
2022 if (method->IsStatic()) {
2023 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
2024 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
2025 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
2026 }
2027 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
2028 state->stats_.large_method_code_bytes += quick_oat_code_size;
2029 }
2030 }
2031 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
2032
Igor Murashkin7617abd2015-07-10 18:27:47 -07002033 uint32_t method_access_flags = method->GetAccessFlags();
2034
Mathieu Chartiere401d142015-04-22 13:56:20 -07002035 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
Igor Murashkin7617abd2015-07-10 18:27:47 -07002036 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd AccessFlags=0x%x\n",
2037 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes,
2038 method_access_flags);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002039
2040 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
Vladimir Marko14632852015-08-17 12:07:23 +01002041 vmap_table_bytes + quick_oat_code_size + ArtMethod::Size(image_pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002042
2043 double expansion =
2044 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
2045 state->stats_.ComputeOutliers(total_size, expansion, method);
2046 }
2047 }
2048
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002049 std::set<const void*> already_seen_;
2050 // Compute the size of the given data within the oat file and whether this is the first time
2051 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07002052 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002053 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002054 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002055 already_seen_.insert(oat_data);
2056 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002057 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002058 }
2059 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002060 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002061
2062 public:
2063 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002064 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002065 size_t file_bytes;
2066
2067 size_t header_bytes;
2068 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002069 size_t art_field_bytes;
2070 size_t art_method_bytes;
Vladimir Marko05792b92015-08-03 11:56:49 +01002071 size_t dex_cache_arrays_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002072 size_t interned_strings_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07002073 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002074 size_t alignment_bytes;
2075
2076 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002077 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002078 size_t managed_to_native_code_bytes;
2079 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07002080 size_t class_initializer_code_bytes;
2081 size_t large_initializer_code_bytes;
2082 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002083
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002084 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002085 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002086 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002087
2088 size_t dex_instruction_bytes;
2089
Mathieu Chartiere401d142015-04-22 13:56:20 -07002090 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002091 std::vector<size_t> method_outlier_size;
2092 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07002093 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002094
Roland Levillain3887c462015-08-12 18:15:42 +01002095 Stats()
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002096 : oat_file_bytes(0),
2097 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002098 header_bytes(0),
2099 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002100 art_field_bytes(0),
2101 art_method_bytes(0),
Vladimir Marko05792b92015-08-03 11:56:49 +01002102 dex_cache_arrays_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002103 interned_strings_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002104 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002105 alignment_bytes(0),
2106 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002107 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002108 managed_to_native_code_bytes(0),
2109 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002110 class_initializer_code_bytes(0),
2111 large_initializer_code_bytes(0),
2112 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002113 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002114 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002115 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002116 dex_instruction_bytes(0) {}
2117
Elliott Hughesa0e18062012-04-13 15:59:59 -07002118 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002119 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002120 size_t bytes;
2121 size_t count;
2122 };
2123 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2124 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002125
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002126 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002127 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2128 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002129 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002130 it->second.count += 1;
2131 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002132 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002133 }
2134 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002135
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002136 double PercentOfOatBytes(size_t size) {
2137 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2138 }
2139
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002140 double PercentOfFileBytes(size_t size) {
2141 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2142 }
2143
2144 double PercentOfObjectBytes(size_t size) {
2145 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2146 }
2147
Mathieu Chartiere401d142015-04-22 13:56:20 -07002148 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002149 method_outlier_size.push_back(total_size);
2150 method_outlier_expansion.push_back(expansion);
2151 method_outlier.push_back(method);
2152 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002153
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002154 void DumpOutliers(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002155 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002156 size_t sum_of_sizes = 0;
2157 size_t sum_of_sizes_squared = 0;
2158 size_t sum_of_expansion = 0;
2159 size_t sum_of_expansion_squared = 0;
2160 size_t n = method_outlier_size.size();
2161 for (size_t i = 0; i < n; i++) {
2162 size_t cur_size = method_outlier_size[i];
2163 sum_of_sizes += cur_size;
2164 sum_of_sizes_squared += cur_size * cur_size;
2165 double cur_expansion = method_outlier_expansion[i];
2166 sum_of_expansion += cur_expansion;
2167 sum_of_expansion_squared += cur_expansion * cur_expansion;
2168 }
2169 size_t size_mean = sum_of_sizes / n;
2170 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2171 double expansion_mean = sum_of_expansion / n;
2172 double expansion_variance =
2173 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2174
2175 // Dump methods whose size is a certain number of standard deviations from the mean
2176 size_t dumped_values = 0;
2177 size_t skipped_values = 0;
2178 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2179 size_t cur_size_variance = i * i * size_variance;
2180 bool first = true;
2181 for (size_t j = 0; j < n; j++) {
2182 size_t cur_size = method_outlier_size[j];
2183 if (cur_size > size_mean) {
2184 size_t cur_var = cur_size - size_mean;
2185 cur_var = cur_var * cur_var;
2186 if (cur_var > cur_size_variance) {
2187 if (dumped_values > 20) {
2188 if (i == 1) {
2189 skipped_values++;
2190 } else {
2191 i = 2; // jump to counting for 1 standard deviation
2192 break;
2193 }
2194 } else {
2195 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002196 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002197 first = false;
2198 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002199 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002200 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002201 method_outlier_size[j] = 0; // don't consider this method again
2202 dumped_values++;
2203 }
2204 }
2205 }
2206 }
2207 }
2208 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002210 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002211 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002212 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002213
2214 // Dump methods whose expansion is a certain number of standard deviations from the mean
2215 dumped_values = 0;
2216 skipped_values = 0;
2217 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2218 double cur_expansion_variance = i * i * expansion_variance;
2219 bool first = true;
2220 for (size_t j = 0; j < n; j++) {
2221 double cur_expansion = method_outlier_expansion[j];
2222 if (cur_expansion > expansion_mean) {
2223 size_t cur_var = cur_expansion - expansion_mean;
2224 cur_var = cur_var * cur_var;
2225 if (cur_var > cur_expansion_variance) {
2226 if (dumped_values > 20) {
2227 if (i == 1) {
2228 skipped_values++;
2229 } else {
2230 i = 2; // jump to counting for 1 standard deviation
2231 break;
2232 }
2233 } else {
2234 if (first) {
2235 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002236 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002237 first = false;
2238 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002239 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002240 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002241 method_outlier_expansion[j] = 0.0; // don't consider this method again
2242 dumped_values++;
2243 }
2244 }
2245 }
2246 }
2247 }
2248 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002249 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002250 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002251 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002252 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002253 }
2254
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002255 void Dump(std::ostream& os, std::ostream& indent_os)
Mathieu Chartier90443472015-07-16 20:32:27 -07002256 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002257 {
2258 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2259 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Vladimir Marko05792b92015-08-03 11:56:49 +01002260 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2261 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2262 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2263 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2264 "dex_cache_arrays_bytes = %8zd (%2.0f%% of art file bytes)\n"
2265 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
2266 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2267 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002268 header_bytes, PercentOfFileBytes(header_bytes),
2269 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002270 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2271 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Vladimir Marko05792b92015-08-03 11:56:49 +01002272 dex_cache_arrays_bytes,
2273 PercentOfFileBytes(dex_cache_arrays_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002274 interned_strings_bytes,
2275 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002276 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002277 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2278 << std::flush;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002279 CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
Vladimir Marko05792b92015-08-03 11:56:49 +01002280 dex_cache_arrays_bytes + interned_strings_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002281 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002282
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002283 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002284 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002285 for (const auto& sizes_and_count : sizes_and_counts) {
2286 const std::string& descriptor(sizes_and_count.first);
2287 double average = static_cast<double>(sizes_and_count.second.bytes) /
2288 static_cast<double>(sizes_and_count.second.count);
2289 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002290 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002291 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002292 descriptor.c_str(), sizes_and_count.second.bytes,
2293 sizes_and_count.second.count, average, percent);
2294 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002295 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002296 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002297 CHECK_EQ(object_bytes, object_bytes_total);
2298
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002299 os << StringPrintf("oat_file_bytes = %8zd\n"
2300 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2301 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2302 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2303 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2304 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2305 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002306 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002307 managed_code_bytes,
2308 PercentOfOatBytes(managed_code_bytes),
2309 managed_to_native_code_bytes,
2310 PercentOfOatBytes(managed_to_native_code_bytes),
2311 native_to_managed_code_bytes,
2312 PercentOfOatBytes(native_to_managed_code_bytes),
2313 class_initializer_code_bytes,
2314 PercentOfOatBytes(class_initializer_code_bytes),
2315 large_initializer_code_bytes,
2316 PercentOfOatBytes(large_initializer_code_bytes),
2317 large_method_code_bytes,
2318 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002319 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002320 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002322 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2323 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002324 }
2325
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2327 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2328 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002329 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2330 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2331 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002332 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002333
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002334 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2335 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002336 static_cast<double>(managed_code_bytes) /
2337 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002338 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002339 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002340 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002341
2342 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002343 }
2344 } stats_;
2345
2346 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002347 enum {
2348 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2349 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2350 kLargeConstructorDexBytes = 4000,
2351 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2352 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2353 kLargeMethodDexBytes = 16000
2354 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002355
2356 // For performance, use the *os_ directly for anything that doesn't need indentation
2357 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002358 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002359 VariableIndentationOutputStream vios_;
2360 ScopedIndentation indent1_;
2361
Ian Rogers1d54e732013-05-02 21:10:01 -07002362 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002363 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002364 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002365 OatDumperOptions* oat_dumper_options_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002366 std::set<mirror::Object*> dex_caches_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002367
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002368 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002369};
2370
Andreas Gampe00b25f32014-09-17 21:49:05 -07002371static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2372 std::ostream* os) {
2373 // Dumping the image, no explicit class loader.
2374 NullHandle<mirror::ClassLoader> null_class_loader;
2375 options->class_loader_ = &null_class_loader;
2376
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002377 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002378 gc::Heap* heap = runtime->GetHeap();
Mathieu Chartier073b16c2015-11-10 14:13:23 -08002379 gc::space::ImageSpace* image_space = heap->GetBootImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002380 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002381 const ImageHeader& image_header = image_space->GetImageHeader();
2382 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002383 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002384 return EXIT_FAILURE;
2385 }
Igor Murashkin37743352014-11-13 14:38:00 -08002386
Andreas Gampe00b25f32014-09-17 21:49:05 -07002387 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002388
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002389 bool success = image_dumper.Dump();
2390 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002391}
2392
Andreas Gampe00b25f32014-09-17 21:49:05 -07002393static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2394 std::ostream* os) {
2395 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2396
2397 Thread* self = Thread::Current();
2398 CHECK(self != nullptr);
2399 // Need well-known-classes.
2400 WellKnownClasses::Init(self->GetJniEnv());
2401
2402 // Need to register dex files to get a working dex cache.
2403 ScopedObjectAccess soa(self);
2404 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002405 runtime->GetOatFileManager().RegisterOatFile(std::unique_ptr<const OatFile>(oat_file));
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002406 std::vector<const DexFile*> class_path;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002407 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2408 std::string error_msg;
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002409 const DexFile* const dex_file = OpenDexFile(odf, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002410 CHECK(dex_file != nullptr) << error_msg;
Mathieu Chartierd57d4542015-10-14 10:55:30 -07002411 class_linker->RegisterDexFile(*dex_file, runtime->GetLinearAlloc());
Mathieu Chartierac8f4392015-08-27 13:54:20 -07002412 class_path.push_back(dex_file);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002413 }
2414
2415 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002416 // Fake that we're a compiler.
Mathieu Chartierd37d3642015-11-19 16:05:58 -08002417 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path, /*parent*/nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002418
2419 // Use the class loader while dumping.
2420 StackHandleScope<1> scope(self);
2421 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2422 soa.Decode<mirror::ClassLoader*>(class_loader));
2423 options->class_loader_ = &loader_handle;
2424
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002425 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002426 bool success = oat_dumper.Dump(*os);
2427 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2428}
2429
2430static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002431 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002432 // No image = no class loader.
2433 NullHandle<mirror::ClassLoader> null_class_loader;
2434 options->class_loader_ = &null_class_loader;
2435
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002436 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002437 bool success = oat_dumper.Dump(*os);
2438 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2439}
2440
2441static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2442 std::ostream* os) {
2443 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002444 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002445 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002446 if (oat_file == nullptr) {
2447 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2448 return EXIT_FAILURE;
2449 }
2450
2451 if (runtime != nullptr) {
2452 return DumpOatWithRuntime(runtime, oat_file, options, os);
2453 } else {
2454 return DumpOatWithoutRuntime(oat_file, options, os);
2455 }
2456}
2457
2458static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2459 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002460 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Igor Murashkinb1d8c312015-08-04 11:18:43 -07002461 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002462 if (oat_file == nullptr) {
2463 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2464 return EXIT_FAILURE;
2465 }
2466
2467 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002468 if (!oat_symbolizer.Symbolize()) {
2469 fprintf(stderr, "Failed to symbolize\n");
2470 return EXIT_FAILURE;
2471 }
2472
2473 return EXIT_SUCCESS;
2474}
2475
Igor Murashkin37743352014-11-13 14:38:00 -08002476struct OatdumpArgs : public CmdlineArgs {
2477 protected:
2478 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002479
Igor Murashkin37743352014-11-13 14:38:00 -08002480 virtual ParseStatus ParseCustom(const StringPiece& option,
2481 std::string* error_msg) OVERRIDE {
2482 {
2483 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2484 if (base_parse != kParseUnknownArgument) {
2485 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002486 }
2487 }
2488
Igor Murashkin37743352014-11-13 14:38:00 -08002489 if (option.starts_with("--oat-file=")) {
2490 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2491 } else if (option.starts_with("--image=")) {
2492 image_location_ = option.substr(strlen("--image=")).data();
2493 } else if (option =="--dump:raw_mapping_table") {
2494 dump_raw_mapping_table_ = true;
2495 } else if (option == "--dump:raw_gc_map") {
2496 dump_raw_gc_map_ = true;
2497 } else if (option == "--no-dump:vmap") {
2498 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002499 } else if (option =="--dump:code_info_stack_maps") {
2500 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002501 } else if (option == "--no-disassemble") {
2502 disassemble_code_ = false;
2503 } else if (option.starts_with("--symbolize=")) {
2504 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2505 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002506 } else if (option.starts_with("--class-filter=")) {
2507 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002508 } else if (option.starts_with("--method-filter=")) {
2509 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002510 } else if (option.starts_with("--list-classes")) {
2511 list_classes_ = true;
2512 } else if (option.starts_with("--list-methods")) {
2513 list_methods_ = true;
2514 } else if (option.starts_with("--export-dex-to=")) {
2515 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2516 } else if (option.starts_with("--addr2instr=")) {
2517 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2518 *error_msg = "Address conversion failed";
2519 return kParseError;
2520 }
Igor Murashkin37743352014-11-13 14:38:00 -08002521 } else {
2522 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002523 }
2524
Igor Murashkin37743352014-11-13 14:38:00 -08002525 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002526 }
2527
Igor Murashkin37743352014-11-13 14:38:00 -08002528 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2529 // Infer boot image location from the image location if possible.
2530 if (boot_image_location_ == nullptr) {
2531 boot_image_location_ = image_location_;
2532 }
2533
2534 // Perform the parent checks.
2535 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2536 if (parent_checks != kParseOk) {
2537 return parent_checks;
2538 }
2539
2540 // Perform our own checks.
2541 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2542 *error_msg = "Either --image or --oat-file must be specified";
2543 return kParseError;
2544 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2545 *error_msg = "Either --image or --oat-file must be specified but not both";
2546 return kParseError;
2547 }
2548
2549 return kParseOk;
2550 }
2551
2552 virtual std::string GetUsage() const {
2553 std::string usage;
2554
2555 usage +=
2556 "Usage: oatdump [options] ...\n"
2557 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2558 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2559 "\n"
2560 // Either oat-file or image is required.
2561 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2562 " Example: --oat-file=/system/framework/boot.oat\n"
2563 "\n"
2564 " --image=<file.art>: specifies an input image location.\n"
2565 " Example: --image=/system/framework/boot.art\n"
2566 "\n";
2567
2568 usage += Base::GetUsage();
2569
2570 usage += // Optional.
2571 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2572 " Example: --dump:raw_mapping_table\n"
2573 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002574 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002575 " Example: --dump:raw_gc_map\n"
2576 "\n"
2577 " --no-dump:vmap may be used to disable vmap dumping.\n"
2578 " Example: --no-dump:vmap\n"
2579 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002580 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2581 " Example: --dump:code_info_stack_maps\n"
2582 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002583 " --no-disassemble may be used to disable disassembly.\n"
2584 " Example: --no-disassemble\n"
2585 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002586 " --list-classes may be used to list target file classes (can be used with filters).\n"
2587 " Example: --list-classes\n"
2588 " Example: --list-classes --class-filter=com.example.foo\n"
2589 "\n"
2590 " --list-methods may be used to list target file methods (can be used with filters).\n"
2591 " Example: --list-methods\n"
2592 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2593 "\n"
2594 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2595 " Example: --symbolize=/system/framework/boot.oat\n"
2596 "\n"
2597 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2598 " Example: --class-filter=com.example.foo\n"
2599 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002600 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2601 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002602 "\n"
2603 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2604 " Example: --export-dex-to=/data/local/tmp\n"
2605 "\n"
2606 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2607 " address (e.g. PC from crash dump)\n"
2608 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002609 "\n";
2610
2611 return usage;
2612 }
2613
2614 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002615 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002616 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002617 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002618 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002619 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002620 bool dump_raw_mapping_table_ = false;
2621 bool dump_raw_gc_map_ = false;
2622 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002623 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002624 bool disassemble_code_ = true;
2625 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002626 bool list_classes_ = false;
2627 bool list_methods_ = false;
2628 uint32_t addr2instr_ = 0;
2629 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002630};
2631
Igor Murashkin37743352014-11-13 14:38:00 -08002632struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2633 virtual bool NeedsRuntime() OVERRIDE {
2634 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002635
Igor Murashkin37743352014-11-13 14:38:00 -08002636 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2637 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2638
2639 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2640 args_->dump_raw_mapping_table_,
2641 args_->dump_raw_gc_map_,
2642 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002643 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002644 args_->disassemble_code_,
2645 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002646 args_->class_filter_,
2647 args_->method_filter_,
2648 args_->list_classes_,
2649 args_->list_methods_,
2650 args_->export_dex_location_,
2651 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002652
2653 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2654 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002655 }
2656
Igor Murashkin37743352014-11-13 14:38:00 -08002657 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2658 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002659 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002660
Mathieu Chartierd424d082014-10-15 10:31:46 -07002661 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002662
Andreas Gampec24f3992014-12-17 20:40:11 -08002663 if (args_->symbolize_) {
2664 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2665 } else {
2666 return DumpOat(nullptr,
2667 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002668 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002669 args_->os_) == EXIT_SUCCESS;
2670 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002671 }
2672
Igor Murashkin37743352014-11-13 14:38:00 -08002673 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2674 CHECK(args_ != nullptr);
2675
2676 if (args_->oat_filename_ != nullptr) {
2677 return DumpOat(runtime,
2678 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002679 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002680 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002681 }
Igor Murashkin37743352014-11-13 14:38:00 -08002682
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002683 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002684 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002685 }
2686
Igor Murashkin37743352014-11-13 14:38:00 -08002687 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2688};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002689
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002690} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002691
2692int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002693 art::OatdumpMain main;
2694 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002695}