blob: 82452bae351e0d5bdf8a0f932d06f85d3b91fc5d [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian Carlstrom78128a62011-09-15 17:21:19 -070016
17#include <stdio.h>
18#include <stdlib.h>
19
Brian Carlstrom27ec9612011-09-19 20:20:38 -070020#include <fstream>
21#include <iostream>
Igor Murashkin37743352014-11-13 14:38:00 -080022#include <map>
23#include <set>
Brian Carlstrom78128a62011-09-15 17:21:19 -070024#include <string>
Andreas Gampe54fc26c2014-09-04 21:47:42 -070025#include <unordered_map>
Brian Carlstrom78128a62011-09-15 17:21:19 -070026#include <vector>
27
Ian Rogersd582fa42014-11-05 23:46:43 -080028#include "arch/instruction_set_features.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070029#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Elliott Hughes76160052012-12-12 16:31:20 -080031#include "base/unix_file/fd_file.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070032#include "class_linker.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "dex_file-inl.h"
Elliott Hughese3c845c2012-02-28 17:23:01 -080035#include "dex_instruction.h"
Ian Rogers3a5c1ce2012-02-29 10:06:46 -080036#include "disassembler.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070037#include "elf_builder.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080038#include "gc_map.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070039#include "gc/space/image_space.h"
40#include "gc/space/large_object_space.h"
41#include "gc/space/space-inl.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070042#include "image.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080043#include "indenter.h"
Ian Rogers1809a722013-08-09 22:05:32 -070044#include "mapping_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "mirror/array-inl.h"
46#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
Brian Carlstrom700c8d32012-11-05 10:42:02 -080049#include "oat.h"
Vladimir Marko8a630572014-04-09 18:45:35 +010050#include "oat_file-inl.h"
Elliott Hughese5448b52012-01-18 16:44:06 -080051#include "os.h"
Andreas Gampe54fc26c2014-09-04 21:47:42 -070052#include "output_stream.h"
Elliott Hughesa0e18062012-04-13 15:59:59 -070053#include "safe_map.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054#include "scoped_thread_state_change.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070055#include "ScopedLocalRef.h"
Mathieu Chartierc22c59e2014-02-24 15:16:06 -080056#include "thread_list.h"
Ian Rogersef7d42f2014-01-06 12:55:46 -080057#include "verifier/dex_gc_map.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080058#include "verifier/method_verifier.h"
Ian Rogers1809a722013-08-09 22:05:32 -070059#include "vmap_table.h"
Andreas Gampe00b25f32014-09-17 21:49:05 -070060#include "well_known_classes.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070061
Igor Murashkin37743352014-11-13 14:38:00 -080062#include <sys/stat.h>
63#include "cmdline.h"
Brian Carlstrom78128a62011-09-15 17:21:19 -070064
Igor Murashkin37743352014-11-13 14:38:00 -080065namespace art {
Brian Carlstrom78128a62011-09-15 17:21:19 -070066
Mathieu Chartiere401d142015-04-22 13:56:20 -070067const char* image_methods_descriptions_[] = {
Ian Rogers19846512012-02-24 11:42:47 -080068 "kResolutionMethod",
Jeff Hao88474b42013-10-23 16:24:40 -070069 "kImtConflictMethod",
Mathieu Chartier2d2621a2014-10-23 16:48:06 -070070 "kImtUnimplementedMethod",
Brian Carlstrome24fa612011-09-29 00:53:55 -070071 "kCalleeSaveMethod",
Brian Carlstromaded5f72011-10-07 17:15:04 -070072 "kRefsOnlySaveMethod",
73 "kRefsAndArgsSaveMethod",
Mathieu Chartiere401d142015-04-22 13:56:20 -070074};
75
76const char* image_roots_descriptions_[] = {
Brian Carlstrom58ae9412011-10-04 00:56:06 -070077 "kDexCaches",
Brian Carlstrom34f426c2011-10-04 12:58:02 -070078 "kClassRoots",
Brian Carlstrom78128a62011-09-15 17:21:19 -070079};
80
David Srbeckybc90fd02015-04-22 19:40:27 +010081class OatSymbolizer FINAL {
Andreas Gampe54fc26c2014-09-04 21:47:42 -070082 public:
David Srbeckybc90fd02015-04-22 19:40:27 +010083 class RodataWriter FINAL : public CodeOutput {
84 public:
85 explicit RodataWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
Andreas Gampe54fc26c2014-09-04 21:47:42 -070086
David Srbeckybc90fd02015-04-22 19:40:27 +010087 bool Write(OutputStream* out) OVERRIDE {
88 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
89 return out->WriteFully(oat_file_->Begin(), rodata_size);
Andreas Gampe54fc26c2014-09-04 21:47:42 -070090 }
91
David Srbeckybc90fd02015-04-22 19:40:27 +010092 private:
93 const OatFile* oat_file_;
94 };
95
96 class TextWriter FINAL : public CodeOutput {
97 public:
98 explicit TextWriter(const OatFile* oat_file) : oat_file_(oat_file) {}
99
100 bool Write(OutputStream* out) OVERRIDE {
101 const size_t rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
102 const uint8_t* text_begin = oat_file_->Begin() + rodata_size;
103 return out->WriteFully(text_begin, oat_file_->End() - text_begin);
104 }
105
106 private:
107 const OatFile* oat_file_;
108 };
109
110 explicit OatSymbolizer(const OatFile* oat_file, const std::string& output_name) :
111 oat_file_(oat_file), builder_(nullptr),
112 output_name_(output_name.empty() ? "symbolized.oat" : output_name) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700113 }
114
115 typedef void (OatSymbolizer::*Callback)(const DexFile::ClassDef&,
116 uint32_t,
117 const OatFile::OatMethod&,
118 const DexFile&,
119 uint32_t,
120 const DexFile::CodeItem*,
121 uint32_t);
122
123 bool Symbolize() {
David Srbeckybc90fd02015-04-22 19:40:27 +0100124 Elf32_Word rodata_size = oat_file_->GetOatHeader().GetExecutableOffset();
125 uint32_t size = static_cast<uint32_t>(oat_file_->End() - oat_file_->Begin());
126 uint32_t text_size = size - rodata_size;
127 uint32_t bss_size = oat_file_->BssSize();
128 RodataWriter rodata_writer(oat_file_);
129 TextWriter text_writer(oat_file_);
130 builder_.reset(new ElfBuilder<ElfTypes32>(
131 oat_file_->GetOatHeader().GetInstructionSet(),
132 rodata_size, &rodata_writer,
133 text_size, &text_writer,
134 bss_size));
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700135
136 Walk(&art::OatSymbolizer::RegisterForDedup);
137
138 NormalizeState();
139
140 Walk(&art::OatSymbolizer::AddSymbol);
141
David Srbeckybc90fd02015-04-22 19:40:27 +0100142 File* elf_output = OS::CreateEmptyFile(output_name_.c_str());
143 bool result = builder_->Write(elf_output);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700144
Andreas Gampe4303ba92014-11-06 01:00:46 -0800145 // Ignore I/O errors.
David Srbeckybc90fd02015-04-22 19:40:27 +0100146 UNUSED(elf_output->FlushClose());
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700147
148 return result;
149 }
150
151 void Walk(Callback callback) {
152 std::vector<const OatFile::OatDexFile*> oat_dex_files = oat_file_->GetOatDexFiles();
153 for (size_t i = 0; i < oat_dex_files.size(); i++) {
154 const OatFile::OatDexFile* oat_dex_file = oat_dex_files[i];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 CHECK(oat_dex_file != nullptr);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700156 WalkOatDexFile(oat_dex_file, callback);
157 }
158 }
159
160 void WalkOatDexFile(const OatFile::OatDexFile* oat_dex_file, Callback callback) {
161 std::string error_msg;
162 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
163 if (dex_file.get() == nullptr) {
164 return;
165 }
166 for (size_t class_def_index = 0;
167 class_def_index < dex_file->NumClassDefs();
168 class_def_index++) {
169 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
170 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
171 OatClassType type = oat_class.GetType();
172 switch (type) {
173 case kOatClassAllCompiled:
174 case kOatClassSomeCompiled:
175 WalkOatClass(oat_class, *dex_file.get(), class_def, callback);
176 break;
177
178 case kOatClassNoneCompiled:
179 case kOatClassMax:
180 // Ignore.
181 break;
182 }
183 }
184 }
185
186 void WalkOatClass(const OatFile::OatClass& oat_class, const DexFile& dex_file,
187 const DexFile::ClassDef& class_def, Callback callback) {
Ian Rogers13735952014-10-08 12:43:28 -0700188 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700189 if (class_data == nullptr) { // empty class such as a marker interface?
190 return;
191 }
192 // Note: even if this is an interface or a native class, we still have to walk it, as there
193 // might be a static initializer.
194 ClassDataItemIterator it(dex_file, class_data);
195 SkipAllFields(&it);
196 uint32_t class_method_idx = 0;
197 while (it.HasNextDirectMethod()) {
198 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
199 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700200 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700201 class_method_idx++;
202 it.Next();
203 }
204 while (it.HasNextVirtualMethod()) {
205 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_idx);
206 WalkOatMethod(class_def, class_method_idx, oat_method, dex_file, it.GetMemberIndex(),
Andreas Gampe51829322014-08-25 15:05:04 -0700207 it.GetMethodCodeItem(), it.GetMethodAccessFlags(), callback);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700208 class_method_idx++;
209 it.Next();
210 }
211 DCHECK(!it.HasNext());
212 }
213
214 void WalkOatMethod(const DexFile::ClassDef& class_def, uint32_t class_method_index,
215 const OatFile::OatMethod& oat_method, const DexFile& dex_file,
216 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
217 uint32_t method_access_flags, Callback callback) {
218 if ((method_access_flags & kAccAbstract) != 0) {
219 // Abstract method, no code.
220 return;
221 }
222 if (oat_method.GetCodeOffset() == 0) {
223 // No code.
224 return;
225 }
226
227 (this->*callback)(class_def, class_method_index, oat_method, dex_file, dex_method_idx, code_item,
228 method_access_flags);
229 }
230
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700231 void RegisterForDedup(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
232 uint32_t class_method_index ATTRIBUTE_UNUSED,
233 const OatFile::OatMethod& oat_method,
234 const DexFile& dex_file ATTRIBUTE_UNUSED,
235 uint32_t dex_method_idx ATTRIBUTE_UNUSED,
236 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
237 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700238 state_[oat_method.GetCodeOffset()]++;
239 }
240
241 void NormalizeState() {
242 for (auto& x : state_) {
243 if (x.second == 1) {
244 state_[x.first] = 0;
245 }
246 }
247 }
248
249 enum class DedupState { // private
250 kNotDeduplicated,
251 kDeduplicatedFirst,
252 kDeduplicatedOther
253 };
254 DedupState IsDuplicated(uint32_t offset) {
255 if (state_[offset] == 0) {
256 return DedupState::kNotDeduplicated;
257 }
258 if (state_[offset] == 1) {
259 return DedupState::kDeduplicatedOther;
260 }
261 state_[offset] = 1;
262 return DedupState::kDeduplicatedFirst;
263 }
264
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700265 void AddSymbol(const DexFile::ClassDef& class_def ATTRIBUTE_UNUSED,
266 uint32_t class_method_index ATTRIBUTE_UNUSED,
267 const OatFile::OatMethod& oat_method,
268 const DexFile& dex_file,
269 uint32_t dex_method_idx,
270 const DexFile::CodeItem* code_item ATTRIBUTE_UNUSED,
271 uint32_t method_access_flags ATTRIBUTE_UNUSED) {
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700272 DedupState dedup = IsDuplicated(oat_method.GetCodeOffset());
273 if (dedup != DedupState::kDeduplicatedOther) {
274 std::string pretty_name = PrettyMethod(dex_method_idx, dex_file, true);
275
276 if (dedup == DedupState::kDeduplicatedFirst) {
277 pretty_name = "[Dedup]" + pretty_name;
278 }
279
David Srbeckybc90fd02015-04-22 19:40:27 +0100280 auto* symtab = builder_->GetSymtab();
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700281
David Srbeckybc90fd02015-04-22 19:40:27 +0100282 symtab->AddSymbol(pretty_name, builder_->GetText(),
Ian Rogers0279ebb2014-10-08 17:27:48 -0700283 oat_method.GetCodeOffset() - oat_file_->GetOatHeader().GetExecutableOffset(),
284 true, oat_method.GetQuickCodeSize(), STB_GLOBAL, STT_FUNC);
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700285 }
286 }
287
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700288 private:
289 static void SkipAllFields(ClassDataItemIterator* it) {
290 while (it->HasNextStaticField()) {
291 it->Next();
292 }
293 while (it->HasNextInstanceField()) {
294 it->Next();
295 }
296 }
297
298 const OatFile* oat_file_;
David Srbecky533c2072015-04-22 12:20:22 +0100299 std::unique_ptr<ElfBuilder<ElfTypes32> > builder_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700300 std::unordered_map<uint32_t, uint32_t> state_;
Ian Rogers0279ebb2014-10-08 17:27:48 -0700301 const std::string output_name_;
Andreas Gampe54fc26c2014-09-04 21:47:42 -0700302};
303
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700304class OatDumperOptions {
305 public:
306 OatDumperOptions(bool dump_raw_mapping_table,
307 bool dump_raw_gc_map,
308 bool dump_vmap,
Roland Levillainf2650d12015-05-28 14:53:28 +0100309 bool dump_code_info_stack_maps,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700310 bool disassemble_code,
Andreas Gampe00b25f32014-09-17 21:49:05 -0700311 bool absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800312 const char* class_filter,
313 const char* method_filter,
314 bool list_classes,
315 bool list_methods,
316 const char* export_dex_location,
317 uint32_t addr2instr)
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700318 : dump_raw_mapping_table_(dump_raw_mapping_table),
319 dump_raw_gc_map_(dump_raw_gc_map),
320 dump_vmap_(dump_vmap),
Roland Levillainf2650d12015-05-28 14:53:28 +0100321 dump_code_info_stack_maps_(dump_code_info_stack_maps),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700322 disassemble_code_(disassemble_code),
Andreas Gampe00b25f32014-09-17 21:49:05 -0700323 absolute_addresses_(absolute_addresses),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800324 class_filter_(class_filter),
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000325 method_filter_(method_filter),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800326 list_classes_(list_classes),
327 list_methods_(list_methods),
328 export_dex_location_(export_dex_location),
329 addr2instr_(addr2instr),
Igor Murashkin37743352014-11-13 14:38:00 -0800330 class_loader_(nullptr) {}
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700331
332 const bool dump_raw_mapping_table_;
333 const bool dump_raw_gc_map_;
334 const bool dump_vmap_;
Roland Levillainf2650d12015-05-28 14:53:28 +0100335 const bool dump_code_info_stack_maps_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700336 const bool disassemble_code_;
337 const bool absolute_addresses_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800338 const char* const class_filter_;
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000339 const char* const method_filter_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800340 const bool list_classes_;
341 const bool list_methods_;
342 const char* const export_dex_location_;
343 uint32_t addr2instr_;
Andreas Gampe00b25f32014-09-17 21:49:05 -0700344 Handle<mirror::ClassLoader>* class_loader_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700345};
346
Elliott Hughese3c845c2012-02-28 17:23:01 -0800347class OatDumper {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700348 public:
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800349 explicit OatDumper(const OatFile& oat_file, const OatDumperOptions& options)
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000350 : oat_file_(oat_file),
Elliott Hughesa72ec822012-03-05 17:12:22 -0800351 oat_dex_files_(oat_file.GetOatDexFiles()),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700352 options_(options),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800353 resolved_addr2instr_(0),
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800354 instruction_set_(oat_file_.GetOatHeader().GetInstructionSet()),
355 disassembler_(Disassembler::Create(instruction_set_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800356 new DisassemblerOptions(options_.absolute_addresses_,
Alexandre Ramesa37d9252014-10-27 11:28:14 +0000357 oat_file.Begin(),
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100358 true /* can_read_literals_ */))) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800359 CHECK(options_.class_loader_ != nullptr);
360 CHECK(options_.class_filter_ != nullptr);
361 CHECK(options_.method_filter_ != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800362 AddAllOffsets();
363 }
364
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700365 ~OatDumper() {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700366 delete disassembler_;
367 }
368
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800369 InstructionSet GetInstructionSet() {
370 return instruction_set_;
371 }
372
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700373 bool Dump(std::ostream& os) {
374 bool success = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800375 const OatHeader& oat_header = oat_file_.GetOatHeader();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700376
377 os << "MAGIC:\n";
378 os << oat_header.GetMagic() << "\n\n";
379
380 os << "CHECKSUM:\n";
Elliott Hughesed2adb62012-02-29 14:41:01 -0800381 os << StringPrintf("0x%08x\n\n", oat_header.GetChecksum());
Brian Carlstromaded5f72011-10-07 17:15:04 -0700382
Elliott Hughesa72ec822012-03-05 17:12:22 -0800383 os << "INSTRUCTION SET:\n";
384 os << oat_header.GetInstructionSet() << "\n\n";
385
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700386 {
387 std::unique_ptr<const InstructionSetFeatures> features(
388 InstructionSetFeatures::FromBitmap(oat_header.GetInstructionSet(),
389 oat_header.GetInstructionSetFeaturesBitmap()));
390 os << "INSTRUCTION SET FEATURES:\n";
391 os << features->GetFeatureString() << "\n\n";
392 }
Dave Allison70202782013-10-22 17:52:19 -0700393
Brian Carlstromaded5f72011-10-07 17:15:04 -0700394 os << "DEX FILE COUNT:\n";
395 os << oat_header.GetDexFileCount() << "\n\n";
396
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800397#define DUMP_OAT_HEADER_OFFSET(label, offset) \
398 os << label " OFFSET:\n"; \
399 os << StringPrintf("0x%08x", oat_header.offset()); \
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800400 if (oat_header.offset() != 0 && options_.absolute_addresses_) { \
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800401 os << StringPrintf(" (%p)", oat_file_.Begin() + oat_header.offset()); \
402 } \
403 os << StringPrintf("\n\n");
404
405 DUMP_OAT_HEADER_OFFSET("EXECUTABLE", GetExecutableOffset);
406 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO INTERPRETER BRIDGE",
407 GetInterpreterToInterpreterBridgeOffset);
408 DUMP_OAT_HEADER_OFFSET("INTERPRETER TO COMPILED CODE BRIDGE",
409 GetInterpreterToCompiledCodeBridgeOffset);
410 DUMP_OAT_HEADER_OFFSET("JNI DLSYM LOOKUP",
411 GetJniDlsymLookupOffset);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800412 DUMP_OAT_HEADER_OFFSET("QUICK GENERIC JNI TRAMPOLINE",
413 GetQuickGenericJniTrampolineOffset);
414 DUMP_OAT_HEADER_OFFSET("QUICK IMT CONFLICT TRAMPOLINE",
415 GetQuickImtConflictTrampolineOffset);
416 DUMP_OAT_HEADER_OFFSET("QUICK RESOLUTION TRAMPOLINE",
417 GetQuickResolutionTrampolineOffset);
418 DUMP_OAT_HEADER_OFFSET("QUICK TO INTERPRETER BRIDGE",
419 GetQuickToInterpreterBridgeOffset);
420#undef DUMP_OAT_HEADER_OFFSET
Brian Carlstromaded5f72011-10-07 17:15:04 -0700421
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700422 os << "IMAGE PATCH DELTA:\n";
423 os << StringPrintf("%d (0x%08x)\n\n",
424 oat_header.GetImagePatchDelta(),
425 oat_header.GetImagePatchDelta());
Alex Lighta59dd802014-07-02 16:28:08 -0700426
Brian Carlstrom28db0122012-10-18 16:20:41 -0700427 os << "IMAGE FILE LOCATION OAT CHECKSUM:\n";
428 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatChecksum());
429
430 os << "IMAGE FILE LOCATION OAT BEGIN:\n";
Brian Carlstrom700c8d32012-11-05 10:42:02 -0800431 os << StringPrintf("0x%08x\n\n", oat_header.GetImageFileLocationOatDataBegin());
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700432
Andreas Gampe22f8e5c2014-07-09 11:38:21 -0700433 // Print the key-value store.
434 {
435 os << "KEY VALUE STORE:\n";
436 size_t index = 0;
437 const char* key;
438 const char* value;
439 while (oat_header.GetStoreKeyValuePairByIndex(index, &key, &value)) {
440 os << key << " = " << value << "\n";
441 index++;
442 }
443 os << "\n";
444 }
Brian Carlstrom81f3ca12012-03-17 00:27:35 -0700445
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800446 if (options_.absolute_addresses_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700447 os << "BEGIN:\n";
448 os << reinterpret_cast<const void*>(oat_file_.Begin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700449
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700450 os << "END:\n";
451 os << reinterpret_cast<const void*>(oat_file_.End()) << "\n\n";
452 }
453
454 os << "SIZE:\n";
455 os << oat_file_.Size() << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -0700456
457 os << std::flush;
458
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800459 // If set, adjust relative address to be searched
460 if (options_.addr2instr_ != 0) {
461 resolved_addr2instr_ = options_.addr2instr_ + oat_header.GetExecutableOffset();
462 os << "SEARCH ADDRESS (executable offset + input):\n";
463 os << StringPrintf("0x%08x\n\n", resolved_addr2instr_);
464 }
465
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800466 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
467 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700468 CHECK(oat_dex_file != nullptr);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800469
470 // If file export selected skip file analysis
471 if (options_.export_dex_location_) {
472 if (!ExportDexFile(os, *oat_dex_file)) {
473 success = false;
474 }
475 } else {
476 if (!DumpOatDexFile(os, *oat_dex_file)) {
477 success = false;
478 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700479 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700480 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700481 os << std::flush;
482 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700483 }
484
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800485 size_t ComputeSize(const void* oat_data) {
Ian Rogers13735952014-10-08 12:43:28 -0700486 if (reinterpret_cast<const uint8_t*>(oat_data) < oat_file_.Begin() ||
487 reinterpret_cast<const uint8_t*>(oat_data) > oat_file_.End()) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800488 return 0; // Address not in oat file
489 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800490 uintptr_t begin_offset = reinterpret_cast<uintptr_t>(oat_data) -
491 reinterpret_cast<uintptr_t>(oat_file_.Begin());
492 auto it = offsets_.upper_bound(begin_offset);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800493 CHECK(it != offsets_.end());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800494 uintptr_t end_offset = *it;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800495 return end_offset - begin_offset;
496 }
497
Mathieu Chartiera7dd0382014-11-20 17:08:58 -0800498 InstructionSet GetOatInstructionSet() {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700499 return oat_file_.GetOatHeader().GetInstructionSet();
500 }
501
Mathieu Chartiere401d142015-04-22 13:56:20 -0700502 const void* GetQuickOatCode(ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800503 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
504 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700505 CHECK(oat_dex_file != nullptr);
506 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700507 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700508 if (dex_file.get() == nullptr) {
509 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
510 << "': " << error_msg;
511 } else {
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800512 const char* descriptor = m->GetDeclaringClassDescriptor();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700513 const DexFile::ClassDef* class_def =
Mathieu Chartiere7c9a8c2014-11-06 16:35:45 -0800514 dex_file->FindClassDef(descriptor, ComputeModifiedUtf8Hash(descriptor));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700515 if (class_def != nullptr) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700516 uint16_t class_def_index = dex_file->GetIndexForClassDef(*class_def);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100517 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800518 size_t method_index = m->GetMethodIndex();
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100519 return oat_class.GetOatMethod(method_index).GetQuickCode();
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800520 }
521 }
522 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700523 return nullptr;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800524 }
525
Brian Carlstromaded5f72011-10-07 17:15:04 -0700526 private:
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800527 void AddAllOffsets() {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800528 // We don't know the length of the code for each method, but we need to know where to stop
529 // when disassembling. What we do know is that a region of code will be followed by some other
530 // region, so if we keep a sorted sequence of the start of each region, we can infer the length
531 // of a piece of code by using upper_bound to find the start of the next region.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800532 for (size_t i = 0; i < oat_dex_files_.size(); i++) {
533 const OatFile::OatDexFile* oat_dex_file = oat_dex_files_[i];
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700534 CHECK(oat_dex_file != nullptr);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700535 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700536 std::unique_ptr<const DexFile> dex_file(oat_dex_file->OpenDexFile(&error_msg));
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700537 if (dex_file.get() == nullptr) {
538 LOG(WARNING) << "Failed to open dex file '" << oat_dex_file->GetDexFileLocation()
539 << "': " << error_msg;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -0800540 continue;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800541 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800542 offsets_.insert(reinterpret_cast<uintptr_t>(&dex_file->GetHeader()));
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800543 for (size_t class_def_index = 0;
544 class_def_index < dex_file->NumClassDefs();
545 class_def_index++) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800546 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100547 const OatFile::OatClass oat_class = oat_dex_file->GetOatClass(class_def_index);
Ian Rogers13735952014-10-08 12:43:28 -0700548 const uint8_t* class_data = dex_file->GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700549 if (class_data != nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -0800550 ClassDataItemIterator it(*dex_file, class_data);
551 SkipAllFields(it);
552 uint32_t class_method_index = 0;
553 while (it.HasNextDirectMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100554 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800555 it.Next();
556 }
557 while (it.HasNextVirtualMethod()) {
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100558 AddOffsets(oat_class.GetOatMethod(class_method_index++));
Elliott Hughese3c845c2012-02-28 17:23:01 -0800559 it.Next();
560 }
561 }
562 }
563 }
564
565 // If the last thing in the file is code for a method, there won't be an offset for the "next"
566 // thing. Instead of having a special case in the upper_bound code, let's just add an entry
567 // for the end of the file.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800568 offsets_.insert(oat_file_.Size());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800569 }
570
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700571 static uint32_t AlignCodeOffset(uint32_t maybe_thumb_offset) {
572 return maybe_thumb_offset & ~0x1; // TODO: Make this Thumb2 specific.
573 }
574
Elliott Hughese3c845c2012-02-28 17:23:01 -0800575 void AddOffsets(const OatFile::OatMethod& oat_method) {
Brian Carlstrom95ba0dc2012-03-05 22:06:14 -0800576 uint32_t code_offset = oat_method.GetCodeOffset();
577 if (oat_file_.GetOatHeader().GetInstructionSet() == kThumb2) {
578 code_offset &= ~0x1;
579 }
580 offsets_.insert(code_offset);
Elliott Hughese3c845c2012-02-28 17:23:01 -0800581 offsets_.insert(oat_method.GetMappingTableOffset());
582 offsets_.insert(oat_method.GetVmapTableOffset());
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800583 offsets_.insert(oat_method.GetGcMapOffset());
Elliott Hughese3c845c2012-02-28 17:23:01 -0800584 }
585
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700586 bool DumpOatDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
587 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800588 bool stop_analysis = false;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700589 os << "OatDexFile:\n";
Brian Carlstroma004aa92012-02-08 18:05:09 -0800590 os << StringPrintf("location: %s\n", oat_dex_file.GetDexFileLocation().c_str());
Elliott Hughesed2adb62012-02-29 14:41:01 -0800591 os << StringPrintf("checksum: 0x%08x\n", oat_dex_file.GetDexFileLocationChecksum());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700592
593 // Create the verifier early.
594
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700595 std::string error_msg;
Ian Rogers700a4022014-05-19 16:49:03 -0700596 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700597 if (dex_file.get() == nullptr) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700598 os << "NOT FOUND: " << error_msg << "\n\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700599 os << std::flush;
600 return false;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700601 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100602
603 VariableIndentationOutputStream vios(&os);
604 ScopedIndentation indent1(&vios);
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800605 for (size_t class_def_index = 0;
606 class_def_index < dex_file->NumClassDefs();
607 class_def_index++) {
Brian Carlstromaded5f72011-10-07 17:15:04 -0700608 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_index);
609 const char* descriptor = dex_file->GetClassDescriptor(class_def);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800610
611 // TODO: Support regex
612 if (DescriptorToDot(descriptor).find(options_.class_filter_) == std::string::npos) {
613 continue;
614 }
615
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700616 uint32_t oat_class_offset = oat_dex_file.GetOatClassOffset(class_def_index);
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100617 const OatFile::OatClass oat_class = oat_dex_file.GetOatClass(class_def_index);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700618 os << StringPrintf("%zd: %s (offset=0x%08x) (type_idx=%d)",
619 class_def_index, descriptor, oat_class_offset, class_def.class_idx_)
Vladimir Markod3c5beb2014-04-11 16:32:51 +0100620 << " (" << oat_class.GetStatus() << ")"
621 << " (" << oat_class.GetType() << ")\n";
622 // TODO: include bitmap here if type is kOatClassSomeCompiled?
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800623 if (options_.list_classes_) continue;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100624 if (!DumpOatClass(&vios, oat_class, *(dex_file.get()), class_def, &stop_analysis)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700625 success = false;
626 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800627 if (stop_analysis) {
628 os << std::flush;
629 return success;
630 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700631 }
632
633 os << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700634 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700635 }
636
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800637 bool ExportDexFile(std::ostream& os, const OatFile::OatDexFile& oat_dex_file) {
638 std::string error_msg;
639 std::string dex_file_location = oat_dex_file.GetDexFileLocation();
640
641 std::unique_ptr<const DexFile> dex_file(oat_dex_file.OpenDexFile(&error_msg));
642 if (dex_file == nullptr) {
643 os << "Failed to open dex file '" << dex_file_location << "': " << error_msg;
644 return false;
645 }
646 size_t fsize = oat_dex_file.FileSize();
647
648 // Some quick checks just in case
649 if (fsize == 0 || fsize < sizeof(DexFile::Header)) {
650 os << "Invalid dex file\n";
651 return false;
652 }
653
654 // Verify output directory exists
655 if (!OS::DirectoryExists(options_.export_dex_location_)) {
656 // TODO: Extend OS::DirectoryExists if symlink support is required
657 os << options_.export_dex_location_ << " output directory not found or symlink\n";
658 return false;
659 }
660
661 // Beautify path names
662 if (dex_file_location.size() > PATH_MAX || dex_file_location.size() <= 0) {
663 return false;
664 }
665
666 std::string dex_orig_name;
667 size_t dex_orig_pos = dex_file_location.rfind('/');
668 if (dex_orig_pos == std::string::npos)
669 dex_orig_name = dex_file_location;
670 else
671 dex_orig_name = dex_file_location.substr(dex_orig_pos + 1);
672
673 // A more elegant approach to efficiently name user installed apps is welcome
674 if (dex_orig_name.size() == 8 && !dex_orig_name.compare("base.apk")) {
675 dex_file_location.erase(dex_orig_pos, strlen("base.apk") + 1);
676 size_t apk_orig_pos = dex_file_location.rfind('/');
677 if (apk_orig_pos != std::string::npos) {
678 dex_orig_name = dex_file_location.substr(++apk_orig_pos);
679 }
680 }
681
682 std::string out_dex_path(options_.export_dex_location_);
683 if (out_dex_path.back() != '/') {
684 out_dex_path.append("/");
685 }
686 out_dex_path.append(dex_orig_name);
687 out_dex_path.append("_export.dex");
688 if (out_dex_path.length() > PATH_MAX) {
689 return false;
690 }
691
692 std::unique_ptr<File> file(OS::CreateEmptyFile(out_dex_path.c_str()));
693 if (file.get() == nullptr) {
694 os << "Failed to open output dex file " << out_dex_path;
695 return false;
696 }
697
698 if (!file->WriteFully(dex_file->Begin(), fsize)) {
699 os << "Failed to write dex file";
700 file->Erase();
701 return false;
702 }
703
704 if (file->FlushCloseOrErase() != 0) {
705 os << "Flush and close failed";
706 return false;
707 }
708
709 os << StringPrintf("Dex file exported at %s (%zd bytes)\n", out_dex_path.c_str(), fsize);
710 os << std::flush;
711
712 return true;
713 }
714
Elliott Hughese3c845c2012-02-28 17:23:01 -0800715 static void SkipAllFields(ClassDataItemIterator& it) {
Ian Rogers0571d352011-11-03 19:51:38 -0700716 while (it.HasNextStaticField()) {
717 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700718 }
Ian Rogers0571d352011-11-03 19:51:38 -0700719 while (it.HasNextInstanceField()) {
720 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700721 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800722 }
Brian Carlstromaded5f72011-10-07 17:15:04 -0700723
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100724 bool DumpOatClass(VariableIndentationOutputStream* vios,
725 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800726 const DexFile::ClassDef& class_def, bool* stop_analysis) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700727 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800728 bool addr_found = false;
Ian Rogers13735952014-10-08 12:43:28 -0700729 const uint8_t* class_data = dex_file.GetClassData(class_def);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700730 if (class_data == nullptr) { // empty class such as a marker interface?
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100731 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700732 return success;
Elliott Hughese3c845c2012-02-28 17:23:01 -0800733 }
734 ClassDataItemIterator it(dex_file, class_data);
735 SkipAllFields(it);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700736 uint32_t class_method_index = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700737 while (it.HasNextDirectMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100738 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700739 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800740 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700741 success = false;
742 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800743 if (addr_found) {
744 *stop_analysis = true;
745 return success;
746 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700747 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700748 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700749 }
Ian Rogers0571d352011-11-03 19:51:38 -0700750 while (it.HasNextVirtualMethod()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100751 if (!DumpOatMethod(vios, class_def, class_method_index, oat_class, dex_file,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700752 it.GetMemberIndex(), it.GetMethodCodeItem(),
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800753 it.GetRawMemberAccessFlags(), &addr_found)) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700754 success = false;
755 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800756 if (addr_found) {
757 *stop_analysis = true;
758 return success;
759 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700760 class_method_index++;
Ian Rogers0571d352011-11-03 19:51:38 -0700761 it.Next();
Brian Carlstromaded5f72011-10-07 17:15:04 -0700762 }
Ian Rogers0571d352011-11-03 19:51:38 -0700763 DCHECK(!it.HasNext());
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100764 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700765 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700766 }
Elliott Hughese3c845c2012-02-28 17:23:01 -0800767
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700768 static constexpr uint32_t kPrologueBytes = 16;
769
770 // When this was picked, the largest arm method was 55,256 bytes and arm64 was 50,412 bytes.
771 static constexpr uint32_t kMaxCodeSize = 100 * 1000;
772
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100773 bool DumpOatMethod(VariableIndentationOutputStream* vios,
774 const DexFile::ClassDef& class_def,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700775 uint32_t class_method_index,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700776 const OatFile::OatClass& oat_class, const DexFile& dex_file,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800777 uint32_t dex_method_idx, const DexFile::CodeItem* code_item,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800778 uint32_t method_access_flags, bool* addr_found) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700779 bool success = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800780
781 // TODO: Support regex
782 std::string method_name = dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
783 if (method_name.find(options_.method_filter_) == std::string::npos) {
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +0000784 return success;
785 }
786
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800787 std::string pretty_method = PrettyMethod(dex_method_idx, dex_file, true);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100788 vios->Stream() << StringPrintf("%d: %s (dex_method_idx=%d)\n",
789 class_method_index, pretty_method.c_str(),
790 dex_method_idx);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800791 if (options_.list_methods_) return success;
792
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800793 uint32_t oat_method_offsets_offset = oat_class.GetOatMethodOffsetsOffset(class_method_index);
794 const OatMethodOffsets* oat_method_offsets = oat_class.GetOatMethodOffsets(class_method_index);
795 const OatFile::OatMethod oat_method = oat_class.GetOatMethod(class_method_index);
796 uint32_t code_offset = oat_method.GetCodeOffset();
797 uint32_t code_size = oat_method.GetQuickCodeSize();
798 if (resolved_addr2instr_ != 0) {
799 if (resolved_addr2instr_ > code_offset + code_size) {
800 return success;
801 } else {
802 *addr_found = true; // stop analyzing file at next iteration
803 }
804 }
805
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100806 // Everything below is indented at least once.
807 ScopedIndentation indent1(vios);
808
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800809 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100810 vios->Stream() << "DEX CODE:\n";
811 ScopedIndentation indent2(vios);
812 DumpDexCode(vios->Stream(), dex_file, code_item);
Ian Rogersb23a7722012-10-09 16:54:26 -0700813 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700814
815 std::unique_ptr<verifier::MethodVerifier> verifier;
816 if (Runtime::Current() != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100817 vios->Stream() << "VERIFIER TYPE ANALYSIS:\n";
818 ScopedIndentation indent2(vios);
819 verifier.reset(DumpVerifier(vios,
820 dex_method_idx, &dex_file, class_def, code_item,
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700821 method_access_flags));
Ian Rogersb23a7722012-10-09 16:54:26 -0700822 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800823 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100824 vios->Stream() << "OatMethodOffsets ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800825 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100826 vios->Stream() << StringPrintf("%p ", oat_method_offsets);
Nicolas Geoffray39468442014-09-02 15:17:15 +0100827 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100828 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method_offsets_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700829 if (oat_method_offsets_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100830 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700831 "WARNING: oat method offsets offset 0x%08x is past end of file 0x%08zx.\n",
832 oat_method_offsets_offset, oat_file_.Size());
833 // If we can't read OatMethodOffsets, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100834 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700835 return false;
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800836 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700837
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100838 ScopedIndentation indent2(vios);
839 vios->Stream() << StringPrintf("code_offset: 0x%08x ", code_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700840 uint32_t aligned_code_begin = AlignCodeOffset(oat_method.GetCodeOffset());
841 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100842 vios->Stream() << StringPrintf("WARNING: "
843 "code offset 0x%08x is past end of file 0x%08zx.\n",
844 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700845 success = false;
846 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100847 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700848
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100849 vios->Stream() << "gc_map: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800850 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100851 vios->Stream() << StringPrintf("%p ", oat_method.GetGcMap());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700852 }
Mathieu Chartier957ca1c2014-11-21 16:51:29 -0800853 uint32_t gc_map_offset = oat_method.GetGcMapOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100854 vios->Stream() << StringPrintf("(offset=0x%08x)\n", gc_map_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700855 if (gc_map_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100856 vios->Stream() << StringPrintf("WARNING: "
857 "gc map table offset 0x%08x is past end of file 0x%08zx.\n",
858 gc_map_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700859 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800860 } else if (options_.dump_raw_gc_map_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100861 ScopedIndentation indent3(vios);
862 DumpGcMap(vios->Stream(), oat_method, code_item);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800863 }
864 }
865 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100866 vios->Stream() << "OatQuickMethodHeader ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700867 uint32_t method_header_offset = oat_method.GetOatQuickMethodHeaderOffset();
868 const OatQuickMethodHeader* method_header = oat_method.GetOatQuickMethodHeader();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700869
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800870 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100871 vios->Stream() << StringPrintf("%p ", method_header);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700872 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100873 vios->Stream() << StringPrintf("(offset=0x%08x)\n", method_header_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700874 if (method_header_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100875 vios->Stream() << StringPrintf(
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700876 "WARNING: oat quick method header offset 0x%08x is past end of file 0x%08zx.\n",
877 method_header_offset, oat_file_.Size());
878 // If we can't read the OatQuickMethodHeader, the rest of the data is dangerous to read.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100879 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700880 return false;
881 }
882
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100883 ScopedIndentation indent2(vios);
884 vios->Stream() << "mapping_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800885 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100886 vios->Stream() << StringPrintf("%p ", oat_method.GetMappingTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700887 }
888 uint32_t mapping_table_offset = oat_method.GetMappingTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100889 vios->Stream() << StringPrintf("(offset=0x%08x)\n", oat_method.GetMappingTableOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700890 if (mapping_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100891 vios->Stream() << StringPrintf("WARNING: "
892 "mapping table offset 0x%08x is past end of file 0x%08zx. "
893 "mapping table offset was loaded from offset 0x%08x.\n",
894 mapping_table_offset, oat_file_.Size(),
895 oat_method.GetMappingTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700896 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800897 } else if (options_.dump_raw_mapping_table_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100898 ScopedIndentation indent3(vios);
899 DumpMappingTable(vios, oat_method);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700900 }
901
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100902 vios->Stream() << "vmap_table: ";
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800903 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100904 vios->Stream() << StringPrintf("%p ", oat_method.GetVmapTable());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700905 }
906 uint32_t vmap_table_offset = oat_method.GetVmapTableOffset();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100907 vios->Stream() << StringPrintf("(offset=0x%08x)\n", vmap_table_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700908 if (vmap_table_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100909 vios->Stream() << StringPrintf("WARNING: "
910 "vmap table offset 0x%08x is past end of file 0x%08zx. "
911 "vmap table offset was loaded from offset 0x%08x.\n",
912 vmap_table_offset, oat_file_.Size(),
913 oat_method.GetVmapTableOffsetOffset());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700914 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800915 } else if (options_.dump_vmap_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100916 DumpVmapData(vios, oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700917 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800918 }
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700919 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100920 vios->Stream() << "QuickMethodFrameInfo\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700921
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100922 ScopedIndentation indent2(vios);
923 vios->Stream()
924 << StringPrintf("frame_size_in_bytes: %zd\n", oat_method.GetFrameSizeInBytes());
925 vios->Stream() << StringPrintf("core_spill_mask: 0x%08x ", oat_method.GetCoreSpillMask());
926 DumpSpillMask(vios->Stream(), oat_method.GetCoreSpillMask(), false);
927 vios->Stream() << "\n";
928 vios->Stream() << StringPrintf("fp_spill_mask: 0x%08x ", oat_method.GetFpSpillMask());
929 DumpSpillMask(vios->Stream(), oat_method.GetFpSpillMask(), true);
930 vios->Stream() << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700931 }
932 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100933 // Based on spill masks from QuickMethodFrameInfo so placed
934 // after it is dumped, but useful for understanding quick
935 // code, so dumped here.
936 ScopedIndentation indent2(vios);
937 DumpVregLocations(vios->Stream(), oat_method, code_item);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700938 }
939 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100940 vios->Stream() << "CODE: ";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700941 uint32_t code_size_offset = oat_method.GetQuickCodeSizeOffset();
942 if (code_size_offset > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100943 ScopedIndentation indent2(vios);
944 vios->Stream() << StringPrintf("WARNING: "
945 "code size offset 0x%08x is past end of file 0x%08zx.",
946 code_size_offset, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700947 success = false;
948 } else {
949 const void* code = oat_method.GetQuickCode();
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700950 uint32_t aligned_code_begin = AlignCodeOffset(code_offset);
951 uint64_t aligned_code_end = aligned_code_begin + code_size;
952
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800953 if (options_.absolute_addresses_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100954 vios->Stream() << StringPrintf("%p ", code);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700955 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100956 vios->Stream() << StringPrintf("(code_offset=0x%08x size_offset=0x%08x size=%u)%s\n",
957 code_offset,
958 code_size_offset,
959 code_size,
960 code != nullptr ? "..." : "");
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700961
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100962 ScopedIndentation indent2(vios);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700963 if (aligned_code_begin > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100964 vios->Stream() << StringPrintf("WARNING: "
965 "start of code at 0x%08x is past end of file 0x%08zx.",
966 aligned_code_begin, oat_file_.Size());
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700967 success = false;
968 } else if (aligned_code_end > oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100969 vios->Stream() << StringPrintf(
970 "WARNING: "
971 "end of code at 0x%08" PRIx64 " is past end of file 0x%08zx. "
972 "code size is 0x%08x loaded from offset 0x%08x.\n",
973 aligned_code_end, oat_file_.Size(),
974 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700975 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800976 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700977 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100978 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700979 }
980 }
981 } else if (code_size > kMaxCodeSize) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100982 vios->Stream() << StringPrintf(
983 "WARNING: "
984 "code size %d is bigger than max expected threshold of %d. "
985 "code size is 0x%08x loaded from offset 0x%08x.\n",
986 code_size, kMaxCodeSize,
987 code_size, code_size_offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700988 success = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800989 if (options_.disassemble_code_) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700990 if (code_size_offset + kPrologueBytes <= oat_file_.Size()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100991 DumpCode(vios, verifier.get(), oat_method, code_item, true, kPrologueBytes);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700992 }
993 }
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -0800994 } else if (options_.disassemble_code_) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100995 DumpCode(vios, verifier.get(), oat_method, code_item, !success, 0);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700996 }
997 }
998 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100999 vios->Stream() << std::flush;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001000 return success;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001001 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001002
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001003 void DumpSpillMask(std::ostream& os, uint32_t spill_mask, bool is_float) {
1004 if (spill_mask == 0) {
1005 return;
1006 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001007 os << "(";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001008 for (size_t i = 0; i < 32; i++) {
1009 if ((spill_mask & (1 << i)) != 0) {
1010 if (is_float) {
1011 os << "fr" << i;
1012 } else {
1013 os << "r" << i;
1014 }
1015 spill_mask ^= 1 << i; // clear bit
1016 if (spill_mask != 0) {
1017 os << ", ";
1018 } else {
1019 break;
1020 }
1021 }
1022 }
1023 os << ")";
1024 }
1025
Roland Levillain442b46a2015-02-18 16:54:21 +00001026 // Display data stored at the the vmap offset of an oat method.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001027 void DumpVmapData(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001028 const OatFile::OatMethod& oat_method,
1029 const DexFile::CodeItem* code_item) {
Roland Levillainf2650d12015-05-28 14:53:28 +01001030 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1031 // The optimizing compiler outputs its CodeInfo data in the vmap table.
Roland Levillain442b46a2015-02-18 16:54:21 +00001032 const void* raw_code_info = oat_method.GetVmapTable();
1033 if (raw_code_info != nullptr) {
1034 CodeInfo code_info(raw_code_info);
1035 DCHECK(code_item != nullptr);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001036 ScopedIndentation indent1(vios);
1037 DumpCodeInfo(vios, code_info, oat_method, *code_item);
Roland Levillain442b46a2015-02-18 16:54:21 +00001038 }
1039 } else {
1040 // Otherwise, display the vmap table.
1041 const uint8_t* raw_table = oat_method.GetVmapTable();
1042 if (raw_table != nullptr) {
1043 VmapTable vmap_table(raw_table);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001044 DumpVmapTable(vios->Stream(), oat_method, vmap_table);
Roland Levillain442b46a2015-02-18 16:54:21 +00001045 }
Nicolas Geoffray20d3eae2014-09-17 11:27:23 +01001046 }
Roland Levillain442b46a2015-02-18 16:54:21 +00001047 }
1048
1049 // Display a CodeInfo object emitted by the optimizing compiler.
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001050 void DumpCodeInfo(VariableIndentationOutputStream* vios,
Roland Levillain442b46a2015-02-18 16:54:21 +00001051 const CodeInfo& code_info,
Roland Levillainf2650d12015-05-28 14:53:28 +01001052 const OatFile::OatMethod& oat_method,
Roland Levillain442b46a2015-02-18 16:54:21 +00001053 const DexFile::CodeItem& code_item) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001054 code_info.Dump(vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001055 oat_method.GetCodeOffset(),
1056 code_item.registers_size_,
1057 options_.dump_code_info_stack_maps_);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001058 }
1059
Roland Levillain442b46a2015-02-18 16:54:21 +00001060 // Display a vmap table.
1061 void DumpVmapTable(std::ostream& os,
1062 const OatFile::OatMethod& oat_method,
1063 const VmapTable& vmap_table) {
1064 bool first = true;
1065 bool processing_fp = false;
1066 uint32_t spill_mask = oat_method.GetCoreSpillMask();
1067 for (size_t i = 0; i < vmap_table.Size(); i++) {
1068 uint16_t dex_reg = vmap_table[i];
1069 uint32_t cpu_reg = vmap_table.ComputeRegister(spill_mask, i,
1070 processing_fp ? kFloatVReg : kIntVReg);
1071 os << (first ? "v" : ", v") << dex_reg;
1072 if (!processing_fp) {
1073 os << "/r" << cpu_reg;
1074 } else {
1075 os << "/fr" << cpu_reg;
1076 }
1077 first = false;
1078 if (!processing_fp && dex_reg == 0xFFFF) {
1079 processing_fp = true;
1080 spill_mask = oat_method.GetFpSpillMask();
1081 }
1082 }
1083 os << "\n";
1084 }
1085
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001086 void DumpVregLocations(std::ostream& os, const OatFile::OatMethod& oat_method,
1087 const DexFile::CodeItem* code_item) {
1088 if (code_item != nullptr) {
1089 size_t num_locals_ins = code_item->registers_size_;
1090 size_t num_ins = code_item->ins_size_;
1091 size_t num_locals = num_locals_ins - num_ins;
1092 size_t num_outs = code_item->outs_size_;
1093
1094 os << "vr_stack_locations:";
1095 for (size_t reg = 0; reg <= num_locals_ins; reg++) {
1096 // For readability, delimit the different kinds of VRs.
1097 if (reg == num_locals_ins) {
1098 os << "\n\tmethod*:";
1099 } else if (reg == num_locals && num_ins > 0) {
1100 os << "\n\tins:";
1101 } else if (reg == 0 && num_locals > 0) {
1102 os << "\n\tlocals:";
1103 }
1104
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001105 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1106 code_item,
1107 oat_method.GetCoreSpillMask(),
1108 oat_method.GetFpSpillMask(),
1109 oat_method.GetFrameSizeInBytes(),
1110 reg,
1111 GetInstructionSet());
Razvan A Lupusorufaf9f0d2014-08-29 17:56:46 -07001112 os << " v" << reg << "[sp + #" << offset << "]";
1113 }
1114
1115 for (size_t out_reg = 0; out_reg < num_outs; out_reg++) {
1116 if (out_reg == 0) {
1117 os << "\n\touts:";
1118 }
1119
1120 uint32_t offset = StackVisitor::GetOutVROffset(out_reg, GetInstructionSet());
1121 os << " v" << out_reg << "[sp + #" << offset << "]";
1122 }
1123
1124 os << "\n";
1125 }
1126 }
1127
Ian Rogersb23a7722012-10-09 16:54:26 -07001128 void DescribeVReg(std::ostream& os, const OatFile::OatMethod& oat_method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001129 const DexFile::CodeItem* code_item, size_t reg, VRegKind kind) {
Ian Rogers1809a722013-08-09 22:05:32 -07001130 const uint8_t* raw_table = oat_method.GetVmapTable();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001131 if (raw_table != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001132 const VmapTable vmap_table(raw_table);
1133 uint32_t vmap_offset;
Ian Rogers1809a722013-08-09 22:05:32 -07001134 if (vmap_table.IsInContext(reg, kind, &vmap_offset)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001135 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
1136 uint32_t spill_mask = is_float ? oat_method.GetFpSpillMask()
1137 : oat_method.GetCoreSpillMask();
1138 os << (is_float ? "fr" : "r") << vmap_table.ComputeRegister(spill_mask, vmap_offset, kind);
Ian Rogersb23a7722012-10-09 16:54:26 -07001139 } else {
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001140 uint32_t offset = StackVisitor::GetVRegOffsetFromQuickCode(
1141 code_item,
1142 oat_method.GetCoreSpillMask(),
1143 oat_method.GetFpSpillMask(),
1144 oat_method.GetFrameSizeInBytes(),
1145 reg,
1146 GetInstructionSet());
Ian Rogersb23a7722012-10-09 16:54:26 -07001147 os << "[sp + #" << offset << "]";
1148 }
1149 }
1150 }
1151
Ian Rogersef7d42f2014-01-06 12:55:46 -08001152 void DumpGcMapRegisters(std::ostream& os, const OatFile::OatMethod& oat_method,
1153 const DexFile::CodeItem* code_item,
1154 size_t num_regs, const uint8_t* reg_bitmap) {
1155 bool first = true;
1156 for (size_t reg = 0; reg < num_regs; reg++) {
1157 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1158 if (first) {
1159 os << " v" << reg << " (";
1160 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1161 os << ")";
1162 first = false;
1163 } else {
1164 os << ", v" << reg << " (";
1165 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
1166 os << ")";
1167 }
1168 }
1169 }
1170 if (first) {
1171 os << "No registers in GC map\n";
1172 } else {
1173 os << "\n";
1174 }
1175 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001176 void DumpGcMap(std::ostream& os, const OatFile::OatMethod& oat_method,
1177 const DexFile::CodeItem* code_item) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001178 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Ian Rogersef7d42f2014-01-06 12:55:46 -08001179 if (gc_map_raw == nullptr) {
1180 return; // No GC map.
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001181 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001182 const void* quick_code = oat_method.GetQuickCode();
Elliott Hughes956af0f2014-12-11 14:34:28 -08001183 NativePcOffsetToReferenceMap map(gc_map_raw);
1184 for (size_t entry = 0; entry < map.NumEntries(); entry++) {
1185 const uint8_t* native_pc = reinterpret_cast<const uint8_t*>(quick_code) +
1186 map.GetNativePcOffset(entry);
1187 os << StringPrintf("%p", native_pc);
1188 DumpGcMapRegisters(os, oat_method, code_item, map.RegWidth() * 8, map.GetBitMap(entry));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001189 }
1190 }
1191
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001192 void DumpMappingTable(VariableIndentationOutputStream* vios,
1193 const OatFile::OatMethod& oat_method) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001194 const void* quick_code = oat_method.GetQuickCode();
1195 if (quick_code == nullptr) {
Elliott Hughese3c845c2012-02-28 17:23:01 -08001196 return;
1197 }
Ian Rogers1809a722013-08-09 22:05:32 -07001198 MappingTable table(oat_method.GetMappingTable());
1199 if (table.TotalSize() != 0) {
Ian Rogers1809a722013-08-09 22:05:32 -07001200 if (table.PcToDexSize() != 0) {
1201 typedef MappingTable::PcToDexIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001202 vios->Stream() << "suspend point mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001203 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001204 ScopedIndentation indent1(vios);
1205 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001206 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001207 vios->Stream() << "}\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001208 }
1209 if (table.DexToPcSize() != 0) {
1210 typedef MappingTable::DexToPcIterator It;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001211 vios->Stream() << "catch entry mappings {\n";
Ian Rogers1809a722013-08-09 22:05:32 -07001212 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001213 ScopedIndentation indent1(vios);
1214 vios->Stream() << StringPrintf("0x%04x -> 0x%04x\n", cur.NativePcOffset(), cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001215 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001216 vios->Stream() << "}\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001217 }
1218 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001219 }
1220
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001221 uint32_t DumpInformationAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001222 const OatFile::OatMethod& oat_method,
1223 const DexFile::CodeItem* code_item,
1224 size_t offset,
1225 bool suspend_point_mapping) {
1226 if (IsMethodGeneratedByOptimizingCompiler(oat_method, code_item)) {
1227 if (suspend_point_mapping) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001228 ScopedIndentation indent1(vios);
1229 DumpDexRegisterMapAtOffset(vios, oat_method, code_item, offset);
Roland Levillainf2650d12015-05-28 14:53:28 +01001230 }
1231 // The return value is not used in the case of a method compiled
1232 // with the optimizing compiler.
1233 return DexFile::kDexNoIndex;
1234 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001235 return DumpMappingAtOffset(vios->Stream(), oat_method, offset, suspend_point_mapping);
Roland Levillainf2650d12015-05-28 14:53:28 +01001236 }
1237 }
1238
Ian Rogers1809a722013-08-09 22:05:32 -07001239 uint32_t DumpMappingAtOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1240 size_t offset, bool suspend_point_mapping) {
1241 MappingTable table(oat_method.GetMappingTable());
1242 if (suspend_point_mapping && table.PcToDexSize() > 0) {
1243 typedef MappingTable::PcToDexIterator It;
1244 for (It cur = table.PcToDexBegin(), end = table.PcToDexEnd(); cur != end; ++cur) {
1245 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001246 os << StringPrintf("suspend point dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001247 return cur.DexPc();
1248 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001249 }
Ian Rogers1809a722013-08-09 22:05:32 -07001250 } else if (!suspend_point_mapping && table.DexToPcSize() > 0) {
1251 typedef MappingTable::DexToPcIterator It;
1252 for (It cur = table.DexToPcBegin(), end = table.DexToPcEnd(); cur != end; ++cur) {
1253 if (offset == cur.NativePcOffset()) {
Brian Carlstrom4b8c13e2013-08-23 18:10:32 -07001254 os << StringPrintf("catch entry dex PC: 0x%04x\n", cur.DexPc());
Ian Rogers1809a722013-08-09 22:05:32 -07001255 return cur.DexPc();
Ian Rogersb23a7722012-10-09 16:54:26 -07001256 }
1257 }
Elliott Hughese3c845c2012-02-28 17:23:01 -08001258 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001259 return DexFile::kDexNoIndex;
Ian Rogersb23a7722012-10-09 16:54:26 -07001260 }
1261
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001262 void DumpGcMapAtNativePcOffset(std::ostream& os, const OatFile::OatMethod& oat_method,
1263 const DexFile::CodeItem* code_item, size_t native_pc_offset) {
Mathieu Chartier957ca1c2014-11-21 16:51:29 -08001264 const uint8_t* gc_map_raw = oat_method.GetGcMap();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001265 if (gc_map_raw != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001266 NativePcOffsetToReferenceMap map(gc_map_raw);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001267 if (map.HasEntry(native_pc_offset)) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001268 size_t num_regs = map.RegWidth() * 8;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001269 const uint8_t* reg_bitmap = map.FindBitMap(native_pc_offset);
Ian Rogersb23a7722012-10-09 16:54:26 -07001270 bool first = true;
1271 for (size_t reg = 0; reg < num_regs; reg++) {
1272 if (((reg_bitmap[reg / 8] >> (reg % 8)) & 0x01) != 0) {
1273 if (first) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001274 os << "GC map objects: v" << reg << " (";
1275 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001276 os << ")";
1277 first = false;
1278 } else {
1279 os << ", v" << reg << " (";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001280 DescribeVReg(os, oat_method, code_item, reg, kReferenceVReg);
Ian Rogersb23a7722012-10-09 16:54:26 -07001281 os << ")";
1282 }
1283 }
1284 }
1285 if (!first) {
1286 os << "\n";
1287 }
1288 }
1289 }
1290 }
1291
Mathieu Chartier590fee92013-09-13 13:46:47 -07001292 void DumpVRegsAtDexPc(std::ostream& os, verifier::MethodVerifier* verifier,
1293 const OatFile::OatMethod& oat_method,
1294 const DexFile::CodeItem* code_item, uint32_t dex_pc) {
1295 DCHECK(verifier != nullptr);
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001296 std::vector<int32_t> kinds = verifier->DescribeVRegs(dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001297 bool first = true;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001298 for (size_t reg = 0; reg < code_item->registers_size_; reg++) {
1299 VRegKind kind = static_cast<VRegKind>(kinds.at(reg * 2));
1300 if (kind != kUndefined) {
1301 if (first) {
1302 os << "VRegs: v";
1303 first = false;
1304 } else {
1305 os << ", v";
1306 }
1307 os << reg << " (";
1308 switch (kind) {
1309 case kImpreciseConstant:
1310 os << "Imprecise Constant: " << kinds.at((reg * 2) + 1) << ", ";
1311 DescribeVReg(os, oat_method, code_item, reg, kind);
1312 break;
1313 case kConstant:
1314 os << "Constant: " << kinds.at((reg * 2) + 1);
1315 break;
1316 default:
1317 DescribeVReg(os, oat_method, code_item, reg, kind);
1318 break;
1319 }
1320 os << ")";
1321 }
1322 }
1323 if (!first) {
1324 os << "\n";
1325 }
1326 }
1327
1328
Ian Rogersb23a7722012-10-09 16:54:26 -07001329 void DumpDexCode(std::ostream& os, const DexFile& dex_file, const DexFile::CodeItem* code_item) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001330 if (code_item != nullptr) {
Ian Rogersb23a7722012-10-09 16:54:26 -07001331 size_t i = 0;
1332 while (i < code_item->insns_size_in_code_units_) {
1333 const Instruction* instruction = Instruction::At(&code_item->insns_[i]);
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001334 os << StringPrintf("0x%04zx: ", i) << instruction->DumpHexLE(5)
1335 << StringPrintf("\t| %s\n", instruction->DumpString(&dex_file).c_str());
Ian Rogersb23a7722012-10-09 16:54:26 -07001336 i += instruction->SizeInCodeUnits();
1337 }
1338 }
1339 }
1340
Roland Levillainf2650d12015-05-28 14:53:28 +01001341 // Has `oat_method` -- corresponding to the Dex `code_item` -- been compiled by
1342 // the optimizing compiler?
1343 static bool IsMethodGeneratedByOptimizingCompiler(const OatFile::OatMethod& oat_method,
1344 const DexFile::CodeItem* code_item) {
1345 // If the native GC map is null and the Dex `code_item` is not
1346 // null, then this method has been compiled with the optimizing
1347 // compiler.
1348 return oat_method.GetGcMap() == nullptr && code_item != nullptr;
1349 }
1350
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001351 void DumpDexRegisterMapAtOffset(VariableIndentationOutputStream* vios,
Roland Levillainf2650d12015-05-28 14:53:28 +01001352 const OatFile::OatMethod& oat_method,
1353 const DexFile::CodeItem* code_item,
1354 size_t offset) {
1355 // This method is only relevant for oat methods compiled with the
1356 // optimizing compiler.
1357 DCHECK(IsMethodGeneratedByOptimizingCompiler(oat_method, code_item));
1358
1359 // The optimizing compiler outputs its CodeInfo data in the vmap table.
1360 const void* raw_code_info = oat_method.GetVmapTable();
1361 if (raw_code_info != nullptr) {
1362 CodeInfo code_info(raw_code_info);
David Brazdilf677ebf2015-05-29 16:29:43 +01001363 StackMapEncoding encoding = code_info.ExtractEncoding();
1364 StackMap stack_map = code_info.GetStackMapForNativePcOffset(offset, encoding);
Roland Levillainf2650d12015-05-28 14:53:28 +01001365 if (stack_map.IsValid()) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001366 stack_map.Dump(vios, code_info, encoding, oat_method.GetCodeOffset(),
1367 code_item->registers_size_);
Roland Levillainf2650d12015-05-28 14:53:28 +01001368 }
1369 }
1370 }
1371
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001372 verifier::MethodVerifier* DumpVerifier(VariableIndentationOutputStream* vios,
1373 uint32_t dex_method_idx,
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001374 const DexFile* dex_file,
1375 const DexFile::ClassDef& class_def,
1376 const DexFile::CodeItem* code_item,
1377 uint32_t method_access_flags) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001378 if ((method_access_flags & kAccNative) == 0) {
1379 ScopedObjectAccess soa(Thread::Current());
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001380 StackHandleScope<1> hs(soa.Self());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001381 Handle<mirror::DexCache> dex_cache(
1382 hs.NewHandle(Runtime::Current()->GetClassLinker()->FindDexCache(*dex_file)));
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001383 DCHECK(options_.class_loader_ != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001384 return verifier::MethodVerifier::VerifyMethodAndDump(
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001385 soa.Self(), vios, dex_method_idx, dex_file, dex_cache, *options_.class_loader_,
1386 &class_def, code_item, nullptr, method_access_flags);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001387 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001388
1389 return nullptr;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001390 }
1391
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001392 void DumpCode(VariableIndentationOutputStream* vios,
1393 verifier::MethodVerifier* verifier,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001394 const OatFile::OatMethod& oat_method, const DexFile::CodeItem* code_item,
1395 bool bad_input, size_t code_size) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001396 const void* quick_code = oat_method.GetQuickCode();
1397
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001398 if (code_size == 0) {
1399 code_size = oat_method.GetQuickCodeSize();
1400 }
Elliott Hughes956af0f2014-12-11 14:34:28 -08001401 if (code_size == 0 || quick_code == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001402 vios->Stream() << "NO CODE!\n";
Ian Rogersb23a7722012-10-09 16:54:26 -07001403 return;
Elliott Hughes956af0f2014-12-11 14:34:28 -08001404 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001405 const uint8_t* quick_native_pc = reinterpret_cast<const uint8_t*>(quick_code);
1406 size_t offset = 0;
1407 while (offset < code_size) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001408 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001409 DumpInformationAtOffset(vios, oat_method, code_item, offset, false);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001410 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001411 offset += disassembler_->Dump(vios->Stream(), quick_native_pc + offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001412 if (!bad_input) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001413 uint32_t dex_pc =
1414 DumpInformationAtOffset(vios, oat_method, code_item, offset, true);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001415 if (dex_pc != DexFile::kDexNoIndex) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001416 DumpGcMapAtNativePcOffset(vios->Stream(), oat_method, code_item, offset);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001417 if (verifier != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001418 DumpVRegsAtDexPc(vios->Stream(), verifier, oat_method, code_item, dex_pc);
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001419 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001420 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001421 }
1422 }
Ian Rogersb23a7722012-10-09 16:54:26 -07001423 }
1424 }
1425
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001426 const OatFile& oat_file_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001427 const std::vector<const OatFile::OatDexFile*> oat_dex_files_;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001428 const OatDumperOptions& options_;
1429 uint32_t resolved_addr2instr_;
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001430 InstructionSet instruction_set_;
Ian Rogersef7d42f2014-01-06 12:55:46 -08001431 std::set<uintptr_t> offsets_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001432 Disassembler* disassembler_;
Brian Carlstromaded5f72011-10-07 17:15:04 -07001433};
1434
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001435class ImageDumper {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001436 public:
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001437 explicit ImageDumper(std::ostream* os, gc::space::ImageSpace& image_space,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001438 const ImageHeader& image_header, OatDumperOptions* oat_dumper_options)
1439 : os_(os),
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001440 vios_(os),
1441 indent1_(&vios_),
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001442 image_space_(image_space),
1443 image_header_(image_header),
1444 oat_dumper_options_(oat_dumper_options) {}
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001445
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001446 bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001447 std::ostream& os = *os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001448 std::ostream& indent_os = vios_.Stream();
1449
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001450 os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
Brian Carlstrome24fa612011-09-29 00:53:55 -07001451
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001452 os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001453
Mathieu Chartiere401d142015-04-22 13:56:20 -07001454 os << "IMAGE SIZE: " << image_header_.GetImageSize() << "\n\n";
1455
1456 for (size_t i = 0; i < ImageHeader::kSectionCount; ++i) {
1457 auto section = static_cast<ImageHeader::ImageSections>(i);
1458 os << "IMAGE SECTION " << section << ": " << image_header_.GetImageSection(section) << "\n\n";
1459 }
Mathieu Chartier31e89252013-08-28 11:29:12 -07001460
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001461 os << "OAT CHECKSUM: " << StringPrintf("0x%08x\n\n", image_header_.GetOatChecksum());
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001462
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001463 os << "OAT FILE BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatFileBegin()) << "\n\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001464
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001465 os << "OAT DATA BEGIN:" << reinterpret_cast<void*>(image_header_.GetOatDataBegin()) << "\n\n";
1466
1467 os << "OAT DATA END:" << reinterpret_cast<void*>(image_header_.GetOatDataEnd()) << "\n\n";
1468
1469 os << "OAT FILE END:" << reinterpret_cast<void*>(image_header_.GetOatFileEnd()) << "\n\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001470
Alex Lighta59dd802014-07-02 16:28:08 -07001471 os << "PATCH DELTA:" << image_header_.GetPatchDelta() << "\n\n";
1472
Igor Murashkin46774762014-10-22 11:37:02 -07001473 os << "COMPILE PIC: " << (image_header_.CompilePic() ? "yes" : "no") << "\n\n";
1474
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001475 {
1476 os << "ROOTS: " << reinterpret_cast<void*>(image_header_.GetImageRoots()) << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001477 static_assert(arraysize(image_roots_descriptions_) ==
1478 static_cast<size_t>(ImageHeader::kImageRootsMax), "sizes must match");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001479 for (int i = 0; i < ImageHeader::kImageRootsMax; i++) {
1480 ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i);
1481 const char* image_root_description = image_roots_descriptions_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001482 mirror::Object* image_root_object = image_header_.GetImageRoot(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001483 indent_os << StringPrintf("%s: %p\n", image_root_description, image_root_object);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001484 if (image_root_object->IsObjectArray()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001485 mirror::ObjectArray<mirror::Object>* image_root_object_array
Ian Rogersfa824272013-11-05 16:12:57 -08001486 = image_root_object->AsObjectArray<mirror::Object>();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001487 ScopedIndentation indent2(&vios_);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001488 for (int j = 0; j < image_root_object_array->GetLength(); j++) {
1489 mirror::Object* value = image_root_object_array->Get(j);
Ian Rogersfa824272013-11-05 16:12:57 -08001490 size_t run = 0;
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001491 for (int32_t k = j + 1; k < image_root_object_array->GetLength(); k++) {
1492 if (value == image_root_object_array->Get(k)) {
Ian Rogersfa824272013-11-05 16:12:57 -08001493 run++;
1494 } else {
1495 break;
1496 }
1497 }
1498 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001499 indent_os << StringPrintf("%d: ", j);
Ian Rogersfa824272013-11-05 16:12:57 -08001500 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001501 indent_os << StringPrintf("%d to %zd: ", j, j + run);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001502 j = j + run;
Ian Rogersfa824272013-11-05 16:12:57 -08001503 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001504 if (value != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001505 PrettyObjectValue(indent_os, value->GetClass(), value);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001507 indent_os << j << ": null\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001508 }
Ian Rogersd5b32602012-02-26 16:40:04 -08001509 }
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001510 }
1511 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001512 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001513
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001514 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001515 os << "METHOD ROOTS\n";
1516 static_assert(arraysize(image_methods_descriptions_) ==
1517 static_cast<size_t>(ImageHeader::kImageMethodsCount), "sizes must match");
1518 for (int i = 0; i < ImageHeader::kImageMethodsCount; i++) {
1519 auto image_root = static_cast<ImageHeader::ImageMethod>(i);
1520 const char* description = image_methods_descriptions_[i];
1521 auto* image_method = image_header_.GetImageMethod(image_root);
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001522 indent_os << StringPrintf("%s: %p\n", description, image_method);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001523 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001524 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001525 os << "\n";
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001526
Brian Carlstromaded5f72011-10-07 17:15:04 -07001527 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001528 std::string image_filename = image_space_.GetImageFilename();
1529 std::string oat_location = ImageHeader::GetOatLocationFromImageLocation(image_filename);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001530 os << "OAT LOCATION: " << oat_location;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 os << "\n";
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001532 std::string error_msg;
Alex Lighta59dd802014-07-02 16:28:08 -07001533 const OatFile* oat_file = class_linker->FindOpenedOatFileFromOatLocation(oat_location);
1534 if (oat_file == nullptr) {
Richard Uhlere5fed032015-03-18 08:21:11 -07001535 oat_file = OatFile::Open(oat_location, oat_location,
1536 nullptr, nullptr, false, nullptr,
1537 &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -07001538 if (oat_file == nullptr) {
1539 os << "NOT FOUND: " << error_msg << "\n";
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001540 return false;
Alex Lighta59dd802014-07-02 16:28:08 -07001541 }
Brian Carlstromaded5f72011-10-07 17:15:04 -07001542 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 os << "\n";
Brian Carlstromaded5f72011-10-07 17:15:04 -07001544
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001545 stats_.oat_file_bytes = oat_file->Size();
1546
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08001547 oat_dumper_.reset(new OatDumper(*oat_file, *oat_dumper_options_));
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001548
Mathieu Chartier02e25112013-08-14 16:14:24 -07001549 for (const OatFile::OatDexFile* oat_dex_file : oat_file->GetOatDexFiles()) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001550 CHECK(oat_dex_file != nullptr);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001551 stats_.oat_dex_file_sizes.push_back(std::make_pair(oat_dex_file->GetDexFileLocation(),
1552 oat_dex_file->FileSize()));
Ian Rogers05f28c62012-10-23 18:12:13 -07001553 }
1554
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 os << "OBJECTS:\n" << std::flush;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001556
1557 // Loop through all the image spaces and dump their objects.
Ian Rogers1d54e732013-05-02 21:10:01 -07001558 gc::Heap* heap = Runtime::Current()->GetHeap();
1559 const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
Ian Rogers50b35e22012-10-04 10:09:15 -07001560 Thread* self = Thread::Current();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001561 {
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001562 {
1563 WriterMutexLock mu(self, *Locks::heap_bitmap_lock_);
1564 heap->FlushAllocStack();
1565 }
Hiroshi Yamauchi90d70682014-02-20 16:17:30 -08001566 // Since FlushAllocStack() above resets the (active) allocation
1567 // stack. Need to revoke the thread-local allocation stacks that
1568 // point into it.
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001569 {
1570 self->TransitionFromRunnableToSuspended(kNative);
1571 ThreadList* thread_list = Runtime::Current()->GetThreadList();
Mathieu Chartierbf9fc582015-03-13 17:21:25 -07001572 thread_list->SuspendAll(__FUNCTION__);
Mathieu Chartierc22c59e2014-02-24 15:16:06 -08001573 heap->RevokeAllThreadLocalAllocationStacks(self);
1574 thread_list->ResumeAll();
1575 self->TransitionFromSuspendedToRunnable();
1576 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001577 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001578 {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001579 // Mark dex caches.
1580 dex_cache_arrays_.clear();
1581 {
1582 ReaderMutexLock mu(self, *class_linker->DexLock());
1583 for (size_t i = 0; i < class_linker->GetDexCacheCount(); ++i) {
1584 auto* dex_cache = class_linker->GetDexCache(i);
1585 dex_cache_arrays_.insert(dex_cache->GetResolvedFields());
1586 dex_cache_arrays_.insert(dex_cache->GetResolvedMethods());
1587 }
1588 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001589 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001590 for (const auto& space : spaces) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001591 if (space->IsImageSpace()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001592 auto* image_space = space->AsImageSpace();
1593 // Dump the normal objects before ArtMethods.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001594 image_space->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
1595 indent_os << "\n";
Mathieu Chartiere401d142015-04-22 13:56:20 -07001596 // TODO: Dump fields.
1597 // Dump methods after.
1598 const auto& methods_section = image_header_.GetMethodsSection();
1599 const auto pointer_size =
1600 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet());
1601 const auto method_size = ArtMethod::ObjectSize(pointer_size);
1602 for (size_t pos = 0; pos < methods_section.Size(); pos += method_size) {
1603 auto* method = reinterpret_cast<ArtMethod*>(
1604 image_space->Begin() + pos + methods_section.Offset());
1605 indent_os << method << " " << " ArtMethod: " << PrettyMethod(method) << "\n";
1606 DumpMethod(method, this, indent_os);
1607 indent_os << "\n";
1608 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001609 }
Mathieu Chartier2fde5332012-09-14 14:51:54 -07001610 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 // Dump the large objects separately.
Mathieu Chartierbbd695c2014-04-16 09:48:48 -07001612 heap->GetLargeObjectsSpace()->GetLiveBitmap()->Walk(ImageDumper::Callback, this);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001613 indent_os << "\n";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001614 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 os << "STATS:\n" << std::flush;
Ian Rogers700a4022014-05-19 16:49:03 -07001616 std::unique_ptr<File> file(OS::OpenFileForReading(image_filename.c_str()));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001617 if (file.get() == nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001618 LOG(WARNING) << "Failed to find image in " << image_filename;
Brian Carlstrom6f277752013-09-30 17:56:45 -07001619 }
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001620 if (file.get() != nullptr) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001621 stats_.file_bytes = file->GetLength();
Brian Carlstrom6f277752013-09-30 17:56:45 -07001622 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001623 size_t header_bytes = sizeof(ImageHeader);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001624 const auto& bitmap_section = image_header_.GetImageSection(ImageHeader::kSectionImageBitmap);
1625 const auto& field_section = image_header_.GetImageSection(ImageHeader::kSectionArtFields);
1626 const auto& method_section = image_header_.GetMethodsSection();
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001627 const auto& intern_section = image_header_.GetImageSection(
1628 ImageHeader::kSectionInternedStrings);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001629 stats_.header_bytes = header_bytes;
1630 size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes;
1631 stats_.alignment_bytes += alignment_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001632 stats_.alignment_bytes += bitmap_section.Offset() - image_header_.GetImageSize();
1633 stats_.bitmap_bytes += bitmap_section.Size();
1634 stats_.art_field_bytes += field_section.Size();
Nicolas Geoffray6fcaf3f2015-07-08 15:55:28 +01001635 // RoundUp to 8 bytes to match the intern table alignment expectation.
1636 stats_.art_method_bytes += RoundUp(method_section.Size(), sizeof(uint64_t));
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001637 stats_.interned_strings_bytes += intern_section.Size();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001638 stats_.Dump(os, indent_os);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001639 os << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001640
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001642
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07001643 return oat_dumper_->Dump(os);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001644 }
1645
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001646 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001647 static void PrettyObjectValue(std::ostream& os, mirror::Class* type, mirror::Object* value)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001648 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001649 CHECK(type != nullptr);
1650 if (value == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001651 os << StringPrintf("null %s\n", PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001652 } else if (type->IsStringClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001653 mirror::String* string = value->AsString();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001654 os << StringPrintf("%p String: %s\n", string,
Ian Rogers68b56852014-08-29 20:19:11 -07001655 PrintableString(string->ToModifiedUtf8().c_str()).c_str());
Ian Rogers64b6d142012-10-29 16:34:15 -07001656 } else if (type->IsClassClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001657 mirror::Class* klass = value->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001658 os << StringPrintf("%p Class: %s\n", klass, PrettyDescriptor(klass).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001659 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001660 os << StringPrintf("%p %s\n", value, PrettyDescriptor(type).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001661 }
1662 }
1663
Mathieu Chartierc7853442015-03-27 14:35:38 -07001664 static void PrintField(std::ostream& os, ArtField* field, mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001665 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07001666 os << StringPrintf("%s: ", field->GetName());
Ian Rogers08f1f502014-12-02 15:04:37 -08001667 switch (field->GetTypeAsPrimitiveType()) {
1668 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -08001669 os << StringPrintf("%" PRId64 " (0x%" PRIx64 ")\n", field->Get64(obj), field->Get64(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001670 break;
1671 case Primitive::kPrimDouble:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001672 os << StringPrintf("%f (%a)\n", field->GetDouble(obj), field->GetDouble(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001673 break;
1674 case Primitive::kPrimFloat:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001675 os << StringPrintf("%f (%a)\n", field->GetFloat(obj), field->GetFloat(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001676 break;
1677 case Primitive::kPrimInt:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001678 os << StringPrintf("%d (0x%x)\n", field->Get32(obj), field->Get32(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001679 break;
1680 case Primitive::kPrimChar:
Fred Shih37f05ef2014-07-16 18:38:08 -07001681 os << StringPrintf("%u (0x%x)\n", field->GetChar(obj), field->GetChar(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001682 break;
1683 case Primitive::kPrimShort:
Fred Shih37f05ef2014-07-16 18:38:08 -07001684 os << StringPrintf("%d (0x%x)\n", field->GetShort(obj), field->GetShort(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001685 break;
1686 case Primitive::kPrimBoolean:
Fred Shih37f05ef2014-07-16 18:38:08 -07001687 os << StringPrintf("%s (0x%x)\n", field->GetBoolean(obj)? "true" : "false",
1688 field->GetBoolean(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001689 break;
1690 case Primitive::kPrimByte:
Fred Shih37f05ef2014-07-16 18:38:08 -07001691 os << StringPrintf("%d (0x%x)\n", field->GetByte(obj), field->GetByte(obj));
Ian Rogers08f1f502014-12-02 15:04:37 -08001692 break;
1693 case Primitive::kPrimNot: {
1694 // Get the value, don't compute the type unless it is non-null as we don't want
1695 // to cause class loading.
1696 mirror::Object* value = field->GetObj(obj);
1697 if (value == nullptr) {
1698 os << StringPrintf("null %s\n", PrettyDescriptor(field->GetTypeDescriptor()).c_str());
Ian Rogers50239c72013-06-17 14:53:22 -07001699 } else {
Ian Rogers08f1f502014-12-02 15:04:37 -08001700 // Grab the field type without causing resolution.
Mathieu Chartierdaaf3262015-03-24 13:30:28 -07001701 mirror::Class* field_type = field->GetType<false>();
Ian Rogers08f1f502014-12-02 15:04:37 -08001702 if (field_type != nullptr) {
1703 PrettyObjectValue(os, field_type, value);
1704 } else {
1705 os << StringPrintf("%p %s\n", value,
1706 PrettyDescriptor(field->GetTypeDescriptor()).c_str());
1707 }
Ian Rogers50239c72013-06-17 14:53:22 -07001708 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001709 break;
Ian Rogers48efc2b2012-08-27 17:20:31 -07001710 }
Ian Rogers08f1f502014-12-02 15:04:37 -08001711 default:
1712 os << "unexpected field type: " << field->GetTypeDescriptor() << "\n";
1713 break;
Ian Rogersd5b32602012-02-26 16:40:04 -08001714 }
1715 }
1716
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001717 static void DumpFields(std::ostream& os, mirror::Object* obj, mirror::Class* klass)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001718 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001719 mirror::Class* super = klass->GetSuperClass();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001720 if (super != nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001721 DumpFields(os, obj, super);
Ian Rogersd5b32602012-02-26 16:40:04 -08001722 }
Mathieu Chartierc7853442015-03-27 14:35:38 -07001723 ArtField* fields = klass->GetIFields();
1724 for (size_t i = 0, count = klass->NumInstanceFields(); i < count; i++) {
1725 PrintField(os, &fields[i], obj);
Ian Rogersd5b32602012-02-26 16:40:04 -08001726 }
1727 }
1728
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001729 bool InDumpSpace(const mirror::Object* object) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001730 return image_space_.Contains(object);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001731 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001732
Mathieu Chartiere401d142015-04-22 13:56:20 -07001733 const void* GetQuickOatCodeBegin(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001734 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiera7dd0382014-11-20 17:08:58 -08001735 const void* quick_code = m->GetEntryPointFromQuickCompiledCodePtrSize(
1736 InstructionSetPointerSize(oat_dumper_->GetOatInstructionSet()));
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001737 if (Runtime::Current()->GetClassLinker()->IsQuickResolutionStub(quick_code)) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001738 quick_code = oat_dumper_->GetQuickOatCode(m);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001739 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001740 if (oat_dumper_->GetInstructionSet() == kThumb2) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001741 quick_code = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(quick_code) & ~0x1);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001742 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001743 return quick_code;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001744 }
1745
Mathieu Chartiere401d142015-04-22 13:56:20 -07001746 uint32_t GetQuickOatCodeSize(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001747 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001748 const uint32_t* oat_code_begin = reinterpret_cast<const uint32_t*>(GetQuickOatCodeBegin(m));
1749 if (oat_code_begin == nullptr) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001750 return 0;
1751 }
1752 return oat_code_begin[-1];
1753 }
1754
Mathieu Chartiere401d142015-04-22 13:56:20 -07001755 const void* GetQuickOatCodeEnd(ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001756 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -08001757 const uint8_t* oat_code_begin = reinterpret_cast<const uint8_t*>(GetQuickOatCodeBegin(m));
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001758 if (oat_code_begin == nullptr) {
1759 return nullptr;
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001760 }
Ian Rogersef7d42f2014-01-06 12:55:46 -08001761 return oat_code_begin + GetQuickOatCodeSize(m);
Brian Carlstromf8bbb842012-03-14 03:01:42 -07001762 }
1763
Mathieu Chartiere401d142015-04-22 13:56:20 -07001764 static void Callback(mirror::Object* obj, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001765 DCHECK(obj != nullptr);
1766 DCHECK(arg != nullptr);
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001767 ImageDumper* state = reinterpret_cast<ImageDumper*>(arg);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001768 if (!state->InDumpSpace(obj)) {
1769 return;
1770 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001771
1772 size_t object_bytes = obj->SizeOf();
1773 size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes;
1774 state->stats_.object_bytes += object_bytes;
1775 state->stats_.alignment_bytes += alignment_bytes;
1776
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001777 std::ostream& os = state->vios_.Stream();
1778
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001779 mirror::Class* obj_class = obj->GetClass();
Ian Rogersd5b32602012-02-26 16:40:04 -08001780 if (obj_class->IsArrayClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001781 os << StringPrintf("%p: %s length:%d\n", obj, PrettyDescriptor(obj_class).c_str(),
1782 obj->AsArray()->GetLength());
Ian Rogersd5b32602012-02-26 16:40:04 -08001783 } else if (obj->IsClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001784 mirror::Class* klass = obj->AsClass();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001785 os << StringPrintf("%p: java.lang.Class \"%s\" (", obj, PrettyDescriptor(klass).c_str())
1786 << klass->GetStatus() << ")\n";
Ian Rogersd5b32602012-02-26 16:40:04 -08001787 } else if (obj_class->IsStringClass()) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001788 os << StringPrintf("%p: java.lang.String %s\n", obj,
Ian Rogers68b56852014-08-29 20:19:11 -07001789 PrintableString(obj->AsString()->ToModifiedUtf8().c_str()).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001790 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001791 os << StringPrintf("%p: %s\n", obj, PrettyDescriptor(obj_class).c_str());
Ian Rogersd5b32602012-02-26 16:40:04 -08001792 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001793 ScopedIndentation indent1(&state->vios_);
1794 DumpFields(os, obj, obj_class);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001795 const auto image_pointer_size =
1796 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
Ian Rogersd5b32602012-02-26 16:40:04 -08001797 if (obj->IsObjectArray()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001798 auto* obj_array = obj->AsObjectArray<mirror::Object>();
1799 for (int32_t i = 0, length = obj_array->GetLength(); i < length; i++) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001800 mirror::Object* value = obj_array->Get(i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001801 size_t run = 0;
1802 for (int32_t j = i + 1; j < length; j++) {
1803 if (value == obj_array->Get(j)) {
1804 run++;
1805 } else {
1806 break;
1807 }
1808 }
1809 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001810 os << StringPrintf("%d: ", i);
Ian Rogersd5b32602012-02-26 16:40:04 -08001811 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001812 os << StringPrintf("%d to %zd: ", i, i + run);
Ian Rogersd5b32602012-02-26 16:40:04 -08001813 i = i + run;
1814 }
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001815 mirror::Class* value_class =
Andreas Gampe2ed8def2014-08-28 14:41:02 -07001816 (value == nullptr) ? obj_class->GetComponentType() : value->GetClass();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001817 PrettyObjectValue(os, value_class, value);
Ian Rogersd5b32602012-02-26 16:40:04 -08001818 }
1819 } else if (obj->IsClass()) {
Mathieu Chartierc7853442015-03-27 14:35:38 -07001820 mirror::Class* klass = obj->AsClass();
1821 ArtField* sfields = klass->GetSFields();
1822 const size_t num_fields = klass->NumStaticFields();
1823 if (num_fields != 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001824 os << "STATICS:\n";
1825 ScopedIndentation indent2(&state->vios_);
Mathieu Chartierc7853442015-03-27 14:35:38 -07001826 for (size_t i = 0; i < num_fields; i++) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001827 PrintField(os, &sfields[i], sfields[i].GetDeclaringClass());
Ian Rogersd5b32602012-02-26 16:40:04 -08001828 }
1829 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001830 } else {
1831 auto it = state->dex_cache_arrays_.find(obj);
1832 if (it != state->dex_cache_arrays_.end()) {
1833 const auto& field_section = state->image_header_.GetImageSection(
1834 ImageHeader::kSectionArtFields);
1835 const auto& method_section = state->image_header_.GetMethodsSection();
1836 auto* arr = down_cast<mirror::PointerArray*>(obj);
1837 for (int32_t i = 0, length = arr->GetLength(); i < length; i++) {
1838 void* elem = arr->GetElementPtrSize<void*>(i, image_pointer_size);
1839 size_t run = 0;
1840 for (int32_t j = i + 1; j < length &&
1841 elem == arr->GetElementPtrSize<void*>(j, image_pointer_size); j++, run++) { }
1842 if (run == 0) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001843 os << StringPrintf("%d: ", i);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001844 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001845 os << StringPrintf("%d to %zd: ", i, i + run);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001846 i = i + run;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001847 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001848 auto offset = reinterpret_cast<uint8_t*>(elem) - state->image_space_.Begin();
1849 std::string msg;
1850 if (field_section.Contains(offset)) {
1851 msg = PrettyField(reinterpret_cast<ArtField*>(elem));
1852 } else if (method_section.Contains(offset)) {
1853 msg = PrettyMethod(reinterpret_cast<ArtMethod*>(elem));
1854 } else {
1855 msg = "Unknown type";
1856 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001857 os << StringPrintf("%p %s\n", elem, msg.c_str());
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001858 }
Brian Carlstrom78128a62011-09-15 17:21:19 -07001859 }
1860 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07001861 std::string temp;
1862 state->stats_.Update(obj_class->GetDescriptor(&temp), object_bytes);
Brian Carlstrom78128a62011-09-15 17:21:19 -07001863 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001864
Mathieu Chartiere401d142015-04-22 13:56:20 -07001865 void DumpMethod(ArtMethod* method, ImageDumper* state, std::ostream& indent_os)
1866 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1867 DCHECK(method != nullptr);
1868 const auto image_pointer_size =
1869 InstructionSetPointerSize(state->oat_dumper_->GetOatInstructionSet());
1870 if (method->IsNative()) {
1871 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1872 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1873 bool first_occurrence;
1874 const void* quick_oat_code = state->GetQuickOatCodeBegin(method);
1875 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1876 state->ComputeOatSize(quick_oat_code, &first_occurrence);
1877 if (first_occurrence) {
1878 state->stats_.native_to_managed_code_bytes += quick_oat_code_size;
1879 }
1880 if (quick_oat_code != method->GetEntryPointFromQuickCompiledCodePtrSize(image_pointer_size)) {
1881 indent_os << StringPrintf("OAT CODE: %p\n", quick_oat_code);
1882 }
1883 } else if (method->IsAbstract() || method->IsCalleeSaveMethod() ||
1884 method->IsResolutionMethod() || method->IsImtConflictMethod() ||
1885 method->IsImtUnimplementedMethod() || method->IsClassInitializer()) {
1886 DCHECK(method->GetNativeGcMap(image_pointer_size) == nullptr) << PrettyMethod(method);
1887 DCHECK(method->GetMappingTable(image_pointer_size) == nullptr) << PrettyMethod(method);
1888 } else {
1889 const DexFile::CodeItem* code_item = method->GetCodeItem();
1890 size_t dex_instruction_bytes = code_item->insns_size_in_code_units_ * 2;
1891 state->stats_.dex_instruction_bytes += dex_instruction_bytes;
1892
1893 bool first_occurrence;
1894 size_t gc_map_bytes = state->ComputeOatSize(
1895 method->GetNativeGcMap(image_pointer_size), &first_occurrence);
1896 if (first_occurrence) {
1897 state->stats_.gc_map_bytes += gc_map_bytes;
1898 }
1899
1900 size_t pc_mapping_table_bytes = state->ComputeOatSize(
1901 method->GetMappingTable(image_pointer_size), &first_occurrence);
1902 if (first_occurrence) {
1903 state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes;
1904 }
1905
Roland Levillain6d7f1792015-07-02 10:59:15 +01001906 size_t vmap_table_bytes = 0u;
1907 if (!method->IsOptimized(image_pointer_size)) {
1908 // Method compiled with the optimizing compiler have no vmap table.
1909 vmap_table_bytes = state->ComputeOatSize(
1910 method->GetVmapTable(image_pointer_size), &first_occurrence);
1911 if (first_occurrence) {
1912 state->stats_.vmap_table_bytes += vmap_table_bytes;
1913 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001914 }
1915
1916 const void* quick_oat_code_begin = state->GetQuickOatCodeBegin(method);
1917 const void* quick_oat_code_end = state->GetQuickOatCodeEnd(method);
1918 uint32_t quick_oat_code_size = state->GetQuickOatCodeSize(method);
1919 state->ComputeOatSize(quick_oat_code_begin, &first_occurrence);
1920 if (first_occurrence) {
1921 state->stats_.managed_code_bytes += quick_oat_code_size;
1922 if (method->IsConstructor()) {
1923 if (method->IsStatic()) {
1924 state->stats_.class_initializer_code_bytes += quick_oat_code_size;
1925 } else if (dex_instruction_bytes > kLargeConstructorDexBytes) {
1926 state->stats_.large_initializer_code_bytes += quick_oat_code_size;
1927 }
1928 } else if (dex_instruction_bytes > kLargeMethodDexBytes) {
1929 state->stats_.large_method_code_bytes += quick_oat_code_size;
1930 }
1931 }
1932 state->stats_.managed_code_bytes_ignoring_deduplication += quick_oat_code_size;
1933
1934 indent_os << StringPrintf("OAT CODE: %p-%p\n", quick_oat_code_begin, quick_oat_code_end);
1935 indent_os << StringPrintf("SIZE: Dex Instructions=%zd GC=%zd Mapping=%zd\n",
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001936 dex_instruction_bytes, gc_map_bytes, pc_mapping_table_bytes);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001937
1938 size_t total_size = dex_instruction_bytes + gc_map_bytes + pc_mapping_table_bytes +
1939 vmap_table_bytes + quick_oat_code_size + ArtMethod::ObjectSize(image_pointer_size);
1940
1941 double expansion =
1942 static_cast<double>(quick_oat_code_size) / static_cast<double>(dex_instruction_bytes);
1943 state->stats_.ComputeOutliers(total_size, expansion, method);
1944 }
1945 }
1946
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001947 std::set<const void*> already_seen_;
1948 // Compute the size of the given data within the oat file and whether this is the first time
1949 // this data has been requested
Elliott Hughesa0e18062012-04-13 15:59:59 -07001950 size_t ComputeOatSize(const void* oat_data, bool* first_occurrence) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001951 if (already_seen_.count(oat_data) == 0) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001952 *first_occurrence = true;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001953 already_seen_.insert(oat_data);
1954 } else {
Elliott Hughesa0e18062012-04-13 15:59:59 -07001955 *first_occurrence = false;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001956 }
1957 return oat_dumper_->ComputeSize(oat_data);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001958 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001959
1960 public:
1961 struct Stats {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001962 size_t oat_file_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001963 size_t file_bytes;
1964
1965 size_t header_bytes;
1966 size_t object_bytes;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001967 size_t art_field_bytes;
1968 size_t art_method_bytes;
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001969 size_t interned_strings_bytes;
Mathieu Chartier32327092013-08-30 14:04:08 -07001970 size_t bitmap_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001971 size_t alignment_bytes;
1972
1973 size_t managed_code_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001974 size_t managed_code_bytes_ignoring_deduplication;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001975 size_t managed_to_native_code_bytes;
1976 size_t native_to_managed_code_bytes;
Ian Rogers0d2d3782012-04-10 11:09:18 -07001977 size_t class_initializer_code_bytes;
1978 size_t large_initializer_code_bytes;
1979 size_t large_method_code_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001980
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001981 size_t gc_map_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001982 size_t pc_mapping_table_bytes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001983 size_t vmap_table_bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001984
1985 size_t dex_instruction_bytes;
1986
Mathieu Chartiere401d142015-04-22 13:56:20 -07001987 std::vector<ArtMethod*> method_outlier;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001988 std::vector<size_t> method_outlier_size;
1989 std::vector<double> method_outlier_expansion;
Ian Rogers700a4022014-05-19 16:49:03 -07001990 std::vector<std::pair<std::string, size_t>> oat_dex_file_sizes;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08001991
1992 explicit Stats()
1993 : oat_file_bytes(0),
1994 file_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07001995 header_bytes(0),
1996 object_bytes(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001997 art_field_bytes(0),
1998 art_method_bytes(0),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07001999 interned_strings_bytes(0),
Mathieu Chartier32327092013-08-30 14:04:08 -07002000 bitmap_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002001 alignment_bytes(0),
2002 managed_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002003 managed_code_bytes_ignoring_deduplication(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002004 managed_to_native_code_bytes(0),
2005 native_to_managed_code_bytes(0),
Ian Rogers0d2d3782012-04-10 11:09:18 -07002006 class_initializer_code_bytes(0),
2007 large_initializer_code_bytes(0),
2008 large_method_code_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002009 gc_map_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002010 pc_mapping_table_bytes(0),
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002011 vmap_table_bytes(0),
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002012 dex_instruction_bytes(0) {}
2013
Elliott Hughesa0e18062012-04-13 15:59:59 -07002014 struct SizeAndCount {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002015 SizeAndCount(size_t bytes_in, size_t count_in) : bytes(bytes_in), count(count_in) {}
Elliott Hughesa0e18062012-04-13 15:59:59 -07002016 size_t bytes;
2017 size_t count;
2018 };
2019 typedef SafeMap<std::string, SizeAndCount> SizeAndCountTable;
2020 SizeAndCountTable sizes_and_counts;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002021
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002022 void Update(const char* descriptor, size_t object_bytes_in) {
Elliott Hughesa0e18062012-04-13 15:59:59 -07002023 SizeAndCountTable::iterator it = sizes_and_counts.find(descriptor);
2024 if (it != sizes_and_counts.end()) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002025 it->second.bytes += object_bytes_in;
Elliott Hughesa0e18062012-04-13 15:59:59 -07002026 it->second.count += 1;
2027 } else {
Andreas Gampe277ccbd2014-11-03 21:36:10 -08002028 sizes_and_counts.Put(descriptor, SizeAndCount(object_bytes_in, 1));
Elliott Hughesa0e18062012-04-13 15:59:59 -07002029 }
2030 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002031
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002032 double PercentOfOatBytes(size_t size) {
2033 return (static_cast<double>(size) / static_cast<double>(oat_file_bytes)) * 100;
2034 }
2035
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002036 double PercentOfFileBytes(size_t size) {
2037 return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100;
2038 }
2039
2040 double PercentOfObjectBytes(size_t size) {
2041 return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100;
2042 }
2043
Mathieu Chartiere401d142015-04-22 13:56:20 -07002044 void ComputeOutliers(size_t total_size, double expansion, ArtMethod* method) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002045 method_outlier_size.push_back(total_size);
2046 method_outlier_expansion.push_back(expansion);
2047 method_outlier.push_back(method);
2048 }
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002049
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002050 void DumpOutliers(std::ostream& os)
Ian Rogersb726dcb2012-09-05 08:57:23 -07002051 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002052 size_t sum_of_sizes = 0;
2053 size_t sum_of_sizes_squared = 0;
2054 size_t sum_of_expansion = 0;
2055 size_t sum_of_expansion_squared = 0;
2056 size_t n = method_outlier_size.size();
2057 for (size_t i = 0; i < n; i++) {
2058 size_t cur_size = method_outlier_size[i];
2059 sum_of_sizes += cur_size;
2060 sum_of_sizes_squared += cur_size * cur_size;
2061 double cur_expansion = method_outlier_expansion[i];
2062 sum_of_expansion += cur_expansion;
2063 sum_of_expansion_squared += cur_expansion * cur_expansion;
2064 }
2065 size_t size_mean = sum_of_sizes / n;
2066 size_t size_variance = (sum_of_sizes_squared - sum_of_sizes * size_mean) / (n - 1);
2067 double expansion_mean = sum_of_expansion / n;
2068 double expansion_variance =
2069 (sum_of_expansion_squared - sum_of_expansion * expansion_mean) / (n - 1);
2070
2071 // Dump methods whose size is a certain number of standard deviations from the mean
2072 size_t dumped_values = 0;
2073 size_t skipped_values = 0;
2074 for (size_t i = 100; i > 0; i--) { // i is the current number of standard deviations
2075 size_t cur_size_variance = i * i * size_variance;
2076 bool first = true;
2077 for (size_t j = 0; j < n; j++) {
2078 size_t cur_size = method_outlier_size[j];
2079 if (cur_size > size_mean) {
2080 size_t cur_var = cur_size - size_mean;
2081 cur_var = cur_var * cur_var;
2082 if (cur_var > cur_size_variance) {
2083 if (dumped_values > 20) {
2084 if (i == 1) {
2085 skipped_values++;
2086 } else {
2087 i = 2; // jump to counting for 1 standard deviation
2088 break;
2089 }
2090 } else {
2091 if (first) {
Elliott Hughesc073b072012-05-24 19:29:17 -07002092 os << "\nBig methods (size > " << i << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002093 first = false;
2094 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002095 os << PrettyMethod(method_outlier[j]) << " requires storage of "
Elliott Hughesc073b072012-05-24 19:29:17 -07002096 << PrettySize(cur_size) << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002097 method_outlier_size[j] = 0; // don't consider this method again
2098 dumped_values++;
2099 }
2100 }
2101 }
2102 }
2103 }
2104 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002105 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002106 << " methods with size > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002107 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002108 os << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002109
2110 // Dump methods whose expansion is a certain number of standard deviations from the mean
2111 dumped_values = 0;
2112 skipped_values = 0;
2113 for (size_t i = 10; i > 0; i--) { // i is the current number of standard deviations
2114 double cur_expansion_variance = i * i * expansion_variance;
2115 bool first = true;
2116 for (size_t j = 0; j < n; j++) {
2117 double cur_expansion = method_outlier_expansion[j];
2118 if (cur_expansion > expansion_mean) {
2119 size_t cur_var = cur_expansion - expansion_mean;
2120 cur_var = cur_var * cur_var;
2121 if (cur_var > cur_expansion_variance) {
2122 if (dumped_values > 20) {
2123 if (i == 1) {
2124 skipped_values++;
2125 } else {
2126 i = 2; // jump to counting for 1 standard deviation
2127 break;
2128 }
2129 } else {
2130 if (first) {
2131 os << "\nLarge expansion methods (size > " << i
Elliott Hughesc073b072012-05-24 19:29:17 -07002132 << " standard deviations the norm):\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002133 first = false;
2134 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002135 os << PrettyMethod(method_outlier[j]) << " expanded code by "
Elliott Hughesc073b072012-05-24 19:29:17 -07002136 << cur_expansion << "\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002137 method_outlier_expansion[j] = 0.0; // don't consider this method again
2138 dumped_values++;
2139 }
2140 }
2141 }
2142 }
2143 }
2144 if (skipped_values > 0) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002145 os << "... skipped " << skipped_values
Elliott Hughesc073b072012-05-24 19:29:17 -07002146 << " methods with expansion > 1 standard deviation from the norm\n";
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002147 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002148 os << "\n" << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002149 }
2150
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002151 void Dump(std::ostream& os, std::ostream& indent_os)
2152 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002153 {
2154 os << "art_file_bytes = " << PrettySize(file_bytes) << "\n\n"
2155 << "art_file_bytes = header_bytes + object_bytes + alignment_bytes\n";
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002156 indent_os << StringPrintf("header_bytes = %8zd (%2.0f%% of art file bytes)\n"
2157 "object_bytes = %8zd (%2.0f%% of art file bytes)\n"
2158 "art_field_bytes = %8zd (%2.0f%% of art file bytes)\n"
2159 "art_method_bytes = %8zd (%2.0f%% of art file bytes)\n"
2160 "interned_string_bytes = %8zd (%2.0f%% of art file bytes)\n"
2161 "bitmap_bytes = %8zd (%2.0f%% of art file bytes)\n"
2162 "alignment_bytes = %8zd (%2.0f%% of art file bytes)\n\n",
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002163 header_bytes, PercentOfFileBytes(header_bytes),
2164 object_bytes, PercentOfFileBytes(object_bytes),
Mathieu Chartiere401d142015-04-22 13:56:20 -07002165 art_field_bytes, PercentOfFileBytes(art_field_bytes),
2166 art_method_bytes, PercentOfFileBytes(art_method_bytes),
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002167 interned_strings_bytes,
2168 PercentOfFileBytes(interned_strings_bytes),
Mathieu Chartier32327092013-08-30 14:04:08 -07002169 bitmap_bytes, PercentOfFileBytes(bitmap_bytes),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002170 alignment_bytes, PercentOfFileBytes(alignment_bytes))
2171 << std::flush;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002172 CHECK_EQ(file_bytes, header_bytes + object_bytes + art_field_bytes + art_method_bytes +
Mathieu Chartierd39645e2015-06-09 17:50:29 -07002173 interned_strings_bytes + bitmap_bytes + alignment_bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002174 }
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002175
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002176 os << "object_bytes breakdown:\n";
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002177 size_t object_bytes_total = 0;
Mathieu Chartier02e25112013-08-14 16:14:24 -07002178 for (const auto& sizes_and_count : sizes_and_counts) {
2179 const std::string& descriptor(sizes_and_count.first);
2180 double average = static_cast<double>(sizes_and_count.second.bytes) /
2181 static_cast<double>(sizes_and_count.second.count);
2182 double percent = PercentOfObjectBytes(sizes_and_count.second.bytes);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002183 os << StringPrintf("%32s %8zd bytes %6zd instances "
Elliott Hughesa0e18062012-04-13 15:59:59 -07002184 "(%4.0f bytes/instance) %2.0f%% of object_bytes\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002185 descriptor.c_str(), sizes_and_count.second.bytes,
2186 sizes_and_count.second.count, average, percent);
2187 object_bytes_total += sizes_and_count.second.bytes;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002188 }
Elliott Hughesc073b072012-05-24 19:29:17 -07002189 os << "\n" << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002190 CHECK_EQ(object_bytes, object_bytes_total);
2191
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 os << StringPrintf("oat_file_bytes = %8zd\n"
2193 "managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2194 "managed_to_native_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2195 "native_to_managed_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n"
2196 "class_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2197 "large_initializer_code_bytes = %8zd (%2.0f%% of oat file bytes)\n"
2198 "large_method_code_bytes = %8zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002199 oat_file_bytes,
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002200 managed_code_bytes,
2201 PercentOfOatBytes(managed_code_bytes),
2202 managed_to_native_code_bytes,
2203 PercentOfOatBytes(managed_to_native_code_bytes),
2204 native_to_managed_code_bytes,
2205 PercentOfOatBytes(native_to_managed_code_bytes),
2206 class_initializer_code_bytes,
2207 PercentOfOatBytes(class_initializer_code_bytes),
2208 large_initializer_code_bytes,
2209 PercentOfOatBytes(large_initializer_code_bytes),
2210 large_method_code_bytes,
2211 PercentOfOatBytes(large_method_code_bytes))
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002212 << "DexFile sizes:\n";
Mathieu Chartier02e25112013-08-14 16:14:24 -07002213 for (const std::pair<std::string, size_t>& oat_dex_file_size : oat_dex_file_sizes) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002214 os << StringPrintf("%s = %zd (%2.0f%% of oat file bytes)\n",
Mathieu Chartier02e25112013-08-14 16:14:24 -07002215 oat_dex_file_size.first.c_str(), oat_dex_file_size.second,
2216 PercentOfOatBytes(oat_dex_file_size.second));
Ian Rogers05f28c62012-10-23 18:12:13 -07002217 }
2218
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002219 os << "\n" << StringPrintf("gc_map_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2220 "pc_mapping_table_bytes = %7zd (%2.0f%% of oat file bytes)\n"
2221 "vmap_table_bytes = %7zd (%2.0f%% of oat file bytes)\n\n",
Ian Rogers05f28c62012-10-23 18:12:13 -07002222 gc_map_bytes, PercentOfOatBytes(gc_map_bytes),
2223 pc_mapping_table_bytes, PercentOfOatBytes(pc_mapping_table_bytes),
2224 vmap_table_bytes, PercentOfOatBytes(vmap_table_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002225 << std::flush;
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002226
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002227 os << StringPrintf("dex_instruction_bytes = %zd\n", dex_instruction_bytes)
2228 << StringPrintf("managed_code_bytes expansion = %.2f (ignoring deduplication %.2f)\n\n",
Brian Carlstrom2ec65202014-03-03 15:16:37 -08002229 static_cast<double>(managed_code_bytes) /
2230 static_cast<double>(dex_instruction_bytes),
Elliott Hughesc073b072012-05-24 19:29:17 -07002231 static_cast<double>(managed_code_bytes_ignoring_deduplication) /
Elliott Hughescf44e6f2012-05-24 19:42:18 -07002232 static_cast<double>(dex_instruction_bytes))
Elliott Hughesc073b072012-05-24 19:29:17 -07002233 << std::flush;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002234
2235 DumpOutliers(os);
Brian Carlstrom916e74e2011-09-23 11:42:01 -07002236 }
2237 } stats_;
2238
2239 private:
Ian Rogers0d2d3782012-04-10 11:09:18 -07002240 enum {
2241 // Number of bytes for a constructor to be considered large. Based on the 1000 basic block
2242 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2243 kLargeConstructorDexBytes = 4000,
2244 // Number of bytes for a method to be considered large. Based on the 4000 basic block
2245 // threshold, we assume 2 bytes per instruction and 2 instructions per block.
2246 kLargeMethodDexBytes = 16000
2247 };
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002248
2249 // For performance, use the *os_ directly for anything that doesn't need indentation
2250 // and prepare an indentation stream with default indentation 1.
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002251 std::ostream* os_;
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01002252 VariableIndentationOutputStream vios_;
2253 ScopedIndentation indent1_;
2254
Ian Rogers1d54e732013-05-02 21:10:01 -07002255 gc::space::ImageSpace& image_space_;
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002256 const ImageHeader& image_header_;
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002257 std::unique_ptr<OatDumper> oat_dumper_;
Andreas Gampedf2bb1f2015-05-04 18:25:23 -07002258 OatDumperOptions* oat_dumper_options_;
Mathieu Chartiere401d142015-04-22 13:56:20 -07002259 std::set<mirror::Object*> dex_cache_arrays_;
Elliott Hughesd1bb4f62011-09-23 14:09:45 -07002260
Ian Rogers3a5c1ce2012-02-29 10:06:46 -08002261 DISALLOW_COPY_AND_ASSIGN(ImageDumper);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002262};
2263
Andreas Gampe00b25f32014-09-17 21:49:05 -07002264static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
2265 std::ostream* os) {
2266 // Dumping the image, no explicit class loader.
2267 NullHandle<mirror::ClassLoader> null_class_loader;
2268 options->class_loader_ = &null_class_loader;
2269
Ian Rogers00f7d0e2012-07-19 15:28:27 -07002270 ScopedObjectAccess soa(Thread::Current());
Andreas Gampe00b25f32014-09-17 21:49:05 -07002271 gc::Heap* heap = runtime->GetHeap();
Ian Rogers1d54e732013-05-02 21:10:01 -07002272 gc::space::ImageSpace* image_space = heap->GetImageSpace();
Andreas Gampe2ed8def2014-08-28 14:41:02 -07002273 CHECK(image_space != nullptr);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002274 const ImageHeader& image_header = image_space->GetImageHeader();
2275 if (!image_header.IsValid()) {
Brian Carlstrom0f5baa02014-05-22 11:54:18 -07002276 fprintf(stderr, "Invalid image header %s\n", image_location);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002277 return EXIT_FAILURE;
2278 }
Igor Murashkin37743352014-11-13 14:38:00 -08002279
Andreas Gampe00b25f32014-09-17 21:49:05 -07002280 ImageDumper image_dumper(os, *image_space, image_header, options);
Igor Murashkin37743352014-11-13 14:38:00 -08002281
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07002282 bool success = image_dumper.Dump();
2283 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
Brian Carlstrom78128a62011-09-15 17:21:19 -07002284}
2285
Andreas Gampe00b25f32014-09-17 21:49:05 -07002286static int DumpOatWithRuntime(Runtime* runtime, OatFile* oat_file, OatDumperOptions* options,
2287 std::ostream* os) {
2288 CHECK(runtime != nullptr && oat_file != nullptr && options != nullptr);
2289
2290 Thread* self = Thread::Current();
2291 CHECK(self != nullptr);
2292 // Need well-known-classes.
2293 WellKnownClasses::Init(self->GetJniEnv());
2294
2295 // Need to register dex files to get a working dex cache.
2296 ScopedObjectAccess soa(self);
2297 ClassLinker* class_linker = runtime->GetClassLinker();
2298 class_linker->RegisterOatFile(oat_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002299 std::vector<std::unique_ptr<const DexFile>> dex_files;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002300 for (const OatFile::OatDexFile* odf : oat_file->GetOatDexFiles()) {
2301 std::string error_msg;
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002302 std::unique_ptr<const DexFile> dex_file = odf->OpenDexFile(&error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002303 CHECK(dex_file != nullptr) << error_msg;
2304 class_linker->RegisterDexFile(*dex_file);
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002305 dex_files.push_back(std::move(dex_file));
Andreas Gampe00b25f32014-09-17 21:49:05 -07002306 }
2307
2308 // Need a class loader.
Andreas Gampe00b25f32014-09-17 21:49:05 -07002309 // Fake that we're a compiler.
Richard Uhlerfbef44d2014-12-23 09:48:51 -08002310 std::vector<const DexFile*> class_path;
2311 for (auto& dex_file : dex_files) {
2312 class_path.push_back(dex_file.get());
2313 }
Andreas Gampe81c6f8d2015-03-25 17:19:53 -07002314 jobject class_loader = class_linker->CreatePathClassLoader(self, class_path);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002315
2316 // Use the class loader while dumping.
2317 StackHandleScope<1> scope(self);
2318 Handle<mirror::ClassLoader> loader_handle = scope.NewHandle(
2319 soa.Decode<mirror::ClassLoader*>(class_loader));
2320 options->class_loader_ = &loader_handle;
2321
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002322 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002323 bool success = oat_dumper.Dump(*os);
2324 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2325}
2326
2327static int DumpOatWithoutRuntime(OatFile* oat_file, OatDumperOptions* options, std::ostream* os) {
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002328 CHECK(oat_file != nullptr && options != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002329 // No image = no class loader.
2330 NullHandle<mirror::ClassLoader> null_class_loader;
2331 options->class_loader_ = &null_class_loader;
2332
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002333 OatDumper oat_dumper(*oat_file, *options);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002334 bool success = oat_dumper.Dump(*os);
2335 return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
2336}
2337
2338static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
2339 std::ostream* os) {
2340 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002341 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Richard Uhlere5fed032015-03-18 08:21:11 -07002342 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002343 if (oat_file == nullptr) {
2344 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2345 return EXIT_FAILURE;
2346 }
2347
2348 if (runtime != nullptr) {
2349 return DumpOatWithRuntime(runtime, oat_file, options, os);
2350 } else {
2351 return DumpOatWithoutRuntime(oat_file, options, os);
2352 }
2353}
2354
2355static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
2356 std::string error_msg;
Igor Murashkin37743352014-11-13 14:38:00 -08002357 OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
Richard Uhlere5fed032015-03-18 08:21:11 -07002358 nullptr, &error_msg);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002359 if (oat_file == nullptr) {
2360 fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
2361 return EXIT_FAILURE;
2362 }
2363
2364 OatSymbolizer oat_symbolizer(oat_file, output_name);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002365 if (!oat_symbolizer.Symbolize()) {
2366 fprintf(stderr, "Failed to symbolize\n");
2367 return EXIT_FAILURE;
2368 }
2369
2370 return EXIT_SUCCESS;
2371}
2372
Igor Murashkin37743352014-11-13 14:38:00 -08002373struct OatdumpArgs : public CmdlineArgs {
2374 protected:
2375 using Base = CmdlineArgs;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002376
Igor Murashkin37743352014-11-13 14:38:00 -08002377 virtual ParseStatus ParseCustom(const StringPiece& option,
2378 std::string* error_msg) OVERRIDE {
2379 {
2380 ParseStatus base_parse = Base::ParseCustom(option, error_msg);
2381 if (base_parse != kParseUnknownArgument) {
2382 return base_parse;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002383 }
2384 }
2385
Igor Murashkin37743352014-11-13 14:38:00 -08002386 if (option.starts_with("--oat-file=")) {
2387 oat_filename_ = option.substr(strlen("--oat-file=")).data();
2388 } else if (option.starts_with("--image=")) {
2389 image_location_ = option.substr(strlen("--image=")).data();
2390 } else if (option =="--dump:raw_mapping_table") {
2391 dump_raw_mapping_table_ = true;
2392 } else if (option == "--dump:raw_gc_map") {
2393 dump_raw_gc_map_ = true;
2394 } else if (option == "--no-dump:vmap") {
2395 dump_vmap_ = false;
Roland Levillainf2650d12015-05-28 14:53:28 +01002396 } else if (option =="--dump:code_info_stack_maps") {
2397 dump_code_info_stack_maps_ = true;
Igor Murashkin37743352014-11-13 14:38:00 -08002398 } else if (option == "--no-disassemble") {
2399 disassemble_code_ = false;
2400 } else if (option.starts_with("--symbolize=")) {
2401 oat_filename_ = option.substr(strlen("--symbolize=")).data();
2402 symbolize_ = true;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002403 } else if (option.starts_with("--class-filter=")) {
2404 class_filter_ = option.substr(strlen("--class-filter=")).data();
Igor Murashkin37743352014-11-13 14:38:00 -08002405 } else if (option.starts_with("--method-filter=")) {
2406 method_filter_ = option.substr(strlen("--method-filter=")).data();
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002407 } else if (option.starts_with("--list-classes")) {
2408 list_classes_ = true;
2409 } else if (option.starts_with("--list-methods")) {
2410 list_methods_ = true;
2411 } else if (option.starts_with("--export-dex-to=")) {
2412 export_dex_location_ = option.substr(strlen("--export-dex-to=")).data();
2413 } else if (option.starts_with("--addr2instr=")) {
2414 if (!ParseUint(option.substr(strlen("--addr2instr=")).data(), &addr2instr_)) {
2415 *error_msg = "Address conversion failed";
2416 return kParseError;
2417 }
Igor Murashkin37743352014-11-13 14:38:00 -08002418 } else {
2419 return kParseUnknownArgument;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002420 }
2421
Igor Murashkin37743352014-11-13 14:38:00 -08002422 return kParseOk;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002423 }
2424
Igor Murashkin37743352014-11-13 14:38:00 -08002425 virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
2426 // Infer boot image location from the image location if possible.
2427 if (boot_image_location_ == nullptr) {
2428 boot_image_location_ = image_location_;
2429 }
2430
2431 // Perform the parent checks.
2432 ParseStatus parent_checks = Base::ParseChecks(error_msg);
2433 if (parent_checks != kParseOk) {
2434 return parent_checks;
2435 }
2436
2437 // Perform our own checks.
2438 if (image_location_ == nullptr && oat_filename_ == nullptr) {
2439 *error_msg = "Either --image or --oat-file must be specified";
2440 return kParseError;
2441 } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
2442 *error_msg = "Either --image or --oat-file must be specified but not both";
2443 return kParseError;
2444 }
2445
2446 return kParseOk;
2447 }
2448
2449 virtual std::string GetUsage() const {
2450 std::string usage;
2451
2452 usage +=
2453 "Usage: oatdump [options] ...\n"
2454 " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
2455 " Example: adb shell oatdump --image=/system/framework/boot.art\n"
2456 "\n"
2457 // Either oat-file or image is required.
2458 " --oat-file=<file.oat>: specifies an input oat filename.\n"
2459 " Example: --oat-file=/system/framework/boot.oat\n"
2460 "\n"
2461 " --image=<file.art>: specifies an input image location.\n"
2462 " Example: --image=/system/framework/boot.art\n"
2463 "\n";
2464
2465 usage += Base::GetUsage();
2466
2467 usage += // Optional.
2468 " --dump:raw_mapping_table enables dumping of the mapping table.\n"
2469 " Example: --dump:raw_mapping_table\n"
2470 "\n"
Mathieu Chartier19510f02015-05-26 14:44:35 -07002471 " --dump:raw_gc_map enables dumping of the GC map.\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002472 " Example: --dump:raw_gc_map\n"
2473 "\n"
2474 " --no-dump:vmap may be used to disable vmap dumping.\n"
2475 " Example: --no-dump:vmap\n"
2476 "\n"
Roland Levillainf2650d12015-05-28 14:53:28 +01002477 " --dump:code_info_stack_maps enables dumping of stack maps in CodeInfo sections.\n"
2478 " Example: --dump:code_info_stack_maps\n"
2479 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002480 " --no-disassemble may be used to disable disassembly.\n"
2481 " Example: --no-disassemble\n"
2482 "\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002483 " --list-classes may be used to list target file classes (can be used with filters).\n"
2484 " Example: --list-classes\n"
2485 " Example: --list-classes --class-filter=com.example.foo\n"
2486 "\n"
2487 " --list-methods may be used to list target file methods (can be used with filters).\n"
2488 " Example: --list-methods\n"
2489 " Example: --list-methods --class-filter=com.example --method-filter=foo\n"
2490 "\n"
2491 " --symbolize=<file.oat>: output a copy of file.oat with elf symbols included.\n"
2492 " Example: --symbolize=/system/framework/boot.oat\n"
2493 "\n"
2494 " --class-filter=<class name>: only dumps classes that contain the filter.\n"
2495 " Example: --class-filter=com.example.foo\n"
2496 "\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002497 " --method-filter=<method name>: only dumps methods that contain the filter.\n"
2498 " Example: --method-filter=foo\n"
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002499 "\n"
2500 " --export-dex-to=<directory>: may be used to export oat embedded dex files.\n"
2501 " Example: --export-dex-to=/data/local/tmp\n"
2502 "\n"
2503 " --addr2instr=<address>: output matching method disassembled code from relative\n"
2504 " address (e.g. PC from crash dump)\n"
2505 " Example: --addr2instr=0x00001a3b\n"
Igor Murashkin37743352014-11-13 14:38:00 -08002506 "\n";
2507
2508 return usage;
2509 }
2510
2511 public:
Andreas Gampe00b25f32014-09-17 21:49:05 -07002512 const char* oat_filename_ = nullptr;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002513 const char* class_filter_ = "";
Nicolas Geoffray3fcd2202014-11-12 18:02:36 +00002514 const char* method_filter_ = "";
Andreas Gampe00b25f32014-09-17 21:49:05 -07002515 const char* image_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002516 std::string elf_filename_prefix_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002517 bool dump_raw_mapping_table_ = false;
2518 bool dump_raw_gc_map_ = false;
2519 bool dump_vmap_ = true;
Roland Levillainf2650d12015-05-28 14:53:28 +01002520 bool dump_code_info_stack_maps_ = false;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002521 bool disassemble_code_ = true;
2522 bool symbolize_ = false;
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002523 bool list_classes_ = false;
2524 bool list_methods_ = false;
2525 uint32_t addr2instr_ = 0;
2526 const char* export_dex_location_ = nullptr;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002527};
2528
Igor Murashkin37743352014-11-13 14:38:00 -08002529struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
2530 virtual bool NeedsRuntime() OVERRIDE {
2531 CHECK(args_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002532
Igor Murashkin37743352014-11-13 14:38:00 -08002533 // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
2534 bool absolute_addresses = (args_->oat_filename_ == nullptr);
2535
2536 oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
2537 args_->dump_raw_mapping_table_,
2538 args_->dump_raw_gc_map_,
2539 args_->dump_vmap_,
Roland Levillainf2650d12015-05-28 14:53:28 +01002540 args_->dump_code_info_stack_maps_,
Igor Murashkin37743352014-11-13 14:38:00 -08002541 args_->disassemble_code_,
2542 absolute_addresses,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002543 args_->class_filter_,
2544 args_->method_filter_,
2545 args_->list_classes_,
2546 args_->list_methods_,
2547 args_->export_dex_location_,
2548 args_->addr2instr_));
Igor Murashkin37743352014-11-13 14:38:00 -08002549
2550 return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
2551 !args_->symbolize_;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002552 }
2553
Igor Murashkin37743352014-11-13 14:38:00 -08002554 virtual bool ExecuteWithoutRuntime() OVERRIDE {
2555 CHECK(args_ != nullptr);
Andreas Gampec24f3992014-12-17 20:40:11 -08002556 CHECK(args_->oat_filename_ != nullptr);
Andreas Gampe00b25f32014-09-17 21:49:05 -07002557
Mathieu Chartierd424d082014-10-15 10:31:46 -07002558 MemMap::Init();
Igor Murashkin37743352014-11-13 14:38:00 -08002559
Andreas Gampec24f3992014-12-17 20:40:11 -08002560 if (args_->symbolize_) {
2561 return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
2562 } else {
2563 return DumpOat(nullptr,
2564 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002565 oat_dumper_options_.get(),
Andreas Gampec24f3992014-12-17 20:40:11 -08002566 args_->os_) == EXIT_SUCCESS;
2567 }
Andreas Gampe00b25f32014-09-17 21:49:05 -07002568 }
2569
Igor Murashkin37743352014-11-13 14:38:00 -08002570 virtual bool ExecuteWithRuntime(Runtime* runtime) {
2571 CHECK(args_ != nullptr);
2572
2573 if (args_->oat_filename_ != nullptr) {
2574 return DumpOat(runtime,
2575 args_->oat_filename_,
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002576 oat_dumper_options_.get(),
Igor Murashkin37743352014-11-13 14:38:00 -08002577 args_->os_) == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002578 }
Igor Murashkin37743352014-11-13 14:38:00 -08002579
Anestis Bechtsoudis32f500d2015-02-22 22:32:57 -08002580 return DumpImage(runtime, args_->image_location_, oat_dumper_options_.get(), args_->os_)
Igor Murashkin37743352014-11-13 14:38:00 -08002581 == EXIT_SUCCESS;
Andreas Gampe00b25f32014-09-17 21:49:05 -07002582 }
2583
Igor Murashkin37743352014-11-13 14:38:00 -08002584 std::unique_ptr<OatDumperOptions> oat_dumper_options_;
2585};
Andreas Gampe00b25f32014-09-17 21:49:05 -07002586
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002587} // namespace art
Brian Carlstrom78128a62011-09-15 17:21:19 -07002588
2589int main(int argc, char** argv) {
Igor Murashkin37743352014-11-13 14:38:00 -08002590 art::OatdumpMain main;
2591 return main.Main(argc, argv);
Brian Carlstrom78128a62011-09-15 17:21:19 -07002592}